@@ -59,7 +59,7 @@ func (c *Config) Wait(ctx context.Context, vlab *VLAB) error {
59
59
}
60
60
61
61
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 {
63
63
return fmt .Errorf ("waiting for switches ready: %w" , err )
64
64
}
65
65
@@ -80,6 +80,40 @@ type SetupVPCsOpts struct {
80
80
InterfaceMTU uint16
81
81
}
82
82
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
+
83
117
func (c * Config ) SetupVPCs (ctx context.Context , vlab * VLAB , opts SetupVPCsOpts ) error {
84
118
ctx , cancel := context .WithTimeout (ctx , 30 * time .Minute )
85
119
defer cancel ()
@@ -308,7 +342,7 @@ func (c *Config) SetupVPCs(ctx context.Context, vlab *VLAB, opts SetupVPCsOpts)
308
342
309
343
if opts .WaitSwitchesReady {
310
344
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 {
312
346
return fmt .Errorf ("waiting for switches ready: %w" , err )
313
347
}
314
348
}
@@ -346,25 +380,11 @@ func (c *Config) SetupVPCs(ctx context.Context, vlab *VLAB, opts SetupVPCsOpts)
346
380
}
347
381
348
382
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 )
356
384
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 )
367
386
}
387
+ changed = changed || iterChanged
368
388
}
369
389
370
390
for _ , attach := range attaches {
@@ -399,7 +419,7 @@ func (c *Config) SetupVPCs(ctx context.Context, vlab *VLAB, opts SetupVPCsOpts)
399
419
case <- time .After (15 * time .Second ):
400
420
}
401
421
402
- if err := waitSwitchesReady (ctx , kube , 0 ); err != nil {
422
+ if err := WaitSwitchesReady (ctx , kube , 0 , 30 * time . Minute ); err != nil {
403
423
return fmt .Errorf ("waiting for switches ready: %w" , err )
404
424
}
405
425
}
@@ -507,7 +527,7 @@ func (c *Config) SetupPeerings(ctx context.Context, vlab *VLAB, opts SetupPeerin
507
527
508
528
if opts .WaitSwitchesReady {
509
529
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 {
511
531
return fmt .Errorf ("waiting for switches ready: %w" , err )
512
532
}
513
533
}
@@ -712,7 +732,16 @@ func (c *Config) SetupPeerings(ctx context.Context, vlab *VLAB, opts SetupPeerin
712
732
}
713
733
}
714
734
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
716
745
717
746
vpcPeeringList := & vpcapi.VPCPeeringList {}
718
747
if err := kube .List (ctx , vpcPeeringList ); err != nil {
@@ -810,7 +839,7 @@ func (c *Config) SetupPeerings(ctx context.Context, vlab *VLAB, opts SetupPeerin
810
839
}
811
840
}
812
841
813
- if changed && opts . WaitSwitchesReady {
842
+ if changed && waitReady {
814
843
slog .Info ("Waiting for switches ready after configuring VPC and External Peerings" )
815
844
816
845
// 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
820
849
case <- time .After (15 * time .Second ):
821
850
}
822
851
823
- if err := waitSwitchesReady (ctx , kube , 0 ); err != nil {
852
+ if err := WaitSwitchesReady (ctx , kube , 0 , 30 * time . Minute ); err != nil {
824
853
return fmt .Errorf ("waiting for switches ready: %w" , err )
825
854
}
826
855
}
827
856
828
- slog .Info ("VPC and External Peerings setup complete" , "took" , time .Since (start ))
829
-
830
857
return nil
831
858
}
832
859
@@ -899,7 +926,7 @@ func (c *Config) TestConnectivity(ctx context.Context, vlab *VLAB, opts TestConn
899
926
if opts .WaitSwitchesReady {
900
927
slog .Info ("Waiting for switches ready before testing connectivity" )
901
928
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 {
903
930
return fmt .Errorf ("waiting for switches ready: %w" , err )
904
931
}
905
932
}
@@ -1123,13 +1150,19 @@ func (c *Config) TestConnectivity(ctx context.Context, vlab *VLAB, opts TestConn
1123
1150
return nil
1124
1151
}
1125
1152
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
+ }
1129
1162
1130
1163
for {
1131
1164
switches := & wiringapi.SwitchList {}
1132
- if err := kube .List (ctx , switches ); err != nil {
1165
+ if err := kube .List (waitCtx , switches ); err != nil {
1133
1166
return fmt .Errorf ("listing switches: %w" , err )
1134
1167
}
1135
1168
@@ -1145,7 +1178,7 @@ func waitSwitchesReady(ctx context.Context, kube client.Reader, appliedFor time.
1145
1178
updated := false
1146
1179
1147
1180
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 )
1149
1182
if err != nil && ! apierrors .IsNotFound (err ) {
1150
1183
return fmt .Errorf ("getting agent %q: %w" , sw .Name , err )
1151
1184
}
@@ -1499,7 +1532,7 @@ func (c *Config) Inspect(ctx context.Context, vlab *VLAB, opts InspectOpts) erro
1499
1532
fail := false
1500
1533
1501
1534
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 {
1503
1536
slog .Error ("Failed to wait for switches ready" , "err" , err )
1504
1537
fail = true
1505
1538
}
0 commit comments