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/net
- k8s.io/utils/strings - k8s.io/utils/strings
- k8s.io/klog - k8s.io/klog
- k8s.io/utils/ptr
- baseImportPath: "./staging/src/k8s.io/api" - baseImportPath: "./staging/src/k8s.io/api"
allowedImports: allowedImports:

View File

@ -18,6 +18,7 @@ package v1
import ( import (
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/utils/ptr"
) )
// IsControlledBy checks if the object has a controllerRef set to the given owner // IsControlledBy checks if the object has a controllerRef set to the given owner
@ -36,10 +37,14 @@ func GetControllerOf(controllee Object) *OwnerReference {
return nil return nil
} }
cp := *ref cp := *ref
cp.Controller = ptr.To(*ref.Controller)
if ref.BlockOwnerDeletion != nil {
cp.BlockOwnerDeletion = ptr.To(*ref.BlockOwnerDeletion)
}
return &cp 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 { func GetControllerOfNoCopy(controllee Object) *OwnerReference {
refs := controllee.GetOwnerReferences() refs := controllee.GetOwnerReferences()
for i := range refs { for i := range refs {
@ -52,14 +57,12 @@ func GetControllerOfNoCopy(controllee Object) *OwnerReference {
// NewControllerRef creates an OwnerReference pointing to the given owner. // NewControllerRef creates an OwnerReference pointing to the given owner.
func NewControllerRef(owner Object, gvk schema.GroupVersionKind) *OwnerReference { func NewControllerRef(owner Object, gvk schema.GroupVersionKind) *OwnerReference {
blockOwnerDeletion := true
isController := true
return &OwnerReference{ return &OwnerReference{
APIVersion: gvk.GroupVersion().String(), APIVersion: gvk.GroupVersion().String(),
Kind: gvk.Kind, Kind: gvk.Kind,
Name: owner.GetName(), Name: owner.GetName(),
UID: owner.GetUID(), UID: owner.GetUID(),
BlockOwnerDeletion: &blockOwnerDeletion, BlockOwnerDeletion: ptr.To(true),
Controller: &isController, Controller: ptr.To(true),
} }
} }

View File

@ -69,6 +69,7 @@ func TestGetControllerOf(t *testing.T) {
}, },
} }
controllerRef := NewControllerRef(obj1, gvk) controllerRef := NewControllerRef(obj1, gvk)
controllerRef.BlockOwnerDeletion = nil
var falseRef = false var falseRef = false
obj2 := &metaObj{ obj2 := &metaObj{
ObjectMeta: ObjectMeta{ ObjectMeta: ObjectMeta{
@ -95,6 +96,12 @@ func TestGetControllerOf(t *testing.T) {
if c.Name != controllerRef.Name || c.UID != controllerRef.UID { if c.Name != controllerRef.Name || c.UID != controllerRef.UID {
t.Errorf("Incorrect result of GetControllerOf: %v", c) 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) { func BenchmarkGetControllerOf(b *testing.B) {