Skip to content

Commit d4325f4

Browse files
committed
Check for sandboxes before deleting the pod from apiserver
1 parent ae7dce7 commit d4325f4

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
@@ -254,20 +254,13 @@ go_test(
254254
"//staging/src/k8s.io/component-base/featuregate/testing:go_default_library",
255255
"//staging/src/k8s.io/component-base/version:go_default_library",
256256
"//vendor/github.com/golang/groupcache/lru:go_default_library",
257+
"//staging/src/k8s.io/cri-api/pkg/apis/runtime/v1alpha2:go_default_library",
257258
"//vendor/github.com/google/cadvisor/info/v1:go_default_library",
258259
"//vendor/github.com/google/cadvisor/info/v2:go_default_library",
259260
"//vendor/github.com/stretchr/testify/assert:go_default_library",
260261
"//vendor/github.com/stretchr/testify/require:go_default_library",
261262
"//vendor/k8s.io/utils/mount:go_default_library",
262-
] + select({
263-
"@io_bazel_rules_go//go/platform:android": [
264-
"//staging/src/k8s.io/cri-api/pkg/apis/runtime/v1alpha2:go_default_library",
265-
],
266-
"@io_bazel_rules_go//go/platform:linux": [
267-
"//staging/src/k8s.io/cri-api/pkg/apis/runtime/v1alpha2:go_default_library",
268-
],
269-
"//conditions:default": [],
270-
}),
263+
],
271264
)
272265

273266
filegroup(

pkg/kubelet/container/runtime.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ type PodStatus struct {
299299
// Status of containers in the pod.
300300
ContainerStatuses []*Status
301301
// Status of the pod sandbox.
302-
// Only for kuberuntime now, other runtime may keep it nil.
303302
SandboxStatuses []*runtimeapi.PodSandboxStatus
304303
}
305304

pkg/kubelet/cri/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
}

pkg/kubelet/kubelet_pods.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,16 @@ func (kl *Kubelet) PodResourcesAreReclaimed(pod *v1.Pod, status v1.PodStatus) bo
969969
klog.V(3).Infof("Pod %q is terminated, but some containers have not been cleaned up: %s", format.Pod(pod), statusStr)
970970
return false
971971
}
972+
// pod's sandboxes should be deleted
973+
if len(runtimeStatus.SandboxStatuses) > 0 {
974+
var sandboxStr string
975+
for _, sandbox := range runtimeStatus.SandboxStatuses {
976+
sandboxStr += fmt.Sprintf("%+v ", *sandbox)
977+
}
978+
klog.V(3).Infof("Pod %q is terminated, but some pod sandboxes have not been cleaned up: %s", format.Pod(pod), sandboxStr)
979+
return false
980+
}
981+
972982
if kl.podVolumesExist(pod.UID) && !kl.keepTerminatedPodVolumes {
973983
// We shouldn't delete pods whose volumes have not been cleaned up if we are not keeping terminated pod volumes
974984
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"
@@ -2421,3 +2422,70 @@ func TestTruncatePodHostname(t *testing.T) {
24212422
assert.Equal(t, test.output, output)
24222423
}
24232424
}
2425+
2426+
func TestPodResourcesAreReclaimed(t *testing.T) {
2427+
2428+
type args struct {
2429+
pod *v1.Pod
2430+
status v1.PodStatus
2431+
runtimeStatus kubecontainer.PodStatus
2432+
}
2433+
tests := []struct {
2434+
name string
2435+
args args
2436+
want bool
2437+
}{
2438+
{
2439+
"pod with running containers",
2440+
args{
2441+
pod: &v1.Pod{},
2442+
status: v1.PodStatus{
2443+
ContainerStatuses: []v1.ContainerStatus{
2444+
runningState("containerA"),
2445+
runningState("containerB"),
2446+
},
2447+
},
2448+
},
2449+
false,
2450+
},
2451+
{
2452+
"pod with containers in runtime cache",
2453+
args{
2454+
pod: &v1.Pod{},
2455+
status: v1.PodStatus{},
2456+
runtimeStatus: kubecontainer.PodStatus{
2457+
ContainerStatuses: []*kubecontainer.ContainerStatus{
2458+
{},
2459+
},
2460+
},
2461+
},
2462+
false,
2463+
},
2464+
{
2465+
"pod with sandbox present",
2466+
args{
2467+
pod: &v1.Pod{},
2468+
status: v1.PodStatus{},
2469+
runtimeStatus: kubecontainer.PodStatus{
2470+
SandboxStatuses: []*runtimeapi.PodSandboxStatus{
2471+
{},
2472+
},
2473+
},
2474+
},
2475+
false,
2476+
},
2477+
}
2478+
2479+
testKubelet := newTestKubelet(t, false)
2480+
defer testKubelet.Cleanup()
2481+
kl := testKubelet.kubelet
2482+
2483+
for _, tt := range tests {
2484+
t.Run(tt.name, func(t *testing.T) {
2485+
testKubelet.fakeRuntime.PodStatus = tt.args.runtimeStatus
2486+
if got := kl.PodResourcesAreReclaimed(tt.args.pod, tt.args.status); got != tt.want {
2487+
t.Errorf("PodResourcesAreReclaimed() = %v, want %v", got, tt.want)
2488+
}
2489+
})
2490+
}
2491+
}

0 commit comments

Comments
 (0)