@@ -18,6 +18,7 @@ package machinesync
1818
1919import (
2020 "context"
21+ "encoding/json"
2122
2223 . "github.com/onsi/ginkgo/v2"
2324 . "github.com/onsi/gomega"
@@ -82,6 +83,26 @@ var _ = Describe("AWS load balancer validation during MAPI->CAPI conversion", fu
8283 <- mgrDone
8384 }
8485
86+ switchAuthoritativeAPI := func (machine * mapiv1beta1.Machine , targetAPI mapiv1beta1.MachineAuthority ) {
87+ Eventually (k .Update (machine , func () {
88+ machine .Spec .AuthoritativeAPI = targetAPI
89+ }), timeout ).Should (Succeed ())
90+ Eventually (func () error {
91+ if err := k8sClient .Get (ctx , client .ObjectKeyFromObject (machine ), machine ); err != nil {
92+ return err
93+ }
94+ machine .Status .AuthoritativeAPI = mapiv1beta1 .MachineAuthorityMigrating
95+ return k8sClient .Status ().Update (ctx , machine )
96+ }, timeout ).Should (Succeed ())
97+ Eventually (func () error {
98+ if err := k8sClient .Get (ctx , client .ObjectKeyFromObject (machine ), machine ); err != nil {
99+ return err
100+ }
101+ machine .Status .AuthoritativeAPI = targetAPI
102+ return k8sClient .Status ().Update (ctx , machine )
103+ }, timeout ).Should (Succeed ())
104+ }
105+
85106 BeforeEach (func () {
86107 k = komega .New (k8sClient )
87108
@@ -91,7 +112,7 @@ var _ = Describe("AWS load balancer validation during MAPI->CAPI conversion", fu
91112 Expect (k8sClient .Create (ctx , capiNamespace )).To (Succeed ())
92113
93114 infrastructureName = "cluster-aws-lb"
94- awsClusterBuilder = awsv1resourcebuilder .AWSCluster ().WithNamespace (capiNamespace .GetName ()).WithName (infrastructureName )
115+ awsClusterBuilder = awsv1resourcebuilder .AWSCluster ().WithNamespace (capiNamespace .GetName ()).WithName (infrastructureName ). WithRegion ( "us-east-1" )
95116
96117 // Create CAPI Cluster that all tests will use
97118 capiCluster := clusterv1resourcebuilder .Cluster ().WithNamespace (capiNamespace .GetName ()).WithName (infrastructureName ).WithInfrastructureRef (& corev1.ObjectReference {
@@ -291,6 +312,86 @@ var _ = Describe("AWS load balancer validation during MAPI->CAPI conversion", fu
291312 capiMachine := clusterv1resourcebuilder .Machine ().WithNamespace (capiNamespace .GetName ()).WithName (mapiMachine .GetName ()).Build ()
292313 Eventually (k8sClient .Get (ctx , client .ObjectKeyFromObject (capiMachine ), capiMachine ), timeout ).Should (Succeed ())
293314 })
315+
316+ It ("should preserve load balancers when toggling authoritativeAPI MAPI -> CAPI -> MAPI" , func () {
317+ By ("Creating AWSCluster with load balancers" )
318+ loadBalancerSpec := & awsv1.AWSLoadBalancerSpec {
319+ Name : ptr .To ("cluster-int" ),
320+ LoadBalancerType : awsv1 .LoadBalancerTypeNLB ,
321+ }
322+ secondaryLoadBalancerSpec := & awsv1.AWSLoadBalancerSpec {
323+ Name : ptr .To ("cluster-ext" ),
324+ LoadBalancerType : awsv1 .LoadBalancerTypeNLB ,
325+ }
326+ awsCluster := awsClusterBuilder .WithControlPlaneLoadBalancer (loadBalancerSpec ).WithSecondaryControlPlaneLoadBalancer (secondaryLoadBalancerSpec ).Build ()
327+ Expect (k8sClient .Create (ctx , awsCluster )).To (Succeed ())
328+
329+ lbRefs := []mapiv1beta1.LoadBalancerReference {
330+ {Name : "cluster-int" , Type : mapiv1beta1 .NetworkLoadBalancerType },
331+ {Name : "cluster-ext" , Type : mapiv1beta1 .NetworkLoadBalancerType },
332+ }
333+
334+ By ("Creating MAPI master machine with load balancers and authoritativeAPI: MachineAPI" )
335+ mapiMachine := machinev1resourcebuilder .Machine ().
336+ WithNamespace (mapiNamespace .GetName ()).
337+ WithGenerateName ("master-" ).
338+ AsMaster ().
339+ WithProviderSpecBuilder (machinev1resourcebuilder .AWSProviderSpec ().WithLoadBalancers (lbRefs )).
340+ Build ()
341+ Expect (k8sClient .Create (ctx , mapiMachine )).To (Succeed ())
342+ Eventually (k .UpdateStatus (mapiMachine , func () { mapiMachine .Status .AuthoritativeAPI = mapiv1beta1 .MachineAuthorityMachineAPI })).Should (Succeed ())
343+
344+ By ("Verifying MAPI -> CAPI sync is successful" )
345+ Eventually (k .Object (mapiMachine ), timeout ).Should (
346+ HaveField ("Status.Conditions" , ContainElement (
347+ SatisfyAll (
348+ HaveField ("Type" , Equal (consts .SynchronizedCondition )),
349+ HaveField ("Status" , Equal (corev1 .ConditionTrue )),
350+ HaveField ("Reason" , Equal ("ResourceSynchronized" )),
351+ HaveField ("Message" , Equal ("Successfully synchronized MAPI Machine to CAPI" )),
352+ ))),
353+ )
354+
355+ By ("Capturing original providerSpec" )
356+ Expect (k8sClient .Get (ctx , client .ObjectKeyFromObject (mapiMachine ), mapiMachine )).To (Succeed ())
357+ originalProviderSpec := mapiMachine .Spec .ProviderSpec .Value .DeepCopy ()
358+
359+ By ("Switching authoritativeAPI to ClusterAPI" )
360+ switchAuthoritativeAPI (mapiMachine , mapiv1beta1 .MachineAuthorityClusterAPI )
361+
362+ By ("Verifying CAPI -> MAPI sync is successful" )
363+ Eventually (k .Object (mapiMachine ), timeout ).Should (
364+ HaveField ("Status.Conditions" , ContainElement (
365+ SatisfyAll (
366+ HaveField ("Type" , Equal (consts .SynchronizedCondition )),
367+ HaveField ("Status" , Equal (corev1 .ConditionTrue )),
368+ HaveField ("Reason" , Equal ("ResourceSynchronized" )),
369+ HaveField ("Message" , Equal ("Successfully synchronized CAPI Machine to MAPI" )),
370+ ))),
371+ )
372+
373+ By ("Switching authoritativeAPI back to MachineAPI" )
374+ switchAuthoritativeAPI (mapiMachine , mapiv1beta1 .MachineAuthorityMachineAPI )
375+
376+ By ("Verifying MAPI -> CAPI sync is successful" )
377+ Eventually (k .Object (mapiMachine ), timeout ).Should (
378+ HaveField ("Status.Conditions" , ContainElement (
379+ SatisfyAll (
380+ HaveField ("Type" , Equal (consts .SynchronizedCondition )),
381+ HaveField ("Status" , Equal (corev1 .ConditionTrue )),
382+ HaveField ("Reason" , Equal ("ResourceSynchronized" )),
383+ HaveField ("Message" , Equal ("Successfully synchronized MAPI Machine to CAPI" )),
384+ ))),
385+ )
386+
387+ By ("Verifying load balancers are unchanged after round trip" )
388+ Expect (k8sClient .Get (ctx , client .ObjectKeyFromObject (mapiMachine ), mapiMachine )).To (Succeed ())
389+
390+ var originalSpec , finalSpec mapiv1beta1.AWSMachineProviderConfig
391+ Expect (json .Unmarshal (originalProviderSpec .Raw , & originalSpec )).To (Succeed ())
392+ Expect (json .Unmarshal (mapiMachine .Spec .ProviderSpec .Value .Raw , & finalSpec )).To (Succeed ())
393+ Expect (finalSpec .LoadBalancers ).To (Equal (originalSpec .LoadBalancers ), "load balancers should not change after round trip" )
394+ })
294395})
295396
296397var _ = Describe ("validateLoadBalancerReferencesAgainstExpected" , func () {
0 commit comments