From 4ffb4bddec1635c4633001eabe56028472b9231b Mon Sep 17 00:00:00 2001 From: Xue Li Date: Thu, 31 Oct 2024 21:36:02 +0800 Subject: [PATCH] OCM-12201 | test: Find a permanent way for proxy setup for cluster creation --- pkg/aws/aws_client/image.go | 5 ++- pkg/aws/aws_client/vpc.go | 27 ++++++++----- pkg/aws/consts/consts.go | 11 +----- pkg/test/vpc_client/bastion.go | 8 ++-- pkg/test/vpc_client/proxy.go | 70 +++++++++++++++++++++++++--------- 5 files changed, 78 insertions(+), 43 deletions(-) diff --git a/pkg/aws/aws_client/image.go b/pkg/aws/aws_client/image.go index b7f51fa4..55ee04a8 100644 --- a/pkg/aws/aws_client/image.go +++ b/pkg/aws/aws_client/image.go @@ -5,6 +5,7 @@ import ( "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "github.com/openshift-online/ocm-common/pkg/aws/consts" "github.com/openshift-online/ocm-common/pkg/log" ) @@ -26,8 +27,9 @@ func (client *AWSClient) DescribeImage(imageIDs []string, filters ...map[string] filterInput := []types.Filter{} for _, filter := range filters { for k, v := range filter { + copyKey := k awsFilter := types.Filter{ - Name: &k, + Name: ©Key, Values: v, } filterInput = append(filterInput, awsFilter) @@ -35,6 +37,7 @@ func (client *AWSClient) DescribeImage(imageIDs []string, filters ...map[string] } describeImageInput := &ec2.DescribeImagesInput{ + Owners: []string{consts.AmazonName}, Filters: filterInput, } diff --git a/pkg/aws/aws_client/vpc.go b/pkg/aws/aws_client/vpc.go index 83eb7595..c5a432e1 100644 --- a/pkg/aws/aws_client/vpc.go +++ b/pkg/aws/aws_client/vpc.go @@ -11,17 +11,11 @@ import ( "github.com/openshift-online/ocm-common/pkg/log" ) -func (client *AWSClient) ListVPCByName(vpcName string) ([]types.Vpc, error) { +func (client *AWSClient) ListVPCs(filter ...types.Filter) ([]types.Vpc, error) { vpcs := []types.Vpc{} - filterKey := "tag:Name" - filter := []types.Filter{ - { - Name: &filterKey, - Values: []string{vpcName}, - }, - } - input := &ec2.DescribeVpcsInput{ - Filters: filter, + input := &ec2.DescribeVpcsInput{} + if len(filter) != 0 { + input.Filters = filter } resp, err := client.Ec2Client.DescribeVpcs(context.TODO(), input) if err != nil { @@ -31,6 +25,19 @@ func (client *AWSClient) ListVPCByName(vpcName string) ([]types.Vpc, error) { return vpcs, nil } +func (client *AWSClient) ListVPCByName(vpcName string) ([]types.Vpc, error) { + + filterKey := "tag:Name" + filter := []types.Filter{ + { + Name: &filterKey, + Values: []string{vpcName}, + }, + } + + return client.ListVPCs(filter...) +} + func (client *AWSClient) CreateVpc(cidr string, name ...string) (*ec2.CreateVpcOutput, error) { vpcName := CON.VpcDefaultName if len(name) == 1 { diff --git a/pkg/aws/consts/consts.go b/pkg/aws/consts/consts.go index 7e9cf914..e1f0b7db 100644 --- a/pkg/aws/consts/consts.go +++ b/pkg/aws/consts/consts.go @@ -55,16 +55,7 @@ const ( BastionName = "ocm-bastion" ) -var PublicImageName = "al2023-ami-2023.5.20241001.1-kernel-6.1-x86_64" - -var BastionImageMap = map[string]string{ - "us-east-1": "ami-01c647eace872fc02", - "us-east-2": "ami-00a9282ce3b5ddfb1", - "us-west-1": "ami-0f1ee917b10382dea", - "ap-southeast-1": "ami-0db1894e055420bc0", - "us-west-2": "ami-0b2b4f610e654d9ac", - "ap-northeast-1": "ami-0a21e01face015dd9", -} +var AmazonName = "amazon" const ( PublicSubNetTagKey = "PublicSubnet" diff --git a/pkg/test/vpc_client/bastion.go b/pkg/test/vpc_client/bastion.go index 6069012b..a30dc9b9 100644 --- a/pkg/test/vpc_client/bastion.go +++ b/pkg/test/vpc_client/bastion.go @@ -15,11 +15,11 @@ import ( func (vpc *VPC) LaunchBastion(imageID string, zone string) (*types.Instance, error) { var inst *types.Instance if imageID == "" { - var ok bool - imageID, ok = CON.BastionImageMap[vpc.Region] - if !ok { + var err error + imageID, err = vpc.FindProxyLaunchImage() + if err != nil { log.LogError("Cannot find bastion image of region %s in map bastionImageMap, please indicate it as parameter", vpc.Region) - return nil, fmt.Errorf("cannot find bastion image of region %s in map bastionImageMap, please indicate it as parameter", vpc.Region) + return nil, err } } pubSubnet, err := vpc.PreparePublicSubnet(zone) diff --git a/pkg/test/vpc_client/proxy.go b/pkg/test/vpc_client/proxy.go index d3469ad1..760975bc 100644 --- a/pkg/test/vpc_client/proxy.go +++ b/pkg/test/vpc_client/proxy.go @@ -2,6 +2,7 @@ package vpc_client import ( "fmt" + "regexp" "time" "github.com/aws/aws-sdk-go-v2/service/ec2/types" @@ -11,34 +12,67 @@ import ( "github.com/openshift-online/ocm-common/pkg/log" ) -// LaunchProxyInstance will launch a proxy instance on the indicated zone. -// If set imageID to empty, it will find the proxy image in the ProxyImageMap map -// LaunchProxyInstance will return proxyInstance detail, privateIPAddress,CAcontent and error -func (vpc *VPC) LaunchProxyInstance(zone string, keypairName string, privateKeyPath string) (inst types.Instance, privateIP string, proxyServerCA string, err error) { - filters := []map[string][]string{ - { - "name": { - CON.PublicImageName, - }, +// FindProxyLaunchImage will try to find a proper image based on the filters to launch the proxy instance +// No parameter needed here +// It will return an image ID and error if happens +func (vpc *VPC) FindProxyLaunchImage() (string, error) { + filters := map[string][]string{ + "architecture": { + "x86_64", + }, + "state": { + "available", + }, + "image-type": { + "machine", + }, + "is-public": { + "true", + }, + "virtualization-type": { + "hvm", + }, + "root-device-type": { + "ebs", }, } - output, err := vpc.AWSClient.DescribeImage([]string{}, filters...) + output, err := vpc.AWSClient.DescribeImage([]string{}, filters) if err != nil { log.LogError("Describe image met error: %s", err) - return inst, "", "", err + return "", err } - if output == nil { + if output == nil || len(output.Images) < 1 { log.LogError("Got the empty image via the filter: %s", filters) - return inst, "", "", nil + err = fmt.Errorf("got empty image list via the filter: %s", filters) + return "", err + } + expectedImageID := "" + nameRegexp := regexp.MustCompile(`al[0-9]{4}-ami[0-9\.-]*kernel[0-9-\._a-z]*`) + for _, image := range output.Images { + if nameRegexp.MatchString(*image.Name) { + expectedImageID = *image.ImageId + break + } } - if len(output.Images) < 1 { - log.LogError("Can't get the vaild image") - return inst, "", "", nil + if expectedImageID != "" { + log.LogInfo("Got the image ID : %s", expectedImageID) + } else { + log.LogError("Got no proper image meet the regex: %s", nameRegexp.String()) + err = fmt.Errorf("got no proper image meet the regex: %s", nameRegexp.String()) } - imageID := *output.Images[0].ImageId - log.LogInfo("Got the image ID : %s", imageID) + return expectedImageID, err +} + +// LaunchProxyInstance will launch a proxy instance on the indicated zone. +// If set imageID to empty, it will find the proxy image in the ProxyImageMap map +// LaunchProxyInstance will return proxyInstance detail, privateIPAddress,CAcontent and error +func (vpc *VPC) LaunchProxyInstance(zone string, keypairName string, privateKeyPath string) (inst types.Instance, privateIP string, proxyServerCA string, err error) { + imageID, err := vpc.FindProxyLaunchImage() + if err != nil { + return inst, "", "", err + } pubSubnet, err := vpc.PreparePublicSubnet(zone) if err != nil { log.LogInfo("Error preparing a subnet in current zone %s with image ID %s: %s", zone, imageID, err)