Skip to content

Commit

Permalink
fix(helm-operator): do not send empty patch requests to kube apiserver (
Browse files Browse the repository at this point in the history
#139)

The change filters out empty patches generated from the 3-way merge,
which could be in the form of the "{}" byte array that
represents an empty map.

Reference: operator-framework/operator-sdk#4957
Co-authored-by: Cheuk Lam
  • Loading branch information
varshaprasad96 committed Dec 7, 2021
1 parent de4f99a commit 2c20f02
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 10 additions & 2 deletions pkg/client/actionclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,17 @@ func createPatch(existing runtime.Object, expected *resource.Info) ([]byte, apit
if err != nil {
return nil, apitypes.StrategicMergePatchType, err
}

patch, err := strategicpatch.CreateThreeWayMergePatch(expectedJSON, expectedJSON, existingJSON, patchMeta, true)
return patch, apitypes.StrategicMergePatchType, err
if err != nil {
return nil, apitypes.StrategicMergePatchType, err
}

// An empty patch could be in the form of "{}" which represents an empty map out of the 3-way merge;
// filter them out here too to avoid sending the apiserver empty patch requests.
if len(patch) == 0 || bytes.Equal(patch, []byte("{}")) {
return nil, apitypes.StrategicMergePatchType, nil
}
return patch, apitypes.StrategicMergePatchType, nil
}

func createJSONMergePatch(existingJSON, expectedJSON []byte) ([]byte, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/actionclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ var _ = Describe("ActionClient", func() {
}
patch, patchType, err := createPatch(o1, o2)
Expect(err).To(BeNil())
Expect(string(patch)).To(Equal(`{}`))
Expect(patch).To(BeNil())
Expect(patchType).To(Equal(apitypes.StrategicMergePatchType))
})
It("replaces incorrect fields in core types", func() {
Expand Down Expand Up @@ -511,7 +511,7 @@ var _ = Describe("ActionClient", func() {
}
patch, patchType, err := createPatch(o1, o2)
Expect(err).To(BeNil())
Expect(string(patch)).To(Equal(`{}`))
Expect(patch).To(BeNil())
Expect(patchType).To(Equal(apitypes.StrategicMergePatchType))
})
})
Expand Down

0 comments on commit 2c20f02

Please sign in to comment.