From 308fc24b452ceb057cb7911d1e3cd2754957aeae Mon Sep 17 00:00:00 2001 From: Wang Bing Date: Wed, 26 Jan 2022 14:30:39 +0800 Subject: [PATCH] Fix transferEnv in initContainer (#897) Signed-off-by: pigletfly --- pkg/webhook/pod/mutating/sidecarset.go | 2 ++ pkg/webhook/pod/mutating/sidecarset_test.go | 25 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pkg/webhook/pod/mutating/sidecarset.go b/pkg/webhook/pod/mutating/sidecarset.go index d55ff0334c..3825cc2abf 100644 --- a/pkg/webhook/pod/mutating/sidecarset.go +++ b/pkg/webhook/pod/mutating/sidecarset.go @@ -264,6 +264,8 @@ func buildSidecars(isUpdated bool, pod *corev1.Pod, oldPod *corev1.Pod, matchedS initContainer := &sidecarSet.Spec.InitContainers[i] //add "IS_INJECTED" env in initContainer's envs initContainer.Env = append(initContainer.Env, corev1.EnvVar{Name: sidecarcontrol.SidecarEnvKey, Value: "true"}) + transferEnvs := sidecarcontrol.GetSidecarTransferEnvs(initContainer, pod) + initContainer.Env = append(initContainer.Env, transferEnvs...) sidecarInitContainers = append(sidecarInitContainers, initContainer) } diff --git a/pkg/webhook/pod/mutating/sidecarset_test.go b/pkg/webhook/pod/mutating/sidecarset_test.go index ff7cb1a69e..6b1110ebd4 100644 --- a/pkg/webhook/pod/mutating/sidecarset_test.go +++ b/pkg/webhook/pod/mutating/sidecarset_test.go @@ -121,6 +121,24 @@ var ( "app": "suxing-test", }, }, + InitContainers: []appsv1alpha1.SidecarContainer{ + { + Container: corev1.Container{ + Name: "dns-e", + Image: "dns-e-image:1.0", + VolumeMounts: []corev1.VolumeMount{ + {Name: "volume-1"}, + }, + }, + PodInjectPolicy: appsv1alpha1.BeforeAppContainerType, + TransferEnv: []appsv1alpha1.TransferEnvVar{ + { + SourceContainerName: "nginx", + EnvName: "hello2", + }, + }, + }, + }, Containers: []appsv1alpha1.SidecarContainer{ { Container: corev1.Container{ @@ -718,7 +736,12 @@ func testSidecarSetTransferEnv(t *testing.T, sidecarSetIn *appsv1alpha1.SidecarS if err != nil { t.Fatalf("inject sidecar into pod failed, err: %v", err) } - + if len(podOut.Spec.InitContainers[1].Env) != 2 { + t.Fatalf("expect 2 envs but got %v", len(podOut.Spec.InitContainers[0].Env)) + } + if podOut.Spec.InitContainers[1].Env[1].Value != "world2" { + t.Fatalf("expect env with value 'world2' but got %v", podOut.Spec.Containers[0].Env[1].Value) + } if len(podOut.Spec.Containers[0].Env) != 2 { t.Fatalf("expect 2 envs but got %v", len(podOut.Spec.Containers[0].Env)) }