@@ -7,8 +7,11 @@ import (
77
88 "github.com/aws/aws-sdk-go/aws"
99 "github.com/aws/aws-sdk-go/service/ec2"
10+ "github.com/aws/aws-sdk-go/service/iam"
1011 "github.com/aws/aws-sdk-go/service/route53"
1112 "github.com/pkg/errors"
13+ "github.com/sirupsen/logrus"
14+ "k8s.io/apimachinery/pkg/util/sets"
1215
1316 "github.com/openshift/installer/pkg/asset/installconfig"
1417 awsic "github.com/openshift/installer/pkg/asset/installconfig/aws"
@@ -34,12 +37,11 @@ func Metadata(clusterID, infraID string, config *types.InstallConfig) *awstypes.
3437// PreTerraform performs any infrastructure initialization which must
3538// happen before Terraform creates the remaining infrastructure.
3639func PreTerraform (ctx context.Context , clusterID string , installConfig * installconfig.InstallConfig ) error {
37-
3840 if err := tagSharedVPCResources (ctx , clusterID , installConfig ); err != nil {
3941 return err
4042 }
4143
42- return nil
44+ return tagSharedIAMRoles ( ctx , clusterID , installConfig )
4345}
4446
4547func tagSharedVPCResources (ctx context.Context , clusterID string , installConfig * installconfig.InstallConfig ) error {
@@ -95,6 +97,64 @@ func tagSharedVPCResources(ctx context.Context, clusterID string, installConfig
9597 return nil
9698}
9799
100+ func tagSharedIAMRoles (ctx context.Context , clusterID string , installConfig * installconfig.InstallConfig ) error {
101+ iamRoles := sets .New [string ]()
102+ {
103+ mpool := awstypes.MachinePool {}
104+ mpool .Set (installConfig .Config .AWS .DefaultMachinePlatform )
105+ if mp := installConfig .Config .ControlPlane ; mp != nil {
106+ mpool .Set (mp .Platform .AWS )
107+ }
108+ if len (mpool .IAMRole ) > 0 {
109+ iamRoles .Insert (mpool .IAMRole )
110+ }
111+ }
112+
113+ for _ , compute := range installConfig .Config .Compute {
114+ mpool := awstypes.MachinePool {}
115+ mpool .Set (installConfig .Config .AWS .DefaultMachinePlatform )
116+ mpool .Set (compute .Platform .AWS )
117+ if len (mpool .IAMRole ) > 0 {
118+ iamRoles .Insert (mpool .IAMRole )
119+ }
120+ }
121+
122+ // If compute stanza was not defined, it will inherit from DefaultMachinePlatform later on.
123+ if installConfig .Config .Compute == nil {
124+ mpool := installConfig .Config .AWS .DefaultMachinePlatform
125+ if mpool != nil && len (mpool .IAMRole ) > 0 {
126+ iamRoles .Insert (mpool .IAMRole )
127+ }
128+ }
129+
130+ if iamRoles .Len () == 0 {
131+ return nil
132+ }
133+
134+ logrus .Debugf ("Tagging shared instance roles: %v" , sets .List (iamRoles ))
135+
136+ session , err := installConfig .AWS .Session (ctx )
137+ if err != nil {
138+ return fmt .Errorf ("could not create AWS session: %w" , err )
139+ }
140+
141+ tagKey , tagValue := sharedTag (clusterID )
142+
143+ iamClient := iam .New (session , aws .NewConfig ().WithRegion (installConfig .Config .Platform .AWS .Region ))
144+ for role := range iamRoles {
145+ if _ , err := iamClient .TagRoleWithContext (ctx , & iam.TagRoleInput {
146+ RoleName : aws .String (role ),
147+ Tags : []* iam.Tag {
148+ {Key : aws .String (tagKey ), Value : aws .String (tagValue )},
149+ },
150+ }); err != nil {
151+ return fmt .Errorf ("could not tag %q instance role: %w" , role , err )
152+ }
153+ }
154+
155+ return nil
156+ }
157+
98158func sharedTag (clusterID string ) (string , string ) {
99159 return fmt .Sprintf ("kubernetes.io/cluster/%s" , clusterID ), "shared"
100160}
0 commit comments