Skip to content

Commit 7bb8cda

Browse files
iQQBotkylos101
andauthored
support custom AMI (#10)
* support custom AMI * Apply suggestions from code review Co-authored-by: Kyle Brennan <[email protected]> --------- Co-authored-by: Kyle Brennan <[email protected]>
1 parent bb54dc0 commit 7bb8cda

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

gitpod-network-check/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ A CLI to check if your network setup is suitable for the installation of Gitpod.
4343
main-subnets: subnet-0554e84f033a64c56, subnet-08584621e7754e505, subnet-094c6fd68aea493b7
4444
pod-subnets: subnet-028d11dce93b8eefc, subnet-04ec8257d95c434b7,subnet-00a83550ce709f39c
4545
https-hosts: accounts.google.com, github.com
46+
instance-ami: # put your custom ami id here if you want to use it, otherwise it will using latest ubuntu AMI from aws
4647
```
4748
4849
2. Run the network diagnosis

gitpod-network-check/cmd/checks.go

+32-6
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,19 @@ func launchInstances(ctx context.Context, ec2Client *ec2.Client, subnets []strin
320320
}
321321

322322
func launchInstanceInSubnet(ctx context.Context, ec2Client *ec2.Client, subnetID, secGroupId string, instanceProfileName *string, instanceType types.InstanceType) (string, error) {
323-
regionalAMI, err := findUbuntuAMI(ctx, ec2Client)
324-
if err != nil {
325-
return "", err
323+
amiId := ""
324+
if networkConfig.InstanceAMI != "" {
325+
customAMIId, err := findCustomAMI(ctx, ec2Client, networkConfig.InstanceAMI)
326+
if err != nil {
327+
return "", err
328+
}
329+
amiId = customAMIId
330+
} else {
331+
regionalAMI, err := findUbuntuAMI(ctx, ec2Client)
332+
if err != nil {
333+
return "", err
334+
}
335+
amiId = regionalAMI
326336
}
327337

328338
// Specify the user data script to install the SSM Agent
@@ -335,7 +345,7 @@ func launchInstanceInSubnet(ctx context.Context, ec2Client *ec2.Client, subnetID
335345
userDataEncoded := base64.StdEncoding.EncodeToString([]byte(userData))
336346

337347
input := &ec2.RunInstancesInput{
338-
ImageId: aws.String(regionalAMI), // Example AMI ID, replace with an actual one
348+
ImageId: aws.String(amiId), // Example AMI ID, replace with an actual one
339349
InstanceType: instanceType,
340350
MaxCount: aws.Int32(1),
341351
MinCount: aws.Int32(1),
@@ -359,7 +369,7 @@ func launchInstanceInSubnet(ctx context.Context, ec2Client *ec2.Client, subnetID
359369
}
360370

361371
var result *ec2.RunInstancesOutput
362-
err = wait.PollUntilContextTimeout(ctx, 500*time.Millisecond, 10*time.Second, false, func(ctx context.Context) (done bool, err error) {
372+
err := wait.PollUntilContextTimeout(ctx, 500*time.Millisecond, 10*time.Second, false, func(ctx context.Context) (done bool, err error) {
363373
result, err = ec2Client.RunInstances(ctx, input)
364374

365375
if err != nil {
@@ -384,6 +394,22 @@ func launchInstanceInSubnet(ctx context.Context, ec2Client *ec2.Client, subnetID
384394
return aws.ToString(result.Instances[0].InstanceId), nil
385395
}
386396

397+
func findCustomAMI(ctx context.Context, client *ec2.Client, amiId string) (string, error) {
398+
input := &ec2.DescribeImagesInput{
399+
ImageIds: []string{amiId},
400+
}
401+
402+
result, err := client.DescribeImages(ctx, input)
403+
if err != nil {
404+
return "", err
405+
}
406+
if len(result.Images) > 0 {
407+
return *result.Images[0].ImageId, nil
408+
}
409+
410+
return "", fmt.Errorf("no custom AMI found")
411+
}
412+
387413
// findUbuntuAMI searches for the latest Ubuntu AMI in the region of the EC2 client.
388414
func findUbuntuAMI(ctx context.Context, client *ec2.Client) (string, error) {
389415
// You may want to update these filters based on your specific requirements
@@ -618,7 +644,7 @@ func instanceTypeExists(ctx context.Context, svc *ec2.Client, instanceType types
618644
input := &ec2.DescribeInstanceTypeOfferingsInput{
619645
Filters: []types.Filter{
620646
{
621-
Name: aws.String("instance-type"),
647+
Name: aws.String("instance-type"),
622648
Values: []string{string(instanceType)},
623649
},
624650
},

gitpod-network-check/cmd/root.go

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type NetworkConfig struct {
2323
MainSubnets []string
2424
PodSubnets []string
2525
HttpsHosts []string
26+
InstanceAMI string
2627
}
2728

2829
var networkConfig = NetworkConfig{LogLevel: "INFO"}
@@ -89,6 +90,7 @@ func init() {
8990
networkCheckCmd.PersistentFlags().StringSliceVar(&networkConfig.PodSubnets, "pod-subnets", []string{}, "List of pod subnets")
9091
networkCheckCmd.PersistentFlags().StringSliceVar(&networkConfig.HttpsHosts, "https-hosts", []string{}, "Hosts to test for outbound HTTPS connectivity")
9192
bindFlags(networkCheckCmd, v)
93+
networkCheckCmd.PersistentFlags().StringVar(&networkConfig.InstanceAMI, "instance-ami", "", "Custom ec2 instance AMI id, if not set will use latest ubuntu")
9294
log.Infof("ℹ️ Running with region `%s`, main subnet `%v`, pod subnet `%v`, and hosts `%v`", networkConfig.AwsRegion, networkConfig.MainSubnets, networkConfig.PodSubnets, networkConfig.HttpsHosts)
9395
}
9496

gitpod-network-check/gitpod-network-check.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ log-level: debug # Options: debug, info, warning, error
22
region: eu-central-1
33
main-subnets: subnet-017c6a80f4879d851, subnet-0215744d52cd1c01f
44
pod-subnets: subnet-00a118009d1d572a5, subnet-062288af00ba50d86
5-
https-hosts: accounts.google.com, https://github.com
5+
https-hosts: accounts.google.com, https://github.com
6+
# put your custom ami id here if you want to use it, otherwise it will using latest ubuntu AMI from aws
7+
instance-ami:

0 commit comments

Comments
 (0)