Skip to content

Commit

Permalink
Merge pull request #79 from varshavaradarajan/config-map-key-ref
Browse files Browse the repository at this point in the history
Check for config map key ref for unused config map check
  • Loading branch information
varshavaradarajan authored Feb 10, 2020
2 parents cb603bc + a3ccd62 commit 2091637
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
7 changes: 6 additions & 1 deletion checks/basic/unused_config_map.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
}
30 changes: 29 additions & 1 deletion checks/basic/unused_config_map_test.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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
}

0 comments on commit 2091637

Please sign in to comment.