Skip to content

Commit

Permalink
bug: Fakeclient: Do not consider an apply patch to be a strategic mer…
Browse files Browse the repository at this point in the history
…ge patch

The fakeclient currently considers an apply patch to be a strategic
merge patch. This is completely wrong, those are different things and
apply patches are not supported in the fakeclientl, because in order to
support them it would need the entire SSA logic which isn't implemented
in upstream yet.
  • Loading branch information
alvaroaleman authored and k8s-infra-cherrypick-robot committed Feb 10, 2024
1 parent 5923139 commit 984aee6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -947,16 +947,18 @@ func dryPatch(action testing.PatchActionImpl, tracker testing.ObjectTracker) (ru
if err := json.Unmarshal(modified, obj); err != nil {
return nil, err
}
case types.StrategicMergePatchType, types.ApplyPatchType:
case types.StrategicMergePatchType:
mergedByte, err := strategicpatch.StrategicMergePatch(old, action.GetPatch(), obj)
if err != nil {
return nil, err
}
if err = json.Unmarshal(mergedByte, obj); err != nil {
return nil, err
}
case types.ApplyPatchType:
return nil, errors.New("apply patches are not supported in the fake client. Follow https://github.com/kubernetes/kubernetes/issues/115598 for the current status")
default:
return nil, fmt.Errorf("PatchType is not supported")
return nil, fmt.Errorf("%s PatchType is not supported", action.GetPatchType())
}
return obj, nil
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,26 @@ var _ = Describe("Fake client", func() {
Expect(list.Items).To(ConsistOf(*dep2))
})

It("should reject apply patches, they are not supported in the fake client", func() {
By("Creating a new configmap")
cm := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: "new-test-cm",
Namespace: "ns2",
},
}
err := cl.Create(context.Background(), cm)
Expect(err).ToNot(HaveOccurred())

cm.Data = map[string]string{"foo": "bar"}
err = cl.Patch(context.Background(), cm, client.Apply, client.ForceOwnership)
Expect(err).To(MatchError(ContainSubstring("apply patches are not supported in the fake client")))
})

It("should be able to Create", func() {
By("Creating a new configmap")
newcm := &corev1.ConfigMap{
Expand Down

0 comments on commit 984aee6

Please sign in to comment.