Skip to content

Commit 0549d0e

Browse files
authored
Merge pull request kubernetes#88943 from tedyu/visitor-container-type
Visitors of Configmaps and Secrets should specify which containers to visit
2 parents ede025a + e0dbbf0 commit 0549d0e

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

pkg/api/pod/util.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ type Visitor func(name string) (shouldContinue bool)
8989
// referenced by the pod spec. If visitor returns false, visiting is short-circuited.
9090
// Transitive references (e.g. pod -> pvc -> pv -> secret) are not visited.
9191
// Returns true if visiting completed, false if visiting was short-circuited.
92-
func VisitPodSecretNames(pod *api.Pod, visitor Visitor) bool {
92+
func VisitPodSecretNames(pod *api.Pod, visitor Visitor, containerType ContainerType) bool {
9393
for _, reference := range pod.Spec.ImagePullSecrets {
9494
if !visitor(reference.Name) {
9595
return false
9696
}
9797
}
98-
VisitContainers(&pod.Spec, AllContainers, func(c *api.Container, containerType ContainerType) bool {
98+
VisitContainers(&pod.Spec, containerType, func(c *api.Container, containerType ContainerType) bool {
9999
return visitContainerSecretNames(c, visitor)
100100
})
101101
var source *api.VolumeSource
@@ -177,8 +177,8 @@ func visitContainerSecretNames(container *api.Container, visitor Visitor) bool {
177177
// referenced by the pod spec. If visitor returns false, visiting is short-circuited.
178178
// Transitive references (e.g. pod -> pvc -> pv -> secret) are not visited.
179179
// Returns true if visiting completed, false if visiting was short-circuited.
180-
func VisitPodConfigmapNames(pod *api.Pod, visitor Visitor) bool {
181-
VisitContainers(&pod.Spec, AllContainers, func(c *api.Container, containerType ContainerType) bool {
180+
func VisitPodConfigmapNames(pod *api.Pod, visitor Visitor, containerType ContainerType) bool {
181+
VisitContainers(&pod.Spec, containerType, func(c *api.Container, containerType ContainerType) bool {
182182
return visitContainerConfigmapNames(c, visitor)
183183
})
184184
var source *api.VolumeSource

pkg/api/pod/util_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func TestPodSecrets(t *testing.T) {
283283
VisitPodSecretNames(pod, func(name string) bool {
284284
extractedNames.Insert(name)
285285
return true
286-
})
286+
}, AllContainers)
287287

288288
// excludedSecretPaths holds struct paths to fields with "secret" in the name that are not actually references to secret API objects
289289
excludedSecretPaths := sets.NewString(
@@ -428,7 +428,7 @@ func TestPodConfigmaps(t *testing.T) {
428428
VisitPodConfigmapNames(pod, func(name string) bool {
429429
extractedNames.Insert(name)
430430
return true
431-
})
431+
}, AllContainers)
432432

433433
// expectedPaths holds struct paths to fields with "ConfigMap" in the name that are references to ConfigMap API objects.
434434
// every path here should be represented as an example in the Pod stub above, with the ConfigMap name set to the path.

plugin/pkg/admission/noderestriction/admission.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,12 @@ func (p *Plugin) admitPodCreate(nodeName string, a admission.Attributes) error {
257257
return admission.NewForbidden(a, fmt.Errorf("node %q can not create pods that reference a service account", nodeName))
258258
}
259259
hasSecrets := false
260-
podutil.VisitPodSecretNames(pod, func(name string) (shouldContinue bool) { hasSecrets = true; return false })
260+
podutil.VisitPodSecretNames(pod, func(name string) (shouldContinue bool) { hasSecrets = true; return false }, podutil.AllContainers)
261261
if hasSecrets {
262262
return admission.NewForbidden(a, fmt.Errorf("node %q can not create pods that reference secrets", nodeName))
263263
}
264264
hasConfigMaps := false
265-
podutil.VisitPodConfigmapNames(pod, func(name string) (shouldContinue bool) { hasConfigMaps = true; return false })
265+
podutil.VisitPodConfigmapNames(pod, func(name string) (shouldContinue bool) { hasConfigMaps = true; return false }, podutil.AllContainers)
266266
if hasConfigMaps {
267267
return admission.NewForbidden(a, fmt.Errorf("node %q can not create pods that reference configmaps", nodeName))
268268
}

plugin/pkg/admission/serviceaccount/admission.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (s *Plugin) Validate(ctx context.Context, a admission.Attributes, o admissi
216216
podutil.VisitPodSecretNames(pod, func(name string) bool {
217217
hasSecrets = true
218218
return false
219-
})
219+
}, podutil.AllContainers)
220220
if hasSecrets {
221221
return admission.NewForbidden(a, fmt.Errorf("a mirror pod may not reference secrets"))
222222
}

0 commit comments

Comments
 (0)