From e6137ff05fd6d6e736a108cf91f5752e8669b268 Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Mon, 5 Feb 2018 15:31:15 -0500 Subject: [PATCH] fix(api-machinery): Fixes patching for unstructured objects CRDs and other objects seen as unstructured cannot use strategic merge patching. It has never been supported on CRDs. Previously, cases like unstructured objects could have caused an unregistered error. This is no longer the case. This change explicitly looks for unstructured objects and handles those using json merge patching. Closes #3382 --- pkg/kube/client.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/kube/client.go b/pkg/kube/client.go index 6b911c8a0..cd5227dd7 100644 --- a/pkg/kube/client.go +++ b/pkg/kube/client.go @@ -399,14 +399,25 @@ func createPatch(mapping *meta.RESTMapping, target, current runtime.Object) ([]b return nil, types.StrategicMergePatchType, fmt.Errorf("serializing target configuration: %s", err) } + // While different objects need different merge types, the parent function + // that calls this does not try to create a patch when the data (first + // returned object) is nil. We can skip calculating the the merge type as + // the returned merge type is ignored. if apiequality.Semantic.DeepEqual(oldData, newData) { return nil, types.StrategicMergePatchType, nil } // Get a versioned object versionedObject, err := mapping.ConvertToVersion(target, mapping.GroupVersionKind.GroupVersion()) + + // Unstructured objects, such as CRDs, may not have an not registered error + // returned from ConvertToVersion. Anything that's unstructured should + // use the jsonpatch.CreateMergePatch. Strategic Merge Patch is not supported + // on objects like CRDs. + _, isUnstructured := versionedObject.(runtime.Unstructured) + switch { - case runtime.IsNotRegisteredError(err): + case runtime.IsNotRegisteredError(err), isUnstructured: // fall back to generic JSON merge patch patch, err := jsonpatch.CreateMergePatch(oldData, newData) return patch, types.MergePatchType, err