Skip to content

Commit 501e7f9

Browse files
fix(aws): replace context.TODO() with proper timeouts in create.go
- Added timeout constants for VPC, subnet, IGW, route table, security group, EC2, and waiter operations - Replaced 10 context.TODO() calls with context.WithTimeout() calls - Modified 6 functions: createVPC, createSubnet, createInternetGateway, createRouteTable, createSecurityGroup, createEC2Instance - Timeouts: 2 minutes for network resources, 10 minutes for EC2 operations Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
1 parent 64d31e8 commit 501e7f9

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

pkg/provider/aws/create.go

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ import (
2727
"github.com/aws/aws-sdk-go-v2/service/ec2"
2828
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
2929
)
30+
const (
31+
defaultVPCTimeout = 2 * time.Minute
32+
defaultSubnetTimeout = 2 * time.Minute
33+
defaultIGWTimeout = 2 * time.Minute
34+
defaultRouteTableTimeout = 2 * time.Minute
35+
defaultSecurityGroupTimeout = 2 * time.Minute
36+
defaultEC2Timeout = 10 * time.Minute
37+
defaultWaiterTimeout = 15 * time.Minute
38+
)
39+
3040

3141
// Create creates an EC2 instance with proper Network configuration
3242
// VPC, Subnet, Internet Gateway, Route Table, Security Group
@@ -101,7 +111,13 @@ func (p *Provider) createVPC(cache *AWS) error {
101111
},
102112
}
103113

104-
vpcOutput, err := p.ec2.CreateVpc(context.TODO(), vpcInput)
114+
ctx, cancel := context.WithTimeout(context.Background(), defaultVPCTimeout)
115+
116+
117+
defer cancel()
118+
119+
120+
vpcOutput, err := p.ec2.CreateVpc(ctx, vpcInput)
105121
if err != nil {
106122
p.fail()
107123
return fmt.Errorf("error creating VPC: %v", err)
@@ -138,7 +154,11 @@ func (p *Provider) createSubnet(cache *AWS) error {
138154
},
139155
},
140156
}
141-
subnetOutput, err := p.ec2.CreateSubnet(context.TODO(), subnetInput)
157+
ctx, cancel := context.WithTimeout(context.Background(), defaultSubnetTimeout)
158+
159+
defer cancel()
160+
161+
subnetOutput, err := p.ec2.CreateSubnet(ctx, subnetInput)
142162
if err != nil {
143163
p.fail()
144164
return fmt.Errorf("error creating subnet: %v", err)
@@ -162,7 +182,11 @@ func (p *Provider) createInternetGateway(cache *AWS) error {
162182
},
163183
},
164184
}
165-
gwOutput, err := p.ec2.CreateInternetGateway(context.TODO(), gwInput)
185+
ctx, cancel := context.WithTimeout(context.Background(), defaultIGWTimeout)
186+
187+
defer cancel()
188+
189+
gwOutput, err := p.ec2.CreateInternetGateway(ctx, gwInput)
166190
if err != nil {
167191
p.fail()
168192
return fmt.Errorf("error creating Internet Gateway: %v", err)
@@ -174,7 +198,7 @@ func (p *Provider) createInternetGateway(cache *AWS) error {
174198
VpcId: aws.String(cache.Vpcid),
175199
InternetGatewayId: gwOutput.InternetGateway.InternetGatewayId,
176200
}
177-
_, err = p.ec2.AttachInternetGateway(context.TODO(), attachInput)
201+
_, err = p.ec2.AttachInternetGateway(ctx, attachInput)
178202
if err != nil {
179203
p.fail()
180204
return fmt.Errorf("error attaching Internet Gateway: %v", err)
@@ -201,7 +225,11 @@ func (p *Provider) createRouteTable(cache *AWS) error {
201225
},
202226
},
203227
}
204-
rtOutput, err := p.ec2.CreateRouteTable(context.TODO(), rtInput)
228+
ctx, cancel := context.WithTimeout(context.Background(), defaultRouteTableTimeout)
229+
230+
defer cancel()
231+
232+
rtOutput, err := p.ec2.CreateRouteTable(ctx, rtInput)
205233
if err != nil {
206234
p.fail()
207235
return fmt.Errorf("error creating route table: %v", err)
@@ -223,7 +251,7 @@ func (p *Provider) createRouteTable(cache *AWS) error {
223251
DestinationCidrBlock: aws.String("0.0.0.0/0"),
224252
GatewayId: aws.String(cache.InternetGwid),
225253
}
226-
if _, err = p.ec2.CreateRoute(context.TODO(), routeInput); err != nil {
254+
if _, err = p.ec2.CreateRoute(ctx, routeInput); err != nil {
227255
return fmt.Errorf("error creating route: %v", err)
228256
}
229257

@@ -248,7 +276,11 @@ func (p *Provider) createSecurityGroup(cache *AWS) error {
248276
},
249277
},
250278
}
251-
sgOutput, err := p.ec2.CreateSecurityGroup(context.TODO(), sgInput)
279+
ctx, cancel := context.WithTimeout(context.Background(), defaultSecurityGroupTimeout)
280+
281+
defer cancel()
282+
283+
sgOutput, err := p.ec2.CreateSecurityGroup(ctx, sgInput)
252284
if err != nil {
253285
p.fail()
254286
return fmt.Errorf("error creating security group: %v", err)
@@ -306,7 +338,7 @@ func (p *Provider) createSecurityGroup(cache *AWS) error {
306338
},
307339
}
308340

309-
if _, err = p.ec2.AuthorizeSecurityGroupIngress(context.TODO(), irInput); err != nil {
341+
if _, err = p.ec2.AuthorizeSecurityGroupIngress(ctx, irInput); err != nil {
310342
p.fail()
311343
return fmt.Errorf("error authorizing security group ingress: %v", err)
312344
}
@@ -401,7 +433,11 @@ func (p *Provider) createEC2Instance(cache *AWS) error {
401433
// tag network interface
402434
instance := instanceOut.Instances[0]
403435
networkInterfaceId := *instance.NetworkInterfaces[0].NetworkInterfaceId
404-
_, err = p.ec2.CreateTags(context.TODO(), &ec2.CreateTagsInput{
436+
ctx, cancel := context.WithTimeout(context.Background(), defaultEC2Timeout)
437+
438+
defer cancel()
439+
440+
_, err = p.ec2.CreateTags(ctx, &ec2.CreateTagsInput{
405441
Resources: []string{networkInterfaceId},
406442
Tags: p.Tags,
407443
})
@@ -413,7 +449,7 @@ func (p *Provider) createEC2Instance(cache *AWS) error {
413449
// Disable Source/Destination Check for Calico networking
414450
// This is required for Kubernetes CNI plugins (Calico, Flannel, etc.) to work correctly
415451
// See: https://github.com/NVIDIA/holodeck/issues/586
416-
_, err = p.ec2.ModifyNetworkInterfaceAttribute(context.TODO(),
452+
_, err = p.ec2.ModifyNetworkInterfaceAttribute(ctx,
417453
&ec2.ModifyNetworkInterfaceAttributeInput{
418454
NetworkInterfaceId: aws.String(networkInterfaceId),
419455
SourceDestCheck: &types.AttributeBooleanValue{

0 commit comments

Comments
 (0)