Skip to content

Commit 2d8525f

Browse files
CustomRoles: remove only thouse previously configured in AKO (#1999)
1 parent b1db538 commit 2d8525f

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

pkg/controller/atlasproject/custom_roles.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5475
func 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)

pkg/controller/atlasproject/custom_roles_test.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func TestEnsureCustomRoles(t *testing.T) {
146146
d, _ := json.Marshal(&akov2.AtlasProjectSpec{
147147
CustomRoles: []akov2.CustomRole{
148148
{
149-
Name: "test-role",
149+
Name: "test-role-1",
150150
InheritedRoles: []akov2.Role{
151151
{Name: "role3", Database: "db1"},
152152
},
@@ -164,7 +164,7 @@ func TestEnsureCustomRoles(t *testing.T) {
164164
return string(d)
165165
}(),
166166
},
167-
name: "Roles not in AKO but are in Atlas (Delete) if there were previous in AKO",
167+
name: "Roles not in AKO but are in Atlas (Delete) if there were previous in AKO. Remove only those that were in AKO",
168168
roleAPI: func() *mockadmin.CustomDatabaseRolesApi {
169169
roleAPI := mockadmin.NewCustomDatabaseRolesApi(t)
170170
roleAPI.EXPECT().ListCustomDatabaseRoles(context.Background(), "").
@@ -186,11 +186,25 @@ func TestEnsureCustomRoles(t *testing.T) {
186186
},
187187
},
188188
},
189+
{
190+
RoleName: "test-role-1",
191+
InheritedRoles: &[]admin.DatabaseInheritedRole{
192+
{Role: "role3", Db: "db1"},
193+
},
194+
Actions: &[]admin.DatabasePrivilegeAction{
195+
{
196+
Action: "action1",
197+
Resources: &[]admin.DatabasePermittedNamespaceResource{
198+
{Db: "db2"},
199+
},
200+
},
201+
},
202+
},
189203
},
190204
&http.Response{},
191205
nil,
192206
)
193-
roleAPI.EXPECT().DeleteCustomDatabaseRole(context.Background(), "", "test-role").
207+
roleAPI.EXPECT().DeleteCustomDatabaseRole(context.Background(), "", "test-role-1").
194208
Return(admin.DeleteCustomDatabaseRoleApiRequest{ApiService: roleAPI})
195209
roleAPI.EXPECT().DeleteCustomDatabaseRoleExecute(mock.Anything).
196210
Return(

0 commit comments

Comments
 (0)