Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add securityContext for pod and container #357

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
limits:
memory: 150Mi
Expand All @@ -97,6 +105,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:
limits:
memory: 100Mi
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,14 +53,16 @@ spec:
volumeMounts:
- name: sloth-common-sli-plugins
mountPath: /plugins/sloth-common-sli-plugins
securityContext:
resources:
limits:
cpu: 50m
memory: 150Mi
requests:
cpu: 5m
memory: 75Mi
- name: git-sync-plugins
image: k8s.gcr.io/git-sync/git-sync:v3.3.4
image: k8s.gcr.io/git-sync/git-sync:v3.6.1
args:
- --repo=https://github.com/slok/sloth-test-common-sli-plugins
- --branch=main
Expand All @@ -69,8 +72,10 @@ spec:
- name: sloth-common-sli-plugins
# Default path for git-sync.
mountPath: /tmp/git
securityContext:
resources:
limits:
cpu: 50m
memory: 100Mi
requests:
cpu: 5m
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,8 +45,10 @@ spec:
- --extra-labels=k1=v1
- --extra-labels=k2=v2
- --disable-optimized-rules
securityContext:
resources:
limits:
cpu: 50m
memory: 150Mi
requests:
cpu: 5m
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,8 +54,10 @@ spec:
volumeMounts:
- name: sloth-windows
mountPath: /windows
securityContext:
resources:
limits:
cpu: 50m
memory: 150Mi
requests:
cpu: 5m
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ spec:
kubectl.kubernetes.io/default-container: sloth
spec:
serviceAccountName: sloth
securityContext:
containers:
- name: sloth
image: ghcr.io/slok/sloth:v0.10.0
image: ghcr.io/slok/sloth:v0.11.0
args:
- kubernetes-controller
- --sli-plugins-path=/plugins
Expand All @@ -43,14 +44,16 @@ spec:
volumeMounts:
- name: sloth-common-sli-plugins
mountPath: /plugins/sloth-common-sli-plugins
securityContext:
resources:
limits:
cpu: 50m
memory: 150Mi
requests:
cpu: 5m
memory: 75Mi
- name: git-sync-plugins
image: k8s.gcr.io/git-sync/git-sync:v3.3.4
image: k8s.gcr.io/git-sync/git-sync:v3.6.1
args:
- --repo=https://github.com/slok/sloth-common-sli-plugins
- --branch=main
Expand All @@ -60,8 +63,10 @@ spec:
- name: sloth-common-sli-plugins
# Default path for git-sync.
mountPath: /tmp/git
securityContext:
resources:
limits:
cpu: 50m
memory: 100Mi
requests:
cpu: 5m
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 @@ -42,4 +42,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: {}