Skip to content

Commit 59c26c0

Browse files
authored
Merge pull request kubernetes-sigs#2681 from k8s-infra-cherrypick-robot/cherry-pick-2679-to-release-0.17
[release-0.17] ⚠ 🐛 Fakeclient: Do not consider an apply patch to be a strategic merge patch
2 parents 5923139 + 984aee6 commit 59c26c0

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

pkg/client/fake/client.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -947,16 +947,18 @@ func dryPatch(action testing.PatchActionImpl, tracker testing.ObjectTracker) (ru
947947
if err := json.Unmarshal(modified, obj); err != nil {
948948
return nil, err
949949
}
950-
case types.StrategicMergePatchType, types.ApplyPatchType:
950+
case types.StrategicMergePatchType:
951951
mergedByte, err := strategicpatch.StrategicMergePatch(old, action.GetPatch(), obj)
952952
if err != nil {
953953
return nil, err
954954
}
955955
if err = json.Unmarshal(mergedByte, obj); err != nil {
956956
return nil, err
957957
}
958+
case types.ApplyPatchType:
959+
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")
958960
default:
959-
return nil, fmt.Errorf("PatchType is not supported")
961+
return nil, fmt.Errorf("%s PatchType is not supported", action.GetPatchType())
960962
}
961963
return obj, nil
962964
}

pkg/client/fake/client_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,26 @@ var _ = Describe("Fake client", func() {
359359
Expect(list.Items).To(ConsistOf(*dep2))
360360
})
361361

362+
It("should reject apply patches, they are not supported in the fake client", func() {
363+
By("Creating a new configmap")
364+
cm := &corev1.ConfigMap{
365+
TypeMeta: metav1.TypeMeta{
366+
APIVersion: "v1",
367+
Kind: "ConfigMap",
368+
},
369+
ObjectMeta: metav1.ObjectMeta{
370+
Name: "new-test-cm",
371+
Namespace: "ns2",
372+
},
373+
}
374+
err := cl.Create(context.Background(), cm)
375+
Expect(err).ToNot(HaveOccurred())
376+
377+
cm.Data = map[string]string{"foo": "bar"}
378+
err = cl.Patch(context.Background(), cm, client.Apply, client.ForceOwnership)
379+
Expect(err).To(MatchError(ContainSubstring("apply patches are not supported in the fake client")))
380+
})
381+
362382
It("should be able to Create", func() {
363383
By("Creating a new configmap")
364384
newcm := &corev1.ConfigMap{

0 commit comments

Comments
 (0)