deep copy issue in getting controller is solved

This commit is contained in:
Hiranmoy Das Chowdhury 2024-04-09 11:14:33 +06:00
parent 3dedb8eb8c
commit 0cd2588d4f
3 changed files with 16 additions and 5 deletions

View File

@ -41,6 +41,7 @@
- k8s.io/utils/net
- k8s.io/utils/strings
- k8s.io/klog
- k8s.io/utils/ptr
- baseImportPath: "./staging/src/k8s.io/api"
allowedImports:

View File

@ -18,6 +18,7 @@ package v1
import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/utils/ptr"
)
// IsControlledBy checks if the object has a controllerRef set to the given owner
@ -36,10 +37,14 @@ func GetControllerOf(controllee Object) *OwnerReference {
return nil
}
cp := *ref
cp.Controller = ptr.To(*ref.Controller)
if ref.BlockOwnerDeletion != nil {
cp.BlockOwnerDeletion = ptr.To(*ref.BlockOwnerDeletion)
}
return &cp
}
// GetControllerOf returns a pointer to the controllerRef if controllee has a controller
// GetControllerOfNoCopy returns a pointer to the controllerRef if controllee has a controller
func GetControllerOfNoCopy(controllee Object) *OwnerReference {
refs := controllee.GetOwnerReferences()
for i := range refs {
@ -52,14 +57,12 @@ func GetControllerOfNoCopy(controllee Object) *OwnerReference {
// 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,
BlockOwnerDeletion: ptr.To(true),
Controller: ptr.To(true),
}
}

View File

@ -69,6 +69,7 @@ func TestGetControllerOf(t *testing.T) {
},
}
controllerRef := NewControllerRef(obj1, gvk)
controllerRef.BlockOwnerDeletion = nil
var falseRef = false
obj2 := &metaObj{
ObjectMeta: ObjectMeta{
@ -95,6 +96,12 @@ func TestGetControllerOf(t *testing.T) {
if c.Name != controllerRef.Name || c.UID != controllerRef.UID {
t.Errorf("Incorrect result of GetControllerOf: %v", c)
}
// test that all pointers are also deep copied
if (c.Controller == controllerRef.Controller) ||
(c.BlockOwnerDeletion != nil && c.BlockOwnerDeletion == controllerRef.BlockOwnerDeletion) {
t.Errorf("GetControllerOf did not return deep copy: %v", c)
}
}
func BenchmarkGetControllerOf(b *testing.B) {