From 4a57d84d255146e9afc4961afe368fb3b28fe141 Mon Sep 17 00:00:00 2001 From: calvix Date: Thu, 2 Nov 2023 11:20:29 +0100 Subject: [PATCH] add-tags-to-resources2 (#211) * add-tags-to-resources2 * add-tags-to-resources2 --- CHANGELOG.md | 4 ++++ controllers/awsmachinepool_controller.go | 1 + controllers/awsmachinetemplate_controller.go | 1 + controllers/awsmanagedcontrolplane_controller.go | 1 + pkg/iam/iam.go | 9 +++++++++ 5 files changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f602546a..18d4d74f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add tags from `AWSCluster.Spec.AdditionalTags` and `AWSManagedControlPlane.Spec.AdditionalTags` to all created resources. + ## [0.11.0] - 2023-11-01 ### Added diff --git a/controllers/awsmachinepool_controller.go b/controllers/awsmachinepool_controller.go index 8bb1426e..85a4e646 100644 --- a/controllers/awsmachinepool_controller.go +++ b/controllers/awsmachinepool_controller.go @@ -105,6 +105,7 @@ func (r *AWSMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl.Reque RoleType: iam.NodesRole, Region: awsCluster.Spec.Region, IAMClientFactory: r.IAMClientFactory, + CustomTags: awsCluster.Spec.AdditionalTags, } iamService, err = iam.New(c) if err != nil { diff --git a/controllers/awsmachinetemplate_controller.go b/controllers/awsmachinetemplate_controller.go index df861d4c..6101c012 100644 --- a/controllers/awsmachinetemplate_controller.go +++ b/controllers/awsmachinetemplate_controller.go @@ -124,6 +124,7 @@ func (r *AWSMachineTemplateReconciler) Reconcile(ctx context.Context, req ctrl.R RoleType: role, Region: awsCluster.Spec.Region, IAMClientFactory: r.IAMClientFactory, + CustomTags: awsCluster.Spec.AdditionalTags, } iamService, err = iam.New(c) if err != nil { diff --git a/controllers/awsmanagedcontrolplane_controller.go b/controllers/awsmanagedcontrolplane_controller.go index 1977e8a7..4ac474d5 100644 --- a/controllers/awsmanagedcontrolplane_controller.go +++ b/controllers/awsmanagedcontrolplane_controller.go @@ -91,6 +91,7 @@ func (r *AWSManagedControlPlaneReconciler) Reconcile(ctx context.Context, req ct RoleType: iam.IRSARole, Region: eksCluster.Spec.Region, IAMClientFactory: r.IAMClientFactory, + CustomTags: eksCluster.Spec.AdditionalTags, } iamService, err = iam.New(c) if err != nil { diff --git a/pkg/iam/iam.go b/pkg/iam/iam.go index 61e31304..9f2cc21c 100644 --- a/pkg/iam/iam.go +++ b/pkg/iam/iam.go @@ -37,6 +37,7 @@ type IAMServiceConfig struct { RoleType string Region string PrincipalRoleARN string + CustomTags map[string]string IAMClientFactory func(awsclientgo.ConfigProvider) iamiface.IAMAPI } @@ -50,6 +51,7 @@ type IAMService struct { region string roleType string principalRoleARN string + customTags map[string]string } type Route53RoleParams struct { @@ -90,6 +92,7 @@ func New(config IAMServiceConfig) (*IAMService, error) { roleType: config.RoleType, region: config.Region, principalRoleARN: config.PrincipalRoleARN, + customTags: config.CustomTags, } return s, nil @@ -259,6 +262,12 @@ func (s *IAMService) createRole(roleName string, roleType string, params interfa Value: aws.String("owned"), }, } + for k, v := range s.customTags { + tags = append(tags, &awsiam.Tag{ + Key: aws.String(k), + Value: aws.String(v), + }) + } _, err = s.iamClient.CreateRole(&awsiam.CreateRoleInput{ RoleName: aws.String(roleName),