Skip to content

Commit

Permalink
OCM-11855 | test: Create bastion with proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
jameszwang committed Nov 20, 2024
1 parent 7e27b75 commit 29391a2
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions pkg/test/vpc_client/bastion.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import (
"errors"
"fmt"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/openshift-online/ocm-common/pkg/file"
"net"
"time"

CON "github.com/openshift-online/ocm-common/pkg/aws/consts"
"github.com/openshift-online/ocm-common/pkg/log"
)

// LaunchBastion will launch a bastion instance on the indicated zone.
// If set imageID to empty, it will find the bastion image using filter with specific name.
func (vpc *VPC) LaunchBastion(imageID string, zone string, userData string) (*types.Instance, error) {
func (vpc *VPC) LaunchBastion(imageID string, zone string, userData string, keypairName string,
privateKeyPath string) (*types.Instance, error) {
var inst *types.Instance
if imageID == "" {

Expand All @@ -38,12 +41,23 @@ func (vpc *VPC) LaunchBastion(imageID string, zone string, userData string) (*ty
log.LogError("Prepare SG failed for the bastion preparation %s", err)
return inst, err
}

key, err := vpc.CreateKeyPair(fmt.Sprintf("%s-bastion", CON.InstanceKeyNamePrefix))
keyName := fmt.Sprintf("%s-%s", CON.InstanceKeyNamePrefix, keypairName)
key, err := vpc.CreateKeyPair(keyName)
if err != nil {
log.LogError("Create key pair failed %s", err)
return inst, err
}
tags := map[string]string{
"Name": CON.BastionName,
}
_, err = vpc.AWSClient.TagResource(*key.KeyPairId, tags)
if err != nil {
log.LogError("Add tag for key pair %s failed %s", *key.KeyPairId, err)
return inst, err
}

privateKeyName := fmt.Sprintf("%s-%s", keypairName, "keyPair.pem")
_, err = file.WriteToFile(*key.KeyMaterial, privateKeyName, privateKeyPath)

Check failure on line 60 in pkg/test/vpc_client/bastion.go

View workflow job for this annotation

GitHub Actions / Lint

ineffectual assignment to err (ineffassign)
instOut, err := vpc.AWSClient.LaunchInstance(pubSubnet.ID, imageID, 1, "t3.medium", *key.KeyName,
[]string{SGID}, true, userData)

Expand All @@ -53,7 +67,7 @@ func (vpc *VPC) LaunchBastion(imageID string, zone string, userData string) (*ty
} else {
log.LogInfo("Launch bastion instance %s succeed", *instOut.Instances[0].InstanceId)
}
tags := map[string]string{
tags = map[string]string{
"Name": CON.BastionName,
}
instID := *instOut.Instances[0].InstanceId
Expand All @@ -68,12 +82,16 @@ func (vpc *VPC) LaunchBastion(imageID string, zone string, userData string) (*ty
return inst, err
}
log.LogInfo("Prepare EIP successfully for the bastion preparation. Launch with IP: %s", publicIP)

time.Sleep(2 * time.Minute)

inst = &instOut.Instances[0]
inst.PublicIpAddress = &publicIP
return inst, nil
}

func (vpc *VPC) PrepareBastionProxy(zone string, cidrBlock string) (*types.Instance, error) {
func (vpc *VPC) PrepareBastionProxy(zone string, cidrBlock string, keypairName string,
privateKeyPath string) (*types.Instance, error) {
filters := []map[string][]string{
{
"vpc-id": {
Expand Down Expand Up @@ -116,7 +134,7 @@ func (vpc *VPC) PrepareBastionProxy(zone string, cidrBlock string) (*types.Insta
systemctl enable squid`, cidrBlock)

encodeUserData := base64.StdEncoding.EncodeToString([]byte(userData))
return vpc.LaunchBastion("", zone, encodeUserData)
return vpc.LaunchBastion("", zone, encodeUserData, keypairName, privateKeyPath)

}
log.LogInfo("Found existing bastion: %s", *insts[0].InstanceId)
Expand Down

0 comments on commit 29391a2

Please sign in to comment.