Skip to content

Commit 6f16e64

Browse files
authored
Merge pull request #50 from AechGG/feat/issue-49/service-account
feat: issue 49: service account creation
2 parents 36e7998 + 61c11e0 commit 6f16e64

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{{- if .Values.serviceAccount.create }}
2+
apiVersion: v1
3+
kind: ServiceAccount
4+
metadata:
5+
name: {{ .Values.serviceAccount.name }}
6+
namespace: {{ $.Release.Namespace }}
7+
labels:
8+
app: {{ template "k8s-service.name" . }}
9+
{{- if .Values.serviceAccount.labels }}
10+
{{- toYaml .Values.serviceAccount.labels | nindent 4 }}
11+
{{- end }}
12+
{{- if .Values.serviceAccount.annotations }}
13+
annotations:
14+
{{ toYaml .Values.serviceAccount.annotations | indent 4 }}
15+
{{- end }}
16+
imagePullSecrets:
17+
{{ toYaml .Values.imagePullSecrets | indent 2 }}
18+
{{- end }}

charts/k8s-service/values.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,17 @@ imagePullSecrets: []
398398
# Namespace.
399399
# - automountServiceAccountToken (bool) : Whether or not to automatically mount the ServiceAccount token as a volume
400400
# into the Pod. Note that this can be used to override the equivalent config
401-
# on the SerrviceAccount.
401+
# on the ServiceAccount.
402+
# - create (bool) : Whether or not to create a service account with the desired name
403+
# - annotations (map) : Annotations will add the provided map to the annotations for the service
404+
# account created
402405
#
403-
# The default config uses empty string to indicate that the default service account should be used.
406+
# The default config uses empty string to indicate that the default service account should be used and one shouldn't
407+
# be created
404408
serviceAccount:
405409
name: ""
410+
create: false
411+
labels: {}
406412

407413
# horizontalPodAutoscaler is a map that configures the Horizontal Pod Autoscaler information for this pod
408414
# The expected keys of hpa are:

test/k8s_service_service_account_template_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,65 @@
66
package test
77

88
import (
9+
"path/filepath"
910
"strings"
1011
"testing"
1112

13+
"github.com/ghodss/yaml"
14+
"github.com/gruntwork-io/terratest/modules/helm"
1215
"github.com/gruntwork-io/terratest/modules/random"
1316
"github.com/stretchr/testify/assert"
1417
"github.com/stretchr/testify/require"
1518
)
1619

20+
// Test that setting serviceAccount.create = true will cause the helm template to render the Service Account resource
21+
func TestK8SServiceAccountCreateTrueCreatesServiceAccount(t *testing.T) {
22+
t.Parallel()
23+
randomSAName := strings.ToLower(random.UniqueId())
24+
25+
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
26+
require.NoError(t, err)
27+
28+
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
29+
// We then use SetValues to override all the defaults.
30+
options := &helm.Options{
31+
ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")},
32+
SetValues: map[string]string{"serviceAccount.name": randomSAName, "serviceAccount.create": "true"},
33+
}
34+
out := helm.RenderTemplate(t, options, helmChartPath, []string{"templates/serviceaccount.yaml"})
35+
36+
// We take the output and render it to a map to validate it has created a service account output or not
37+
rendered := map[string]interface{}{}
38+
err = yaml.Unmarshal([]byte(out), &rendered)
39+
assert.NoError(t, err)
40+
assert.NotEqual(t, 0, len(rendered))
41+
assert.Equal(t, randomSAName, rendered["metadata"].(map[string]interface{})["name"])
42+
}
43+
44+
// Test that setting serviceAccount.create = false will cause the helm template to not render the Service Account
45+
// resource
46+
func TestK8SServiceAccountCreateFalse(t *testing.T) {
47+
t.Parallel()
48+
randomSAName := strings.ToLower(random.UniqueId())
49+
50+
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
51+
require.NoError(t, err)
52+
53+
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
54+
// We then use SetValues to override all the defaults.
55+
options := &helm.Options{
56+
ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")},
57+
SetValues: map[string]string{"serviceAccount.name": randomSAName, "serviceAccount.create": "false"},
58+
}
59+
out := helm.RenderTemplate(t, options, helmChartPath, []string{"templates/serviceaccount.yaml"})
60+
61+
// We take the output and render it to a map to validate it has created a service account output or not
62+
rendered := map[string]interface{}{}
63+
err = yaml.Unmarshal([]byte(out), &rendered)
64+
assert.NoError(t, err)
65+
assert.Equal(t, 0, len(rendered))
66+
}
67+
1768
func TestK8SServiceServiceAccountInjection(t *testing.T) {
1869
t.Parallel()
1970
randomSAName := strings.ToLower(random.UniqueId())

0 commit comments

Comments
 (0)