@@ -37,18 +37,39 @@ func hasSkippedCustomRoles(atlasProject *akov2.AtlasProject) (bool, error) {
3737 return false , nil
3838}
3939
40- func hasLastAppliedCustomRoles (atlasProject * akov2.AtlasProject ) (bool , error ) {
40+ func getLastAppliedCustomRoles (atlasProject * akov2.AtlasProject ) ([]akov2. CustomRole , error ) {
4141 lastAppliedSpec := akov2.AtlasProjectSpec {}
4242 lastAppliedSpecStr , ok := atlasProject .Annotations [customresource .AnnotationLastAppliedConfiguration ]
4343 if ! ok {
44- return false , nil
44+ return nil , nil
4545 }
4646
4747 if err := json .Unmarshal ([]byte (lastAppliedSpecStr ), & lastAppliedSpec ); err != nil {
48- return false , fmt .Errorf ("failed to parse last applied configuration: %w" , err )
48+ return nil , fmt .Errorf ("failed to parse last applied configuration: %w" , err )
4949 }
5050
51- return len (lastAppliedSpec .CustomRoles ) != 0 , nil
51+ return lastAppliedSpec .CustomRoles , nil
52+ }
53+
54+ func findRolesToDelete (prevSpec , atlasRoles []customroles.CustomRole ) map [string ]customroles.CustomRole {
55+ result := map [string ]customroles.CustomRole {}
56+ for atlasRoleIdx := range atlasRoles {
57+ for specRoleIdx := range prevSpec {
58+ if atlasRoles [atlasRoleIdx ].Name == prevSpec [specRoleIdx ].Name {
59+ result [prevSpec [specRoleIdx ].Name ] = prevSpec [specRoleIdx ]
60+ continue
61+ }
62+ }
63+ }
64+ return result
65+ }
66+
67+ func convertToInternalRoles (roles []akov2.CustomRole ) []customroles.CustomRole {
68+ result := make ([]customroles.CustomRole , 0 , len (roles ))
69+ for i := range roles {
70+ result = append (result , customroles .NewCustomRole (& roles [i ]))
71+ }
72+ return result
5273}
5374
5475func ensureCustomRoles (workflowCtx * workflow.Context , project * akov2.AtlasProject ) workflow.Result {
@@ -63,7 +84,7 @@ func ensureCustomRoles(workflowCtx *workflow.Context, project *akov2.AtlasProjec
6384 return workflow .OK ()
6485 }
6586
66- hadPreviousCustomRoles , err := hasLastAppliedCustomRoles (project )
87+ lastAppliedCustomRoles , err := getLastAppliedCustomRoles (project )
6788 if err != nil {
6889 return workflow .Terminate (workflow .Internal , err .Error ())
6990 }
@@ -87,8 +108,9 @@ func ensureCustomRoles(workflowCtx *workflow.Context, project *akov2.AtlasProjec
87108 ops := calculateChanges (currentCustomRoles , akoRoles )
88109
89110 var deleteStatus map [string ]status.CustomRole
90- if hadPreviousCustomRoles {
91- deleteStatus = r .deleteCustomRoles (workflowCtx , project .ID (), ops .Delete )
111+ if len (lastAppliedCustomRoles ) > 0 {
112+ deleteStatus = r .deleteCustomRoles (workflowCtx , project .ID (),
113+ findRolesToDelete (convertToInternalRoles (lastAppliedCustomRoles ), currentCustomRoles ))
92114 }
93115 updateStatus := r .updateCustomRoles (workflowCtx , project .ID (), ops .Update )
94116 createStatus := r .createCustomRoles (workflowCtx , project .ID (), ops .Create )
0 commit comments