kuberuntime_gc_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package kuberuntime
  14. import (
  15. "os"
  16. "path/filepath"
  17. "testing"
  18. "time"
  19. "github.com/golang/mock/gomock"
  20. "github.com/stretchr/testify/assert"
  21. "k8s.io/api/core/v1"
  22. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  23. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  24. containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
  25. )
  26. func TestSandboxGC(t *testing.T) {
  27. fakeRuntime, _, m, err := createTestRuntimeManager()
  28. assert.NoError(t, err)
  29. podStateProvider := m.containerGC.podStateProvider.(*fakePodStateProvider)
  30. makeGCSandbox := func(pod *v1.Pod, attempt uint32, state runtimeapi.PodSandboxState, withPodStateProvider bool, createdAt int64) sandboxTemplate {
  31. if withPodStateProvider {
  32. // initialize the pod getter
  33. podStateProvider.existingPods[pod.UID] = struct{}{}
  34. }
  35. return sandboxTemplate{
  36. pod: pod,
  37. state: state,
  38. attempt: attempt,
  39. createdAt: createdAt,
  40. }
  41. }
  42. pods := []*v1.Pod{
  43. makeTestPod("foo1", "new", "1234", []v1.Container{
  44. makeTestContainer("bar1", "busybox"),
  45. makeTestContainer("bar2", "busybox"),
  46. }),
  47. makeTestPod("foo2", "new", "5678", []v1.Container{
  48. makeTestContainer("bar3", "busybox"),
  49. }),
  50. makeTestPod("deleted", "new", "9012", []v1.Container{
  51. makeTestContainer("bar4", "busybox"),
  52. }),
  53. }
  54. for c, test := range []struct {
  55. description string // description of the test case
  56. sandboxes []sandboxTemplate // templates of sandboxes
  57. containers []containerTemplate // templates of containers
  58. remain []int // template indexes of remaining sandboxes
  59. evictTerminatedPods bool
  60. }{
  61. {
  62. description: "notready sandboxes without containers for deleted pods should be garbage collected.",
  63. sandboxes: []sandboxTemplate{
  64. makeGCSandbox(pods[2], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, false, 0),
  65. },
  66. containers: []containerTemplate{},
  67. remain: []int{},
  68. evictTerminatedPods: false,
  69. },
  70. {
  71. description: "ready sandboxes without containers for deleted pods should not be garbage collected.",
  72. sandboxes: []sandboxTemplate{
  73. makeGCSandbox(pods[2], 0, runtimeapi.PodSandboxState_SANDBOX_READY, false, 0),
  74. },
  75. containers: []containerTemplate{},
  76. remain: []int{0},
  77. evictTerminatedPods: false,
  78. },
  79. {
  80. description: "sandboxes for existing pods should not be garbage collected.",
  81. sandboxes: []sandboxTemplate{
  82. makeGCSandbox(pods[0], 0, runtimeapi.PodSandboxState_SANDBOX_READY, true, 0),
  83. makeGCSandbox(pods[1], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, true, 0),
  84. },
  85. containers: []containerTemplate{},
  86. remain: []int{0, 1},
  87. evictTerminatedPods: false,
  88. },
  89. {
  90. description: "older exited sandboxes without containers for existing pods should be garbage collected if there are more than one exited sandboxes.",
  91. sandboxes: []sandboxTemplate{
  92. makeGCSandbox(pods[0], 1, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, true, 1),
  93. makeGCSandbox(pods[0], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, true, 0),
  94. },
  95. containers: []containerTemplate{},
  96. remain: []int{0},
  97. evictTerminatedPods: false,
  98. },
  99. {
  100. description: "older exited sandboxes with containers for existing pods should not be garbage collected even if there are more than one exited sandboxes.",
  101. sandboxes: []sandboxTemplate{
  102. makeGCSandbox(pods[0], 1, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, true, 1),
  103. makeGCSandbox(pods[0], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, true, 0),
  104. },
  105. containers: []containerTemplate{
  106. {pod: pods[0], container: &pods[0].Spec.Containers[0], sandboxAttempt: 0, state: runtimeapi.ContainerState_CONTAINER_EXITED},
  107. },
  108. remain: []int{0, 1},
  109. evictTerminatedPods: false,
  110. },
  111. {
  112. description: "non-running sandboxes for existing pods should be garbage collected if evictTerminatedPods is set.",
  113. sandboxes: []sandboxTemplate{
  114. makeGCSandbox(pods[0], 0, runtimeapi.PodSandboxState_SANDBOX_READY, true, 0),
  115. makeGCSandbox(pods[1], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, true, 0),
  116. },
  117. containers: []containerTemplate{},
  118. remain: []int{0},
  119. evictTerminatedPods: true,
  120. },
  121. {
  122. description: "sandbox with containers should not be garbage collected.",
  123. sandboxes: []sandboxTemplate{
  124. makeGCSandbox(pods[0], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, false, 0),
  125. },
  126. containers: []containerTemplate{
  127. {pod: pods[0], container: &pods[0].Spec.Containers[0], state: runtimeapi.ContainerState_CONTAINER_EXITED},
  128. },
  129. remain: []int{0},
  130. evictTerminatedPods: false,
  131. },
  132. {
  133. description: "multiple sandboxes should be handled properly.",
  134. sandboxes: []sandboxTemplate{
  135. // running sandbox.
  136. makeGCSandbox(pods[0], 1, runtimeapi.PodSandboxState_SANDBOX_READY, true, 1),
  137. // exited sandbox without containers.
  138. makeGCSandbox(pods[0], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, true, 0),
  139. // exited sandbox with containers.
  140. makeGCSandbox(pods[1], 1, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, true, 1),
  141. // exited sandbox without containers.
  142. makeGCSandbox(pods[1], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, true, 0),
  143. // exited sandbox without containers for deleted pods.
  144. makeGCSandbox(pods[2], 0, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, false, 0),
  145. },
  146. containers: []containerTemplate{
  147. {pod: pods[1], container: &pods[1].Spec.Containers[0], sandboxAttempt: 1, state: runtimeapi.ContainerState_CONTAINER_EXITED},
  148. },
  149. remain: []int{0, 2},
  150. evictTerminatedPods: false,
  151. },
  152. } {
  153. t.Logf("TestCase #%d: %+v", c, test)
  154. fakeSandboxes := makeFakePodSandboxes(t, m, test.sandboxes)
  155. fakeContainers := makeFakeContainers(t, m, test.containers)
  156. fakeRuntime.SetFakeSandboxes(fakeSandboxes)
  157. fakeRuntime.SetFakeContainers(fakeContainers)
  158. err := m.containerGC.evictSandboxes(test.evictTerminatedPods)
  159. assert.NoError(t, err)
  160. realRemain, err := fakeRuntime.ListPodSandbox(nil)
  161. assert.NoError(t, err)
  162. assert.Len(t, realRemain, len(test.remain))
  163. for _, remain := range test.remain {
  164. status, err := fakeRuntime.PodSandboxStatus(fakeSandboxes[remain].Id)
  165. assert.NoError(t, err)
  166. assert.Equal(t, &fakeSandboxes[remain].PodSandboxStatus, status)
  167. }
  168. }
  169. }
  170. func makeGCContainer(podStateProvider *fakePodStateProvider, podName, containerName string, attempt int, createdAt int64, state runtimeapi.ContainerState) containerTemplate {
  171. container := makeTestContainer(containerName, "test-image")
  172. pod := makeTestPod(podName, "test-ns", podName, []v1.Container{container})
  173. if podName == "running" {
  174. podStateProvider.runningPods[pod.UID] = struct{}{}
  175. }
  176. if podName != "deleted" {
  177. podStateProvider.existingPods[pod.UID] = struct{}{}
  178. }
  179. return containerTemplate{
  180. pod: pod,
  181. container: &container,
  182. attempt: attempt,
  183. createdAt: createdAt,
  184. state: state,
  185. }
  186. }
  187. func TestContainerGC(t *testing.T) {
  188. fakeRuntime, _, m, err := createTestRuntimeManager()
  189. assert.NoError(t, err)
  190. podStateProvider := m.containerGC.podStateProvider.(*fakePodStateProvider)
  191. defaultGCPolicy := kubecontainer.ContainerGCPolicy{MinAge: time.Hour, MaxPerPodContainer: 2, MaxContainers: 6}
  192. for c, test := range []struct {
  193. description string // description of the test case
  194. containers []containerTemplate // templates of containers
  195. policy *kubecontainer.ContainerGCPolicy // container gc policy
  196. remain []int // template indexes of remaining containers
  197. evictTerminatedPods bool
  198. allSourcesReady bool
  199. }{
  200. {
  201. description: "all containers should be removed when max container limit is 0",
  202. containers: []containerTemplate{
  203. makeGCContainer(podStateProvider, "foo", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),
  204. },
  205. policy: &kubecontainer.ContainerGCPolicy{MinAge: time.Minute, MaxPerPodContainer: 1, MaxContainers: 0},
  206. remain: []int{},
  207. evictTerminatedPods: false,
  208. allSourcesReady: true,
  209. },
  210. {
  211. description: "max containers should be complied when no max per pod container limit is set",
  212. containers: []containerTemplate{
  213. makeGCContainer(podStateProvider, "foo", "bar", 4, 4, runtimeapi.ContainerState_CONTAINER_EXITED),
  214. makeGCContainer(podStateProvider, "foo", "bar", 3, 3, runtimeapi.ContainerState_CONTAINER_EXITED),
  215. makeGCContainer(podStateProvider, "foo", "bar", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),
  216. makeGCContainer(podStateProvider, "foo", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  217. makeGCContainer(podStateProvider, "foo", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),
  218. },
  219. policy: &kubecontainer.ContainerGCPolicy{MinAge: time.Minute, MaxPerPodContainer: -1, MaxContainers: 4},
  220. remain: []int{0, 1, 2, 3},
  221. evictTerminatedPods: false,
  222. allSourcesReady: true,
  223. },
  224. {
  225. description: "no containers should be removed if both max container and per pod container limits are not set",
  226. containers: []containerTemplate{
  227. makeGCContainer(podStateProvider, "foo", "bar", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),
  228. makeGCContainer(podStateProvider, "foo", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  229. makeGCContainer(podStateProvider, "foo", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),
  230. },
  231. policy: &kubecontainer.ContainerGCPolicy{MinAge: time.Minute, MaxPerPodContainer: -1, MaxContainers: -1},
  232. remain: []int{0, 1, 2},
  233. evictTerminatedPods: false,
  234. allSourcesReady: true,
  235. },
  236. {
  237. description: "recently started containers should not be removed",
  238. containers: []containerTemplate{
  239. makeGCContainer(podStateProvider, "foo", "bar", 2, time.Now().UnixNano(), runtimeapi.ContainerState_CONTAINER_EXITED),
  240. makeGCContainer(podStateProvider, "foo", "bar", 1, time.Now().UnixNano(), runtimeapi.ContainerState_CONTAINER_EXITED),
  241. makeGCContainer(podStateProvider, "foo", "bar", 0, time.Now().UnixNano(), runtimeapi.ContainerState_CONTAINER_EXITED),
  242. },
  243. remain: []int{0, 1, 2},
  244. evictTerminatedPods: false,
  245. allSourcesReady: true,
  246. },
  247. {
  248. description: "oldest containers should be removed when per pod container limit exceeded",
  249. containers: []containerTemplate{
  250. makeGCContainer(podStateProvider, "foo", "bar", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),
  251. makeGCContainer(podStateProvider, "foo", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  252. makeGCContainer(podStateProvider, "foo", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),
  253. },
  254. remain: []int{0, 1},
  255. evictTerminatedPods: false,
  256. allSourcesReady: true,
  257. },
  258. {
  259. description: "running containers should not be removed",
  260. containers: []containerTemplate{
  261. makeGCContainer(podStateProvider, "foo", "bar", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),
  262. makeGCContainer(podStateProvider, "foo", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  263. makeGCContainer(podStateProvider, "foo", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_RUNNING),
  264. },
  265. remain: []int{0, 1, 2},
  266. evictTerminatedPods: false,
  267. allSourcesReady: true,
  268. },
  269. {
  270. description: "no containers should be removed when limits are not exceeded",
  271. containers: []containerTemplate{
  272. makeGCContainer(podStateProvider, "foo", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  273. makeGCContainer(podStateProvider, "foo", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),
  274. },
  275. remain: []int{0, 1},
  276. evictTerminatedPods: false,
  277. allSourcesReady: true,
  278. },
  279. {
  280. description: "max container count should apply per (UID, container) pair",
  281. containers: []containerTemplate{
  282. makeGCContainer(podStateProvider, "foo", "bar", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),
  283. makeGCContainer(podStateProvider, "foo", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  284. makeGCContainer(podStateProvider, "foo", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),
  285. makeGCContainer(podStateProvider, "foo1", "baz", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),
  286. makeGCContainer(podStateProvider, "foo1", "baz", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  287. makeGCContainer(podStateProvider, "foo1", "baz", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),
  288. makeGCContainer(podStateProvider, "foo2", "bar", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),
  289. makeGCContainer(podStateProvider, "foo2", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  290. makeGCContainer(podStateProvider, "foo2", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),
  291. },
  292. remain: []int{0, 1, 3, 4, 6, 7},
  293. evictTerminatedPods: false,
  294. allSourcesReady: true,
  295. },
  296. {
  297. description: "max limit should apply and try to keep from every pod",
  298. containers: []containerTemplate{
  299. makeGCContainer(podStateProvider, "foo", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  300. makeGCContainer(podStateProvider, "foo", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),
  301. makeGCContainer(podStateProvider, "foo1", "bar1", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  302. makeGCContainer(podStateProvider, "foo1", "bar1", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),
  303. makeGCContainer(podStateProvider, "foo2", "bar2", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  304. makeGCContainer(podStateProvider, "foo2", "bar2", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),
  305. makeGCContainer(podStateProvider, "foo3", "bar3", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  306. makeGCContainer(podStateProvider, "foo3", "bar3", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),
  307. makeGCContainer(podStateProvider, "foo4", "bar4", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  308. makeGCContainer(podStateProvider, "foo4", "bar4", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),
  309. },
  310. remain: []int{0, 2, 4, 6, 8},
  311. evictTerminatedPods: false,
  312. allSourcesReady: true,
  313. },
  314. {
  315. description: "oldest pods should be removed if limit exceeded",
  316. containers: []containerTemplate{
  317. makeGCContainer(podStateProvider, "foo", "bar", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),
  318. makeGCContainer(podStateProvider, "foo", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  319. makeGCContainer(podStateProvider, "foo1", "bar1", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),
  320. makeGCContainer(podStateProvider, "foo1", "bar1", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  321. makeGCContainer(podStateProvider, "foo2", "bar2", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  322. makeGCContainer(podStateProvider, "foo3", "bar3", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),
  323. makeGCContainer(podStateProvider, "foo4", "bar4", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  324. makeGCContainer(podStateProvider, "foo5", "bar5", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),
  325. makeGCContainer(podStateProvider, "foo6", "bar6", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),
  326. makeGCContainer(podStateProvider, "foo7", "bar7", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  327. },
  328. remain: []int{0, 2, 4, 6, 8, 9},
  329. evictTerminatedPods: false,
  330. allSourcesReady: true,
  331. },
  332. {
  333. description: "all non-running containers should be removed when evictTerminatedPods is set",
  334. containers: []containerTemplate{
  335. makeGCContainer(podStateProvider, "foo", "bar", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),
  336. makeGCContainer(podStateProvider, "foo", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  337. makeGCContainer(podStateProvider, "foo1", "bar1", 2, 2, runtimeapi.ContainerState_CONTAINER_EXITED),
  338. makeGCContainer(podStateProvider, "foo1", "bar1", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  339. makeGCContainer(podStateProvider, "running", "bar2", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  340. makeGCContainer(podStateProvider, "foo3", "bar3", 0, 0, runtimeapi.ContainerState_CONTAINER_RUNNING),
  341. },
  342. remain: []int{4, 5},
  343. evictTerminatedPods: true,
  344. allSourcesReady: true,
  345. },
  346. {
  347. description: "containers for deleted pods should be removed",
  348. containers: []containerTemplate{
  349. makeGCContainer(podStateProvider, "foo", "bar", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  350. makeGCContainer(podStateProvider, "foo", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),
  351. // deleted pods still respect MinAge.
  352. makeGCContainer(podStateProvider, "deleted", "bar1", 2, time.Now().UnixNano(), runtimeapi.ContainerState_CONTAINER_EXITED),
  353. makeGCContainer(podStateProvider, "deleted", "bar1", 1, 1, runtimeapi.ContainerState_CONTAINER_EXITED),
  354. makeGCContainer(podStateProvider, "deleted", "bar1", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),
  355. },
  356. remain: []int{0, 1, 2},
  357. evictTerminatedPods: false,
  358. allSourcesReady: true,
  359. },
  360. {
  361. description: "containers for deleted pods may not be removed if allSourcesReady is set false ",
  362. containers: []containerTemplate{
  363. makeGCContainer(podStateProvider, "deleted", "bar1", 0, 0, runtimeapi.ContainerState_CONTAINER_EXITED),
  364. },
  365. remain: []int{0},
  366. evictTerminatedPods: true,
  367. allSourcesReady: false,
  368. },
  369. } {
  370. t.Logf("TestCase #%d: %+v", c, test)
  371. fakeContainers := makeFakeContainers(t, m, test.containers)
  372. fakeRuntime.SetFakeContainers(fakeContainers)
  373. if test.policy == nil {
  374. test.policy = &defaultGCPolicy
  375. }
  376. err := m.containerGC.evictContainers(*test.policy, test.allSourcesReady, test.evictTerminatedPods)
  377. assert.NoError(t, err)
  378. realRemain, err := fakeRuntime.ListContainers(nil)
  379. assert.NoError(t, err)
  380. assert.Len(t, realRemain, len(test.remain))
  381. for _, remain := range test.remain {
  382. status, err := fakeRuntime.ContainerStatus(fakeContainers[remain].Id)
  383. assert.NoError(t, err)
  384. assert.Equal(t, &fakeContainers[remain].ContainerStatus, status)
  385. }
  386. }
  387. }
  388. // Notice that legacy container symlink is not tested since it may be deprecated soon.
  389. func TestPodLogDirectoryGC(t *testing.T) {
  390. _, _, m, err := createTestRuntimeManager()
  391. assert.NoError(t, err)
  392. fakeOS := m.osInterface.(*containertest.FakeOS)
  393. podStateProvider := m.containerGC.podStateProvider.(*fakePodStateProvider)
  394. // pod log directories without corresponding pods should be removed.
  395. podStateProvider.existingPods["123"] = struct{}{}
  396. podStateProvider.existingPods["456"] = struct{}{}
  397. podStateProvider.existingPods["321"] = struct{}{}
  398. podStateProvider.runningPods["123"] = struct{}{}
  399. podStateProvider.runningPods["456"] = struct{}{}
  400. podStateProvider.existingPods["321"] = struct{}{}
  401. files := []string{"123", "456", "789", "012", "name_namespace_321", "name_namespace_654"}
  402. removed := []string{
  403. filepath.Join(podLogsRootDirectory, "789"),
  404. filepath.Join(podLogsRootDirectory, "012"),
  405. filepath.Join(podLogsRootDirectory, "name_namespace_654"),
  406. }
  407. ctrl := gomock.NewController(t)
  408. defer ctrl.Finish()
  409. fakeOS.ReadDirFn = func(string) ([]os.FileInfo, error) {
  410. var fileInfos []os.FileInfo
  411. for _, file := range files {
  412. mockFI := containertest.NewMockFileInfo(ctrl)
  413. mockFI.EXPECT().Name().Return(file)
  414. fileInfos = append(fileInfos, mockFI)
  415. }
  416. return fileInfos, nil
  417. }
  418. // allSourcesReady == true, pod log directories without corresponding pod should be removed.
  419. err = m.containerGC.evictPodLogsDirectories(true)
  420. assert.NoError(t, err)
  421. assert.Equal(t, removed, fakeOS.Removes)
  422. // allSourcesReady == false, pod log directories should not be removed.
  423. fakeOS.Removes = []string{}
  424. err = m.containerGC.evictPodLogsDirectories(false)
  425. assert.NoError(t, err)
  426. assert.Empty(t, fakeOS.Removes)
  427. }
  428. func TestUnknownStateContainerGC(t *testing.T) {
  429. fakeRuntime, _, m, err := createTestRuntimeManager()
  430. assert.NoError(t, err)
  431. podStateProvider := m.containerGC.podStateProvider.(*fakePodStateProvider)
  432. defaultGCPolicy := kubecontainer.ContainerGCPolicy{MinAge: time.Hour, MaxPerPodContainer: 0, MaxContainers: 0}
  433. fakeContainers := makeFakeContainers(t, m, []containerTemplate{
  434. makeGCContainer(podStateProvider, "foo", "bar", 0, 0, runtimeapi.ContainerState_CONTAINER_UNKNOWN),
  435. })
  436. fakeRuntime.SetFakeContainers(fakeContainers)
  437. err = m.containerGC.evictContainers(defaultGCPolicy, true, false)
  438. assert.NoError(t, err)
  439. assert.Contains(t, fakeRuntime.GetCalls(), "StopContainer", "RemoveContainer",
  440. "container in unknown state should be stopped before being removed")
  441. remain, err := fakeRuntime.ListContainers(nil)
  442. assert.NoError(t, err)
  443. assert.Empty(t, remain)
  444. }