@@ -253,13 +253,13 @@ func TestReconcileNormalNode(t *testing.T) {
253253func TestReconcileNormalEtcd (t * testing.T ) {
254254 inMemoryMachineWithNodeNotYetProvisioned := & infrav1.InMemoryMachine {
255255 ObjectMeta : metav1.ObjectMeta {
256- Name : "bar " ,
256+ Name : "bar0 " ,
257257 },
258258 }
259259
260- inMemoryMachineWithNodeProvisioned := & infrav1.InMemoryMachine {
260+ inMemoryMachineWithNodeProvisioned1 := & infrav1.InMemoryMachine {
261261 ObjectMeta : metav1.ObjectMeta {
262- Name : "bar " ,
262+ Name : "bar1 " ,
263263 },
264264 Spec : infrav1.InMemoryMachineSpec {
265265 Behaviour : & infrav1.InMemoryMachineBehaviour {
@@ -313,7 +313,7 @@ func TestReconcileNormalEtcd(t *testing.T) {
313313
314314 manager := cmanager .New (scheme )
315315
316- host := "127.0.0.1"
316+ host := "127.0.0.1" //nolint:goconst
317317 wcmux , err := server .NewWorkloadClustersMux (manager , host , server.CustomPorts {
318318 // NOTE: make sure to use ports different than other tests, so we can run tests in parallel
319319 MinPort : server .DefaultMinPort + 1000 ,
@@ -332,15 +332,15 @@ func TestReconcileNormalEtcd(t *testing.T) {
332332 r .CloudManager .AddResourceGroup (klog .KObj (cluster ).String ())
333333 c := r .CloudManager .GetResourceGroup (klog .KObj (cluster ).String ()).GetClient ()
334334
335- res , err := r .reconcileNormalETCD (ctx , cluster , cpMachine , inMemoryMachineWithNodeProvisioned )
335+ res , err := r .reconcileNormalETCD (ctx , cluster , cpMachine , inMemoryMachineWithNodeProvisioned1 )
336336 g .Expect (err ).ToNot (HaveOccurred ())
337337 g .Expect (res .IsZero ()).To (BeFalse ())
338- g .Expect (conditions .IsFalse (inMemoryMachineWithNodeProvisioned , infrav1 .EtcdProvisionedCondition )).To (BeTrue ())
338+ g .Expect (conditions .IsFalse (inMemoryMachineWithNodeProvisioned1 , infrav1 .EtcdProvisionedCondition )).To (BeTrue ())
339339
340340 got := & corev1.Pod {
341341 ObjectMeta : metav1.ObjectMeta {
342342 Namespace : metav1 .NamespaceSystem ,
343- Name : fmt .Sprintf ("etcd-%s" , inMemoryMachineWithNodeNotYetProvisioned .Name ),
343+ Name : fmt .Sprintf ("etcd-%s" , inMemoryMachineWithNodeProvisioned1 .Name ),
344344 },
345345 }
346346 err = c .Get (ctx , client .ObjectKeyFromObject (got ), got )
@@ -350,32 +350,111 @@ func TestReconcileNormalEtcd(t *testing.T) {
350350 g := NewWithT (t )
351351
352352 g .Eventually (func () bool {
353- res , err := r .reconcileNormalETCD (ctx , cluster , cpMachine , inMemoryMachineWithNodeProvisioned )
353+ res , err := r .reconcileNormalETCD (ctx , cluster , cpMachine , inMemoryMachineWithNodeProvisioned1 )
354354 g .Expect (err ).ToNot (HaveOccurred ())
355355 if ! res .IsZero () {
356356 time .Sleep (res .RequeueAfter / 100 * 90 )
357357 }
358358 return res .IsZero ()
359- }, inMemoryMachineWithNodeProvisioned .Spec .Behaviour .Etcd .Provisioning .StartupDuration .Duration * 2 ).Should (BeTrue ())
359+ }, inMemoryMachineWithNodeProvisioned1 .Spec .Behaviour .Etcd .Provisioning .StartupDuration .Duration * 2 ).Should (BeTrue ())
360360
361361 err = c .Get (ctx , client .ObjectKeyFromObject (got ), got )
362362 g .Expect (err ).ToNot (HaveOccurred ())
363363
364- g .Expect (conditions .IsTrue (inMemoryMachineWithNodeProvisioned , infrav1 .EtcdProvisionedCondition )).To (BeTrue ())
365- g .Expect (conditions .Get (inMemoryMachineWithNodeProvisioned , infrav1 .EtcdProvisionedCondition ).LastTransitionTime .Time ).To (BeTemporally (">" , conditions .Get (inMemoryMachineWithNodeProvisioned , infrav1 .NodeProvisionedCondition ).LastTransitionTime .Time , inMemoryMachineWithNodeProvisioned .Spec .Behaviour .Etcd .Provisioning .StartupDuration .Duration ))
364+ g .Expect (got .Annotations ).To (HaveKey (cloudv1 .EtcdClusterIDAnnotationName ))
365+ g .Expect (got .Annotations ).To (HaveKey (cloudv1 .EtcdMemberIDAnnotationName ))
366+ g .Expect (got .Annotations ).To (HaveKey (cloudv1 .EtcdLeaderFromAnnotationName ))
367+
368+ g .Expect (conditions .IsTrue (inMemoryMachineWithNodeProvisioned1 , infrav1 .EtcdProvisionedCondition )).To (BeTrue ())
369+ g .Expect (conditions .Get (inMemoryMachineWithNodeProvisioned1 , infrav1 .EtcdProvisionedCondition ).LastTransitionTime .Time ).To (BeTemporally (">" , conditions .Get (inMemoryMachineWithNodeProvisioned1 , infrav1 .NodeProvisionedCondition ).LastTransitionTime .Time , inMemoryMachineWithNodeProvisioned1 .Spec .Behaviour .Etcd .Provisioning .StartupDuration .Duration ))
366370 })
367371
368372 t .Run ("no-op after it is provisioned" , func (t * testing.T ) {
369373 g := NewWithT (t )
370374
371- res , err := r .reconcileNormalETCD (ctx , cluster , cpMachine , inMemoryMachineWithNodeProvisioned )
375+ res , err := r .reconcileNormalETCD (ctx , cluster , cpMachine , inMemoryMachineWithNodeProvisioned1 )
372376 g .Expect (err ).ToNot (HaveOccurred ())
373377 g .Expect (res .IsZero ()).To (BeTrue ())
374378 })
375379
376380 err = wcmux .Shutdown (ctx )
377381 g .Expect (err ).ToNot (HaveOccurred ())
378382 })
383+
384+ t .Run ("takes care of the etcd cluster annotations" , func (t * testing.T ) {
385+ g := NewWithT (t )
386+
387+ inMemoryMachineWithNodeProvisioned1 := inMemoryMachineWithNodeProvisioned1 .DeepCopy ()
388+ inMemoryMachineWithNodeProvisioned1 .Spec = infrav1.InMemoryMachineSpec {}
389+
390+ inMemoryMachineWithNodeProvisioned2 := inMemoryMachineWithNodeProvisioned1 .DeepCopy ()
391+ inMemoryMachineWithNodeProvisioned2 .Name = "bar2"
392+
393+ manager := cmanager .New (scheme )
394+
395+ host := "127.0.0.1"
396+ wcmux , err := server .NewWorkloadClustersMux (manager , host , server.CustomPorts {
397+ // NOTE: make sure to use ports different than other tests, so we can run tests in parallel
398+ MinPort : server .DefaultMinPort + 1200 ,
399+ MaxPort : server .DefaultMinPort + 1299 ,
400+ DebugPort : server .DefaultDebugPort + 20 ,
401+ })
402+ g .Expect (err ).ToNot (HaveOccurred ())
403+ _ , err = wcmux .InitWorkloadClusterListener (klog .KObj (cluster ).String ())
404+ g .Expect (err ).ToNot (HaveOccurred ())
405+
406+ r := InMemoryMachineReconciler {
407+ Client : fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (createCASecret (t , cluster , secretutil .EtcdCA )).Build (),
408+ CloudManager : manager ,
409+ APIServerMux : wcmux ,
410+ }
411+ r .CloudManager .AddResourceGroup (klog .KObj (cluster ).String ())
412+ c := r .CloudManager .GetResourceGroup (klog .KObj (cluster ).String ()).GetClient ()
413+
414+ // first etcd pod gets annotated with clusterID, memberID, and also set as a leader
415+
416+ res , err := r .reconcileNormalETCD (ctx , cluster , cpMachine , inMemoryMachineWithNodeProvisioned1 )
417+ g .Expect (err ).ToNot (HaveOccurred ())
418+ g .Expect (res .IsZero ()).To (BeTrue ())
419+ g .Expect (conditions .IsTrue (inMemoryMachineWithNodeProvisioned1 , infrav1 .EtcdProvisionedCondition )).To (BeTrue ())
420+
421+ got1 := & corev1.Pod {
422+ ObjectMeta : metav1.ObjectMeta {
423+ Namespace : metav1 .NamespaceSystem ,
424+ Name : fmt .Sprintf ("etcd-%s" , inMemoryMachineWithNodeProvisioned1 .Name ),
425+ },
426+ }
427+
428+ err = c .Get (ctx , client .ObjectKeyFromObject (got1 ), got1 )
429+ g .Expect (err ).ToNot (HaveOccurred ())
430+
431+ g .Expect (got1 .Annotations ).To (HaveKey (cloudv1 .EtcdClusterIDAnnotationName ))
432+ g .Expect (got1 .Annotations ).To (HaveKey (cloudv1 .EtcdMemberIDAnnotationName ))
433+ g .Expect (got1 .Annotations ).To (HaveKey (cloudv1 .EtcdLeaderFromAnnotationName ))
434+
435+ // second etcd pod gets annotated with the same clusterID, a new memberID (but it is not set as a leader
436+
437+ res , err = r .reconcileNormalETCD (ctx , cluster , cpMachine , inMemoryMachineWithNodeProvisioned2 )
438+ g .Expect (err ).ToNot (HaveOccurred ())
439+ g .Expect (res .IsZero ()).To (BeTrue ())
440+ g .Expect (conditions .IsTrue (inMemoryMachineWithNodeProvisioned2 , infrav1 .EtcdProvisionedCondition )).To (BeTrue ())
441+
442+ got2 := & corev1.Pod {
443+ ObjectMeta : metav1.ObjectMeta {
444+ Namespace : metav1 .NamespaceSystem ,
445+ Name : fmt .Sprintf ("etcd-%s" , inMemoryMachineWithNodeProvisioned2 .Name ),
446+ },
447+ }
448+
449+ err = c .Get (ctx , client .ObjectKeyFromObject (got2 ), got2 )
450+ g .Expect (err ).ToNot (HaveOccurred ())
451+
452+ g .Expect (got2 .Annotations ).To (HaveKey (cloudv1 .EtcdClusterIDAnnotationName ))
453+ g .Expect (got1 .Annotations [cloudv1 .EtcdClusterIDAnnotationName ]).To (Equal (got2 .Annotations [cloudv1 .EtcdClusterIDAnnotationName ]))
454+ g .Expect (got2 .Annotations ).To (HaveKey (cloudv1 .EtcdMemberIDAnnotationName ))
455+ g .Expect (got1 .Annotations [cloudv1 .EtcdMemberIDAnnotationName ]).ToNot (Equal (got2 .Annotations [cloudv1 .EtcdMemberIDAnnotationName ]))
456+ g .Expect (got2 .Annotations ).ToNot (HaveKey (cloudv1 .EtcdLeaderFromAnnotationName ))
457+ })
379458}
380459
381460func TestReconcileNormalApiServer (t * testing.T ) {
0 commit comments