Skip to content

Commit cad5a61

Browse files
fix(aws): replace context.TODO() with proper timeouts in create.go (NVIDIA#611)
- 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 31b611e commit cad5a61

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

pkg/provider/aws/create.go

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ import (
2828
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
2929
)
3030

31+
const (
32+
defaultVPCTimeout = 2 * time.Minute
33+
defaultSubnetTimeout = 2 * time.Minute
34+
defaultIGWTimeout = 2 * time.Minute
35+
defaultRouteTableTimeout = 2 * time.Minute
36+
defaultSecurityGroupTimeout = 2 * time.Minute
37+
defaultEC2Timeout = 10 * time.Minute
38+
defaultWaiterTimeout = 15 * time.Minute
39+
)
40+
3141
// Create creates an EC2 instance with proper Network configuration
3242
// VPC, Subnet, Internet Gateway, Route Table, Security Group
3343
// If the environment specifies a cluster configuration, it delegates to CreateCluster()
@@ -125,7 +135,11 @@ func (p *Provider) createVPC(cache *AWS) error {
125135
},
126136
}
127137

128-
vpcOutput, err := p.ec2.CreateVpc(context.TODO(), vpcInput)
138+
ctx, cancel := context.WithTimeout(context.Background(), defaultVPCTimeout)
139+
140+
defer cancel()
141+
142+
vpcOutput, err := p.ec2.CreateVpc(ctx, vpcInput)
129143
if err != nil {
130144
p.fail()
131145
return fmt.Errorf("error creating VPC: %w", err)
@@ -162,7 +176,11 @@ func (p *Provider) createSubnet(cache *AWS) error {
162176
},
163177
},
164178
}
165-
subnetOutput, err := p.ec2.CreateSubnet(context.TODO(), subnetInput)
179+
ctx, cancel := context.WithTimeout(context.Background(), defaultSubnetTimeout)
180+
181+
defer cancel()
182+
183+
subnetOutput, err := p.ec2.CreateSubnet(ctx, subnetInput)
166184
if err != nil {
167185
p.fail()
168186
return fmt.Errorf("error creating subnet: %w", err)
@@ -186,7 +204,11 @@ func (p *Provider) createInternetGateway(cache *AWS) error {
186204
},
187205
},
188206
}
189-
gwOutput, err := p.ec2.CreateInternetGateway(context.TODO(), gwInput)
207+
ctx, cancel := context.WithTimeout(context.Background(), defaultIGWTimeout)
208+
209+
defer cancel()
210+
211+
gwOutput, err := p.ec2.CreateInternetGateway(ctx, gwInput)
190212
if err != nil {
191213
p.fail()
192214
return fmt.Errorf("error creating Internet Gateway: %w", err)
@@ -198,7 +220,7 @@ func (p *Provider) createInternetGateway(cache *AWS) error {
198220
VpcId: aws.String(cache.Vpcid),
199221
InternetGatewayId: gwOutput.InternetGateway.InternetGatewayId,
200222
}
201-
_, err = p.ec2.AttachInternetGateway(context.TODO(), attachInput)
223+
_, err = p.ec2.AttachInternetGateway(ctx, attachInput)
202224
if err != nil {
203225
p.fail()
204226
return fmt.Errorf("error attaching Internet Gateway: %w", err)
@@ -225,7 +247,11 @@ func (p *Provider) createRouteTable(cache *AWS) error {
225247
},
226248
},
227249
}
228-
rtOutput, err := p.ec2.CreateRouteTable(context.TODO(), rtInput)
250+
ctx, cancel := context.WithTimeout(context.Background(), defaultRouteTableTimeout)
251+
252+
defer cancel()
253+
254+
rtOutput, err := p.ec2.CreateRouteTable(ctx, rtInput)
229255
if err != nil {
230256
p.fail()
231257
return fmt.Errorf("error creating route table: %w", err)
@@ -247,7 +273,7 @@ func (p *Provider) createRouteTable(cache *AWS) error {
247273
DestinationCidrBlock: aws.String("0.0.0.0/0"),
248274
GatewayId: aws.String(cache.InternetGwid),
249275
}
250-
if _, err = p.ec2.CreateRoute(context.TODO(), routeInput); err != nil {
276+
if _, err = p.ec2.CreateRoute(ctx, routeInput); err != nil {
251277
return fmt.Errorf("error creating route: %w", err)
252278
}
253279

@@ -272,7 +298,11 @@ func (p *Provider) createSecurityGroup(cache *AWS) error {
272298
},
273299
},
274300
}
275-
sgOutput, err := p.ec2.CreateSecurityGroup(context.TODO(), sgInput)
301+
ctx, cancel := context.WithTimeout(context.Background(), defaultSecurityGroupTimeout)
302+
303+
defer cancel()
304+
305+
sgOutput, err := p.ec2.CreateSecurityGroup(ctx, sgInput)
276306
if err != nil {
277307
p.fail()
278308
return fmt.Errorf("error creating security group: %w", err)
@@ -330,7 +360,7 @@ func (p *Provider) createSecurityGroup(cache *AWS) error {
330360
},
331361
}
332362

333-
if _, err = p.ec2.AuthorizeSecurityGroupIngress(context.TODO(), irInput); err != nil {
363+
if _, err = p.ec2.AuthorizeSecurityGroupIngress(ctx, irInput); err != nil {
334364
p.fail()
335365
return fmt.Errorf("error authorizing security group ingress: %w", err)
336366
}
@@ -425,7 +455,11 @@ func (p *Provider) createEC2Instance(cache *AWS) error {
425455
// tag network interface
426456
instance := instanceOut.Instances[0]
427457
networkInterfaceId := *instance.NetworkInterfaces[0].NetworkInterfaceId
428-
_, err = p.ec2.CreateTags(context.TODO(), &ec2.CreateTagsInput{
458+
ctx, cancel := context.WithTimeout(context.Background(), defaultEC2Timeout)
459+
460+
defer cancel()
461+
462+
_, err = p.ec2.CreateTags(ctx, &ec2.CreateTagsInput{
429463
Resources: []string{networkInterfaceId},
430464
Tags: p.Tags,
431465
})
@@ -437,7 +471,7 @@ func (p *Provider) createEC2Instance(cache *AWS) error {
437471
// Disable Source/Destination Check for Calico networking
438472
// This is required for Kubernetes CNI plugins (Calico, Flannel, etc.) to work correctly
439473
// See: https://github.com/NVIDIA/holodeck/issues/586
440-
_, err = p.ec2.ModifyNetworkInterfaceAttribute(context.TODO(),
474+
_, err = p.ec2.ModifyNetworkInterfaceAttribute(ctx,
441475
&ec2.ModifyNetworkInterfaceAttributeInput{
442476
NetworkInterfaceId: aws.String(networkInterfaceId),
443477
SourceDestCheck: &types.AttributeBooleanValue{

0 commit comments

Comments
 (0)