Skip to content

Commit

Permalink
Merge pull request #455 from rramkumar1/master
Browse files Browse the repository at this point in the history
Remove JSONMergePatch util
  • Loading branch information
k8s-ci-robot authored Aug 29, 2018
2 parents ad082f3 + d583dcd commit 29b03e7
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 79 deletions.
49 changes: 0 additions & 49 deletions pkg/utils/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ import (
"encoding/json"
"fmt"

api_v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/util/jsonmergepatch"
"k8s.io/apimachinery/pkg/util/strategicpatch"
)

Expand All @@ -43,49 +40,3 @@ func StrategicMergePatchBytes(old, cur, refStruct interface{}) ([]byte, error) {

return patchBytes, nil
}

// JSONMergePatchBytes returns a patch between the old and new object using a JSON merge patch.
func JSONMergePatchBytes(old, cur interface{}) ([]byte, error) {
oldBytes, err := json.Marshal(old)
if err != nil {
return nil, fmt.Errorf("failed to marshal old object: %v", err)
}

newBytes, err := json.Marshal(cur)
if err != nil {
return nil, fmt.Errorf("failed to marshal new object: %v", err)
}

lastAppliedBytes, err := lastAppliedConfigurationBytes(old)
if err != nil {
return nil, fmt.Errorf("failed to get last applied config for old object: %v", err)
}

patchBytes, err := jsonmergepatch.CreateThreeWayJSONMergePatch(lastAppliedBytes, newBytes, oldBytes)
if err != nil {
return nil, fmt.Errorf("failed to create patch: %v", err)
}

return patchBytes, nil
}

// lastAppliedConfigurationBytes returns the bytes of the original configuration of an object
// from the annotation, or nil if no annotation is found. This is needed for the 3-way JSON merge patch.
func lastAppliedConfigurationBytes(obj interface{}) ([]byte, error) {
accessor, err := meta.Accessor(obj)
if err != nil {
return nil, err
}

annotations := accessor.GetAnnotations()
if annotations == nil {
return nil, nil
}

original, ok := annotations[api_v1.LastAppliedConfigAnnotation]
if !ok {
return nil, nil
}

return []byte(original), nil
}
30 changes: 0 additions & 30 deletions pkg/utils/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,33 +49,3 @@ func TestStrategicMergePatchBytes(t *testing.T) {
t.Errorf("StrategicMergePatchBytes(%+v, %+v) = %s ; want %s", ing, updated, string(b), expected)
}
}

func TestJSONMergePatchBytes(t *testing.T) {
// Patch an Ingress w/ a finalizer
ing := &extensions.Ingress{}
updated := &extensions.Ingress{
ObjectMeta: metav1.ObjectMeta{
Finalizers: []string{"foo"},
},
}
b, err := JSONMergePatchBytes(ing, updated)
if err != nil {
t.Fatal(err)
}
expected := `{"metadata":{"creationTimestamp":null,"finalizers":["foo"]}}`
if string(b) != expected {
t.Errorf("JSONMergePatchBytes(%+v, %+v) = %s ; want %s", ing, updated, string(b), expected)
}

// Patch an Ingress with the finalizer removed
ing = updated
updated = &extensions.Ingress{}
b, err = JSONMergePatchBytes(ing, updated)
if err != nil {
t.Fatal(err)
}
expected = `{"metadata":{"creationTimestamp":null}}`
if string(b) != expected {
t.Errorf("JSONMergePatchBytes(%+v, %+v) = %s ; want %s", ing, updated, string(b), expected)
}
}

0 comments on commit 29b03e7

Please sign in to comment.