From 12660b313b6c537305148875b51f5b3f09e5ad80 Mon Sep 17 00:00:00 2001 From: Stefan Bueringer Date: Tue, 20 Aug 2024 19:52:04 +0200 Subject: [PATCH] further cleanup --- internal/controllers/machine/drain/cordon.go | 25 ++++--------------- internal/controllers/machine/drain/default.go | 2 +- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/internal/controllers/machine/drain/cordon.go b/internal/controllers/machine/drain/cordon.go index 4f79817166df..30d2742246b5 100644 --- a/internal/controllers/machine/drain/cordon.go +++ b/internal/controllers/machine/drain/cordon.go @@ -1,5 +1,5 @@ /* -Copyright 2019 The Kubernetes Authors. +Copyright 2024 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -48,17 +48,9 @@ func (c *CordonHelper) UpdateIfRequired(desired bool) bool { return c.node.Spec.Unschedulable != c.desired } -// PatchOrReplace uses given clientset to update the node status, either by patching or -// updating the given node object; it may return error if the object cannot be encoded as -// JSON, or if either patch or update calls fail; it will also return a second error -// whenever creating a patch has failed -func (c *CordonHelper) PatchOrReplace(ctx context.Context, clientset kubernetes.Interface, serverDryRun bool) (error, error) { - return c.PatchOrReplaceWithContext(ctx, clientset, serverDryRun) -} - // PatchOrReplaceWithContext provides the option to pass a custom context while updating // the node status -func (c *CordonHelper) PatchOrReplaceWithContext(clientCtx context.Context, clientset kubernetes.Interface, serverDryRun bool) (error, error) { +func (c *CordonHelper) PatchOrReplaceWithContext(clientCtx context.Context, clientset kubernetes.Interface) (error, error) { client := clientset.CoreV1().Nodes() oldData, err := json.Marshal(c.node) @@ -73,19 +65,12 @@ func (c *CordonHelper) PatchOrReplaceWithContext(clientCtx context.Context, clie return err, nil } + // FIXME: try to use CR client + a simple patch call (ideally just patch helper) patchBytes, patchErr := strategicpatch.CreateTwoWayMergePatch(oldData, newData, c.node) if patchErr == nil { - patchOptions := metav1.PatchOptions{} - if serverDryRun { - patchOptions.DryRun = []string{metav1.DryRunAll} - } - _, err = client.Patch(clientCtx, c.node.Name, types.StrategicMergePatchType, patchBytes, patchOptions) + _, err = client.Patch(clientCtx, c.node.Name, types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{}) } else { - updateOptions := metav1.UpdateOptions{} - if serverDryRun { - updateOptions.DryRun = []string{metav1.DryRunAll} - } - _, err = client.Update(clientCtx, c.node, updateOptions) + _, err = client.Update(clientCtx, c.node, metav1.UpdateOptions{}) } return err, patchErr } diff --git a/internal/controllers/machine/drain/default.go b/internal/controllers/machine/drain/default.go index 3ac2556e1e7c..97d703fa8ca4 100644 --- a/internal/controllers/machine/drain/default.go +++ b/internal/controllers/machine/drain/default.go @@ -60,7 +60,7 @@ func RunCordonOrUncordon(ctx context.Context, drainer *Helper, node *corev1.Node return nil } - err, patchErr := c.PatchOrReplaceWithContext(ctx, drainer.Client, false) + err, patchErr := c.PatchOrReplaceWithContext(ctx, drainer.Client) if err != nil { if patchErr != nil { return fmt.Errorf("cordon error: %s; merge patch error: %w", err.Error(), patchErr)