Skip to content

Commit

Permalink
Merge pull request #446 from rramkumar1/finalizer-lib
Browse files Browse the repository at this point in the history
Add some utility functions to support finalizers
  • Loading branch information
k8s-ci-robot authored Aug 22, 2018
2 parents 85266ca + 87d5555 commit 7501865
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Gopkg.lock

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

62 changes: 62 additions & 0 deletions pkg/utils/finalizer.go
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
}
51 changes: 51 additions & 0 deletions pkg/utils/finalizer_test.go
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)
}
}
120 changes: 120 additions & 0 deletions vendor/k8s.io/apimachinery/pkg/util/rand/rand.go

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

33 changes: 33 additions & 0 deletions vendor/k8s.io/kubernetes/pkg/util/slice/BUILD

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

91 changes: 91 additions & 0 deletions vendor/k8s.io/kubernetes/pkg/util/slice/slice.go

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

0 comments on commit 7501865

Please sign in to comment.