From a3ccd62f2de7f6cab98d1c3c1a13f1a45ae2099a Mon Sep 17 00:00:00 2001 From: Varsha Varadarajan Date: Mon, 10 Feb 2020 11:28:06 -0800 Subject: [PATCH] Check for config map key ref for unused config map test --- checks/basic/unused_config_map.go | 7 +++++- checks/basic/unused_config_map_test.go | 30 +++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/checks/basic/unused_config_map.go b/checks/basic/unused_config_map.go index aec06f53..87aead78 100644 --- a/checks/basic/unused_config_map.go +++ b/checks/basic/unused_config_map.go @@ -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 } diff --git a/checks/basic/unused_config_map_test.go b/checks/basic/unused_config_map_test.go index 7e72434e..6a777780 100644 --- a/checks/basic/unused_config_map_test.go +++ b/checks/basic/unused_config_map_test.go @@ -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 +}