Skip to content

Commit

Permalink
Merge pull request #357 from ChrisFraun/feat/add-securityContext-for-…
Browse files Browse the repository at this point in the history
…pod-and-container
  • Loading branch information
slok authored Oct 26, 2022
2 parents fb2810b + 1afdf91 commit 162a75d
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 1 deletion.
12 changes: 12 additions & 0 deletions deploy/kubernetes/helm/sloth/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ spec:
{{- end }}
spec:
serviceAccountName: {{ include "sloth.fullname" . }}
securityContext:
{{- with .Values.securityContext.pod }}
{{- toYaml . | nindent 8 }}
{{- end }}
containers:
- name: sloth
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
Expand Down Expand Up @@ -75,6 +79,10 @@ spec:
mountPath: {{ .Values.customSloConfig.path }}
{{- end }}
{{- end }}
securityContext:
{{- with .Values.securityContext.container }}
{{- toYaml . | nindent 12 }}
{{- end }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- with .Values.tolerations }}
Expand All @@ -93,6 +101,10 @@ spec:
- name: sloth-common-sli-plugins
# Default path for git-sync.
mountPath: /tmp/git
securityContext:
{{- with .Values.securityContext.container }}
{{- toYaml . | nindent 12 }}
{{- end }}
resources:
{{- toYaml .Values.commonPlugins.gitRepo.resources | nindent 12 }}
{{- end }}
Expand Down
59 changes: 59 additions & 0 deletions deploy/kubernetes/helm/sloth/tests/helm_chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,62 @@ func TestChartConfigMap(t *testing.T) {
})
}
}

