mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-20 09:33:52 +00:00
Skip visiting empty secret and configmap names
This commit is contained in:
@@ -92,11 +92,23 @@ func VisitContainers(podSpec *api.PodSpec, mask ContainerType, visitor Container
|
||||
// Visitor is called with each object name, and returns true if visiting should continue
|
||||
type Visitor func(name string) (shouldContinue bool)
|
||||
|
||||
func skipEmptyNames(visitor Visitor) Visitor {
|
||||
return func(name string) bool {
|
||||
if len(name) == 0 {
|
||||
// continue visiting
|
||||
return true
|
||||
}
|
||||
// delegate to visitor
|
||||
return visitor(name)
|
||||
}
|
||||
}
|
||||
|
||||
// VisitPodSecretNames invokes the visitor function with the name of every secret
|
||||
// referenced by the pod spec. If visitor returns false, visiting is short-circuited.
|
||||
// Transitive references (e.g. pod -> pvc -> pv -> secret) are not visited.
|
||||
// Returns true if visiting completed, false if visiting was short-circuited.
|
||||
func VisitPodSecretNames(pod *api.Pod, visitor Visitor, containerType ContainerType) bool {
|
||||
visitor = skipEmptyNames(visitor)
|
||||
for _, reference := range pod.Spec.ImagePullSecrets {
|
||||
if !visitor(reference.Name) {
|
||||
return false
|
||||
@@ -185,6 +197,7 @@ func visitContainerSecretNames(container *api.Container, visitor Visitor) bool {
|
||||
// Transitive references (e.g. pod -> pvc -> pv -> secret) are not visited.
|
||||
// Returns true if visiting completed, false if visiting was short-circuited.
|
||||
func VisitPodConfigmapNames(pod *api.Pod, visitor Visitor, containerType ContainerType) bool {
|
||||
visitor = skipEmptyNames(visitor)
|
||||
VisitContainers(&pod.Spec, containerType, func(c *api.Container, containerType ContainerType) bool {
|
||||
return visitContainerConfigmapNames(c, visitor)
|
||||
})
|
||||
|
@@ -365,6 +365,21 @@ func TestPodSecrets(t *testing.T) {
|
||||
t.Logf("Extra secret names:\n%s", strings.Join(extraNames.List(), "\n"))
|
||||
t.Error("Extra secret names extracted. Verify VisitPodSecretNames() is correctly extracting secret names")
|
||||
}
|
||||
|
||||
// emptyPod is a stub containing empty object names
|
||||
emptyPod := &api.Pod{
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{{
|
||||
EnvFrom: []api.EnvFromSource{{
|
||||
SecretRef: &api.SecretEnvSource{
|
||||
LocalObjectReference: api.LocalObjectReference{
|
||||
Name: ""}}}}}},
|
||||
},
|
||||
}
|
||||
VisitPodSecretNames(emptyPod, func(name string) bool {
|
||||
t.Fatalf("expected no empty names collected, got %q", name)
|
||||
return false
|
||||
}, AllContainers)
|
||||
}
|
||||
|
||||
// collectResourcePaths traverses the object, computing all the struct paths that lead to fields with resourcename in the name.
|
||||
@@ -494,6 +509,21 @@ func TestPodConfigmaps(t *testing.T) {
|
||||
t.Logf("Extra names:\n%s", strings.Join(extraNames.List(), "\n"))
|
||||
t.Error("Extra names extracted. Verify VisitPodConfigmapNames() is correctly extracting resource names")
|
||||
}
|
||||
|
||||
// emptyPod is a stub containing empty object names
|
||||
emptyPod := &api.Pod{
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{{
|
||||
EnvFrom: []api.EnvFromSource{{
|
||||
ConfigMapRef: &api.ConfigMapEnvSource{
|
||||
LocalObjectReference: api.LocalObjectReference{
|
||||
Name: ""}}}}}},
|
||||
},
|
||||
}
|
||||
VisitPodConfigmapNames(emptyPod, func(name string) bool {
|
||||
t.Fatalf("expected no empty names collected, got %q", name)
|
||||
return false
|
||||
}, AllContainers)
|
||||
}
|
||||
|
||||
func TestDropFSGroupFields(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user