diff --git a/api/v1beta2/tags.go b/api/v1beta2/tags.go index 829dd514c6..b5e868c3a6 100644 --- a/api/v1beta2/tags.go +++ b/api/v1beta2/tags.go @@ -243,12 +243,6 @@ func (b BuildParams) WithCloudProvider(name string) BuildParams { // Build builds tags including the cluster tag and returns them in map form. func Build(params BuildParams) Tags { tags := make(Tags) - - // Add the name tag first so that it can be overwritten by a user-provided tag in the `Additional` tags. - if params.Name != nil { - tags["Name"] = *params.Name - } - for k, v := range params.Additional { tags[k] = v } @@ -260,5 +254,9 @@ func Build(params BuildParams) Tags { tags[NameAWSClusterAPIRole] = *params.Role } + if params.Name != nil { + tags["Name"] = *params.Name + } + return tags } diff --git a/pkg/cloud/services/network/subnets.go b/pkg/cloud/services/network/subnets.go index 84766c4289..61aa8bcb5a 100644 --- a/pkg/cloud/services/network/subnets.go +++ b/pkg/cloud/services/network/subnets.go @@ -554,17 +554,12 @@ func (s *Service) getSubnetTagParams(unmanagedVPC bool, id string, public bool, additionalTags[k] = v } - // Prefer `Name` tag if given, else generate a name var name strings.Builder - if manualTagName, ok := manualTags["Name"]; ok { - name.WriteString(manualTagName) - } else { - name.WriteString(s.scope.Name()) - name.WriteString("-subnet-") - name.WriteString(role) - name.WriteString("-") - name.WriteString(zone) - } + name.WriteString(s.scope.Name()) + name.WriteString("-subnet-") + name.WriteString(role) + name.WriteString("-") + name.WriteString(zone) return infrav1.BuildParams{ ClusterName: s.scope.Name(), diff --git a/pkg/cloud/services/network/subnets_test.go b/pkg/cloud/services/network/subnets_test.go index f7c02d4359..a423c7a71f 100644 --- a/pkg/cloud/services/network/subnets_test.go +++ b/pkg/cloud/services/network/subnets_test.go @@ -1920,138 +1920,6 @@ func TestReconcileSubnets(t *testing.T) { Return(nil, nil) }, }, - { - name: "Managed VPC, existing public subnet, 2 subnets in spec, should create 1 subnet, custom Name tag", - input: NewClusterScope().WithNetwork(&infrav1.NetworkSpec{ - VPC: infrav1.VPCSpec{ - ID: subnetsVPCID, - Tags: infrav1.Tags{ - infrav1.ClusterTagKey("test-cluster"): "owned", - }, - }, - Subnets: []infrav1.SubnetSpec{ - { - ID: "subnet-1", - AvailabilityZone: "us-east-1a", - CidrBlock: "10.0.0.0/17", - IsPublic: true, - }, - { - AvailabilityZone: "us-east-1a", - CidrBlock: "10.0.128.0/17", - IsPublic: false, - Tags: map[string]string{"Name": "custom-sub"}, - }, - }, - }), - expect: func(m *mocks.MockEC2APIMockRecorder) { - m.DescribeSubnetsWithContext(context.TODO(), gomock.Eq(&ec2.DescribeSubnetsInput{ - Filters: []*ec2.Filter{ - { - Name: aws.String("state"), - Values: []*string{aws.String("pending"), aws.String("available")}, - }, - { - Name: aws.String("vpc-id"), - Values: []*string{aws.String(subnetsVPCID)}, - }, - }, - })). - Return(&ec2.DescribeSubnetsOutput{ - Subnets: []*ec2.Subnet{ - { - VpcId: aws.String(subnetsVPCID), - SubnetId: aws.String("subnet-1"), - AvailabilityZone: aws.String("us-east-1a"), - CidrBlock: aws.String("10.0.0.0/17"), - Tags: []*ec2.Tag{ - { - Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test-cluster"), - Value: aws.String("owned"), - }, - { - Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"), - Value: aws.String("public"), - }, - { - Key: aws.String("Name"), - Value: aws.String("test-cluster-subnet-public"), - }, - { - Key: aws.String("kubernetes.io/cluster/test-cluster"), - Value: aws.String("shared"), - }, - }, - }, - }, - }, nil) - - m.DescribeRouteTablesWithContext(context.TODO(), gomock.AssignableToTypeOf(&ec2.DescribeRouteTablesInput{})). - Return(&ec2.DescribeRouteTablesOutput{}, nil) - - m.DescribeNatGatewaysPagesWithContext(context.TODO(), - gomock.Eq(&ec2.DescribeNatGatewaysInput{ - Filter: []*ec2.Filter{ - { - Name: aws.String("vpc-id"), - Values: []*string{aws.String(subnetsVPCID)}, - }, - { - Name: aws.String("state"), - Values: []*string{aws.String("pending"), aws.String("available")}, - }, - }, - }), - gomock.Any()).Return(nil) - - m.CreateSubnetWithContext(context.TODO(), gomock.Eq(&ec2.CreateSubnetInput{ - VpcId: aws.String(subnetsVPCID), - CidrBlock: aws.String("10.0.128.0/17"), - AvailabilityZone: aws.String("us-east-1a"), - TagSpecifications: []*ec2.TagSpecification{ - { - ResourceType: aws.String("subnet"), - Tags: []*ec2.Tag{ - { - Key: aws.String("Name"), - Value: aws.String("custom-sub"), // must use the provided `Name` tag, not generate a name - }, - { - Key: aws.String("kubernetes.io/cluster/test-cluster"), - Value: aws.String("shared"), - }, - { - Key: aws.String("kubernetes.io/role/internal-elb"), - Value: aws.String("1"), - }, - { - Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test-cluster"), - Value: aws.String("owned"), - }, - { - Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"), - Value: aws.String("private"), - }, - }, - }, - }, - })). - Return(&ec2.CreateSubnetOutput{ - Subnet: &ec2.Subnet{ - VpcId: aws.String(subnetsVPCID), - SubnetId: aws.String("subnet-2"), - CidrBlock: aws.String("10.0.128.0/17"), - AvailabilityZone: aws.String("us-east-1a"), - }, - }, nil) - - m.WaitUntilSubnetAvailableWithContext(context.TODO(), gomock.Any()) - - // Public subnet - m.CreateTagsWithContext(context.TODO(), gomock.AssignableToTypeOf(&ec2.CreateTagsInput{})). - Return(nil, nil) - }, - }, { name: "With ManagedControlPlaneScope, Managed VPC, no existing subnets exist, two az's, expect two private and two public from default, created with tag including eksClusterName not a name of Cluster resource", input: NewManagedControlPlaneScope().