-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #453 from rramkumar1/finalizer-utils-refactor
Add JSONMerge patch utilities and move some files around
- Loading branch information
Showing
11 changed files
with
1,456 additions
and
77 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.