Skip to content

Commit

Permalink
Fix machine implementation updating
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Makhov <amakhov@mirantis.com>
  • Loading branch information
makhov committed Nov 1, 2024
1 parent 8e92a20 commit 0c183cf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
5 changes: 3 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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{
Expand Down
46 changes: 20 additions & 26 deletions internal/controller/controlplane/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0c183cf

Please sign in to comment.