Skip to content

Commit 6e7fa64

Browse files
committed
fix: default init container securitycontext as recommended
Signed-off-by: Sophian Mehboub <[email protected]>
1 parent 1b088e0 commit 6e7fa64

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

cmd/traffic/cmd/manager/mutator/agent_injector.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ func addInitContainer(pod *core.Pod, config *agentconfig.Sidecar, patches PatchO
258258
if ic.Name == oc.Name {
259259
if ic.Image == oc.Image &&
260260
slices.Equal(ic.Args, oc.Args) &&
261-
compareVolumeMounts(ic.VolumeMounts, oc.VolumeMounts) {
261+
compareVolumeMounts(ic.VolumeMounts, oc.VolumeMounts) &&
262+
compareCapabilities(ic.SecurityContext, oc.SecurityContext) {
262263
return patches
263264
}
264265
return append(patches, PatchOperation{
@@ -323,6 +324,29 @@ func compareProbes(a, b *core.Probe) bool {
323324
return eq
324325
}
325326

327+
func compareCapabilities(a *core.SecurityContext, b *core.SecurityContext) bool {
328+
ac := a.Capabilities
329+
bc := b.Capabilities
330+
if ac == bc {
331+
return true
332+
}
333+
if ac == nil || bc == nil {
334+
return false
335+
}
336+
compareCaps := func(acs []core.Capability, bcs []core.Capability) bool {
337+
if len(acs) != len(bcs) {
338+
return false
339+
}
340+
for i := range acs {
341+
if acs[i] != bcs[i] {
342+
return false
343+
}
344+
}
345+
return true
346+
}
347+
return compareCaps(ac.Add, bc.Add) && compareCaps(ac.Drop, bc.Drop)
348+
}
349+
326350
// compareVolumeMounts compares two VolumeMount slices but will not include volume mounts using "kube-api-access-" prefix.
327351
func compareVolumeMounts(a, b []core.VolumeMount) bool {
328352
stripKubeAPI := func(vs []core.VolumeMount) []core.VolumeMount {

cmd/traffic/cmd/manager/mutator/agent_injector_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,10 @@ matchExpressions:
15181518
image: ghcr.io/telepresenceio/tel2:2.13.3
15191519
name: tel-agent-init
15201520
resources: {}
1521+
securityContext:
1522+
capabilities:
1523+
add:
1524+
- NET_ADMIN
15211525
- op: add
15221526
path: /spec/containers/-
15231527
value:
@@ -1631,6 +1635,10 @@ matchExpressions:
16311635
image: ghcr.io/telepresenceio/tel2:2.13.3
16321636
name: tel-agent-init
16331637
resources: {}
1638+
securityContext:
1639+
capabilities:
1640+
add:
1641+
- NET_ADMIN
16341642
- op: add
16351643
path: /spec/containers/-
16361644
value:
@@ -1766,6 +1774,11 @@ matchExpressions:
17661774
},
17671775
},
17681776
},
1777+
SecurityContext: &core.SecurityContext{
1778+
Capabilities: &core.Capabilities{
1779+
Add: []core.Capability{"NET_ADMIN"},
1780+
},
1781+
},
17691782
}},
17701783
Containers: []core.Container{
17711784
{

pkg/agentconfig/container.go

+5
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@ func InitContainer(config *Sidecar) *core.Container {
268268
},
269269
},
270270
},
271+
SecurityContext: &core.SecurityContext{
272+
Capabilities: &core.Capabilities{
273+
Add: []core.Capability{"NET_ADMIN"},
274+
},
275+
},
271276
}
272277
if r := config.InitResources; r != nil {
273278
ic.Resources = *r

0 commit comments

Comments
 (0)