bloaded put
This commit is contained in:
		| @@ -3,6 +3,7 @@ package internal | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"context" | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"html/template" | ||||
|  | ||||
| @@ -48,22 +49,38 @@ type CrdConfig struct { | ||||
| } | ||||
|  | ||||
| func RecreateIPPoolCrd(cfg *Config, name string, ips []string) error { | ||||
|  | ||||
| 	routeclient, err := createRestClient() | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("error creating REST Client: %v", err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	body, err := generateIpPool(name, ips) | ||||
| 	resourceVersion, err := getResourceVersion(routeclient, name) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("error getting resourceVersion: %v", err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	body, err := generateIpPool(name, ips) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("error generating CRD: %v", err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	res := routeclient.Post(). | ||||
| 	// Inject resourceVersion into the JSON | ||||
| 	var obj map[string]interface{} | ||||
| 	if err := json.Unmarshal([]byte(body), &obj); err != nil { | ||||
| 		return fmt.Errorf("could not unmarshal generated CRD: %v", err) | ||||
| 	} | ||||
| 	if meta, ok := obj["metadata"].(map[string]interface{}); ok { | ||||
| 		meta["resourceVersion"] = resourceVersion | ||||
| 	} | ||||
| 	finalBody, err := json.Marshal(obj) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("could not marshal final CRD: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	res := routeclient.Put(). | ||||
| 		Resource("ciliumloadbalancerippools"). | ||||
| 		Body([]byte(body)). | ||||
| 		Name(name). | ||||
| 		Body(finalBody). | ||||
| 		Do(context.TODO()) | ||||
|  | ||||
| 	var status int | ||||
| @@ -85,7 +102,6 @@ func RecreateIPPoolCrd(cfg *Config, name string, ips []string) error { | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func createRestClient() (*rest.RESTClient, error) { | ||||
| 	k8s_config, err := rest.InClusterConfig() | ||||
| 	if err != nil { | ||||
| @@ -122,6 +138,30 @@ func generateIpPool(name string, ips []string) (string, error) { | ||||
| 	return buf.String(), nil | ||||
| } | ||||
|  | ||||
| func getResourceVersion(client *rest.RESTClient, name string) (string, error) { | ||||
| 	res := client.Get(). | ||||
| 		Resource("ciliumloadbalancerippools"). | ||||
| 		Name(name). | ||||
| 		Do(context.TODO()) | ||||
| 	raw, err := res.Raw() | ||||
| 	if err != nil { | ||||
| 		return "", fmt.Errorf("could not fetch CRD: %v", err) | ||||
| 	} | ||||
| 	var obj map[string]interface{} | ||||
| 	if err := json.Unmarshal(raw, &obj); err != nil { | ||||
| 		return "", fmt.Errorf("could not unmarshal CRD: %v", err) | ||||
| 	} | ||||
| 	meta, ok := obj["metadata"].(map[string]interface{}) | ||||
| 	if !ok { | ||||
| 		return "", fmt.Errorf("metadata missing in CRD") | ||||
| 	} | ||||
| 	rv, ok := meta["resourceVersion"].(string) | ||||
| 	if !ok { | ||||
| 		return "", fmt.Errorf("resourceVersion missing in metadata") | ||||
| 	} | ||||
| 	return rv, nil | ||||
| } | ||||
|  | ||||
| // func RegisterCiliumCrd() error { | ||||
| // 	SchemeBuilder := &apiSchema.Builder{GroupVersion: CILIUM_GROUP_VERSION} | ||||
| // 	err := SchemeBuilder.AddToScheme(scheme.Scheme) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user