@@ -59,7 +59,7 @@ func (c *Config) Wait(ctx context.Context, vlab *VLAB) error {
5959 }
6060
6161 slog .Info ("Waiting for all switches ready" )
62- if err := waitSwitchesReady (ctx , kube , 0 ); err != nil {
62+ if err := WaitSwitchesReady (ctx , kube , 0 , 30 * time . Minute ); err != nil {
6363 return fmt .Errorf ("waiting for switches ready: %w" , err )
6464 }
6565
@@ -80,6 +80,40 @@ type SetupVPCsOpts struct {
8080 InterfaceMTU uint16
8181}
8282
83+ func CreateOrUpdateVpc (ctx context.Context , kube client.Client , vpc * vpcapi.VPC ) (bool , error ) {
84+ var changed bool
85+ some := & vpcapi.VPC {ObjectMeta : metav1.ObjectMeta {Name : vpc .Name , Namespace : vpc .Namespace }}
86+ res , err := ctrlutil .CreateOrUpdate (ctx , kube , some , func () error {
87+ some .Spec = vpc .Spec
88+ some .Default ()
89+
90+ return nil
91+ })
92+ if err != nil {
93+ return changed , fmt .Errorf ("creating or updating VPC %q: %w" , vpc .Name , err )
94+ }
95+
96+ switch res {
97+ case ctrlutil .OperationResultCreated :
98+ slog .Info ("Created" , "vpc" , vpc .Name , "subnets" , len (vpc .Spec .Subnets ))
99+ changed = true
100+ case ctrlutil .OperationResultUpdated :
101+ slog .Info ("Updated" , "vpc" , vpc .Name , "subnets" , len (vpc .Spec .Subnets ))
102+ changed = true
103+ }
104+
105+ return changed , nil
106+ }
107+
108+ func GetKubeClient (ctx context.Context , workDir string ) (client.Client , error ) {
109+ kubeconfig := filepath .Join (workDir , VLABDir , VLABKubeConfig )
110+ return kubeutil .NewClient (ctx , kubeconfig ,
111+ wiringapi .SchemeBuilder ,
112+ vpcapi .SchemeBuilder ,
113+ agentapi .SchemeBuilder ,
114+ )
115+ }
116+
83117func (c * Config ) SetupVPCs (ctx context.Context , vlab * VLAB , opts SetupVPCsOpts ) error {
84118 ctx , cancel := context .WithTimeout (ctx , 30 * time .Minute )
85119 defer cancel ()
@@ -308,7 +342,7 @@ func (c *Config) SetupVPCs(ctx context.Context, vlab *VLAB, opts SetupVPCsOpts)
308342
309343 if opts .WaitSwitchesReady {
310344 slog .Info ("Waiting for switches ready before configuring VPCs and VPCAttachments" )
311- if err := waitSwitchesReady (ctx , kube , 0 ); err != nil {
345+ if err := WaitSwitchesReady (ctx , kube , 0 , 30 * time . Minute ); err != nil {
312346 return fmt .Errorf ("waiting for switches ready: %w" , err )
313347 }
314348 }
@@ -346,25 +380,11 @@ func (c *Config) SetupVPCs(ctx context.Context, vlab *VLAB, opts SetupVPCsOpts)
346380 }
347381
348382 for _ , vpc := range vpcs {
349- some := & vpcapi.VPC {ObjectMeta : metav1.ObjectMeta {Name : vpc .Name , Namespace : vpc .Namespace }}
350- res , err := ctrlutil .CreateOrUpdate (ctx , kube , some , func () error {
351- some .Spec = vpc .Spec
352- some .Default ()
353-
354- return nil
355- })
383+ iterChanged , err := CreateOrUpdateVpc (ctx , kube , vpc )
356384 if err != nil {
357- return fmt .Errorf ("creating or updating VPC %q: %w" , vpc .Name , err )
358- }
359-
360- switch res {
361- case ctrlutil .OperationResultCreated :
362- slog .Info ("Created" , "vpc" , vpc .Name , "subnets" , len (vpc .Spec .Subnets ))
363- changed = true
364- case ctrlutil .OperationResultUpdated :
365- slog .Info ("Updated" , "vpc" , vpc .Name , "subnets" , len (vpc .Spec .Subnets ))
366- changed = true
385+ return fmt .Errorf ("creating or updating vpc %q: %w" , vpc .Name , err )
367386 }
387+ changed = changed || iterChanged
368388 }
369389
370390 for _ , attach := range attaches {
@@ -399,7 +419,7 @@ func (c *Config) SetupVPCs(ctx context.Context, vlab *VLAB, opts SetupVPCsOpts)
399419 case <- time .After (15 * time .Second ):
400420 }
401421
402- if err := waitSwitchesReady (ctx , kube , 0 ); err != nil {
422+ if err := WaitSwitchesReady (ctx , kube , 0 , 30 * time . Minute ); err != nil {
403423 return fmt .Errorf ("waiting for switches ready: %w" , err )
404424 }
405425 }
@@ -507,7 +527,7 @@ func (c *Config) SetupPeerings(ctx context.Context, vlab *VLAB, opts SetupPeerin
507527
508528 if opts .WaitSwitchesReady {
509529 slog .Info ("Waiting for switches ready before configuring VPC and External Peerings" )
510- if err := waitSwitchesReady (ctx , kube , 0 ); err != nil {
530+ if err := WaitSwitchesReady (ctx , kube , 0 , 30 * time . Minute ); err != nil {
511531 return fmt .Errorf ("waiting for switches ready: %w" , err )
512532 }
513533 }
@@ -712,7 +732,16 @@ func (c *Config) SetupPeerings(ctx context.Context, vlab *VLAB, opts SetupPeerin
712732 }
713733 }
714734
715- changed := false
735+ if err := DoSetupPeerings (ctx , kube , vpcPeerings , externalPeerings , opts .WaitSwitchesReady ); err != nil {
736+ return err
737+ }
738+ slog .Info ("VPC and External Peerings setup complete" , "took" , time .Since (start ))
739+
740+ return nil
741+ }
742+
743+ func DoSetupPeerings (ctx context.Context , kube client.Client , vpcPeerings map [string ]* vpcapi.VPCPeeringSpec , externalPeerings map [string ]* vpcapi.ExternalPeeringSpec , waitReady bool ) error {
744+ var changed bool
716745
717746 vpcPeeringList := & vpcapi.VPCPeeringList {}
718747 if err := kube .List (ctx , vpcPeeringList ); err != nil {
@@ -810,7 +839,7 @@ func (c *Config) SetupPeerings(ctx context.Context, vlab *VLAB, opts SetupPeerin
810839 }
811840 }
812841
813- if changed && opts . WaitSwitchesReady {
842+ if changed && waitReady {
814843 slog .Info ("Waiting for switches ready after configuring VPC and External Peerings" )
815844
816845 // TODO remove it when we can actually know that changes to VPC/VPCAttachment are reflected in agents
@@ -820,13 +849,11 @@ func (c *Config) SetupPeerings(ctx context.Context, vlab *VLAB, opts SetupPeerin
820849 case <- time .After (15 * time .Second ):
821850 }
822851
823- if err := waitSwitchesReady (ctx , kube , 0 ); err != nil {
852+ if err := WaitSwitchesReady (ctx , kube , 0 , 30 * time . Minute ); err != nil {
824853 return fmt .Errorf ("waiting for switches ready: %w" , err )
825854 }
826855 }
827856
828- slog .Info ("VPC and External Peerings setup complete" , "took" , time .Since (start ))
829-
830857 return nil
831858}
832859
@@ -899,7 +926,7 @@ func (c *Config) TestConnectivity(ctx context.Context, vlab *VLAB, opts TestConn
899926 if opts .WaitSwitchesReady {
900927 slog .Info ("Waiting for switches ready before testing connectivity" )
901928
902- if err := waitSwitchesReady (ctx , kube , 30 * time .Second ); err != nil {
929+ if err := WaitSwitchesReady (ctx , kube , 30 * time .Second , 30 * time . Minute ); err != nil {
903930 return fmt .Errorf ("waiting for switches ready: %w" , err )
904931 }
905932 }
@@ -1123,13 +1150,19 @@ func (c *Config) TestConnectivity(ctx context.Context, vlab *VLAB, opts TestConn
11231150 return nil
11241151}
11251152
1126- func waitSwitchesReady (ctx context.Context , kube client.Reader , appliedFor time.Duration ) error {
1127- ctx , cancel := context .WithTimeout (ctx , 30 * time .Minute )
1128- defer cancel ()
1153+ func WaitSwitchesReady (ctx context.Context , kube client.Reader , appliedFor time.Duration , timeout time.Duration ) error {
1154+ var waitCtx context.Context
1155+ var cancel context.CancelFunc
1156+ if timeout > 0 {
1157+ waitCtx , cancel = context .WithTimeout (ctx , timeout )
1158+ defer cancel ()
1159+ } else {
1160+ waitCtx = ctx
1161+ }
11291162
11301163 for {
11311164 switches := & wiringapi.SwitchList {}
1132- if err := kube .List (ctx , switches ); err != nil {
1165+ if err := kube .List (waitCtx , switches ); err != nil {
11331166 return fmt .Errorf ("listing switches: %w" , err )
11341167 }
11351168
@@ -1145,7 +1178,7 @@ func waitSwitchesReady(ctx context.Context, kube client.Reader, appliedFor time.
11451178 updated := false
11461179
11471180 ag := & agentapi.Agent {}
1148- err := kube .Get (ctx , client.ObjectKey {Name : sw .Name , Namespace : sw .Namespace }, ag )
1181+ err := kube .Get (waitCtx , client.ObjectKey {Name : sw .Name , Namespace : sw .Namespace }, ag )
11491182 if err != nil && ! apierrors .IsNotFound (err ) {
11501183 return fmt .Errorf ("getting agent %q: %w" , sw .Name , err )
11511184 }
@@ -1499,7 +1532,7 @@ func (c *Config) Inspect(ctx context.Context, vlab *VLAB, opts InspectOpts) erro
14991532 fail := false
15001533
15011534 slog .Info ("Waiting for switches ready before inspecting" )
1502- if err := waitSwitchesReady (ctx , kube , opts .WaitAppliedFor ); err != nil {
1535+ if err := WaitSwitchesReady (ctx , kube , opts .WaitAppliedFor , 30 * time . Minute ); err != nil {
15031536 slog .Error ("Failed to wait for switches ready" , "err" , err )
15041537 fail = true
15051538 }
0 commit comments