Skip to content

Commit 0fcc2d1

Browse files
Merge pull request #68 from marklogic/feature/CLD-596
CLD-596: Add support for securityContext in the helm chart
2 parents e80b7fc + 7fecf28 commit 0fcc2d1

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

charts/templates/statefulset.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ spec:
206206
sleep 10s
207207
fi
208208
done
209+
{{- if .Values.containerSecurityContext.enabled }}
210+
securityContext: {{- omit .Values.containerSecurityContext "enabled" | toYaml | nindent 12 }}
211+
{{- end }}
209212
{{- if .Values.livenessProbe.enabled }}
210213
livenessProbe:
211214
httpGet:

charts/values.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ serviceAccount:
100100
# If not set and create is true, a name is generated using the fullname template
101101
name: ""
102102

103+
# Below are the security configurations for container, by default security will be enabled
104+
containerSecurityContext:
105+
enabled: true
106+
runAsUser: 1000
107+
runAsNonRoot: true
108+
allowPrivilegeEscalation: true
103109

104110
# Below are the advanced configurations, please understand read the reference before you make changes
105111

test/template/sec_template_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package template_test
2+
3+
import (
4+
"path/filepath"
5+
"strings"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
appsv1 "k8s.io/api/apps/v1"
10+
11+
"github.com/gruntwork-io/terratest/modules/helm"
12+
"github.com/gruntwork-io/terratest/modules/k8s"
13+
"github.com/gruntwork-io/terratest/modules/random"
14+
)
15+
16+
func TestChartTemplateSecurityEnabled(t *testing.T) {
17+
t.Parallel()
18+
19+
// Path to the helm chart we will test
20+
helmChartPath, err := filepath.Abs("../../charts")
21+
releaseName := "marklogic-sec-test"
22+
t.Log(helmChartPath, releaseName)
23+
require.NoError(t, err)
24+
25+
// Set up the namespace; confirm that the template renders the expected value for the namespace.
26+
namespaceName := "marklogic-" + strings.ToLower(random.UniqueId())
27+
t.Logf("Namespace: %s\n", namespaceName)
28+
29+
// Setup the args for helm install
30+
options := &helm.Options{
31+
SetValues: map[string]string{
32+
"image.repository": "marklogicdb/marklogic-db",
33+
"image.tag": "latest",
34+
"persistence.enabled": "false",
35+
"containerSecurityContext.enabled": "true",
36+
},
37+
KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName),
38+
}
39+
40+
// render the tempate
41+
output := helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/statefulset.yaml"})
42+
43+
var statefulset appsv1.StatefulSet
44+
helm.UnmarshalK8SYaml(t, output, &statefulset)
45+
46+
// Verify the name and namespace matches
47+
require.Equal(t, namespaceName, statefulset.Namespace)
48+
49+
// Verify the securityContext values are set for container
50+
expectedRunAsUser := 1000
51+
statefulSetContainers := statefulset.Spec.Template.Spec.Containers
52+
actualRunAsUser := *(statefulSetContainers[0].SecurityContext.RunAsUser)
53+
require.Equal(t, len(statefulSetContainers), 1)
54+
require.Equal(t, int(actualRunAsUser), expectedRunAsUser)
55+
}
56+
57+
func TestChartTemplateSecurityDisabled(t *testing.T) {
58+
t.Parallel()
59+
60+
// Path to the helm chart we will test
61+
helmChartPath, err := filepath.Abs("../../charts")
62+
releaseName := "marklogic-sec-test"
63+
t.Log(helmChartPath, releaseName)
64+
require.NoError(t, err)
65+
66+
// Set up the namespace; confirm that the template renders the expected value for the namespace.
67+
namespaceName := "marklogic-" + strings.ToLower(random.UniqueId())
68+
t.Logf("Namespace: %s\n", namespaceName)
69+
70+
// Setup the args for helm install
71+
options := &helm.Options{
72+
SetValues: map[string]string{
73+
"image.repository": "marklogicdb/marklogic-db",
74+
"image.tag": "latest",
75+
"persistence.enabled": "false",
76+
"containerSecurityContext.enabled": "false",
77+
},
78+
KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName),
79+
}
80+
81+
// render the tempate
82+
output := helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/statefulset.yaml"})
83+
84+
var statefulset appsv1.StatefulSet
85+
helm.UnmarshalK8SYaml(t, output, &statefulset)
86+
87+
// Verify the name and namespace matches
88+
require.Equal(t, namespaceName, statefulset.Namespace)
89+
90+
// Verify SecurityContext is not set for container
91+
statefulSetContainers := statefulset.Spec.Template.Spec.Containers
92+
require.Equal(t, len(statefulSetContainers), 1)
93+
require.Nil(t, statefulSetContainers[0].SecurityContext)
94+
}

0 commit comments

Comments
 (0)