@@ -164,20 +164,29 @@ func (m *EKSCluster) AnalyzeRoleRelationshipsForPodIdentity() error {
164164 if err != nil {
165165 return fmt .Errorf ("unable to describe pod identity association %s: %v" , podAssociation .ID , err )
166166 }
167- assumableIamRole := AssumableIAMRole {
168- IAMRole : & IAMRole {Arn : * podAssociationDetails .Association .RoleArn },
169- Reason : AssumeIAMRoleReasonPodIdentity ,
170- }
171-
172167 pods , ok := m .PodsByNamespace [podAssociationNamespace ]
173168 if ! ok {
174169 // no pods in podAssociationNamespace, go to the next one
175170 continue
176171 }
172+
173+ // cache to avoid counting multiple IAM roles for a given SA
174+ serviceAccountsHandledForPodAssociation := map [string ]bool {}
175+
177176 // All pods in this podAssociationNamespace with this service account can assume the role
178- for i , _ := range pods {
179- if pods [i ].ServiceAccount .Name == podAssociation .ServiceAccountName {
180- pods [i ].ServiceAccount .AssumableRoles = append (pods [i ].ServiceAccount .AssumableRoles , & assumableIamRole )
177+ for _ , pod := range pods {
178+ if pod .ServiceAccount .Name == podAssociation .ServiceAccountName {
179+ assumableIamRole := AssumableIAMRole {
180+ IAMRole : & IAMRole {Arn : * podAssociationDetails .Association .RoleArn },
181+ Reason : AssumeIAMRoleReasonPodIdentity ,
182+ }
183+
184+ // Did we already find this role for this SA? (case where multiple pods have the same SA)
185+ if _ , ok := serviceAccountsHandledForPodAssociation [pod .ServiceAccount .Name ]; ! ok {
186+ log .Println ("Adding assumable role " + assumableIamRole .IAMRole .Arn + " to pod " + pod .Name + " in namespace " + pod .Namespace )
187+ pod .ServiceAccount .AssumableRoles = append (pod .ServiceAccount .AssumableRoles , & assumableIamRole )
188+ serviceAccountsHandledForPodAssociation [pod .ServiceAccount .Name ] = true
189+ }
181190 }
182191 }
183192 }
0 commit comments