9292
9393// createIAMRoles creates the roles used by control-plane and compute nodes.
9494func createIAMRoles (ctx context.Context , infraID string , ic * installconfig.InstallConfig ) error {
95- logrus .Infoln ("Creating IAM roles for control-plane and compute nodes" )
95+ logrus .Infoln ("Reconciling IAM roles for control-plane and compute nodes" )
9696 // Create the IAM Role with the aws sdk.
9797 // https://docs.aws.amazon.com/sdk-for-go/api/service/iam/#IAM.CreateRole
9898 session , err := ic .AWS .Session (ctx )
@@ -140,21 +140,7 @@ func createIAMRoles(ctx context.Context, infraID string, ic *installconfig.Insta
140140 for _ , role := range []string {master , worker } {
141141 roleName , err := getOrCreateIAMRole (ctx , role , infraID , string (assumePolicyBytes ), * ic , tags , svc )
142142 if err != nil {
143- return fmt .Errorf ("failed to create IAM roles: %w" , err )
144- }
145-
146- // Put the policy inline.
147- policyName := aws .String (fmt .Sprintf ("%s-%s-policy" , infraID , role ))
148- b , err := json .Marshal (policies [role ])
149- if err != nil {
150- return fmt .Errorf ("failed to marshal %s policy: %w" , role , err )
151- }
152- if _ , err := svc .PutRolePolicyWithContext (ctx , & iam.PutRolePolicyInput {
153- PolicyDocument : aws .String (string (b )),
154- PolicyName : policyName ,
155- RoleName : aws .String (roleName ),
156- }); err != nil {
157- return fmt .Errorf ("failed to create inline policy for role %s: %w" , role , err )
143+ return fmt .Errorf ("failed to create IAM %s role: %w" , role , err )
158144 }
159145
160146 profileName := aws .String (fmt .Sprintf ("%s-%s-profile" , infraID , role ))
@@ -220,6 +206,7 @@ func getOrCreateIAMRole(ctx context.Context, nodeRole, infraID, assumePolicy str
220206 return "" , fmt .Errorf ("failed to get %s role: %w" , nodeRole , err )
221207 }
222208 // If the role does not exist, create it.
209+ logrus .Infof ("Creating IAM role for %s" , nodeRole )
223210 createRoleInput := & iam.CreateRoleInput {
224211 RoleName : roleName ,
225212 AssumeRolePolicyDocument : aws .String (assumePolicy ),
@@ -233,6 +220,21 @@ func getOrCreateIAMRole(ctx context.Context, nodeRole, infraID, assumePolicy str
233220 return "" , fmt .Errorf ("failed to wait for %s role to exist: %w" , nodeRole , err )
234221 }
235222 }
223+
224+ // Put the policy inline.
225+ policyName := aws .String (fmt .Sprintf ("%s-%s-policy" , infraID , nodeRole ))
226+ b , err := json .Marshal (policies [nodeRole ])
227+ if err != nil {
228+ return "" , fmt .Errorf ("failed to marshal %s policy: %w" , nodeRole , err )
229+ }
230+ if _ , err := svc .PutRolePolicyWithContext (ctx , & iam.PutRolePolicyInput {
231+ PolicyDocument : aws .String (string (b )),
232+ PolicyName : policyName ,
233+ RoleName : roleName ,
234+ }); err != nil {
235+ return "" , fmt .Errorf ("failed to create inline policy for role %s: %w" , nodeRole , err )
236+ }
237+
236238 return * roleName , nil
237239}
238240
0 commit comments