Skip to content

Commit 5dc0df4

Browse files
committed
Perform network check in multiple subnets
1 parent 273f3b4 commit 5dc0df4

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

gitpod-network-check/cmd/checks.go

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ var networkCheckTag = []iam_types.Tag{
3333
},
3434
}
3535

36-
func initAwsConfig(ctx context.Context) (aws.Config, error) {
37-
return config.LoadDefaultConfig(ctx)
36+
func initAwsConfig(ctx context.Context, region string) (aws.Config, error) {
37+
return config.LoadDefaultConfig(ctx, config.WithRegion(region))
3838
}
3939

4040
// this will be useful when we are cleaning up things at the end
@@ -51,7 +51,7 @@ var checkCommand = &cobra.Command{ // nolint:gochecknoglobals
5151
Short: "Runs the network check diagnosis",
5252
SilenceUsage: false,
5353
RunE: func(cmd *cobra.Command, args []string) error {
54-
cfg, err := initAwsConfig(cmd.Context())
54+
cfg, err := initAwsConfig(cmd.Context(), networkConfig.AwsRegion)
5555
if err != nil {
5656
return err
5757
}
@@ -61,7 +61,10 @@ var checkCommand = &cobra.Command{ // nolint:gochecknoglobals
6161
iamClient := iam.NewFromConfig(cfg)
6262

6363
defer cleanup(cmd.Context(), ec2Client, iamClient)
64-
checkSMPrerequisites(cmd.Context(), ec2Client)
64+
err = checkSMPrerequisites(cmd.Context(), ec2Client)
65+
if err != nil {
66+
return fmt.Errorf("❌ failed to check prerequisites: %v", err)
67+
}
6568

6669
role, err := createIAMRoleAndAttachPolicy(cmd.Context(), iamClient)
6770
if err != nil {
@@ -75,18 +78,19 @@ var checkCommand = &cobra.Command{ // nolint:gochecknoglobals
7578
}
7679
InstanceProfile = aws.ToString(instanceProfile.InstanceProfileName)
7780

78-
log.Infof("ℹ️ Launching EC2 instance in a Main subnet")
79-
mainInstanceId, err := launchInstance(cmd.Context(), ec2Client, networkConfig.MainSubnets[0], instanceProfile.Arn)
81+
log.Infof("ℹ️ Launching EC2 instances in Main subnets")
82+
mainInstanceIds, err := launchInstances(cmd.Context(), ec2Client, networkConfig.MainSubnets, instanceProfile.Arn)
8083
if err != nil {
81-
return fmt.Errorf("❌ Failed to launch instances in subnet %s: %v", networkConfig.MainSubnets[0], err)
84+
return err
8285
}
86+
InstanceIds = append(InstanceIds, mainInstanceIds...)
8387

84-
log.Infof("ℹ️ Launching EC2 instance in a Pod subnet")
85-
podInstanceId, err := launchInstance(cmd.Context(), ec2Client, networkConfig.PodSubnets[0], instanceProfile.Arn)
88+
log.Infof("ℹ️ Launching EC2 instances in a Pod subnets")
89+
podInstanceIds, err := launchInstances(cmd.Context(), ec2Client, networkConfig.PodSubnets, instanceProfile.Arn)
8690
if err != nil {
87-
return fmt.Errorf("❌ Failed to launch instances in subnet %s: %v", networkConfig.PodSubnets[0], err)
91+
return err
8892
}
89-
InstanceIds = []string{mainInstanceId, podInstanceId}
93+
InstanceIds = append(InstanceIds, podInstanceIds...)
9094

9195
log.Infof("ℹ️ Waiting for EC2 instances to become ready (can take up to 2 minutes)")
9296
waiter := ec2.NewInstanceRunningWaiter(ec2Client, func(irwo *ec2.InstanceRunningWaiterOptions) {
@@ -129,7 +133,7 @@ var checkCommand = &cobra.Command{ // nolint:gochecknoglobals
129133
"S3": fmt.Sprintf("https://s3.%s.amazonaws.com", networkConfig.AwsRegion),
130134
"DynamoDB": fmt.Sprintf("https://dynamodb.%s.amazonaws.com", networkConfig.AwsRegion),
131135
}
132-
checkServicesAvailability(cmd.Context(), ssmClient, []string{mainInstanceId}, serviceEndpointsForMain)
136+
checkServicesAvailability(cmd.Context(), ssmClient, mainInstanceIds, serviceEndpointsForMain)
133137

134138
return nil
135139
},
@@ -240,8 +244,22 @@ func validateSubnets(cmd *cobra.Command, args []string) error {
240244
return nil
241245
}
242246

243-
func launchInstance(ctx context.Context, ec2Svc *ec2.Client, subnetID string, instanceProfileName *string) (string, error) {
244-
regionalAMI, err := findUbuntuAMI(ctx, ec2Svc)
247+
func launchInstances(ctx context.Context, ec2Client *ec2.Client, subnets []string, profileArn *string) ([]string, error) {
248+
var instanceIds []string
249+
for _, subnet := range subnets {
250+
instanceId, err := launchInstanceInSubnet(ctx, ec2Client, subnet, profileArn)
251+
if err != nil {
252+
return nil, fmt.Errorf("❌ Failed to launch instances in subnet %s: %v", subnet, err)
253+
}
254+
255+
instanceIds = append(instanceIds, instanceId)
256+
}
257+
258+
return instanceIds, nil
259+
}
260+
261+
func launchInstanceInSubnet(ctx context.Context, ec2Client *ec2.Client, subnetID string, instanceProfileName *string) (string, error) {
262+
regionalAMI, err := findUbuntuAMI(ctx, ec2Client)
245263
if err != nil {
246264
return "", err
247265
}
@@ -275,7 +293,7 @@ func launchInstance(ctx context.Context, ec2Svc *ec2.Client, subnetID string, in
275293

276294
var result *ec2.RunInstancesOutput
277295
err = wait.PollUntilContextTimeout(ctx, 500*time.Millisecond, 10*time.Second, false, func(ctx context.Context) (done bool, err error) {
278-
result, err = ec2Svc.RunInstances(ctx, input)
296+
result, err = ec2Client.RunInstances(ctx, input)
279297

280298
if err != nil {
281299
if strings.Contains(err.Error(), "Invalid IAM Instance Profile ARN") {

0 commit comments

Comments
 (0)