Skip to content

Commit 044033e

Browse files
authored
Merge pull request #3895 from k8s-infra-cherrypick-robot/cherry-pick-3886-to-release-2.0
[release-2.0] [E2E] Fix AWS services creation flake in external infrastructure test
2 parents 59aebd6 + c0779e2 commit 044033e

File tree

1 file changed

+23
-40
lines changed

1 file changed

+23
-40
lines changed

test/e2e/shared/aws.go

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -165,36 +165,27 @@ func (i *AWSInfrastructure) AllocateAddress() AWSInfrastructure {
165165
return *i
166166
}
167167

168-
t := 0
169-
addr, _ := GetAddress(i.Context, *aa.AllocationId)
170-
for addr == nil && t < 180 {
171-
time.Sleep(1 * time.Second)
168+
var addr *ec2.Address
169+
Eventually(func(gomega Gomega) {
172170
addr, _ = GetAddress(i.Context, *aa.AllocationId)
173-
t++
174-
}
171+
}, 2*time.Minute, 5*time.Second).Should(Succeed())
175172
i.ElasticIP = addr
176173
return *i
177174
}
178175

179176
func (i *AWSInfrastructure) CreateNatGateway(ct string) AWSInfrastructure {
180-
t := 0
181-
s, serr := GetSubnetByName(i.Context, i.Spec.ClusterName+"-subnet-"+ct)
182-
if serr != nil {
183-
return *i
184-
}
185-
for s == nil && t < 180 {
186-
time.Sleep(1 * time.Second)
177+
var s *ec2.Subnet
178+
Eventually(func(gomega Gomega) {
187179
s, _ = GetSubnetByName(i.Context, i.Spec.ClusterName+"-subnet-"+ct)
188-
t++
189-
}
180+
}, 2*time.Minute, 5*time.Second).Should(Succeed())
190181
if s == nil {
191182
return *i
192183
}
193184
ngwC, ngwce := CreateNatGateway(i.Context, i.Spec.ClusterName+"-nat", ct, *i.ElasticIP.AllocationId, *s.SubnetId)
194185
if ngwce != nil {
195186
return *i
196187
}
197-
if WaitForNatGatewayState(i.Context, *ngwC.NatGatewayId, 180, "available") {
188+
if WaitForNatGatewayState(i.Context, *ngwC.NatGatewayId, "available") {
198189
ngw, _ := GetNatGateway(i.Context, *ngwC.NatGatewayId)
199190
i.NatGateway = ngw
200191
i.State.NatGatewayState = ngw.State
@@ -238,6 +229,10 @@ func (i *AWSInfrastructure) GetRouteTable(rtID string) AWSInfrastructure {
238229
// routes to their respective gateway.
239230
func (i *AWSInfrastructure) CreateInfrastructure() AWSInfrastructure {
240231
i.CreateVPC()
232+
Eventually(func(gomega Gomega) bool {
233+
return *i.RefreshVPCState().State.VpcState == "available"
234+
}, 2*time.Minute, 5*time.Second).Should(BeTrue())
235+
241236
Byf("Created VPC - %s", *i.VPC.VpcId)
242237
if i.VPC != nil {
243238
i.CreatePublicSubnet()
@@ -248,12 +243,6 @@ func (i *AWSInfrastructure) CreateInfrastructure() AWSInfrastructure {
248243
if i.State.PrivateSubnetID != nil {
249244
Byf("Created Private Subnet - %s", *i.State.PrivateSubnetID)
250245
}
251-
for t := 0; t < 30; t++ {
252-
if *i.RefreshVPCState().State.VpcState == "available" {
253-
break
254-
}
255-
time.Sleep(1 * time.Second)
256-
}
257246
i.CreateInternetGateway()
258247
if i.InternetGateway != nil {
259248
Byf("Created Internet Gateway - %s", *i.InternetGateway.InternetGatewayId)
@@ -264,7 +253,7 @@ func (i *AWSInfrastructure) CreateInfrastructure() AWSInfrastructure {
264253
Byf("Created Elastic IP - %s", *i.ElasticIP.AllocationId)
265254
i.CreateNatGateway("public")
266255
if i.NatGateway != nil && i.NatGateway.NatGatewayId != nil {
267-
WaitForNatGatewayState(i.Context, *i.NatGateway.NatGatewayId, 180, "available")
256+
WaitForNatGatewayState(i.Context, *i.NatGateway.NatGatewayId, "available")
268257
Byf("Created NAT Gateway - %s", *i.NatGateway.NatGatewayId)
269258
}
270259
}
@@ -304,7 +293,7 @@ func (i *AWSInfrastructure) DeleteInfrastructure() {
304293
Byf("Deleting orphaned instance: %s - %v", *instance.InstanceId, TerminateInstance(i.Context, *instance.InstanceId))
305294
}
306295
}
307-
WaitForInstanceState(i.Context, i.Spec.ClusterName, 300, "terminated")
296+
WaitForInstanceState(i.Context, i.Spec.ClusterName, "terminated")
308297

309298
loadbalancers, _ := ListLoadBalancers(i.Context, i.Spec.ClusterName)
310299
for _, lb := range loadbalancers {
@@ -320,7 +309,7 @@ func (i *AWSInfrastructure) DeleteInfrastructure() {
320309

321310
if i.NatGateway != nil {
322311
Byf("Deleting NAT Gateway - %s - %v", *i.NatGateway.NatGatewayId, DeleteNatGateway(i.Context, *i.NatGateway.NatGatewayId))
323-
WaitForNatGatewayState(i.Context, *i.NatGateway.NatGatewayId, 180, "deleted")
312+
WaitForNatGatewayState(i.Context, *i.NatGateway.NatGatewayId, "deleted")
324313
}
325314

326315
if i.ElasticIP != nil {
@@ -1010,9 +999,8 @@ func ListClusterEC2Instances(e2eCtx *E2EContext, clusterName string) ([]*ec2.Ins
1010999
return instances, nil
10111000
}
10121001

1013-
func WaitForInstanceState(e2eCtx *E2EContext, clusterName string, timeout int, state string) bool {
1014-
t := 0
1015-
for t < timeout {
1002+
func WaitForInstanceState(e2eCtx *E2EContext, clusterName string, state string) bool {
1003+
Eventually(func(gomega Gomega) bool {
10161004
st := map[string]int{
10171005
"pending": 0,
10181006
"running": 0,
@@ -1027,9 +1015,9 @@ func WaitForInstanceState(e2eCtx *E2EContext, clusterName string, timeout int, s
10271015
if st[state] == len(instances) || len(instances) == 0 {
10281016
return true
10291017
}
1030-
time.Sleep(1 * time.Second)
1031-
t++
1032-
}
1018+
return false
1019+
}, 5*time.Minute, 5*time.Second).Should(BeTrue())
1020+
10331021
return false
10341022
}
10351023

@@ -1417,17 +1405,12 @@ func DeleteNatGateway(e2eCtx *E2EContext, gatewayID string) bool {
14171405
return true
14181406
}
14191407

1420-
func WaitForNatGatewayState(e2eCtx *E2EContext, gatewayID string, timeout int, state string) bool {
1421-
t := 0
1422-
for t < timeout {
1408+
func WaitForNatGatewayState(e2eCtx *E2EContext, gatewayID string, state string) bool {
1409+
Eventually(func(gomega Gomega) bool {
14231410
gw, _ := GetNatGateway(e2eCtx, gatewayID)
14241411
gwState := *gw.State
1425-
if gwState == state {
1426-
return true
1427-
}
1428-
time.Sleep(1 * time.Second)
1429-
t++
1430-
}
1412+
return gwState == state
1413+
}, 3*time.Minute, 5*time.Second).Should(BeTrue())
14311414
return false
14321415
}
14331416

0 commit comments

Comments
 (0)