Skip to content

Commit 984aee6

Browse files
alvaroalemank8s-infra-cherrypick-robot
authored and
k8s-infra-cherrypick-robot
committed
bug: Fakeclient: Do not consider an apply patch to be a strategic merge 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.
1 parent 5923139 commit 984aee6

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)