Skip to content
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
38 changes: 26 additions & 12 deletions cmd/bastion/aws/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type CreateBastionOpts struct {
AWSKey string
AWSSecretKey string
Wait bool
AdditionalTags []string
}

func NewCreateCommand() *cobra.Command {
Expand All @@ -59,6 +60,7 @@ func NewCreateCommand() *cobra.Command {
cmd.Flags().StringVar(&opts.SSHKeyFile, "ssh-key-file", opts.SSHKeyFile, "File with public SSH key to use for bastion instance")
cmd.Flags().StringVar(&opts.AWSCredentialsFile, "aws-creds", opts.AWSCredentialsFile, "File with AWS credentials")
cmd.Flags().BoolVar(&opts.Wait, "wait", opts.Wait, "Wait for instance to be running")
cmd.Flags().StringSliceVar(&opts.AdditionalTags, "additional-tags", opts.AdditionalTags, "Additional tags to set on AWS resources (key=value)")

_ = cmd.MarkFlagRequired("aws-creds")

Expand Down Expand Up @@ -157,19 +159,31 @@ func (o *CreateBastionOpts) Run(ctx context.Context, logger logr.Logger) (string
o.Retryer = awsConfig()
})

tagMap, err := util.ParseAWSTags(o.AdditionalTags)
if err != nil {
return "", "", fmt.Errorf("failed to parse additional tags: %w", err)
}
var additionalTags []ec2types.Tag
for k, v := range tagMap {
additionalTags = append(additionalTags, ec2types.Tag{
Key: aws.String(k),
Value: aws.String(v),
})
}

// Ensure security group exists
sgID, err := ensureBastionSecurityGroup(ctx, logger, ec2Client, infraID, o.Name)
sgID, err := ensureBastionSecurityGroup(ctx, logger, ec2Client, infraID, o.Name, additionalTags)
if err != nil {
return "", "", fmt.Errorf("failed to ensure security group for bastion: %w", err)
}

// Ensure keypair exists
if err := ensureBastionKeyPair(ctx, logger, ec2Client, infraID, o.Name, sshPublicKey); err != nil {
if err := ensureBastionKeyPair(ctx, logger, ec2Client, infraID, o.Name, sshPublicKey, additionalTags); err != nil {
return "", "", fmt.Errorf("failed to ensure bastion keypair: %w", err)
}

// Create ec2 instance
instanceID, err := runEC2BastionInstance(ctx, logger, ec2Client, sgID, infraID, o.Name)
instanceID, err := runEC2BastionInstance(ctx, logger, ec2Client, sgID, infraID, o.Name, additionalTags)
if err != nil {
return "", "", fmt.Errorf("failed to run bastion machine instance: %w", err)
}
Expand All @@ -186,7 +200,7 @@ func (o *CreateBastionOpts) Run(ctx context.Context, logger logr.Logger) (string
return instanceID, publicIP, nil
}

func ensureBastionSecurityGroup(ctx context.Context, logger logr.Logger, ec2Client *ec2.Client, infraID, clusterName string) (string, error) {
func ensureBastionSecurityGroup(ctx context.Context, logger logr.Logger, ec2Client *ec2.Client, infraID, clusterName string, additionalTags []ec2types.Tag) (string, error) {
// find VPC
vpcID, err := existingVPC(ctx, ec2Client, infraID)
if err != nil {
Expand All @@ -210,7 +224,7 @@ func ensureBastionSecurityGroup(ctx context.Context, logger logr.Logger, ec2Clie
TagSpecifications: []ec2types.TagSpecification{
{
ResourceType: ec2types.ResourceTypeSecurityGroup,
Tags: []ec2types.Tag{
Tags: append([]ec2types.Tag{
{
Key: aws.String(fmt.Sprintf("kubernetes.io/cluster/%s", infraID)),
Value: aws.String("owned"),
Expand All @@ -227,7 +241,7 @@ func ensureBastionSecurityGroup(ctx context.Context, logger logr.Logger, ec2Clie
Key: aws.String(supportawsutil.HypershiftClusterNameTagKey),
Value: aws.String(clusterName),
},
},
}, additionalTags...),
},
},
})
Expand Down Expand Up @@ -349,7 +363,7 @@ func existingVPC(ctx context.Context, ec2Client *ec2.Client, infraID string) (st
return vpcID, nil
}

func ensureBastionKeyPair(ctx context.Context, logger logr.Logger, ec2Client *ec2.Client, infraID, clusterName string, publicKey []byte) error {
func ensureBastionKeyPair(ctx context.Context, logger logr.Logger, ec2Client *ec2.Client, infraID, clusterName string, publicKey []byte, additionalTags []ec2types.Tag) error {
keyPairID, err := existingKeyPair(ctx, ec2Client, infraID)
if err != nil {
return fmt.Errorf("failed to check for existing keypair: %w", err)
Expand All @@ -366,7 +380,7 @@ func ensureBastionKeyPair(ctx context.Context, logger logr.Logger, ec2Client *ec
TagSpecifications: []ec2types.TagSpecification{
{
ResourceType: ec2types.ResourceTypeKeyPair,
Tags: []ec2types.Tag{
Tags: append([]ec2types.Tag{
{
Key: aws.String(fmt.Sprintf("kubernetes.io/cluster/%s", infraID)),
Value: aws.String("owned"),
Expand All @@ -383,7 +397,7 @@ func ensureBastionKeyPair(ctx context.Context, logger logr.Logger, ec2Client *ec
Key: aws.String(supportawsutil.HypershiftClusterNameTagKey),
Value: aws.String(clusterName),
},
},
}, additionalTags...),
},
},
})
Expand Down Expand Up @@ -454,7 +468,7 @@ func getLatestAmazonLinux2AMI(ctx context.Context, ec2Client *ec2.Client) (strin
return aws.ToString(latestAMI.ImageId), nil
}

func runEC2BastionInstance(ctx context.Context, logger logr.Logger, ec2Client *ec2.Client, sgID, infraID, clusterName string) (string, error) {
func runEC2BastionInstance(ctx context.Context, logger logr.Logger, ec2Client *ec2.Client, sgID, infraID, clusterName string, additionalTags []ec2types.Tag) (string, error) {
// find existing instance
instanceID, err := existingInstance(ctx, ec2Client, infraID)
if err != nil {
Expand Down Expand Up @@ -501,7 +515,7 @@ func runEC2BastionInstance(ctx context.Context, logger logr.Logger, ec2Client *e
TagSpecifications: []ec2types.TagSpecification{
{
ResourceType: ec2types.ResourceTypeInstance,
Tags: []ec2types.Tag{
Tags: append([]ec2types.Tag{
{
Key: aws.String(fmt.Sprintf("kubernetes.io/cluster/%s", infraID)),
Value: aws.String("owned"),
Expand All @@ -518,7 +532,7 @@ func runEC2BastionInstance(ctx context.Context, logger logr.Logger, ec2Client *e
Key: aws.String(supportawsutil.HypershiftClusterNameTagKey),
Value: aws.String(clusterName),
},
},
}, additionalTags...),
},
},
})
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/util/dump/journals.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
bastionaws "github.com/openshift/hypershift/cmd/bastion/aws"
awsutil "github.com/openshift/hypershift/cmd/infra/aws/util"
cmdutil "github.com/openshift/hypershift/cmd/util"
supportawsutil "github.com/openshift/hypershift/support/awsutil"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
Expand Down Expand Up @@ -151,11 +152,19 @@ func setupBastion(t *testing.T, ctx context.Context, hc *hyperv1.HostedCluster,
if hc.Annotations[hyperv1.AWSMachinePublicIPs] == "true" {
return "", nil
}
additionalTags := []string{
supportawsutil.HypershiftSourceTagKey + "=e2e",
}
if prowJobID := os.Getenv("PROW_JOB_ID"); prowJobID != "" {
additionalTags = append(additionalTags, supportawsutil.HypershiftProwJobIDTagKey+"="+prowJobID)
}

createBastion := bastionaws.CreateBastionOpts{
Namespace: hc.Namespace,
Name: hc.Name,
AWSCredentialsFile: awsCreds,
Wait: true,
AdditionalTags: additionalTags,
}
_, bastionIP, err := createBastion.Run(ctx, zapr.NewLoggerWithOptions(createLogger))
if err != nil {
Expand Down