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-6723 | feat: allow to supply user tags on creation of hcp machine pool #1850

Merged
merged 1 commit into from
Apr 23, 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
9 changes: 9 additions & 0 deletions cmd/create/machinepool/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ var args struct {
rootDiskSize string
securityGroupIds []string
nodeDrainGracePeriod string
tags []string
}

var Cmd = &cobra.Command{
Expand Down Expand Up @@ -230,6 +231,14 @@ func init() {
"This flag is only supported for Hosted Control Planes.",
)

flags.StringSliceVar(
&args.tags,
"tags",
nil,
"Apply user defined tags to all resources created by ROSA in AWS. "+
"Tags are comma separated, for example: 'key value, foo bar'",
)

interactive.AddFlag(flags)
output.AddFlag(Cmd)
}
Expand Down
25 changes: 21 additions & 4 deletions cmd/create/machinepool/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,42 @@ var _ = Describe("Machine pool helper", func() {
})

Context("It create an AWS node pool builder successfully", func() {
It("Create AWS node pool with security group IDs when provided", func() {
It("Create AWS node pool with aws tags when provided", func() {
instanceType := "123"
securityGroupIds := []string{"123"}
awsTags := map[string]string{"label": "value"}

awsNpBuilder := createAwsNodePoolBuilder(instanceType, securityGroupIds)
awsNpBuilder := createAwsNodePoolBuilder(
instanceType,
securityGroupIds,
awsTags,
)
awsNodePool, err := awsNpBuilder.Build()
Expect(err).ToNot(HaveOccurred())
Expect(awsNodePool.AdditionalSecurityGroupIds()).To(Equal(securityGroupIds))
Expect(awsNodePool.InstanceType()).To(Equal(instanceType))
Expect(awsNodePool.Tags()).To(Equal(awsTags))
})
It("Create AWS node pool with security group IDs when provided", func() {
instanceType := "123"
securityGroupIds := []string{"123"}

awsNpBuilder := createAwsNodePoolBuilder(instanceType, securityGroupIds, map[string]string{})
awsNodePool, err := awsNpBuilder.Build()
Expect(err).ToNot(HaveOccurred())
Expect(awsNodePool.AdditionalSecurityGroupIds()).To(Equal(securityGroupIds))
Expect(awsNodePool.InstanceType()).To(Equal(instanceType))
Expect(awsNodePool.Tags()).To(HaveLen(0))
})
It("Create AWS node pool without security group IDs if not provided", func() {
instanceType := "123"

awsNpBuilder := createAwsNodePoolBuilder(instanceType, []string{})
awsNpBuilder := createAwsNodePoolBuilder(instanceType, []string{}, map[string]string{})
awsNodePool, err := awsNpBuilder.Build()
Expect(err).ToNot(HaveOccurred())
Expect(len(awsNodePool.AdditionalSecurityGroupIds())).To(Equal(0))
Expect(awsNodePool.AdditionalSecurityGroupIds()).To(HaveLen(0))
Expect(awsNodePool.InstanceType()).To(Equal(instanceType))
Expect(awsNodePool.Tags()).To(HaveLen(0))
})
})

Expand Down
5 changes: 5 additions & 0 deletions cmd/create/machinepool/machinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import (
func addMachinePool(cmd *cobra.Command, clusterKey string, cluster *cmv1.Cluster, r *rosa.Runtime) {
var err error

if cmd.Flags().Changed("tags") {
r.Reporter.Errorf("Setting the 'tags' flag is currently only allowed for Hosted Control Plane clusters")
os.Exit(1)
}

// Validate flags that are only allowed for multi-AZ clusters
isMultiAvailabilityZoneSet := cmd.Flags().Changed("multi-availability-zone")
if isMultiAvailabilityZoneSet && !cluster.MultiAZ() {
Expand Down
14 changes: 12 additions & 2 deletions cmd/create/machinepool/nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ func addNodePool(cmd *cobra.Command, clusterKey string, cluster *cmv1.Cluster, r
securityGroupIds[i] = strings.TrimSpace(sg)
}

awsTags := machinepools.GetAwsTags(cmd, r, args.tags)

npBuilder := cmv1.NewNodePool()
npBuilder.ID(name).Labels(labelMap).
Taints(taintBuilders...)
Expand Down Expand Up @@ -384,7 +386,7 @@ func addNodePool(cmd *cobra.Command, clusterKey string, cluster *cmv1.Cluster, r
npBuilder.TuningConfigs(inputTuningConfig...)
}

npBuilder.AWSNodePool(createAwsNodePoolBuilder(instanceType, securityGroupIds))
npBuilder.AWSNodePool(createAwsNodePoolBuilder(instanceType, securityGroupIds, awsTags))

nodeDrainGracePeriod := args.nodeDrainGracePeriod
if interactive.Enabled() {
Expand Down Expand Up @@ -494,12 +496,20 @@ func getSubnetFromAvailabilityZone(cmd *cobra.Command, r *rosa.Runtime, isAvaila
return "", fmt.Errorf("Failed to find a private subnet for '%s' availability zone", availabilityZone)
}

func createAwsNodePoolBuilder(instanceType string, securityGroupIds []string) *cmv1.AWSNodePoolBuilder {
func createAwsNodePoolBuilder(
instanceType string,
securityGroupIds []string,
awsTags map[string]string,
) *cmv1.AWSNodePoolBuilder {
awsNpBuilder := cmv1.NewAWSNodePool().InstanceType(instanceType)

if len(securityGroupIds) > 0 {
awsNpBuilder.AdditionalSecurityGroupIds(securityGroupIds...)
}

if len(awsTags) > 0 {
awsNpBuilder.Tags(awsTags)
}

return awsNpBuilder
}
37 changes: 37 additions & 0 deletions pkg/helper/machinepools/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/spf13/cobra"

"github.com/openshift/rosa/pkg/aws"
"github.com/openshift/rosa/pkg/interactive"
"github.com/openshift/rosa/pkg/rosa"
)
Expand Down Expand Up @@ -189,6 +190,42 @@ func taintValidator(val interface{}) error {
return fmt.Errorf("can only validate strings, got %v", val)
}

func GetAwsTags(cmd *cobra.Command, r *rosa.Runtime, inputTags []string) map[string]string {
// Custom tags for AWS resources
tags := inputTags
tagsList := map[string]string{}
if interactive.Enabled() {
tagsInput, err := interactive.GetString(interactive.Input{
Question: "Tags",
Help: cmd.Flags().Lookup("tags").Usage,
Default: strings.Join(tags, ","),
Validators: []interactive.Validator{
aws.UserTagValidator,
aws.UserTagDuplicateValidator,
},
})
if err != nil {
r.Reporter.Errorf("Expected a valid set of tags: %s", err)
os.Exit(1)
}
if len(tagsInput) > 0 {
tags = strings.Split(tagsInput, ",")
}
}
if len(tags) > 0 {
if err := aws.UserTagValidator(tags); err != nil {
r.Reporter.Errorf("%s", err)
os.Exit(1)
}
delim := aws.GetTagsDelimiter(tags)
for _, tag := range tags {
t := strings.Split(tag, delim)
tagsList[t[0]] = strings.TrimSpace(t[1])
}
}
return tagsList
}

func GetLabelMap(cmd *cobra.Command, r *rosa.Runtime, existingLabels map[string]string,
inputLabels string) map[string]string {
if interactive.Enabled() {
Expand Down