Skip to content

Commit bb54dc0

Browse files
authored
Fix support for me-central-1 & panic from private Gitpod network (#9)
* Fix panic on diagnose for private networks * Introduce getPreferredInstanceType The available types vary by region. T2 is the cheapest, but not always available. T3a is cheaper than T3 but uncommon. T3 more expensive, but available in most regions. * Log the instance type selected * Refactor * Log input params
1 parent e6e821a commit bb54dc0

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

gitpod-network-check/cmd/checks.go

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,14 @@ func launchInstances(ctx context.Context, ec2Client *ec2.Client, subnets []strin
297297
return nil, fmt.Errorf("❌ failed to create security group for subnet '%v': %v", subnet, err)
298298
}
299299
SecurityGroups = append(SecurityGroups, secGroup)
300-
instanceId, err := launchInstanceInSubnet(ctx, ec2Client, subnet, secGroup, profileArn)
300+
301+
instanceType, err := getPreferredInstanceType(ctx, ec2Client)
302+
if err != nil {
303+
return nil, fmt.Errorf("❌ failed to get preferred instance type: %v", err)
304+
}
305+
log.Infof("ℹ️ Instance type %s shall be used", instanceType)
306+
307+
instanceId, err := launchInstanceInSubnet(ctx, ec2Client, subnet, secGroup, profileArn, instanceType)
301308
if err != nil {
302309
return nil, fmt.Errorf("❌ Failed to launch instances in subnet %s: %v", subnet, err)
303310
}
@@ -312,7 +319,7 @@ func launchInstances(ctx context.Context, ec2Client *ec2.Client, subnets []strin
312319
return instanceIds, nil
313320
}
314321

315-
func launchInstanceInSubnet(ctx context.Context, ec2Client *ec2.Client, subnetID, secGroupId string, instanceProfileName *string) (string, error) {
322+
func launchInstanceInSubnet(ctx context.Context, ec2Client *ec2.Client, subnetID, secGroupId string, instanceProfileName *string, instanceType types.InstanceType) (string, error) {
316323
regionalAMI, err := findUbuntuAMI(ctx, ec2Client)
317324
if err != nil {
318325
return "", err
@@ -329,7 +336,7 @@ func launchInstanceInSubnet(ctx context.Context, ec2Client *ec2.Client, subnetID
329336

330337
input := &ec2.RunInstancesInput{
331338
ImageId: aws.String(regionalAMI), // Example AMI ID, replace with an actual one
332-
InstanceType: types.InstanceTypeT2Micro,
339+
InstanceType: instanceType,
333340
MaxCount: aws.Int32(1),
334341
MinCount: aws.Int32(1),
335342
UserData: &userDataEncoded,
@@ -588,3 +595,40 @@ func createInstanceProfileAndAttachRole(ctx context.Context, svc *iam.Client, ro
588595

589596
return instanceProfileOutput.InstanceProfile, nil
590597
}
598+
599+
func getPreferredInstanceType(ctx context.Context, svc *ec2.Client) (types.InstanceType, error) {
600+
instanceTypes := []types.InstanceType{
601+
types.InstanceTypeT2Micro,
602+
types.InstanceTypeT3aMicro,
603+
types.InstanceTypeT3Micro,
604+
}
605+
for _, instanceType := range instanceTypes {
606+
exists, err := instanceTypeExists(ctx, svc, instanceType)
607+
if err != nil {
608+
return "", err
609+
}
610+
if exists {
611+
return instanceType, nil
612+
}
613+
}
614+
return "", fmt.Errorf("No preferred instance type available in region: %s", networkConfig.AwsRegion)
615+
}
616+
617+
func instanceTypeExists(ctx context.Context, svc *ec2.Client, instanceType types.InstanceType) (bool, error) {
618+
input := &ec2.DescribeInstanceTypeOfferingsInput{
619+
Filters: []types.Filter{
620+
{
621+
Name: aws.String("instance-type"),
622+
Values: []string{string(instanceType)},
623+
},
624+
},
625+
LocationType: types.LocationTypeRegion,
626+
}
627+
628+
resp, err := svc.DescribeInstanceTypeOfferings(ctx, input)
629+
if err != nil {
630+
return false, err
631+
}
632+
633+
return len(resp.InstanceTypeOfferings) > 0, nil
634+
}

gitpod-network-check/cmd/common.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,16 @@ func cleanup(ctx context.Context, svc *ec2.Client, iamsvc *iam.Client) {
5151
},
5252
})
5353
if err != nil {
54-
log.WithError(err).Warn("Failed to list instances, please cleanup manually")
54+
log.WithError(err).Error("Failed to list instances, please cleanup instances manually")
5555
} else if len(instances.Reservations) == 0 {
5656
log.Info("No instances found.")
5757
}
5858

59-
for _, r := range instances.Reservations {
60-
for _, i := range r.Instances {
61-
InstanceIds = append(InstanceIds, *i.InstanceId)
59+
if instances != nil {
60+
for _, r := range instances.Reservations {
61+
for _, i := range r.Instances {
62+
InstanceIds = append(InstanceIds, *i.InstanceId)
63+
}
6264
}
6365
}
6466
}
@@ -155,8 +157,10 @@ func cleanup(ctx context.Context, svc *ec2.Client, iamsvc *iam.Client) {
155157
log.Info("No security groups found.")
156158
}
157159

158-
for _, sg := range securityGroups.SecurityGroups {
159-
SecurityGroups = append(SecurityGroups, *sg.GroupId)
160+
if securityGroups != nil {
161+
for _, sg := range securityGroups.SecurityGroups {
162+
SecurityGroups = append(SecurityGroups, *sg.GroupId)
163+
}
160164
}
161165
}
162166

gitpod-network-check/cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func init() {
8989
networkCheckCmd.PersistentFlags().StringSliceVar(&networkConfig.PodSubnets, "pod-subnets", []string{}, "List of pod subnets")
9090
networkCheckCmd.PersistentFlags().StringSliceVar(&networkConfig.HttpsHosts, "https-hosts", []string{}, "Hosts to test for outbound HTTPS connectivity")
9191
bindFlags(networkCheckCmd, v)
92+
log.Infof("ℹ️ Running with region `%s`, main subnet `%v`, pod subnet `%v`, and hosts `%v`", networkConfig.AwsRegion, networkConfig.MainSubnets, networkConfig.PodSubnets, networkConfig.HttpsHosts)
9293
}
9394

9495
func readConfigFile() *viper.Viper {

0 commit comments

Comments
 (0)