Skip to content

Commit

Permalink
Fix support for me-central-1 & panic from private Gitpod network (#9)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kylos101 authored Oct 21, 2024
1 parent e6e821a commit bb54dc0
Showing 3 changed files with 58 additions and 9 deletions.
50 changes: 47 additions & 3 deletions gitpod-network-check/cmd/checks.go
Original file line number Diff line number Diff line change
@@ -297,7 +297,14 @@ func launchInstances(ctx context.Context, ec2Client *ec2.Client, subnets []strin
return nil, fmt.Errorf("❌ failed to create security group for subnet '%v': %v", subnet, err)
}
SecurityGroups = append(SecurityGroups, secGroup)
instanceId, err := launchInstanceInSubnet(ctx, ec2Client, subnet, secGroup, profileArn)

instanceType, err := getPreferredInstanceType(ctx, ec2Client)
if err != nil {
return nil, fmt.Errorf("❌ failed to get preferred instance type: %v", err)
}
log.Infof("ℹ️ Instance type %s shall be used", instanceType)

instanceId, err := launchInstanceInSubnet(ctx, ec2Client, subnet, secGroup, profileArn, instanceType)
if err != nil {
return nil, fmt.Errorf("❌ Failed to launch instances in subnet %s: %v", subnet, err)
}
@@ -312,7 +319,7 @@ func launchInstances(ctx context.Context, ec2Client *ec2.Client, subnets []strin
return instanceIds, nil
}

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

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

return instanceProfileOutput.InstanceProfile, nil
}

func getPreferredInstanceType(ctx context.Context, svc *ec2.Client) (types.InstanceType, error) {
instanceTypes := []types.InstanceType{
types.InstanceTypeT2Micro,
types.InstanceTypeT3aMicro,
types.InstanceTypeT3Micro,
}
for _, instanceType := range instanceTypes {
exists, err := instanceTypeExists(ctx, svc, instanceType)
if err != nil {
return "", err
}
if exists {
return instanceType, nil
}
}
return "", fmt.Errorf("No preferred instance type available in region: %s", networkConfig.AwsRegion)
}

func instanceTypeExists(ctx context.Context, svc *ec2.Client, instanceType types.InstanceType) (bool, error) {
input := &ec2.DescribeInstanceTypeOfferingsInput{
Filters: []types.Filter{
{
Name: aws.String("instance-type"),
Values: []string{string(instanceType)},
},
},
LocationType: types.LocationTypeRegion,
}

resp, err := svc.DescribeInstanceTypeOfferings(ctx, input)
if err != nil {
return false, err
}

return len(resp.InstanceTypeOfferings) > 0, nil
}
16 changes: 10 additions & 6 deletions gitpod-network-check/cmd/common.go
Original file line number Diff line number Diff line change
@@ -51,14 +51,16 @@ func cleanup(ctx context.Context, svc *ec2.Client, iamsvc *iam.Client) {
},
})
if err != nil {
log.WithError(err).Warn("Failed to list instances, please cleanup manually")
log.WithError(err).Error("Failed to list instances, please cleanup instances manually")
} else if len(instances.Reservations) == 0 {
log.Info("No instances found.")
}

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

for _, sg := range securityGroups.SecurityGroups {
SecurityGroups = append(SecurityGroups, *sg.GroupId)
if securityGroups != nil {
for _, sg := range securityGroups.SecurityGroups {
SecurityGroups = append(SecurityGroups, *sg.GroupId)
}
}
}

1 change: 1 addition & 0 deletions gitpod-network-check/cmd/root.go
Original file line number Diff line number Diff line change
@@ -89,6 +89,7 @@ func init() {
networkCheckCmd.PersistentFlags().StringSliceVar(&networkConfig.PodSubnets, "pod-subnets", []string{}, "List of pod subnets")
networkCheckCmd.PersistentFlags().StringSliceVar(&networkConfig.HttpsHosts, "https-hosts", []string{}, "Hosts to test for outbound HTTPS connectivity")
bindFlags(networkCheckCmd, v)
log.Infof("ℹ️ Running with region `%s`, main subnet `%v`, pod subnet `%v`, and hosts `%v`", networkConfig.AwsRegion, networkConfig.MainSubnets, networkConfig.PodSubnets, networkConfig.HttpsHosts)
}

func readConfigFile() *viper.Viper {

0 comments on commit bb54dc0

Please sign in to comment.