Skip to content

Commit

Permalink
Check if envFrom has ConfigMapRef set (#2342)
Browse files Browse the repository at this point in the history
EnvFrom can also be used for SecretRefs in which case the existing logic
would result in a nil pointer dereference.

Signed-off-by: Ed Cook <[email protected]>
  • Loading branch information
edwardecook authored Oct 15, 2023
1 parent e744123 commit 26ae0c9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
3 changes: 3 additions & 0 deletions pkg/inject/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,9 @@ func GetConfigMapsMatchedEnvFromInDeployment(dep appsv1.Deployment, configMaps [
matchedConfigMaps := []corev1.ConfigMap{}
for _, container := range dep.Spec.Template.Spec.Containers {
for _, envConfigMap := range container.EnvFrom {
if envConfigMap.ConfigMapRef == nil {
continue
}
if matchedCM, ok := configMapSearchMap[envConfigMap.ConfigMapRef.Name]; ok {
matchedConfigMaps = append(matchedConfigMaps, matchedCM)
}
Expand Down
44 changes: 40 additions & 4 deletions pkg/inject/sidecar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func TestInjectSidecarWithEnvFromK8sAppName(t *testing.T) {
envVarPropagation: "test-service",
},
}}
dep := depEnvFromConfigRef(map[string]string{}, map[string]string{
dep := depEnvFrom(map[string]string{}, map[string]string{
"app": "noapp",
"app.kubernetes.io/name": "testapp",
}, []corev1.EnvFromSource{{ConfigMapRef: &corev1.ConfigMapEnvSource{LocalObjectReference: corev1.LocalObjectReference{Name: "test-config"}}}})
Expand Down Expand Up @@ -237,7 +237,7 @@ func TestInjectSidecarWithoutEnvFromK8sAppName(t *testing.T) {
"PASSWORD": "+S=KDKS",
},
}}
dep := depEnvFromConfigRef(map[string]string{}, map[string]string{
dep := depEnvFrom(map[string]string{}, map[string]string{
"app": "noapp",
"app.kubernetes.io/name": "testapp",
}, []corev1.EnvFromSource{{ConfigMapRef: &corev1.ConfigMapEnvSource{LocalObjectReference: corev1.LocalObjectReference{Name: "test-config"}}}})
Expand Down Expand Up @@ -823,7 +823,7 @@ func dep(annotations map[string]string, labels map[string]string) *appsv1.Deploy
}
}

func depEnvFromConfigRef(annotations map[string]string, labels map[string]string, envFromConfigRef []corev1.EnvFromSource) *appsv1.Deployment {
func depEnvFrom(annotations map[string]string, labels map[string]string, envFrom []corev1.EnvFromSource) *appsv1.Deployment {
return &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Annotations: annotations,
Expand All @@ -837,7 +837,7 @@ func depEnvFromConfigRef(annotations map[string]string, labels map[string]string
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: "only_container",
EnvFrom: envFromConfigRef,
EnvFrom: envFrom,
}},
},
},
Expand Down Expand Up @@ -1077,3 +1077,39 @@ func TestGetContainerNameWithAppContainerAndJaegerAgent(t *testing.T) {
containerName := getContainerName(deploy.Spec.Template.Spec.Containers, agentIdx)
assert.Equal(t, "only_container", containerName)
}

func TestGetConfigMapsMatchedEnvFromInDeploymentWithEnvFromSecretRef(t *testing.T) {
deploy := depEnvFrom(map[string]string{}, map[string]string{},
[]corev1.EnvFromSource{{SecretRef: &corev1.SecretEnvSource{LocalObjectReference: corev1.LocalObjectReference{Name: "test-secret"}}}})
configMaps := []corev1.ConfigMap{}

matchedConfigMaps := GetConfigMapsMatchedEnvFromInDeployment(*deploy, configMaps)

assert.Len(t, matchedConfigMaps, 0)
}

func TestGetConfigMapsMatchedEnvFromInDeploymentWithEnvFromConfigMapRef(t *testing.T) {
deploy := depEnvFrom(map[string]string{}, map[string]string{},
[]corev1.EnvFromSource{{ConfigMapRef: &corev1.ConfigMapEnvSource{LocalObjectReference: corev1.LocalObjectReference{Name: "test-config"}}}})
configMaps := []corev1.ConfigMap{{ObjectMeta: metav1.ObjectMeta{Name: "test-config"}}}

matchedConfigMaps := GetConfigMapsMatchedEnvFromInDeployment(*deploy, configMaps)

assert.Len(t, matchedConfigMaps, 1)
assert.Equal(t, matchedConfigMaps[0].Name, "test-config")
}

func TestGetConfigMapsMatchedEnvFromInDeploymentWithEnvFromConfigAndSecret(t *testing.T) {
deploy := depEnvFrom(map[string]string{}, map[string]string{},
[]corev1.EnvFromSource{
{ConfigMapRef: &corev1.ConfigMapEnvSource{LocalObjectReference: corev1.LocalObjectReference{Name: "test-config"}}},
{SecretRef: &corev1.SecretEnvSource{LocalObjectReference: corev1.LocalObjectReference{Name: "test-secret"}}},
},
)
configMaps := []corev1.ConfigMap{{ObjectMeta: metav1.ObjectMeta{Name: "test-config"}}}

matchedConfigMaps := GetConfigMapsMatchedEnvFromInDeployment(*deploy, configMaps)

assert.Len(t, matchedConfigMaps, 1)
assert.Equal(t, matchedConfigMaps[0].Name, "test-config")
}

0 comments on commit 26ae0c9

Please sign in to comment.