@@ -22,9 +22,12 @@ import (
2222 . "github.com/onsi/gomega"
2323 appsv1 "k8s.io/api/apps/v1"
2424 corev1 "k8s.io/api/core/v1"
25+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2526 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2627 "k8s.io/apimachinery/pkg/runtime"
28+ "k8s.io/apimachinery/pkg/types"
2729 "k8s.io/client-go/kubernetes/scheme"
30+ "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2831)
2932
3033var _ = Describe ("Metautils" , func () {
@@ -81,4 +84,90 @@ var _ = Describe("Metautils", func() {
8184 )).To (HaveOccurred ())
8285 })
8386 })
87+
88+ Describe ("IsControlledBy" , func () {
89+ It ("should report true if the object is controlled by another" , func () {
90+ By ("making a controlling object" )
91+ owner := & corev1.ConfigMap {
92+ ObjectMeta : metav1.ObjectMeta {
93+ Namespace : corev1 .NamespaceDefault ,
94+ Name : "owner" ,
95+ UID : types .UID ("owner-uuid" ),
96+ },
97+ }
98+
99+ By ("making an object to be controlled" )
100+ owned := & corev1.ConfigMap {
101+ ObjectMeta : metav1.ObjectMeta {
102+ Namespace : corev1 .NamespaceDefault ,
103+ Name : "owned" ,
104+ UID : types .UID ("owned-uuid" ),
105+ },
106+ }
107+
108+ By ("setting the controller reference" )
109+ Expect (controllerutil .SetControllerReference (owner , owned , scheme .Scheme )).To (Succeed ())
110+
111+ By ("asserting the object reports as controlled" )
112+ Expect (IsControlledBy (scheme .Scheme , owner , owned )).To (BeTrue (), "object should be controlled by owner, object: %#v, owner: %#v" , owned , owner )
113+ })
114+
115+ It ("should report false if the object is not controlled by another" , func () {
116+ By ("making two regular objects" )
117+ obj1 := & corev1.ConfigMap {
118+ ObjectMeta : metav1.ObjectMeta {
119+ Namespace : corev1 .NamespaceDefault ,
120+ Name : "obj1" ,
121+ UID : types .UID ("obj1-uuid" ),
122+ },
123+ }
124+ obj2 := & corev1.ConfigMap {
125+ ObjectMeta : metav1.ObjectMeta {
126+ Namespace : corev1 .NamespaceDefault ,
127+ Name : "obj2" ,
128+ UID : types .UID ("obj2-uuid" ),
129+ },
130+ }
131+
132+ By ("asserting the object does not report as controlled" )
133+ Expect (IsControlledBy (scheme .Scheme , obj1 , obj2 )).To (BeFalse (), "object should not be controlled, obj1: %#v, obj2: %#v" , obj1 , obj2 )
134+ })
135+
136+ It ("should error if it cannot determine the gvk of an object" , func () {
137+ By ("creating an object whose type is not registered in the default scheme" )
138+ obj1 := & struct { corev1.ConfigMap }{
139+ ConfigMap : corev1.ConfigMap {
140+ ObjectMeta : metav1.ObjectMeta {
141+ Namespace : corev1 .NamespaceDefault ,
142+ Name : "obj1" ,
143+ UID : types .UID ("obj1-uuid" ),
144+ },
145+ },
146+ }
147+
148+ By ("making a controlling object" )
149+ owner := & corev1.ConfigMap {
150+ ObjectMeta : metav1.ObjectMeta {
151+ Namespace : corev1 .NamespaceDefault ,
152+ Name : "owner" ,
153+ UID : types .UID ("owner-uuid" ),
154+ },
155+ }
156+
157+ By ("making a regular object" )
158+ owned := & corev1.ConfigMap {
159+ ObjectMeta : metav1.ObjectMeta {
160+ Namespace : corev1 .NamespaceDefault ,
161+ Name : "owned" ,
162+ UID : types .UID ("owned-uuid" ),
163+ },
164+ }
165+
166+ By ("setting the controller for owned" )
167+ Expect (controllerutil .SetControllerReference (owner , owned , scheme .Scheme )).To (Succeed ())
168+
169+ _ , err := IsControlledBy (scheme .Scheme , obj1 , owned )
170+ Expect (err ).To (HaveOccurred ())
171+ })
172+ })
84173})
0 commit comments