Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCM-12201 | test: Find a permanent way for proxy setup for cluster creation #76

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pkg/aws/aws_client/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -26,15 +27,17 @@ 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{
xueli181114 marked this conversation as resolved.
Show resolved Hide resolved
Name: &k,
Name: &copyKey,
Values: v,
}
filterInput = append(filterInput, awsFilter)
}
}

describeImageInput := &ec2.DescribeImagesInput{
Owners: []string{consts.AmazonName},
Filters: filterInput,
}

Expand Down
27 changes: 17 additions & 10 deletions pkg/aws/aws_client/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
11 changes: 1 addition & 10 deletions pkg/aws/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions pkg/test/vpc_client/bastion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
70 changes: 52 additions & 18 deletions pkg/test/vpc_client/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vpc_client

import (
"fmt"
"regexp"
"time"

"github.com/aws/aws-sdk-go-v2/service/ec2/types"
Expand All @@ -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)
Expand Down
Loading