@@ -117,6 +117,105 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfKubeadmConfigIsReady(t *
117
117
g .Expect (result .RequeueAfter ).To (Equal (time .Duration (0 )))
118
118
}
119
119
120
+ // Reconcile returns early if the kubeadm config is ready because it should never re-generate bootstrap data.
121
+ func TestKubeadmConfigReconciler_TestSecretOwnerReferenceReconciliation (t * testing.T ) {
122
+ g := NewWithT (t )
123
+
124
+ clusterName := "my-cluster"
125
+ cluster := builder .Cluster (metav1 .NamespaceDefault , clusterName ).Build ()
126
+ machine := builder .Machine (metav1 .NamespaceDefault , "machine" ).
127
+ WithVersion ("v1.19.1" ).
128
+ WithClusterName (clusterName ).
129
+ WithBootstrapTemplate (bootstrapbuilder .KubeadmConfig (metav1 .NamespaceDefault , "cfg" ).Unstructured ()).
130
+ Build ()
131
+ machine .Spec .Bootstrap .DataSecretName = pointer .String ("something" )
132
+
133
+ config := newKubeadmConfig (metav1 .NamespaceDefault , "cfg" )
134
+ config .SetOwnerReferences (util .EnsureOwnerRef (config .GetOwnerReferences (), metav1.OwnerReference {
135
+ APIVersion : machine .APIVersion ,
136
+ Kind : machine .Kind ,
137
+ Name : machine .Name ,
138
+ UID : machine .UID ,
139
+ }))
140
+ secret := & corev1.Secret {
141
+ ObjectMeta : metav1.ObjectMeta {
142
+ Name : config .Name ,
143
+ Namespace : config .Namespace ,
144
+ },
145
+ Type : corev1 .SecretTypeBootstrapToken ,
146
+ }
147
+ config .Status .Ready = true
148
+
149
+ objects := []client.Object {
150
+ config ,
151
+ machine ,
152
+ secret ,
153
+ cluster ,
154
+ }
155
+ myclient := fake .NewClientBuilder ().WithObjects (objects ... ).Build ()
156
+
157
+ k := & KubeadmConfigReconciler {
158
+ Client : myclient ,
159
+ }
160
+
161
+ request := ctrl.Request {
162
+ NamespacedName : client.ObjectKey {
163
+ Namespace : metav1 .NamespaceDefault ,
164
+ Name : "cfg" ,
165
+ },
166
+ }
167
+ var err error
168
+ key := client .ObjectKeyFromObject (config )
169
+ actual := & corev1.Secret {}
170
+
171
+ t .Run ("KubeadmConfig ownerReference is added on first reconcile" , func (t * testing.T ) {
172
+ _ , err = k .Reconcile (ctx , request )
173
+ g .Expect (err ).NotTo (HaveOccurred ())
174
+
175
+ g .Expect (myclient .Get (ctx , key , actual )).To (Succeed ())
176
+
177
+ controllerOwner := metav1 .GetControllerOf (actual )
178
+ g .Expect (controllerOwner ).To (Not (BeNil ()))
179
+ g .Expect (controllerOwner .Kind ).To (Equal (config .Kind ))
180
+ g .Expect (controllerOwner .Name ).To (Equal (config .Name ))
181
+ })
182
+
183
+ t .Run ("KubeadmConfig ownerReference re-reconciled without error" , func (t * testing.T ) {
184
+ _ , err = k .Reconcile (ctx , request )
185
+ g .Expect (err ).NotTo (HaveOccurred ())
186
+
187
+ g .Expect (myclient .Get (ctx , key , actual )).To (Succeed ())
188
+
189
+ controllerOwner := metav1 .GetControllerOf (actual )
190
+ g .Expect (controllerOwner ).To (Not (BeNil ()))
191
+ g .Expect (controllerOwner .Kind ).To (Equal (config .Kind ))
192
+ g .Expect (controllerOwner .Name ).To (Equal (config .Name ))
193
+ })
194
+ t .Run ("non-KubeadmConfig controller OwnerReference is replaced" , func (t * testing.T ) {
195
+ g .Expect (myclient .Get (ctx , key , actual )).To (Succeed ())
196
+
197
+ actual .SetOwnerReferences ([]metav1.OwnerReference {
198
+ {
199
+ APIVersion : machine .APIVersion ,
200
+ Kind : machine .Kind ,
201
+ Name : machine .Name ,
202
+ UID : machine .UID ,
203
+ Controller : pointer .Bool (true ),
204
+ }})
205
+ g .Expect (myclient .Update (ctx , actual )).To (Succeed ())
206
+
207
+ _ , err = k .Reconcile (ctx , request )
208
+ g .Expect (err ).NotTo (HaveOccurred ())
209
+
210
+ g .Expect (myclient .Get (ctx , key , actual )).To (Succeed ())
211
+
212
+ controllerOwner := metav1 .GetControllerOf (actual )
213
+ g .Expect (controllerOwner ).To (Not (BeNil ()))
214
+ g .Expect (controllerOwner .Kind ).To (Equal (config .Kind ))
215
+ g .Expect (controllerOwner .Name ).To (Equal (config .Name ))
216
+ })
217
+ }
218
+
120
219
// Reconcile returns nil if the referenced Machine cannot be found.
121
220
func TestKubeadmConfigReconciler_Reconcile_ReturnNilIfReferencedMachineIsNotFound (t * testing.T ) {
122
221
g := NewWithT (t )
0 commit comments