Skip to content

Commit 98b4ee6

Browse files
authored
Merge pull request kubernetes#126525 from dshebib/addSidecarE2EImgTest
Restart sidecar container when the image has changed
2 parents 2f3da71 + 43d527a commit 98b4ee6

File tree

3 files changed

+1358
-40
lines changed

3 files changed

+1358
-40
lines changed

pkg/kubelet/kuberuntime/kuberuntime_container.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,18 @@ func (m *kubeGenericRuntimeManager) computeInitContainerActions(pod *v1.Pod, pod
11271127
changes.InitContainersToStart = append(changes.InitContainersToStart, i+1)
11281128
}
11291129

1130+
// Restart running sidecar containers which have had their definition changed.
1131+
if _, _, changed := containerChanged(container, status); changed {
1132+
changes.ContainersToKill[status.ID] = containerToKillInfo{
1133+
name: container.Name,
1134+
container: container,
1135+
message: fmt.Sprintf("Init container %s definition changed", container.Name),
1136+
reason: "",
1137+
}
1138+
changes.InitContainersToStart = append(changes.InitContainersToStart, i)
1139+
break
1140+
}
1141+
11301142
// A restartable init container does not have to take into account its
11311143
// liveness probe when it determines to start the next init container.
11321144
if container.LivenessProbe != nil {

pkg/kubelet/kuberuntime/kuberuntime_manager_test.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,19 @@ func getKillMapWithInitContainers(pod *v1.Pod, status *kubecontainer.PodStatus,
12381238
return m
12391239
}
12401240

1241+
func modifyKillMapContainerImage(containersToKill map[kubecontainer.ContainerID]containerToKillInfo, status *kubecontainer.PodStatus, cIndexes []int, imageNames []string) map[kubecontainer.ContainerID]containerToKillInfo {
1242+
for idx, i := range cIndexes {
1243+
containerKillInfo := containersToKill[status.ContainerStatuses[i].ID]
1244+
updatedContainer := containerKillInfo.container.DeepCopy()
1245+
updatedContainer.Image = imageNames[idx]
1246+
containersToKill[status.ContainerStatuses[i].ID] = containerToKillInfo{
1247+
container: updatedContainer,
1248+
name: containerKillInfo.name,
1249+
}
1250+
}
1251+
return containersToKill
1252+
}
1253+
12411254
func verifyActions(t *testing.T, expected, actual *podActions, desc string) {
12421255
if actual.ContainersToKill != nil {
12431256
// Clear the message and reason fields since we don't need to verify them.
@@ -1524,12 +1537,12 @@ func makeBasePodAndStatusWithInitContainers() (*v1.Pod, *kubecontainer.PodStatus
15241537
{
15251538
ID: kubecontainer.ContainerID{ID: "initid2"},
15261539
Name: "init2", State: kubecontainer.ContainerStateExited,
1527-
Hash: kubecontainer.HashContainer(&pod.Spec.InitContainers[0]),
1540+
Hash: kubecontainer.HashContainer(&pod.Spec.InitContainers[1]),
15281541
},
15291542
{
15301543
ID: kubecontainer.ContainerID{ID: "initid3"},
15311544
Name: "init3", State: kubecontainer.ContainerStateExited,
1532-
Hash: kubecontainer.HashContainer(&pod.Spec.InitContainers[0]),
1545+
Hash: kubecontainer.HashContainer(&pod.Spec.InitContainers[2]),
15331546
},
15341547
}
15351548
return pod, status
@@ -1702,6 +1715,18 @@ func TestComputePodActionsWithRestartableInitContainers(t *testing.T) {
17021715
m.startupManager.Remove(status.ContainerStatuses[2].ID)
17031716
},
17041717
},
1718+
"kill and recreate the restartable init container if the container definition changes": {
1719+
mutatePodFn: func(pod *v1.Pod) {
1720+
pod.Spec.RestartPolicy = v1.RestartPolicyAlways
1721+
pod.Spec.InitContainers[2].Image = "foo-image"
1722+
},
1723+
actions: podActions{
1724+
SandboxID: baseStatus.SandboxStatuses[0].Id,
1725+
InitContainersToStart: []int{2},
1726+
ContainersToKill: modifyKillMapContainerImage(getKillMapWithInitContainers(basePod, baseStatus, []int{2}), baseStatus, []int{2}, []string{"foo-image"}),
1727+
ContainersToStart: []int{0, 1, 2},
1728+
},
1729+
},
17051730
"restart terminated restartable init container and next init container": {
17061731
mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyAlways },
17071732
mutateStatusFn: func(pod *v1.Pod, status *kubecontainer.PodStatus) {
@@ -1928,12 +1953,12 @@ func makeBasePodAndStatusWithRestartableInitContainers() (*v1.Pod, *kubecontaine
19281953
{
19291954
ID: kubecontainer.ContainerID{ID: "initid2"},
19301955
Name: "restartable-init-2", State: kubecontainer.ContainerStateRunning,
1931-
Hash: kubecontainer.HashContainer(&pod.Spec.InitContainers[0]),
1956+
Hash: kubecontainer.HashContainer(&pod.Spec.InitContainers[1]),
19321957
},
19331958
{
19341959
ID: kubecontainer.ContainerID{ID: "initid3"},
19351960
Name: "restartable-init-3", State: kubecontainer.ContainerStateRunning,
1936-
Hash: kubecontainer.HashContainer(&pod.Spec.InitContainers[0]),
1961+
Hash: kubecontainer.HashContainer(&pod.Spec.InitContainers[2]),
19371962
},
19381963
}
19391964
return pod, status

0 commit comments

Comments
 (0)