Skip to content

Commit

Permalink
Merge pull request #453 from rramkumar1/finalizer-utils-refactor
Browse files Browse the repository at this point in the history
Add JSONMerge patch utilities and move some files around
  • Loading branch information
k8s-ci-robot authored Aug 29, 2018
2 parents 55429c8 + f3c93a3 commit ad082f3
Show file tree
Hide file tree
Showing 11 changed files with 1,456 additions and 77 deletions.
9 changes: 8 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 0 additions & 25 deletions pkg/utils/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ limitations under the License.
package utils

import (
"encoding/json"
"fmt"

meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/kubernetes/pkg/util/slice"
)

Expand All @@ -39,24 +35,3 @@ func NeedToAddFinalizer(m meta_v1.ObjectMeta, key string) bool {
func hasFinalizer(m meta_v1.ObjectMeta, key string) bool {
return slice.ContainsString(m.Finalizers, key, nil)
}

// GetPatchBytes returns a patch which is the difference between the old and new objects.
// Note: refStruct is a empty struct of the type which the patch is being generated for.
func GetPatchBytes(old interface{}, cur interface{}, refStruct 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)
}

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

return patchBytes, nil
}
51 changes: 0 additions & 51 deletions pkg/utils/finalizer_test.go

This file was deleted.

91 changes: 91 additions & 0 deletions pkg/utils/patch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Copyright 2018 The Kubernetes Authors.
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 utils

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"
)

// StrategicMergePatchBytes returns a patch between the old and new object using a strategic merge patch.
// Note: refStruct is a empty struct of the type which the patch is being generated for.
func StrategicMergePatchBytes(old, cur, refStruct 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)
}

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

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
}
81 changes: 81 additions & 0 deletions pkg/utils/patch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Copyright 2018 The Kubernetes Authors.
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 utils

import (
"testing"

extensions "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

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

// Patch an Ingress with the finalizer removed
ing = updated
updated = &extensions.Ingress{}
b, err = StrategicMergePatchBytes(ing, updated, extensions.Ingress{})
if err != nil {
t.Fatal(err)
}
expected = `{"metadata":{"finalizers":null}}`
if string(b) != expected {
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)
}
}
16 changes: 16 additions & 0 deletions vendor/github.com/evanphx/json-patch/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions vendor/github.com/evanphx/json-patch/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions vendor/github.com/evanphx/json-patch/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ad082f3

Please sign in to comment.