Skip to content

Commit 4fbc3e9

Browse files
CLOUDP-298518: Fixed customRoles handler when lastSkipped annotation is not empty (#2094)
Fixed customRoles handler when lastSkipped annotation is not empty
1 parent 7b2e787 commit 4fbc3e9

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

internal/controller/atlasproject/custom_roles.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ type roleController struct {
2222
service customroles.CustomRoleService
2323
}
2424

25-
func hasSkippedCustomRoles(atlasProject *akov2.AtlasProject) (bool, error) {
25+
func shouldCustomRolesSkipReconciling(atlasProject *akov2.AtlasProject) (bool, error) {
2626
lastSkippedSpec := akov2.AtlasProjectSpec{}
2727
lastSkippedSpecString, ok := atlasProject.Annotations[customresource.AnnotationLastSkippedConfiguration]
2828
if ok {
2929
if err := json.Unmarshal([]byte(lastSkippedSpecString), &lastSkippedSpec); err != nil {
3030
return false, fmt.Errorf("failed to parse last skipped configuration: %w", err)
3131
}
3232

33-
return len(lastSkippedSpec.CustomRoles) != 0, nil
33+
return len(lastSkippedSpec.CustomRoles) == 0, nil
3434
}
3535

3636
return false, nil
@@ -76,14 +76,13 @@ func convertToInternalRoles(roles []akov2.CustomRole) []customroles.CustomRole {
7676
}
7777

7878
func ensureCustomRoles(workflowCtx *workflow.Context, project *akov2.AtlasProject) workflow.Result {
79-
skipped, err := hasSkippedCustomRoles(project)
79+
skipped, err := shouldCustomRolesSkipReconciling(project)
8080
if err != nil {
8181
return workflow.Terminate(workflow.Internal, err)
8282
}
8383

8484
if skipped {
8585
workflowCtx.UnsetCondition(api.ProjectCustomRolesReadyType)
86-
8786
return workflow.OK()
8887
}
8988

internal/controller/atlasproject/custom_roles_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"net/http"
77
"testing"
88

9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
911
"github.com/stretchr/testify/assert"
1012
"github.com/stretchr/testify/mock"
1113
"go.mongodb.org/atlas-sdk/v20231115008/admin"
@@ -18,6 +20,57 @@ import (
1820
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/pointer"
1921
)
2022

23+
func TestShouldCustomRolesSkipReconciling(t *testing.T) {
24+
tests := []struct {
25+
name string
26+
annotations map[string]string
27+
expected bool
28+
shouldFail bool
29+
}{
30+
{
31+
name: "No annotations present",
32+
annotations: map[string]string{},
33+
expected: false,
34+
shouldFail: false,
35+
},
36+
{
37+
name: "Annotation present but invalid JSON",
38+
annotations: map[string]string{customresource.AnnotationLastSkippedConfiguration: "invalid"},
39+
expected: false,
40+
shouldFail: true,
41+
},
42+
{
43+
name: "Annotation present with empty CustomRoles",
44+
annotations: map[string]string{customresource.AnnotationLastSkippedConfiguration: "{\"CustomRoles\": []}"},
45+
expected: true,
46+
shouldFail: false,
47+
},
48+
{
49+
name: "Annotation present with non-empty CustomRoles",
50+
annotations: map[string]string{customresource.AnnotationLastSkippedConfiguration: "{\"CustomRoles\": [{\"Name\": \"role1\"}]}"},
51+
expected: false,
52+
shouldFail: false,
53+
},
54+
}
55+
56+
for _, tt := range tests {
57+
t.Run(tt.name, func(t *testing.T) {
58+
atlasProject := &akov2.AtlasProject{
59+
ObjectMeta: metav1.ObjectMeta{
60+
Annotations: tt.annotations,
61+
},
62+
}
63+
result, err := shouldCustomRolesSkipReconciling(atlasProject)
64+
if tt.shouldFail {
65+
assert.Error(t, err)
66+
} else {
67+
assert.NoError(t, err)
68+
}
69+
assert.Equal(t, tt.expected, result)
70+
})
71+
}
72+
}
73+
2174
func TestEnsureCustomRoles(t *testing.T) {
2275
testRole := []akov2.CustomRole{
2376
{

test/e2e/independent_customroles_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ var _ = Describe("Migrate one CustomRole from AtlasProject to AtlasCustomRole re
136136
By("Enabled reconciliation for AtlasProject", func() {
137137
_, err := akoretry.RetryUpdateOnConflict(testData.Context, testData.K8SClient,
138138
client.ObjectKeyFromObject(testData.Project), func(p *akov2.AtlasProject) {
139-
p.Annotations = map[string]string{}
139+
delete(p.Annotations, customresource.ReconciliationPolicyAnnotation)
140140
})
141141
Expect(err).To(BeNil())
142142

0 commit comments

Comments
 (0)