Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.

Commit 95ffb44

Browse files
author
Rodrigo Valin
authored
Adds securityContext to PodSpec builder (#191)
1 parent 244f35b commit 95ffb44

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

pkg/kube/podtemplatespec/podspec_template.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,11 @@ func WithTerminationGracePeriodSeconds(seconds int) Modification {
140140
}
141141
}
142142

143-
// WithFsGroup sets the PodTemplateSpec's fs group
144-
func WithFsGroup(fsGroup int) Modification {
143+
// WithSecurityContext sets the PodTemplateSpec's SecurityContext
144+
func WithSecurityContext(securityContext corev1.PodSecurityContext) Modification {
145145
return func(podTemplateSpec *corev1.PodTemplateSpec) {
146146
spec := &podTemplateSpec.Spec
147-
fsGroup64 := int64(fsGroup)
148-
spec.SecurityContext = &corev1.PodSecurityContext{
149-
FSGroup: &fsGroup64,
150-
}
147+
spec.SecurityContext = &securityContext
151148
}
152149
}
153150

pkg/kube/podtemplatespec/podspec_template_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@ func TestPodTemplateSpec(t *testing.T) {
1818
Name: "vol-2",
1919
}
2020

21+
runAsUser := int64(1111)
22+
runAsGroup := int64(2222)
23+
fsGroup := int64(3333)
24+
2125
p := New(
2226
WithVolume(corev1.Volume{
2327
Name: "vol-1",
2428
}),
2529
WithVolume(corev1.Volume{
2630
Name: "vol-2",
2731
}),
28-
WithFsGroup(100),
32+
WithSecurityContext(corev1.PodSecurityContext{
33+
RunAsUser: &runAsUser,
34+
RunAsGroup: &runAsGroup,
35+
FSGroup: &fsGroup,
36+
}),
2937
WithImagePullSecrets("pull-secrets"),
3038
WithInitContainerByIndex(0, container.Apply(
3139
container.WithName("init-container-0"),
@@ -50,8 +58,12 @@ func TestPodTemplateSpec(t *testing.T) {
5058
assert.Equal(t, p.Spec.Volumes[0].Name, "vol-1")
5159
assert.Equal(t, p.Spec.Volumes[1].Name, "vol-2")
5260

53-
expected := int64(100)
54-
assert.Equal(t, &expected, p.Spec.SecurityContext.FSGroup)
61+
expectedRunAsUser := int64(1111)
62+
expectedRunAsGroup := int64(2222)
63+
expectedFsGroup := int64(3333)
64+
assert.Equal(t, &expectedRunAsUser, p.Spec.SecurityContext.RunAsUser)
65+
assert.Equal(t, &expectedRunAsGroup, p.Spec.SecurityContext.RunAsGroup)
66+
assert.Equal(t, &expectedFsGroup, p.Spec.SecurityContext.FSGroup)
5567

5668
assert.Len(t, p.Spec.ImagePullSecrets, 1)
5769
assert.Equal(t, "pull-secrets", p.Spec.ImagePullSecrets[0].Name)

0 commit comments

Comments
 (0)