-
Notifications
You must be signed in to change notification settings - Fork 303
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 #446 from rramkumar1/finalizer-lib
Add some utility functions to support finalizers
- Loading branch information
Showing
6 changed files
with
360 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
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" | ||
|
||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/strategicpatch" | ||
"k8s.io/kubernetes/pkg/util/slice" | ||
) | ||
|
||
// FinalizerKeySuffix is a suffix for finalizers added by the controller. | ||
// A full key could be something like "ingress.finalizer.cloud.google.com" | ||
const FinalizerKeySuffix = "finalizer.cloud.google.com" | ||
|
||
// IsDeletionCandidate is true if the passed in meta contains the specified finalizer. | ||
func IsDeletionCandidate(m meta_v1.ObjectMeta, key string) bool { | ||
return m.DeletionTimestamp != nil && hasFinalizer(m, key) | ||
} | ||
|
||
// NeedToAddFinalizer is true if the passed in meta does not contain the specified finalizer. | ||
func NeedToAddFinalizer(m meta_v1.ObjectMeta, key string) bool { | ||
return m.DeletionTimestamp == nil && !hasFinalizer(m, key) | ||
} | ||
|
||
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 | ||
} |
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,51 @@ | ||
/* | ||
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 TestGetPatchBytes(t *testing.T) { | ||
// Patch an Ingress w/ a finalizer | ||
ing := &extensions.Ingress{} | ||
updated := &extensions.Ingress{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Finalizers: []string{"foo"}, | ||
}, | ||
} | ||
b, err := getPatchBytes(ing, updated, extensions.Ingress{}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
expected := `{"metadata":{"finalizers":["foo"]}}` | ||
if string(b) != expected { | ||
t.Errorf("getPatchBytes(%+v, %+v) = %s ; want %s", ing, updated, string(b), expected) | ||
} | ||
|
||
// Patch an Ingress with the finalizer removed | ||
ing = updated | ||
updated = &extensions.Ingress{} | ||
b, err = getPatchBytes(ing, updated, extensions.Ingress{}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
expected = `{"metadata":{"finalizers":null}}` | ||
if string(b) != expected { | ||
t.Errorf("getPatchBytes(%+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.