Skip to content

Commit

Permalink
virt-handler: Use patchset for patch operations
Browse files Browse the repository at this point in the history
Replace hardcoded patch operation with patchSet.

Signed-off-by: fossedihelm <[email protected]>
  • Loading branch information
fossedihelm committed Jun 17, 2024
1 parent 6cb0afa commit 70a754c
Showing 1 changed file with 11 additions and 23 deletions.
34 changes: 11 additions & 23 deletions pkg/virt-handler/node-labeller/node_labeller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package nodelabeller

import (
"context"
"encoding/json"
"fmt"
"os/exec"
"runtime"
Expand Down Expand Up @@ -206,32 +205,21 @@ func skipNodeLabelling(node *v1.Node) bool {
}

func (n *NodeLabeller) patchNode(originalNode, node *v1.Node) error {
p := make([]patch.PatchOperation, 0)
if !equality.Semantic.DeepEqual(originalNode.Labels, node.Labels) {
p = append(p, patch.PatchOperation{
Op: "test",
Path: "/metadata/labels",
Value: originalNode.Labels,
}, patch.PatchOperation{
Op: "replace",
Path: "/metadata/labels",
Value: node.Labels,
})
if equality.Semantic.DeepEqual(originalNode.Labels, node.Labels) {
return nil
}

// patch node only if there is change in labels
if len(p) > 0 {
payloadBytes, err := json.Marshal(p)
if err != nil {
return err
}
_, err = n.nodeClient.Patch(context.Background(), node.Name, types.JSONPatchType, payloadBytes, metav1.PatchOptions{})
if err != nil {
return err
}
patchBytes, err := patch.New(
patch.WithTest("/metadata/labels", originalNode.Labels),
patch.WithReplace("/metadata/labels", node.Labels),
).GeneratePayload()

if err != nil {
return err
}

return nil
_, err = n.nodeClient.Patch(context.Background(), node.Name, types.JSONPatchType, patchBytes, metav1.PatchOptions{})
return err
}

func (n *NodeLabeller) loadHypervFeatures() {
Expand Down

0 comments on commit 70a754c

Please sign in to comment.