diff --git a/api/operator/v1alpha1/zz_generated.deepcopy.go b/api/operator/v1alpha1/zz_generated.deepcopy.go index 255f8c6..998ca64 100644 --- a/api/operator/v1alpha1/zz_generated.deepcopy.go +++ b/api/operator/v1alpha1/zz_generated.deepcopy.go @@ -21,7 +21,7 @@ limitations under the License. package v1alpha1 import ( - "k8s.io/apimachinery/pkg/apis/meta/v1" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" ) diff --git a/go.mod b/go.mod index e6c205e..298eb96 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.21 require ( github.com/anthhub/forwarder v1.1.1-0.20230315114022-63dcf7b46a1a - github.com/evanphx/json-patch v5.7.0+incompatible github.com/go-logr/logr v1.2.4 github.com/hashicorp/go-cleanhttp v0.5.2 github.com/onsi/gomega v1.28.1 @@ -13,7 +12,6 @@ require ( github.com/operator-framework/operator-lifecycle-manager v0.22.0 github.com/rs/xid v1.5.0 github.com/spf13/cobra v1.7.0 - github.com/stretchr/testify v1.8.4 github.com/wI2L/jsondiff v0.4.0 golang.org/x/time v0.3.0 gopkg.in/yaml.v3 v3.0.1 @@ -52,6 +50,7 @@ require ( github.com/docker/go-metrics v0.0.1 // indirect github.com/docker/go-units v0.5.0 // indirect github.com/emicklei/go-restful/v3 v3.11.0 // indirect + github.com/evanphx/json-patch v5.7.0+incompatible // indirect github.com/evanphx/json-patch/v5 v5.6.0 // indirect github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect github.com/fatih/color v1.13.0 // indirect @@ -109,7 +108,6 @@ require ( github.com/opencontainers/image-spec v1.1.0-rc5 // indirect github.com/peterbourgon/diskv v2.0.1+incompatible // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_golang v1.16.0 // indirect github.com/prometheus/client_model v0.4.0 // indirect github.com/prometheus/common v0.44.0 // indirect diff --git a/pkg/patch/patch.go b/pkg/patch/patch.go deleted file mode 100644 index ab9beeb..0000000 --- a/pkg/patch/patch.go +++ /dev/null @@ -1,114 +0,0 @@ -/* -Copyright 2022. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package patch - -import ( - "reflect" - - jsonpatch "github.com/evanphx/json-patch" - - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/util/json" -) - -func MergePatch(source interface{}, target interface{}) ([]byte, error) { - sourceJSON, err := json.Marshal(source) - if err != nil { - return nil, err - } - targetJSON, err := json.Marshal(target) - if err != nil { - return nil, err - } - mergePatch, err := jsonpatch.CreateMergePatch(sourceJSON, targetJSON) - if err != nil { - return nil, err - } - - switch target.(type) { - case *unstructured.Unstructured: - // Take the JSON merge patch as is for unstructured objects - return mergePatch, nil - default: - // Otherwise, for typed objects, remove null fields from the JSON merge patch, - // so that values managed by controllers server-side are not deleted. - var positivePatch map[string]interface{} - err = json.Unmarshal(mergePatch, &positivePatch) - if err != nil { - return nil, err - } - removeNilValues(reflect.ValueOf(positivePatch), reflect.Value{}) - // Return an empty patch if no keys remain - if len(positivePatch) == 0 { - return make([]byte, 0), nil - } - return json.Marshal(positivePatch) - } -} - -func ApplyPatch(source runtime.Object) (*unstructured.Unstructured, error) { - switch s := source.(type) { - case *unstructured.Unstructured: - // Directly take the unstructured object as apply patch - return s, nil - default: - // Otherwise, for typed objects, remove null fields from the apply patch, - // so that ownership is not taken for non-managed fields. - // See https://github.com/kubernetes/enhancements/tree/master/keps/sig-api-machinery/2155-clientgo-apply - sourceJSON, err := json.Marshal(source) - if err != nil { - return nil, err - } - var positivePatch map[string]interface{} - err = json.Unmarshal(sourceJSON, &positivePatch) - if err != nil { - return nil, err - } - removeNilValues(reflect.ValueOf(positivePatch), reflect.Value{}) - - return &unstructured.Unstructured{Object: positivePatch}, nil - } -} - -func removeNilValues(v reflect.Value, parent reflect.Value) { - for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface { - v = v.Elem() - } - switch v.Kind() { - case reflect.Array, reflect.Slice: - for i := 0; i < v.Len(); i++ { - removeNilValues(v.Index(i), v) - } - case reflect.Map: - for _, k := range v.MapKeys() { - switch c := v.MapIndex(k); { - case !c.IsValid(): - // Skip keys previously deleted - continue - case c.IsNil(), c.Elem().Kind() == reflect.Map && len(c.Elem().MapKeys()) == 0: - v.SetMapIndex(k, reflect.Value{}) - default: - removeNilValues(c, v) - } - } - // Back process the parent map in case it has been emptied so that it's deleted as well - if len(v.MapKeys()) == 0 && parent.Kind() == reflect.Map { - removeNilValues(parent, reflect.Value{}) - } - } -} diff --git a/pkg/patch/patch_test.go b/pkg/patch/patch_test.go deleted file mode 100644 index 2105ce1..0000000 --- a/pkg/patch/patch_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package patch - -import ( - "testing" - - "github.com/stretchr/testify/assert" - appsv1 "k8s.io/api/apps/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestMergePatch(t *testing.T) { - d1 := appsv1.Deployment{ - ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - }, - } - d2 := appsv1.Deployment{ - ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - }, - } - - data, err := MergePatch(d1, d2) - - assert.Nil(t, err) - assert.NotNil(t, data) - assert.Len(t, data, 0) -}