-
Notifications
You must be signed in to change notification settings - Fork 38.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
kubelet: fix sandbox garbage collection #43053
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,19 @@ func TestSandboxGC(t *testing.T) { | |
fakeRuntime, _, m, err := createTestRuntimeManager() | ||
assert.NoError(t, err) | ||
|
||
fakePodGetter := m.containerGC.podGetter.(*fakePodGetter) | ||
makeGCSandbox := func(pod *v1.Pod, attempt uint32, state runtimeapi.PodSandboxState, withPodGetter bool) sandboxTemplate { | ||
if withPodGetter { | ||
// initialize the pod getter | ||
fakePodGetter.pods[pod.UID] = pod | ||
} | ||
return sandboxTemplate{ | ||
pod: pod, | ||
state: state, | ||
attempt: attempt, | ||
} | ||
} | ||
|
||
pods := []*v1.Pod{ | ||
makeTestPod("foo1", "new", "1234", []v1.Container{ | ||
makeTestContainer("bar1", "busybox"), | ||
|
@@ -42,6 +55,9 @@ func TestSandboxGC(t *testing.T) { | |
makeTestPod("foo2", "new", "5678", []v1.Container{ | ||
makeTestContainer("bar3", "busybox"), | ||
}), | ||
makeTestPod("deleted", "new", "9012", []v1.Container{ | ||
makeTestContainer("bar4", "busybox"), | ||
}), | ||
} | ||
|
||
for c, test := range []struct { | ||
|
@@ -52,55 +68,58 @@ func TestSandboxGC(t *testing.T) { | |
remain []int // template indexes of remaining sandboxes | ||
}{ | ||
{ | ||
description: "sandbox with no containers should be garbage collected.", | ||
description: "notready sandboxes without containers for deleted pods should be garbage collected.", | ||
sandboxes: []sandboxTemplate{ | ||
{pod: pods[0], state: runtimeapi.PodSandboxState_SANDBOX_NOTREADY}, | ||
makeGCSandbox(pods[2], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, false), | ||
}, | ||
containers: []containerTemplate{}, | ||
remain: []int{}, | ||
}, | ||
{ | ||
description: "running sandbox should not be garbage collected.", | ||
description: "ready sandboxes without containers for deleted pods should not be garbage collected.", | ||
sandboxes: []sandboxTemplate{ | ||
{pod: pods[0], state: runtimeapi.PodSandboxState_SANDBOX_READY}, | ||
makeGCSandbox(pods[2], 0, runtimeapi.PodSandboxState_SANDBOX_READY, false), | ||
}, | ||
containers: []containerTemplate{}, | ||
remain: []int{0}, | ||
}, | ||
{ | ||
description: "sandboxes for existing pods should not be garbage collected.", | ||
sandboxes: []sandboxTemplate{ | ||
makeGCSandbox(pods[0], 0, runtimeapi.PodSandboxState_SANDBOX_READY, true), | ||
makeGCSandbox(pods[1], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, true), | ||
}, | ||
containers: []containerTemplate{}, | ||
remain: []int{0, 1}, | ||
}, | ||
{ | ||
description: "sandbox with containers should not be garbage collected.", | ||
sandboxes: []sandboxTemplate{ | ||
{pod: pods[0], state: runtimeapi.PodSandboxState_SANDBOX_NOTREADY}, | ||
makeGCSandbox(pods[0], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, false), | ||
}, | ||
containers: []containerTemplate{ | ||
{pod: pods[0], container: &pods[0].Spec.Containers[0], state: runtimeapi.ContainerState_CONTAINER_EXITED}, | ||
}, | ||
remain: []int{0}, | ||
}, | ||
{ | ||
description: "sandbox within min age should not be garbage collected.", | ||
sandboxes: []sandboxTemplate{ | ||
{pod: pods[0], createdAt: time.Now().UnixNano(), state: runtimeapi.PodSandboxState_SANDBOX_NOTREADY}, | ||
{pod: pods[1], createdAt: time.Now().Add(-2 * time.Hour).UnixNano(), state: runtimeapi.PodSandboxState_SANDBOX_NOTREADY}, | ||
}, | ||
containers: []containerTemplate{}, | ||
minAge: time.Hour, // assume the test won't take an hour | ||
remain: []int{0}, | ||
}, | ||
{ | ||
description: "multiple sandboxes should be handled properly.", | ||
sandboxes: []sandboxTemplate{ | ||
// running sandbox. | ||
{pod: pods[0], attempt: 1, state: runtimeapi.PodSandboxState_SANDBOX_READY}, | ||
makeGCSandbox(pods[0], 1, runtimeapi.PodSandboxState_SANDBOX_READY, true), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should have another exit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ACK. |
||
// exited sandbox without containers. | ||
makeGCSandbox(pods[0], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, false), | ||
// exited sandbox with containers. | ||
{pod: pods[1], attempt: 1, state: runtimeapi.PodSandboxState_SANDBOX_NOTREADY}, | ||
makeGCSandbox(pods[1], 1, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, true), | ||
// exited sandbox without containers. | ||
{pod: pods[1], attempt: 0, state: runtimeapi.PodSandboxState_SANDBOX_NOTREADY}, | ||
makeGCSandbox(pods[1], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, false), | ||
// exited sandbox without containers for deleted pods. | ||
makeGCSandbox(pods[2], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, false), | ||
}, | ||
containers: []containerTemplate{ | ||
{pod: pods[1], container: &pods[1].Spec.Containers[0], sandboxAttempt: 1, state: runtimeapi.ContainerState_CONTAINER_EXITED}, | ||
}, | ||
remain: []int{0, 1}, | ||
remain: []int{0, 2}, | ||
}, | ||
} { | ||
t.Logf("TestCase #%d: %+v", c, test) | ||
|
@@ -109,7 +128,7 @@ func TestSandboxGC(t *testing.T) { | |
fakeRuntime.SetFakeSandboxes(fakeSandboxes) | ||
fakeRuntime.SetFakeContainers(fakeContainers) | ||
|
||
err := m.containerGC.evictSandboxes(test.minAge) | ||
err := m.containerGC.evictSandboxes() | ||
assert.NoError(t, err) | ||
realRemain, err := fakeRuntime.ListPodSandbox(nil) | ||
assert.NoError(t, err) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this
oldest to newest
? Seems to be newest to oldest...And also the same question for the container remove function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK. Would fix this.