diff --git a/cmd/main.go b/cmd/main.go index 371b50593..4c6d8c4e8 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -20,11 +20,11 @@ import ( "crypto/md5" "flag" "fmt" - "k8s.io/client-go/dynamic" "os" // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) // to ensure that exec-entrypoint and run can make use of them. + "k8s.io/client-go/dynamic" _ "k8s.io/client-go/plugin/pkg/client/auth" "k8s.io/client-go/tools/clientcmd" @@ -143,7 +143,8 @@ func main() { } dynamicClient, err := dynamic.NewForConfig(restConfig) if err != nil { - panic(err) + setupLog.Error(err, "unable to get kubernetes dynamic client") + os.Exit(1) } if err = (&controller.ClusterReconciler{ diff --git a/internal/controller/controlplane/helper.go b/internal/controller/controlplane/helper.go index 5b2a022ec..9da30f5d0 100644 --- a/internal/controller/controlplane/helper.go +++ b/internal/controller/controlplane/helper.go @@ -3,12 +3,11 @@ package controlplane import ( "context" "fmt" - "github.com/imdario/mergo" - "k8s.io/utils/ptr" "sort" "strings" "time" + "github.com/imdario/mergo" "github.com/k0sproject/version" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -120,35 +119,30 @@ func (c *K0sController) createMachineFromTemplate(ctx context.Context, name stri return nil, fmt.Errorf("error getting machine implementation: %w", err) } - //gv, _ := schema.ParseGroupVersion(existingMachineFromTemplate.GetAPIVersion()) - //gvr := gv.WithResource(strings.ToLower(existingMachineFromTemplate.GetKind()) + "s") - //patchBytes, _ := machineFromTemplate.MarshalJSON() - err = mergo.Merge(existingMachineFromTemplate.Object["spec"], machineFromTemplate.Object["spec"], mergo.WithOverride) + + err = mergo.Merge(existingMachineFromTemplate, machineFromTemplate, mergo.WithSliceDeepCopy) if err != nil { - return nil, fmt.Errorf("error merging: %w", err) + return nil, err } - if err = c.Client.Patch(ctx, existingMachineFromTemplate, client.Merge, &client.PatchOptions{ - FieldManager: "k0smotron", - Force: ptr.To(true), - }); err != nil { - return nil, fmt.Errorf("error apply patching: %w", err) + spec, _, _ := unstructured.NestedMap(existingMachineFromTemplate.Object, "spec") + patch := unstructured.Unstructured{Object: map[string]interface{}{ + "spec": spec, + }} + data, err := patch.MarshalJSON() + if err != nil { + return nil, err + } + + resourceType := strings.ToLower(machineFromTemplate.GetKind()) + "s" + req := c.ClientSet.RESTClient().Patch(types.MergePatchType). + Body(data). + AbsPath("apis", machineFromTemplate.GetAPIVersion(), "namespaces", machineFromTemplate.GetNamespace(), resourceType, machineFromTemplate.GetName()) + _, err = req.DoRaw(ctx) + if err != nil { + return nil, fmt.Errorf("error patching: %w", err) } return machineFromTemplate, nil - //return machineFromTemplate, nil - - //data, err := machineFromTemplate.MarshalJSON() - //if err != nil { - // return nil, err - //} - //req := c.ClientSet.RESTClient().Patch(types.MergePatchType). - // Body(data). - // AbsPath("apis", machineFromTemplate.GetAPIVersion(), "namespaces", machineFromTemplate.GetNamespace(), strings.ToLower(machineFromTemplate.GetKind())+"s", machineFromTemplate.GetName()) - //_, err = req.DoRaw(ctx) - //if err != nil { - // return nil, fmt.Errorf("error patching: %w", err) - //} - //return machineFromTemplate, nil } func (c *K0sController) deleteMachineFromTemplate(ctx context.Context, name string, cluster *clusterv1.Cluster, kcp *cpv1beta1.K0sControlPlane) error {