Skip to content

Commit

Permalink
Generate unique names for ClusterRoles and CRBindings (#371)
Browse files Browse the repository at this point in the history
To avoid conflicts between multiple admin deployments, generate unique names for ClusterRole and ClusterRoleBinding by prepending the namespace and helm release name.
  • Loading branch information
kontaras authored Aug 15, 2024
1 parent 1660991 commit ccbb4af
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 33 deletions.
13 changes: 13 additions & 0 deletions stable/admin/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -526,3 +526,16 @@ trusted-certificate" command which doesn't require AP restart.
checksum/tls-passwords: {{ sha256sum $passwords }}
{{- end }}
{{- end -}}
{{/*
Create a cluster unique app name.
*/}}
{{- define "admin.fullclustername" -}}
{{- $name := include "admin.fullname" . -}}
{{- $ns := default .Release.Namespace .Values.admin.namespace | trunc 50 | trimSuffix "-" -}}
{{- if contains $name $ns -}}
{{- printf "%s" $name -}}
{{- else -}}
{{- printf "%s-%s" $name $ns -}}
{{- end -}}
{{- end -}}
4 changes: 3 additions & 1 deletion stable/admin/templates/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ rules:
- create
- update
{{- if eq (include "defaulttrue" .Values.nuodb.addClusterRoleBinding) "true" }}
{{- $namespace := default .Release.Namespace .Values.admin.namespace | trunc 50 | trimSuffix "-" -}}
{{- $adminName := include "admin.fullname" . }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: nuodb-kube-inspector
name: {{ include "admin.fullclustername" . }}-kube-inspector
rules:
- apiGroups:
- ""
Expand Down
6 changes: 4 additions & 2 deletions stable/admin/templates/rolebinding.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ subjects:
- kind: ServiceAccount
name: {{ default "nuodb" .Values.nuodb.serviceAccount }}
{{- if eq (include "defaulttrue" .Values.nuodb.addClusterRoleBinding) "true" }}
{{- $namespace := default .Release.Namespace .Values.admin.namespace | trunc 50 | trimSuffix "-" -}}
{{- $adminName := include "admin.fullname" . }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: nuodb-kube-inspector
name: {{ include "admin.fullclustername" . }}-kube-inspector
roleRef:
kind: ClusterRole
name: nuodb-kube-inspector
name: {{ include "admin.fullclustername" . }}-kube-inspector
apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
Expand Down
83 changes: 53 additions & 30 deletions test/integration/template_admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/gruntwork-io/terratest/modules/helm"
"github.com/gruntwork-io/terratest/modules/k8s"

"github.com/nuodb/nuodb-helm-charts/v3/test/testlib"
)
Expand Down Expand Up @@ -1560,40 +1561,62 @@ func TestClusterRole(t *testing.T) {
helmChartPath := testlib.ADMIN_HELM_CHART_PATH

t.Run("testEnabled", func(t *testing.T) {
output := helm.RenderTemplate(t, &helm.Options{}, helmChartPath,
"release-name", []string{"templates/role.yaml", "templates/rolebinding.yaml"})

// Verify that nuodb-kube-inspector ClusterRole is created
for _, obj := range testlib.SplitAndRenderClusterRole(t, output, 1) {
assert.Equal(t, "nuodb-kube-inspector", obj.Name)

for _, rule := range obj.Rules {
isNode := false
for _, resource := range rule.Resources {
if resource == "nodes" {
isNode = true
break
options := []*helm.Options{
{
SetValues: map[string]string{
"admin.fullnameOverride": "full-name",
},
KubectlOptions: &k8s.KubectlOptions{
Namespace: "ns-name",
},
},
// Override namespace name
{
SetValues: map[string]string{
"admin.fullnameOverride": "full-name",
"admin.namespace": "ns-name",
},
KubectlOptions: &k8s.KubectlOptions{
Namespace: "default",
},
},
}
for _, option := range options {
output := helm.RenderTemplate(t, option, helmChartPath,
"release-name", []string{"templates/role.yaml", "templates/rolebinding.yaml"})

// Verify that nuodb-kube-inspector ClusterRole is created
for _, obj := range testlib.SplitAndRenderClusterRole(t, output, 1) {
assert.Equal(t, "full-name-ns-name-kube-inspector", obj.Name)

for _, rule := range obj.Rules {
isNode := false
for _, resource := range rule.Resources {
if resource == "nodes" {
isNode = true
break
}
}
if !isNode {
continue
}
}
if !isNode {
continue
}

assert.Contains(t, rule.Verbs, "get")
assert.Contains(t, rule.Verbs, "get")
}
}
}

// Verify that nuodb-kube-inspector ClusterRoleBinding is created
for _, obj := range testlib.SplitAndRenderClusterClusterRoleBinding(t, output, 1) {
assert.Equal(t, "nuodb-kube-inspector", obj.Name)
// Verify that it is binding to the correct role
assert.Equal(t, "ClusterRole", obj.RoleRef.Kind)
assert.Equal(t, "nuodb-kube-inspector", obj.RoleRef.Name)
// Verify that it is binding to the correct user
subjects := obj.Subjects
assert.Equal(t, 1, len(subjects))
assert.Equal(t, "ServiceAccount", subjects[0].Kind)
assert.Equal(t, "nuodb", subjects[0].Name)
// Verify that nuodb-kube-inspector ClusterRoleBinding is created
for _, obj := range testlib.SplitAndRenderClusterClusterRoleBinding(t, output, 1) {
assert.Equal(t, "full-name-ns-name-kube-inspector", obj.Name)
// Verify that it is binding to the correct role
assert.Equal(t, "ClusterRole", obj.RoleRef.Kind)
assert.Equal(t, "full-name-ns-name-kube-inspector", obj.RoleRef.Name)
// Verify that it is binding to the correct user
subjects := obj.Subjects
assert.Equal(t, 1, len(subjects))
assert.Equal(t, "ServiceAccount", subjects[0].Kind)
assert.Equal(t, "nuodb", subjects[0].Name)
}
}
})

Expand Down

0 comments on commit ccbb4af

Please sign in to comment.