Skip to content

Commit

Permalink
Move to only adding two roles for managed namespaces (#954)
Browse files Browse the repository at this point in the history
* Move to only adding two roles for managed namespaces
---------

Signed-off-by: Salem Elrahal <[email protected]>
Co-authored-by: Salem Elrahal <[email protected]>
  • Loading branch information
selrahal and Salem Elrahal authored Jul 26, 2023
1 parent 4d09aca commit 45f3597
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
4 changes: 2 additions & 2 deletions controllers/argocd/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ func (r *ReconcileArgoCD) reconcileRole(name string, policyRules []v1.PolicyRule
}
// only skip creation of dex and redisHa roles for namespaces that no argocd instance is deployed in
if len(list.Items) < 1 {
// only create dexServer and redisHa roles for the namespace where the argocd instance is deployed
if cr.ObjectMeta.Namespace != namespace.Name && (name == common.ArgoCDDexServerComponent || name == common.ArgoCDRedisHAComponent) {
// namespace doesn't contain argocd instance, so skipe all the ArgoCD internal roles
if cr.ObjectMeta.Namespace != namespace.Name && (name != common.ArgoCDApplicationControllerComponent && name != common.ArgoCDServerComponent) {
continue
}
}
Expand Down
18 changes: 16 additions & 2 deletions controllers/argocd/role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestReconcileArgoCD_reconcileRole(t *testing.T) {
assert.NoError(t, createNamespace(r, a.Namespace, ""))
assert.NoError(t, createNamespace(r, "newNamespaceTest", a.Namespace))

workloadIdentifier := "x"
workloadIdentifier := common.ArgoCDApplicationControllerComponent
expectedRules := policyRuleForApplicationController()
_, err := r.reconcileRole(workloadIdentifier, expectedRules, a)
assert.NoError(t, err)
Expand Down Expand Up @@ -74,6 +74,20 @@ func TestReconcileArgoCD_reconcileRole_for_new_namespace(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, expectedNumberOfRoles, len(redisHaRoles))
assert.Equal(t, expectedRoleNamespace, redisHaRoles[0].ObjectMeta.Namespace)
// check no redis role is created for the new namespace with managed-by label
workloadIdentifier = common.ArgoCDRedisComponent
expectedRedisRules := policyRuleForRedis(r.Client)
redisRoles, err := r.reconcileRole(workloadIdentifier, expectedRedisRules, a)
assert.NoError(t, err)
assert.Equal(t, expectedNumberOfRoles, len(redisRoles))
assert.Equal(t, expectedRoleNamespace, redisRoles[0].ObjectMeta.Namespace)
// check no grafana role is created for the new namespace with managed-by label
workloadIdentifier = common.ArgoCDOperatorGrafanaComponent
expectedGrafanaRules := policyRuleForGrafana(r.Client)
grafanaRoles, err := r.reconcileRole(workloadIdentifier, expectedGrafanaRules, a)
assert.NoError(t, err)
assert.Equal(t, expectedNumberOfRoles, len(grafanaRoles))
assert.Equal(t, expectedRoleNamespace, grafanaRoles[0].ObjectMeta.Namespace)
}

func TestReconcileArgoCD_reconcileClusterRole(t *testing.T) {
Expand Down Expand Up @@ -217,7 +231,7 @@ func TestReconcileRoles_ManagedTerminatingNamespace(t *testing.T) {
// Create a managed namespace
assert.NoError(t, createNamespace(r, "managedNS", a.Namespace))

workloadIdentifier := "x"
workloadIdentifier := common.ArgoCDApplicationControllerComponent
expectedRules := policyRuleForApplicationController()
_, err := r.reconcileRole(workloadIdentifier, expectedRules, a)
assert.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions controllers/argocd/rolebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ func (r *ReconcileArgoCD) reconcileRoleBinding(name string, rules []v1.PolicyRul
}
// only skip creation of dex and redisHa rolebindings for namespaces that no argocd instance is deployed in
if len(list.Items) < 1 {
// only create dexServer and redisHa rolebindings for the namespace where the argocd instance is deployed
if cr.ObjectMeta.Namespace != namespace.Name && (name == common.ArgoCDDexServerComponent || name == common.ArgoCDRedisHAComponent) {
// namespace doesn't contain argocd instance, so skipe all the ArgoCD internal roles
if cr.ObjectMeta.Namespace != namespace.Name && (name != common.ArgoCDApplicationControllerComponent && name != common.ArgoCDServerComponent) {
continue
}
}
Expand Down
14 changes: 13 additions & 1 deletion controllers/argocd/rolebinding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestReconcileArgoCD_reconcileRoleBinding(t *testing.T) {
assert.NoError(t, createNamespace(r, a.Namespace, ""))
assert.NoError(t, createNamespace(r, "newTestNamespace", a.Namespace))

workloadIdentifier := "xrb"
workloadIdentifier := common.ArgoCDApplicationControllerComponent

assert.NoError(t, r.reconcileRoleBinding(workloadIdentifier, p, a))

Expand Down Expand Up @@ -67,6 +67,18 @@ func TestReconcileArgoCD_reconcileRoleBinding_for_new_namespace(t *testing.T) {
expectedRedisHaRules := policyRuleForRedisHa(r.Client)
assert.NoError(t, r.reconcileRoleBinding(workloadIdentifier, expectedRedisHaRules, a))
assert.Error(t, r.Client.Get(context.TODO(), types.NamespacedName{Name: expectedName, Namespace: "newTestNamespace"}, roleBinding))

// check no redis rolebinding is created for the new namespace with managed-by label
workloadIdentifier = common.ArgoCDRedisComponent
expectedRedisRules := policyRuleForRedis(r.Client)
assert.NoError(t, r.reconcileRoleBinding(workloadIdentifier, expectedRedisRules, a))
assert.Error(t, r.Client.Get(context.TODO(), types.NamespacedName{Name: expectedName, Namespace: "newTestNamespace"}, roleBinding))

// check no grafana rolebinding is created for the new namespace with managed-by label
workloadIdentifier = common.ArgoCDOperatorGrafanaComponent
expectedGrafanaRules := policyRuleForGrafana(r.Client)
assert.NoError(t, r.reconcileRoleBinding(workloadIdentifier, expectedGrafanaRules, a))
assert.Error(t, r.Client.Get(context.TODO(), types.NamespacedName{Name: expectedName, Namespace: "newTestNamespace"}, roleBinding))
}

// This test validates the behavior of the operator reconciliation when a managed namespace is not properly terminated
Expand Down

0 comments on commit 45f3597

Please sign in to comment.