kubelet: Sync completed pods until their containers have been terminated

This commit is contained in:
Geonju Kim 2021-02-04 10:17:42 +09:00 committed by Geonju Kim
parent 26744ac58d
commit 321ca8af52
2 changed files with 130 additions and 2 deletions

View File

@ -2020,8 +2020,9 @@ func (kl *Kubelet) dispatchWork(pod *v1.Pod, syncType kubetypes.SyncPodType, mir
}
// optimization: avoid invoking the pod worker if no further changes are possible to the pod definition
if podWorkerTerminal {
klog.V(4).Infof("Pod %q has completed, ignoring remaining sync work: %s", format.Pod(pod), syncType)
// (i.e. the pod has completed and its containers have been terminated)
if podWorkerTerminal && containersTerminal {
klog.V(4).InfoS("Pod has completed and its containers have been terminated, ignoring remaining sync work", "pod", klog.KObj(pod), "syncType", syncType)
return
}

View File

@ -470,6 +470,133 @@ func TestSyncPodsDeletesWhenSourcesAreReadyPerQOS(t *testing.T) {
assert.True(t, calledFunctionCount > 2, "expect more than two PodContainerManager calls")
}
func TestDispatchWorkOfCompletedPod(t *testing.T) {
testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
defer testKubelet.Cleanup()
kubelet := testKubelet.kubelet
kubelet.podWorkers = &fakePodWorkers{
syncPodFn: func(options syncPodOptions) error {
return fmt.Errorf("should ignore completed pod %q", options.pod.Name)
},
cache: kubelet.podCache,
t: t,
}
now := metav1.NewTime(time.Now())
pods := []*v1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
UID: "1",
Name: "completed-pod1",
Namespace: "ns",
Annotations: make(map[string]string),
},
Status: v1.PodStatus{
Phase: v1.PodFailed,
ContainerStatuses: []v1.ContainerStatus{
{
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{},
},
},
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
UID: "2",
Name: "completed-pod2",
Namespace: "ns",
Annotations: make(map[string]string),
},
Status: v1.PodStatus{
Phase: v1.PodSucceeded,
ContainerStatuses: []v1.ContainerStatus{
{
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{},
},
},
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
UID: "3",
Name: "completed-pod3",
Namespace: "ns",
Annotations: make(map[string]string),
DeletionTimestamp: &now,
},
Status: v1.PodStatus{
ContainerStatuses: []v1.ContainerStatus{
{
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{},
},
},
},
},
},
}
for _, pod := range pods {
kubelet.dispatchWork(pod, kubetypes.SyncPodSync, nil, time.Now())
}
}
func TestDispatchWorkOfActivePod(t *testing.T) {
testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
defer testKubelet.Cleanup()
kubelet := testKubelet.kubelet
var got bool
kubelet.podWorkers = &fakePodWorkers{
syncPodFn: func(options syncPodOptions) error {
got = true
return nil
},
cache: kubelet.podCache,
t: t,
}
pods := []*v1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
UID: "1",
Name: "active-pod1",
Namespace: "ns",
Annotations: make(map[string]string),
},
Status: v1.PodStatus{
Phase: v1.PodRunning,
},
},
{
ObjectMeta: metav1.ObjectMeta{
UID: "2",
Name: "active-pod2",
Namespace: "ns",
Annotations: make(map[string]string),
},
Status: v1.PodStatus{
Phase: v1.PodFailed,
ContainerStatuses: []v1.ContainerStatus{
{
State: v1.ContainerState{
Running: &v1.ContainerStateRunning{},
},
},
},
},
},
}
for _, pod := range pods {
kubelet.dispatchWork(pod, kubetypes.SyncPodSync, nil, time.Now())
if !got {
t.Errorf("Should not skip active pod %q", pod.Name)
}
got = false
}
}
func TestSyncPodsDeletesWhenSourcesAreReady(t *testing.T) {
ready := false