Check for config map key ref for unused config map test

wwarren/update-k8s-deps
Varsha Varadarajan 2020-02-10 11:28:06 -08:00
parent cb603bc39a
commit a3ccd62f2d
2 changed files with 35 additions and 2 deletions

View File

@ -1,5 +1,5 @@
/*
Copyright 2019 DigitalOcean
Copyright 2020 DigitalOcean
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@ -155,6 +155,11 @@ func checkEnvVars(containers []corev1.Container, namespace string) []kube.Identi
refs = append(refs, kube.Identifier{Name: env.ConfigMapRef.LocalObjectReference.Name, Namespace: namespace})
}
}
for _, env := range container.Env {
if env.ValueFrom != nil && env.ValueFrom.ConfigMapKeyRef != nil {
refs = append(refs, kube.Identifier{Name: env.ValueFrom.ConfigMapKeyRef.LocalObjectReference.Name, Namespace: namespace})
}
}
}
return refs
}

View File

@ -1,5 +1,5 @@
/*
Copyright 2019 DigitalOcean
Copyright 2020 DigitalOcean
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@ -65,6 +65,11 @@ func TestUnusedConfigMapWarning(t *testing.T) {
objs: configMapEnvSource(),
expected: nil,
},
{
name: "environment variable value from references config map",
objs: configMapEnvVarValueFromSource(),
expected: nil,
},
{
name: "projected volume references config map",
objs: projectedVolume(),
@ -198,3 +203,26 @@ func configMapEnvSource() *kube.Objects {
}
return objs
}
func configMapEnvVarValueFromSource() *kube.Objects {
objs := initConfigMap()
objs.Pods.Items[0].Spec = corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "test-container",
Image: "docker.io/nginx",
Env: []corev1.EnvVar{
{
Name: "special_env_var",
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: "cm_foo"},
},
},
},
},
},
},
}
return objs
}