forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Helper methods dealing with ControllerRef
- Loading branch information
Nail Islamov
committed
Jul 12, 2017
1 parent
0744964
commit e86faca
Showing
3 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
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
54 changes: 54 additions & 0 deletions
54
staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/controller_ref.go
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,54 @@ | ||
/* | ||
Copyright 2017 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 v1 | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
// IsControlledBy checks if the object has a controllerRef set to the given owner | ||
func IsControlledBy(obj Object, owner Object) bool { | ||
ref := GetControllerOf(obj) | ||
if ref == nil { | ||
return false | ||
} | ||
return ref.UID == owner.GetUID() | ||
} | ||
|
||
// GetControllerOf returns a pointer to a copy of the controllerRef if controllee has a controller | ||
func GetControllerOf(controllee Object) *OwnerReference { | ||
for _, ref := range controllee.GetOwnerReferences() { | ||
if ref.Controller != nil && *ref.Controller { | ||
return &ref | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// NewControllerRef creates an OwnerReference pointing to the given owner. | ||
func NewControllerRef(owner Object, gvk schema.GroupVersionKind) *OwnerReference { | ||
blockOwnerDeletion := true | ||
isController := true | ||
return &OwnerReference{ | ||
APIVersion: gvk.GroupVersion().String(), | ||
Kind: gvk.Kind, | ||
Name: owner.GetName(), | ||
UID: owner.GetUID(), | ||
BlockOwnerDeletion: &blockOwnerDeletion, | ||
Controller: &isController, | ||
} | ||
} |
133 changes: 133 additions & 0 deletions
133
staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/controller_ref_test.go
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,133 @@ | ||
/* | ||
Copyright 2017 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 v1 | ||
|
||
import ( | ||
"testing" | ||
|
||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
type metaObj struct { | ||
ObjectMeta | ||
TypeMeta | ||
} | ||
|
||
func TestNewControllerRef(t *testing.T) { | ||
gvk := schema.GroupVersionKind{ | ||
Group: "group", | ||
Version: "v1", | ||
Kind: "Kind", | ||
} | ||
obj1 := &metaObj{ | ||
ObjectMeta: ObjectMeta{ | ||
Name: "name", | ||
UID: "uid1", | ||
}, | ||
} | ||
controllerRef := NewControllerRef(obj1, gvk) | ||
if controllerRef.UID != obj1.UID { | ||
t.Errorf("Incorrect UID: %s", controllerRef.UID) | ||
} | ||
if controllerRef.Controller == nil || *controllerRef.Controller != true { | ||
t.Error("Controller must be set to true") | ||
} | ||
if controllerRef.BlockOwnerDeletion == nil || *controllerRef.BlockOwnerDeletion != true { | ||
t.Error("BlockOwnerDeletion must be set to true") | ||
} | ||
if controllerRef.APIVersion == "" || | ||
controllerRef.Kind == "" || | ||
controllerRef.Name == "" { | ||
t.Errorf("All controllerRef fields must be set: %v", controllerRef) | ||
} | ||
} | ||
|
||
func TestGetControllerOf(t *testing.T) { | ||
gvk := schema.GroupVersionKind{ | ||
Group: "group", | ||
Version: "v1", | ||
Kind: "Kind", | ||
} | ||
obj1 := &metaObj{ | ||
ObjectMeta: ObjectMeta{ | ||
UID: "uid1", | ||
Name: "name1", | ||
}, | ||
} | ||
controllerRef := NewControllerRef(obj1, gvk) | ||
var falseRef = false | ||
obj2 := &metaObj{ | ||
ObjectMeta: ObjectMeta{ | ||
UID: "uid2", | ||
Name: "name1", | ||
OwnerReferences: []OwnerReference{ | ||
{ | ||
Name: "owner1", | ||
Controller: &falseRef, | ||
}, | ||
*controllerRef, | ||
{ | ||
Name: "owner2", | ||
Controller: &falseRef, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
if GetControllerOf(obj1) != nil { | ||
t.Error("GetControllerOf must return null") | ||
} | ||
c := GetControllerOf(obj2) | ||
if c.Name != controllerRef.Name || c.UID != controllerRef.UID { | ||
t.Errorf("Incorrect result of GetControllerOf: %v", c) | ||
} | ||
} | ||
|
||
func TestIsControlledBy(t *testing.T) { | ||
gvk := schema.GroupVersionKind{ | ||
Group: "group", | ||
Version: "v1", | ||
Kind: "Kind", | ||
} | ||
obj1 := &metaObj{ | ||
ObjectMeta: ObjectMeta{ | ||
UID: "uid1", | ||
}, | ||
} | ||
obj2 := &metaObj{ | ||
ObjectMeta: ObjectMeta{ | ||
UID: "uid2", | ||
OwnerReferences: []OwnerReference{ | ||
*NewControllerRef(obj1, gvk), | ||
}, | ||
}, | ||
} | ||
obj3 := &metaObj{ | ||
ObjectMeta: ObjectMeta{ | ||
UID: "uid3", | ||
OwnerReferences: []OwnerReference{ | ||
*NewControllerRef(obj2, gvk), | ||
}, | ||
}, | ||
} | ||
if !IsControlledBy(obj2, obj1) || !IsControlledBy(obj3, obj2) { | ||
t.Error("Incorrect IsControlledBy result: false") | ||
} | ||
if IsControlledBy(obj3, obj1) { | ||
t.Error("Incorrect IsControlledBy result: true") | ||
} | ||
} |