func TestChartSecurityContext(t *testing.T) {
tests := map[string]struct {
name string
namespace string
values func() map[string]interface{}
expErr bool
expTplFile string
}{
"A chart without security values should render correctly.": {
name: "sloth",
namespace: "default",
values: defaultValues,
expTplFile: "testdata/output/deployment_default.yaml",
},

"A chart with custom security values should render correctly.": {
name: "test",
namespace: "custom",
values: func() map[string]interface{} {
v := securityValues()
v["securityContext"].(msi)["enabled"] = true

return v
},
expTplFile: "testdata/output/deployment_securityContext.yaml",
},
}

checksumNormalizer := regexp.MustCompile(`checksum/config: [a-z0-9]+`)

for name, test := range tests {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

gotTpl, err := helm.Template(context.TODO(), helm.TemplateConfig{
Chart: slothChart,
Namespace: test.namespace,
ReleaseName: test.name,
Values: test.values(),
ShowFiles: []string{"templates/deployment.yaml"},
})

// Check.
if test.expErr {
assert.Error(err)
} else if assert.NoError(err) {
gotTpl := checksumNormalizer.ReplaceAllString(gotTpl, "checksum/config: <checksum>")

expTpl, err := os.ReadFile(test.expTplFile)
require.NoError(err)
expTplS := strings.TrimSpace(string(expTpl))

assert.Equal(expTplS, normalizeVersion(gotTpl))
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ spec:
kubectl.kubernetes.io/default-container: sloth
spec:
serviceAccountName: sloth-test
securityContext:
containers:
- name: sloth
image: slok/sloth-test:v1.42.42
Expand All @@ -52,6 +53,7 @@ spec:
volumeMounts:
- name: sloth-common-sli-plugins
mountPath: /plugins/sloth-common-sli-plugins
securityContext:
resources:
limits:
cpu: 50m
Expand All @@ -70,6 +72,7 @@ spec:
- name: sloth-common-sli-plugins
# Default path for git-sync.
mountPath: /tmp/git
securityContext:
resources:
limits:
cpu: 50m
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ spec:
kubectl.kubernetes.io/default-container: sloth
spec:
serviceAccountName: sloth-test
securityContext:
containers:
- name: sloth
image: slok/sloth-test:v1.42.42
Expand All @@ -44,6 +45,7 @@ spec:
- --extra-labels=k1=v1
- --extra-labels=k2=v2
- --disable-optimized-rules
securityContext:
resources:
limits:
cpu: 50m
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ spec:
checksum/config: <checksum>
spec:
serviceAccountName: sloth-test
securityContext:
containers:
- name: sloth
image: slok/sloth-test:v1.42.42
Expand All @@ -53,6 +54,7 @@ spec:
volumeMounts:
- name: sloth-windows
mountPath: /windows
securityContext:
resources:
limits:
cpu: 50m
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ spec:
kubectl.kubernetes.io/default-container: sloth
spec:
serviceAccountName: sloth
securityContext:
containers:
- name: sloth
image: ghcr.io/slok/sloth:v0.11.0
Expand All @@ -43,6 +44,7 @@ spec:
volumeMounts:
- name: sloth-common-sli-plugins
mountPath: /plugins/sloth-common-sli-plugins
securityContext:
resources:
limits:
cpu: 50m
Expand All @@ -61,6 +63,7 @@ spec:
- name: sloth-common-sli-plugins
# Default path for git-sync.
mountPath: /tmp/git
securityContext:
resources:
limits:
cpu: 50m
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
# Source: sloth/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: sloth-test
namespace: custom
labels:
helm.sh/chart: sloth-<version>
app.kubernetes.io/managed-by: Helm
app: sloth
app.kubernetes.io/name: sloth
app.kubernetes.io/instance: test
spec:
replicas: 1
selector:
matchLabels:
app: sloth
app.kubernetes.io/name: sloth
app.kubernetes.io/instance: test
template:
metadata:
labels:
helm.sh/chart: sloth-<version>
app.kubernetes.io/managed-by: Helm
app: sloth
app.kubernetes.io/name: sloth
app.kubernetes.io/instance: test
annotations:
kubectl.kubernetes.io/default-container: sloth
spec:
serviceAccountName: sloth-test
securityContext:
fsGroup: 100
runAsGroup: 1000
runAsNonRoot: true
runAsUser: 100
supplementalGroups: "100"
containers:
- name: sloth
image: ghcr.io/slok/sloth:v0.11.0
args:
- kubernetes-controller
- --sli-plugins-path=/plugins
ports:
- containerPort: 8081
name: metrics
protocol: TCP
volumeMounts:
- name: sloth-common-sli-plugins
mountPath: /plugins/sloth-common-sli-plugins
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop: ALL
resources:
limits:
cpu: 50m
memory: 150Mi
requests:
cpu: 5m
memory: 75Mi
- name: git-sync-plugins
image: k8s.gcr.io/git-sync/git-sync:v3.6.1
args:
- --repo=https://github.com/slok/sloth-common-sli-plugins
- --branch=main
- --wait=30
- --webhook-url=http://localhost:8082/-/reload
volumeMounts:
- name: sloth-common-sli-plugins
# Default path for git-sync.
mountPath: /tmp/git
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop: ALL
resources:
limits:
cpu: 50m
memory: 100Mi
requests:
cpu: 5m
memory: 50Mi
volumes:
- name: sloth-common-sli-plugins
emptyDir: {}
20 changes: 20 additions & 0 deletions deploy/kubernetes/helm/sloth/tests/values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,23 @@ func customValues() msi {
},
}
}

func securityValues() msi {
return msi{
"securityContext": msi{
"pod": msi{
"runAsNonRoot": true,
"runAsGroup": 1000,
"runAsUser": 100,
"fsGroup": 100,
"supplementalGroups": "100",
},
"container": msi{
"allowPrivilegeEscalation": false,
"capabilities": msi{
"drop": "ALL",
},
},
},
}
}
7 changes: 6 additions & 1 deletion deploy/kubernetes/helm/sloth/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ customSloConfig:
# - key: kubernetes.azure.com/scalesetpriority
# operator: Equal
# value: spot
# effect: NoSchedule
# effect: NoSchedule

# add securityContext for pod and container level
securityContext:
pod: {}
container: {}

0 comments on commit 162a75d

Please sign in to comment.