Skip to content

Commit

Permalink
Fix transferEnv in initContainer (openkruise#897)
Browse files Browse the repository at this point in the history
  • Loading branch information
pigletfly authored Jan 26, 2022
1 parent 4cd92e3 commit eb930cd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pkg/webhook/pod/mutating/sidecarset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
25 changes: 24 additions & 1 deletion pkg/webhook/pod/mutating/sidecarset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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))
}
Expand Down

0 comments on commit eb930cd

Please sign in to comment.