Skip to content
Open
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
6 changes: 2 additions & 4 deletions controllers/awscluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,7 @@ func mockedVPCCallsForExistingVPCAndSubnets(m *mocks.MockEC2APIMockRecorder) {
},
},
}, nil)
// subnet-1 has no existing tags, so both new tags are applied
m.CreateTags(context.TODO(), gomock.Eq(&ec2.CreateTagsInput{
Resources: []string{"subnet-1"},
Tags: []ec2types.Tag{
Expand All @@ -836,13 +837,10 @@ func mockedVPCCallsForExistingVPCAndSubnets(m *mocks.MockEC2APIMockRecorder) {
},
},
})).Return(&ec2.CreateTagsOutput{}, nil)
// subnet-2 already has `kubernetes.io/cluster/test-cluster=shared`, so only the ELB role tag is new
m.CreateTags(context.TODO(), gomock.Eq(&ec2.CreateTagsInput{
Resources: []string{"subnet-2"},
Tags: []ec2types.Tag{
{
Key: aws.String("kubernetes.io/cluster/test-cluster"),
Value: aws.String("shared"),
},
{
Key: aws.String("kubernetes.io/role/elb"),
Value: aws.String("1"),
Expand Down
20 changes: 2 additions & 18 deletions pkg/cloud/services/network/subnets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2779,16 +2779,12 @@ func TestReconcileSubnets(t *testing.T) {
},
}), gomock.Any()).Return(&ec2.DescribeNatGatewaysOutput{}, nil)

// Public subnet
// Public subnet - only new or changed tags
expectedAppliedAwsTagsForSubnet1 := []types.Tag{
{
Key: aws.String("Name"),
Value: aws.String("test-cluster-subnet-public-us-east-1a"),
},
{
Key: aws.String("kubernetes.io/cluster/test-cluster"),
Value: aws.String("owned"),
},
{
Key: aws.String("kubernetes.io/role/elb"),
Value: aws.String("1"),
Expand All @@ -2797,10 +2793,6 @@ func TestReconcileSubnets(t *testing.T) {
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("this-tag-is-in-the-spec"),
Value: aws.String("but-its-not-on-aws"),
Expand All @@ -2811,16 +2803,12 @@ func TestReconcileSubnets(t *testing.T) {
})).
Return(nil, nil)

// Private subnet
// Private subnet - only new or changed tags
expectedAppliedAwsTagsForSubnet2 := []types.Tag{
{
Key: aws.String("Name"),
Value: aws.String("test-cluster-subnet-private-us-east-1a"),
},
{
Key: aws.String("kubernetes.io/cluster/test-cluster"),
Value: aws.String("owned"),
},
{
Key: aws.String("kubernetes.io/role/internal-elb"),
Value: aws.String("1"),
Expand All @@ -2829,10 +2817,6 @@ func TestReconcileSubnets(t *testing.T) {
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"),
},
{
Key: aws.String("subnet-2-this-tag-is-in-the-spec"),
Value: aws.String("subnet-2-but-its-not-on-aws"),
Expand Down
12 changes: 0 additions & 12 deletions pkg/cloud/services/network/vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,10 @@ func TestReconcileVPC(t *testing.T) {
m.CreateTags(context.TODO(), &ec2.CreateTagsInput{
Resources: []string{"vpc-exists"},
Tags: []types.Tag{
{
Key: aws.String("Name"),
Value: aws.String("test-cluster-vpc"),
},
{
Key: aws.String("additional"),
Value: aws.String("tags"),
},
{
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("common"),
},
},
})
m.DescribeVpcAttribute(context.TODO(), gomock.AssignableToTypeOf(&ec2.DescribeVpcAttributeInput{})).
Expand Down
41 changes: 15 additions & 26 deletions pkg/cloud/tags/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type BuilderOption func(*Builder)
// Builder is the interface for a tags builder.
type Builder struct {
params *infrav1.BuildParams
applyFunc func(params *infrav1.BuildParams) error
applyFunc func(params *infrav1.BuildParams, diff infrav1.Tags) error
}

// New creates a new TagsBuilder with the specified build parameters
Expand All @@ -74,42 +74,32 @@ func New(params *infrav1.BuildParams, opts ...BuilderOption) *Builder {
return builder
}

// Apply tags a resource with tags including the cluster tag.
func (b *Builder) Apply() error {
// Ensure applies the tags if the current tags differ from the params.
// Only new or changed tags are passed to the apply function.
func (b *Builder) Ensure(current infrav1.Tags) error {
if b.params == nil {
return ErrBuildParamsRequired
}
if b.applyFunc == nil {
return ErrApplyFuncRequired
}

if err := b.applyFunc(b.params); err != nil {
return fmt.Errorf("failed applying tags: %w", err)
}
return nil
}

// Ensure applies the tags if the current tags differ from the params.
func (b *Builder) Ensure(current infrav1.Tags) error {
if b.params == nil {
return ErrBuildParamsRequired
}
if diff := computeDiff(current, *b.params); len(diff) > 0 {
return b.Apply()
if err := b.applyFunc(b.params, diff); err != nil {
return fmt.Errorf("failed applying tags: %w", err)
}
}
return nil
}

// WithEC2 is used to denote that the tags builder will be using EC2.
func WithEC2(ec2client common.EC2API) BuilderOption {
return func(b *Builder) {
b.applyFunc = func(params *infrav1.BuildParams) error {
tags := infrav1.Build(*params)
awsTags := make([]ec2types.Tag, 0, len(tags))
b.applyFunc = func(params *infrav1.BuildParams, diff infrav1.Tags) error {
awsTags := make([]ec2types.Tag, 0, len(diff))

// For testing, we need sorted keys
sortedKeys := make([]string, 0, len(tags))
for k := range tags {
sortedKeys := make([]string, 0, len(diff))
for k := range diff {
// We want to filter out the tag keys that start with `aws:` as they are reserved for internal AWS use.
if !strings.HasPrefix(k, AwsInternalTagPrefix) {
sortedKeys = append(sortedKeys, k)
Expand All @@ -120,7 +110,7 @@ func WithEC2(ec2client common.EC2API) BuilderOption {
for _, key := range sortedKeys {
tag := ec2types.Tag{
Key: aws.String(key),
Value: aws.String(tags[key]),
Value: aws.String(diff[key]),
}
awsTags = append(awsTags, tag)
}
Expand All @@ -139,10 +129,9 @@ func WithEC2(ec2client common.EC2API) BuilderOption {
// WithEKS is used to specify that the tags builder will be targeting EKS.
func WithEKS(ctx context.Context, eksclient eksClient) BuilderOption {
return func(b *Builder) {
b.applyFunc = func(params *infrav1.BuildParams) error {
tags := infrav1.Build(*params)
eksTags := make(map[string]*string, len(tags))
for k, v := range tags {
b.applyFunc = func(params *infrav1.BuildParams, diff infrav1.Tags) error {
eksTags := make(map[string]*string, len(diff))
for k, v := range diff {
// We want to filter out the tag keys that start with `aws:` as they are reserved for internal AWS use.
if !strings.HasPrefix(k, AwsInternalTagPrefix) {
eksTags[k] = aws.String(v)
Expand Down
Loading
Loading