Skip to content

Commit fa785a5

Browse files
authored
Merge pull request kubernetes#89667 from kmala/kubelet
Check for sandboxes before deleting the pod from apiserver
2 parents cf13f8d + 9b9cf33 commit fa785a5

File tree

5 files changed

+81
-11
lines changed

5 files changed

+81
-11
lines changed

pkg/kubelet/BUILD

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,20 +250,13 @@ go_test(
250250
"//staging/src/k8s.io/client-go/util/testing:go_default_library",
251251
"//staging/src/k8s.io/component-base/featuregate/testing:go_default_library",
252252
"//staging/src/k8s.io/component-base/version:go_default_library",
253+
"//staging/src/k8s.io/cri-api/pkg/apis/runtime/v1alpha2:go_default_library",
253254
"//vendor/github.com/google/cadvisor/info/v1:go_default_library",
254255
"//vendor/github.com/google/cadvisor/info/v2:go_default_library",
255256
"//vendor/github.com/stretchr/testify/assert:go_default_library",
256257
"//vendor/github.com/stretchr/testify/require:go_default_library",
257258
"//vendor/k8s.io/utils/mount:go_default_library",
258-
] + select({
259-
"@io_bazel_rules_go//go/platform:android": [
260-
"//staging/src/k8s.io/cri-api/pkg/apis/runtime/v1alpha2:go_default_library",
261-
],
262-
"@io_bazel_rules_go//go/platform:linux": [
263-
"//staging/src/k8s.io/cri-api/pkg/apis/runtime/v1alpha2:go_default_library",
264-
],
265-
"//conditions:default": [],
266-
}),
259+
],
267260
)
268261

269262
filegroup(

pkg/kubelet/container/runtime.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ type PodStatus struct {
285285
// Status of containers in the pod.
286286
ContainerStatuses []*ContainerStatus
287287
// Status of the pod sandbox.
288-
// Only for kuberuntime now, other runtime may keep it nil.
289288
SandboxStatuses []*runtimeapi.PodSandboxStatus
290289
}
291290

pkg/kubelet/kubelet_pods.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,16 @@ func (kl *Kubelet) PodResourcesAreReclaimed(pod *v1.Pod, status v1.PodStatus) bo
940940
klog.V(3).Infof("Pod %q is terminated, but some containers have not been cleaned up: %s", format.Pod(pod), statusStr)
941941
return false
942942
}
943+
// pod's sandboxes should be deleted
944+
if len(runtimeStatus.SandboxStatuses) > 0 {
945+
var sandboxStr string
946+
for _, sandbox := range runtimeStatus.SandboxStatuses {
947+
sandboxStr += fmt.Sprintf("%+v ", *sandbox)
948+
}
949+
klog.V(3).Infof("Pod %q is terminated, but some pod sandboxes have not been cleaned up: %s", format.Pod(pod), sandboxStr)
950+
return false
951+
}
952+
943953
if kl.podVolumesExist(pod.UID) && !kl.keepTerminatedPodVolumes {
944954
// We shouldn't delete pods whose volumes have not been cleaned up if we are not keeping terminated pod volumes
945955
klog.V(3).Infof("Pod %q is terminated, but some volumes have not been cleaned up", format.Pod(pod))

pkg/kubelet/kubelet_pods_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
// api.Registry.GroupOrDie(v1.GroupName).GroupVersions[0].String() is changed
4343
// to "v1"?
4444

45+
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
4546
_ "k8s.io/kubernetes/pkg/apis/core/install"
4647
"k8s.io/kubernetes/pkg/features"
4748
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
@@ -2385,3 +2386,70 @@ func TestTruncatePodHostname(t *testing.T) {
23852386
assert.Equal(t, test.output, output)
23862387
}
23872388
}
2389+
2390+
func TestPodResourcesAreReclaimed(t *testing.T) {
2391+
2392+
type args struct {
2393+
pod *v1.Pod
2394+
status v1.PodStatus
2395+
runtimeStatus kubecontainer.PodStatus
2396+
}
2397+
tests := []struct {
2398+
name string
2399+
args args
2400+
want bool
2401+
}{
2402+
{
2403+
"pod with running containers",
2404+
args{
2405+
pod: &v1.Pod{},
2406+
status: v1.PodStatus{
2407+
ContainerStatuses: []v1.ContainerStatus{
2408+
runningState("containerA"),
2409+
runningState("containerB"),
2410+
},
2411+
},
2412+
},
2413+
false,
2414+
},
2415+
{
2416+
"pod with containers in runtime cache",
2417+
args{
2418+
pod: &v1.Pod{},
2419+
status: v1.PodStatus{},
2420+
runtimeStatus: kubecontainer.PodStatus{
2421+
ContainerStatuses: []*kubecontainer.ContainerStatus{
2422+
{},
2423+
},
2424+
},
2425+
},
2426+
false,
2427+
},
2428+
{
2429+
"pod with sandbox present",
2430+
args{
2431+
pod: &v1.Pod{},
2432+
status: v1.PodStatus{},
2433+
runtimeStatus: kubecontainer.PodStatus{
2434+
SandboxStatuses: []*runtimeapi.PodSandboxStatus{
2435+
{},
2436+
},
2437+
},
2438+
},
2439+
false,
2440+
},
2441+
}
2442+
2443+
testKubelet := newTestKubelet(t, false)
2444+
defer testKubelet.Cleanup()
2445+
kl := testKubelet.kubelet
2446+
2447+
for _, tt := range tests {
2448+
t.Run(tt.name, func(t *testing.T) {
2449+
testKubelet.fakeRuntime.PodStatus = tt.args.runtimeStatus
2450+
if got := kl.PodResourcesAreReclaimed(tt.args.pod, tt.args.status); got != tt.want {
2451+
t.Errorf("PodResourcesAreReclaimed() = %v, want %v", got, tt.want)
2452+
}
2453+
})
2454+
}
2455+
}

pkg/kubelet/remote/fake/fake_runtime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (f *RemoteRuntime) StopPodSandbox(ctx context.Context, req *kubeapi.StopPod
112112
// This call is idempotent, and must not return an error if the sandbox has
113113
// already been removed.
114114
func (f *RemoteRuntime) RemovePodSandbox(ctx context.Context, req *kubeapi.RemovePodSandboxRequest) (*kubeapi.RemovePodSandboxResponse, error) {
115-
err := f.RuntimeService.StopPodSandbox(req.PodSandboxId)
115+
err := f.RuntimeService.RemovePodSandbox(req.PodSandboxId)
116116
if err != nil {
117117
return nil, err
118118
}

0 commit comments

Comments
 (0)