Skip to content

Commit

Permalink
further cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sbueringer committed Aug 20, 2024
1 parent 10c173a commit 12660b3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 21 deletions.
25 changes: 5 additions & 20 deletions internal/controllers/machine/drain/cordon.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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

Check failure on line 52 in internal/controllers/machine/drain/cordon.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)

Check failure on line 52 in internal/controllers/machine/drain/cordon.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)

Check failure on line 52 in internal/controllers/machine/drain/cordon.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)

Check failure on line 52 in internal/controllers/machine/drain/cordon.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
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)
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion internal/controllers/machine/drain/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 12660b3

Please sign in to comment.