From 4e41c0fb0d16cb81719acf33c448b3277bac2a9d Mon Sep 17 00:00:00 2001 From: Gerred Dillon Date: Thu, 27 Jun 2019 23:33:01 -0400 Subject: [PATCH] Removes patches and uses the raw template object as the patch instead (#447) Co-authored-by: jbarrick@mesosphere.com --- .../planexecution/planexecution_controller.go | 36 ++-------- .../planexecution_controller_test.go | 68 ------------------- 2 files changed, 5 insertions(+), 99 deletions(-) delete mode 100644 pkg/controller/planexecution/planexecution_controller_test.go diff --git a/pkg/controller/planexecution/planexecution_controller.go b/pkg/controller/planexecution/planexecution_controller.go index abe2c92b1..4ee091823 100644 --- a/pkg/controller/planexecution/planexecution_controller.go +++ b/pkg/controller/planexecution/planexecution_controller.go @@ -21,6 +21,8 @@ import ( "log" "strconv" + "k8s.io/apimachinery/pkg/util/json" + kudoengine "github.com/kudobuilder/kudo/pkg/engine" "gopkg.in/yaml.v2" @@ -38,7 +40,6 @@ import ( kudov1alpha1 "github.com/kudobuilder/kudo/pkg/apis/kudo/v1alpha1" "github.com/kudobuilder/kudo/pkg/util/health" "github.com/kudobuilder/kudo/pkg/util/template" - "github.com/tidwall/gjson" appsv1 "k8s.io/api/apps/v1" batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" @@ -488,15 +489,15 @@ func (r *ReconcilePlanExecution) Reconcile(request reconcile.Request) (reconcile key, _ := client.ObjectKeyFromObject(obj) truth := obj.DeepCopyObject() err := r.Client.Get(context.TODO(), key, truth) + rawObj, _ := json.Marshal(obj) if err == nil { log.Printf("PlanExecutionController: CreateOrUpdate Object present") //update - patchString, err := patchObject(obj, truth) - log.Printf("Going to apply patch\n%+v\n\n to object\n%+v\n", patchString, truth) + log.Printf("Going to apply patch\n%+v\n\n to object\n%+v\n", string(rawObj), truth) if err != nil { log.Printf("Error getting patch between truth and obj: %v\n", err) } else { - err = r.Client.Patch(context.TODO(), truth, client.ConstantPatch(types.MergePatchType, []byte(patchString))) + err = r.Client.Patch(context.TODO(), truth, client.ConstantPatch(types.StrategicMergePatchType, rawObj)) log.Printf("PlanExecutionController: CreateOrUpdate Patch: %v", err) } } else { @@ -620,30 +621,3 @@ func (r *ReconcilePlanExecution) Cleanup(obj runtime.Object) error { return nil } - -func patchObject(expected runtime.Object, actual runtime.Object) (string, error) { - p := client.MergeFrom(actual) - - mergePatch, err := p.Data(expected) - if err != nil { - return "", err - } - - specPatch := gjson.Get(string(mergePatch), "spec") - annotationPatch := gjson.Get(string(mergePatch), "metadata.annotations") - - annotationPatchStr := annotationPatch.String() - if annotationPatchStr == "" { - annotationPatchStr = "{}" - } - - specPatchStr := specPatch.String() - if specPatchStr == "" { - specPatchStr = "{}" - } - - return fmt.Sprintf("{\"metadata\": {\"annotations\": %v}, \"spec\": %v}", - annotationPatchStr, - specPatchStr, - ), nil -} diff --git a/pkg/controller/planexecution/planexecution_controller_test.go b/pkg/controller/planexecution/planexecution_controller_test.go deleted file mode 100644 index 06d724223..000000000 --- a/pkg/controller/planexecution/planexecution_controller_test.go +++ /dev/null @@ -1,68 +0,0 @@ -package planexecution - -import ( - "testing" - - testutils "github.com/kudobuilder/kudo/pkg/test/utils" - "github.com/stretchr/testify/assert" - "k8s.io/apimachinery/pkg/runtime" -) - -func TestPatchObject(t *testing.T) { - tests := []struct { - name string - actual runtime.Object - expected runtime.Object - patch string - }{ - { - name: "empty patch", - actual: testutils.NewPod("hello", "world"), - expected: testutils.NewPod("hello", "world"), - patch: `{"metadata": {"annotations": {}}, "spec": {}}`, - }, - { - name: "add annotations", - actual: testutils.NewPod("hello", "world"), - expected: testutils.WithAnnotations(testutils.NewPod("hello", "world"), map[string]string{"app": "nginx"}), - patch: `{"metadata": {"annotations": {"app":"nginx"}}, "spec": {}}`, - }, - { - name: "add spec", - actual: testutils.NewPod("hello", "world"), - expected: testutils.WithSpec(testutils.NewPod("hello", "world"), map[string]interface{}{"RestartPolicy": "Always"}), - patch: `{"metadata": {"annotations": {}}, "spec": {"RestartPolicy":"Always"}}`, - }, - { - name: "remove annotations", - actual: testutils.WithAnnotations(testutils.NewPod("hello", "world"), map[string]string{"app": "nginx"}), - expected: testutils.NewPod("hello", "world"), - patch: `{"metadata": {"annotations": {}}, "spec": {}}`, - }, - { - name: "remove spec", - actual: testutils.WithSpec(testutils.NewPod("hello", "world"), map[string]interface{}{"RestartPolicy": "Always"}), - expected: testutils.NewPod("hello", "world"), - patch: `{"metadata": {"annotations": {}}, "spec": {}}`, - }, - { - name: "change annotations", - actual: testutils.WithAnnotations(testutils.NewPod("hello", "world"), map[string]string{"app": "memcached"}), - expected: testutils.WithAnnotations(testutils.NewPod("hello", "world"), map[string]string{"app": "nginx"}), - patch: `{"metadata": {"annotations": {"app":"nginx"}}, "spec": {}}`, - }, - { - name: "change spec", - actual: testutils.WithSpec(testutils.NewPod("hello", "world"), map[string]interface{}{"RestartPolicy": "Never"}), - expected: testutils.WithSpec(testutils.NewPod("hello", "world"), map[string]interface{}{"RestartPolicy": "Always"}), - patch: `{"metadata": {"annotations": {}}, "spec": {"RestartPolicy":"Always"}}`, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - patch, err := patchObject(tt.expected, tt.actual) - assert.Nil(t, err) - assert.Equal(t, tt.patch, patch) - }) - } -}