Skip to content

Commit 92776f5

Browse files
committed
Avoid launching instances for duplicated subnets
1 parent d5bd508 commit 92776f5

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

gitpod-network-check/cmd/checks.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/base64"
66
"errors"
77
"fmt"
8+
"slices"
89
"sort"
910
"strings"
1011
"time"
@@ -55,6 +56,13 @@ var checkCommand = &cobra.Command{ // nolint:gochecknoglobals
5556
}
5657
InstanceProfile = aws.ToString(instanceProfile.InstanceProfileName)
5758

59+
allSubnets := slices.Concat(networkConfig.MainSubnets, networkConfig.PodSubnets)
60+
slices.Sort(allSubnets)
61+
distinctSubnets := slices.Compact(allSubnets)
62+
if len(distinctSubnets) < len(allSubnets) {
63+
log.Info("ℹ️ Found duplicate subnets. We'll test each subnet only once, starting with main.")
64+
}
65+
5866
log.Infof("ℹ️ Launching EC2 instances in Main subnets")
5967
mainInstanceIds, err := launchInstances(cmd.Context(), ec2Client, networkConfig.MainSubnets, instanceProfile.Arn)
6068
if err != nil {
@@ -247,6 +255,10 @@ func validateSubnets(cmd *cobra.Command, args []string) error {
247255
func launchInstances(ctx context.Context, ec2Client *ec2.Client, subnets []string, profileArn *string) ([]string, error) {
248256
var instanceIds []string
249257
for _, subnet := range subnets {
258+
if _, ok := Subnets[subnet]; ok {
259+
log.Warnf("Subnet '%v' was already launched, skipping", subnet)
260+
continue
261+
}
250262
secGroup, err := createSecurityGroups(ctx, ec2Client, subnet)
251263
if err != nil {
252264
return nil, fmt.Errorf("❌ failed to create security group for subnet '%v': %v", subnet, err)
@@ -258,6 +270,7 @@ func launchInstances(ctx context.Context, ec2Client *ec2.Client, subnets []strin
258270
}
259271

260272
instanceIds = append(instanceIds, instanceId)
273+
Subnets[subnet] = true
261274
}
262275

263276
return instanceIds, nil

0 commit comments

Comments
 (0)