kuberuntime_manager_test.go 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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. "path/filepath"
  16. "reflect"
  17. "sort"
  18. "testing"
  19. "time"
  20. cadvisorapi "github.com/google/cadvisor/info/v1"
  21. "github.com/stretchr/testify/assert"
  22. "github.com/stretchr/testify/require"
  23. "k8s.io/api/core/v1"
  24. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  25. "k8s.io/apimachinery/pkg/types"
  26. "k8s.io/apimachinery/pkg/util/sets"
  27. "k8s.io/client-go/util/flowcontrol"
  28. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  29. apitest "k8s.io/cri-api/pkg/apis/testing"
  30. "k8s.io/kubernetes/pkg/credentialprovider"
  31. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  32. containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
  33. )
  34. var (
  35. fakeCreatedAt int64 = 1
  36. )
  37. func createTestRuntimeManager() (*apitest.FakeRuntimeService, *apitest.FakeImageService, *kubeGenericRuntimeManager, error) {
  38. return customTestRuntimeManager(&credentialprovider.BasicDockerKeyring{})
  39. }
  40. func customTestRuntimeManager(keyring *credentialprovider.BasicDockerKeyring) (*apitest.FakeRuntimeService, *apitest.FakeImageService, *kubeGenericRuntimeManager, error) {
  41. fakeRuntimeService := apitest.NewFakeRuntimeService()
  42. fakeImageService := apitest.NewFakeImageService()
  43. // Only an empty machineInfo is needed here, because in unit test all containers are besteffort,
  44. // data in machineInfo is not used. If burstable containers are used in unit test in the future,
  45. // we may want to set memory capacity.
  46. machineInfo := &cadvisorapi.MachineInfo{}
  47. osInterface := &containertest.FakeOS{}
  48. manager, err := newFakeKubeRuntimeManager(fakeRuntimeService, fakeImageService, machineInfo, osInterface, &containertest.FakeRuntimeHelper{}, keyring)
  49. return fakeRuntimeService, fakeImageService, manager, err
  50. }
  51. // sandboxTemplate is a sandbox template to create fake sandbox.
  52. type sandboxTemplate struct {
  53. pod *v1.Pod
  54. attempt uint32
  55. createdAt int64
  56. state runtimeapi.PodSandboxState
  57. }
  58. // containerTemplate is a container template to create fake container.
  59. type containerTemplate struct {
  60. pod *v1.Pod
  61. container *v1.Container
  62. sandboxAttempt uint32
  63. attempt int
  64. createdAt int64
  65. state runtimeapi.ContainerState
  66. }
  67. // makeAndSetFakePod is a helper function to create and set one fake sandbox for a pod and
  68. // one fake container for each of its container.
  69. func makeAndSetFakePod(t *testing.T, m *kubeGenericRuntimeManager, fakeRuntime *apitest.FakeRuntimeService,
  70. pod *v1.Pod) (*apitest.FakePodSandbox, []*apitest.FakeContainer) {
  71. sandbox := makeFakePodSandbox(t, m, sandboxTemplate{
  72. pod: pod,
  73. createdAt: fakeCreatedAt,
  74. state: runtimeapi.PodSandboxState_SANDBOX_READY,
  75. })
  76. var containers []*apitest.FakeContainer
  77. newTemplate := func(c *v1.Container) containerTemplate {
  78. return containerTemplate{
  79. pod: pod,
  80. container: c,
  81. createdAt: fakeCreatedAt,
  82. state: runtimeapi.ContainerState_CONTAINER_RUNNING,
  83. }
  84. }
  85. for i := range pod.Spec.Containers {
  86. containers = append(containers, makeFakeContainer(t, m, newTemplate(&pod.Spec.Containers[i])))
  87. }
  88. for i := range pod.Spec.InitContainers {
  89. containers = append(containers, makeFakeContainer(t, m, newTemplate(&pod.Spec.InitContainers[i])))
  90. }
  91. fakeRuntime.SetFakeSandboxes([]*apitest.FakePodSandbox{sandbox})
  92. fakeRuntime.SetFakeContainers(containers)
  93. return sandbox, containers
  94. }
  95. // makeFakePodSandbox creates a fake pod sandbox based on a sandbox template.
  96. func makeFakePodSandbox(t *testing.T, m *kubeGenericRuntimeManager, template sandboxTemplate) *apitest.FakePodSandbox {
  97. config, err := m.generatePodSandboxConfig(template.pod, template.attempt)
  98. assert.NoError(t, err, "generatePodSandboxConfig for sandbox template %+v", template)
  99. podSandboxID := apitest.BuildSandboxName(config.Metadata)
  100. return &apitest.FakePodSandbox{
  101. PodSandboxStatus: runtimeapi.PodSandboxStatus{
  102. Id: podSandboxID,
  103. Metadata: config.Metadata,
  104. State: template.state,
  105. CreatedAt: template.createdAt,
  106. Network: &runtimeapi.PodSandboxNetworkStatus{
  107. Ip: apitest.FakePodSandboxIP,
  108. },
  109. Labels: config.Labels,
  110. },
  111. }
  112. }
  113. // makeFakePodSandboxes creates a group of fake pod sandboxes based on the sandbox templates.
  114. // The function guarantees the order of the fake pod sandboxes is the same with the templates.
  115. func makeFakePodSandboxes(t *testing.T, m *kubeGenericRuntimeManager, templates []sandboxTemplate) []*apitest.FakePodSandbox {
  116. var fakePodSandboxes []*apitest.FakePodSandbox
  117. for _, template := range templates {
  118. fakePodSandboxes = append(fakePodSandboxes, makeFakePodSandbox(t, m, template))
  119. }
  120. return fakePodSandboxes
  121. }
  122. // makeFakeContainer creates a fake container based on a container template.
  123. func makeFakeContainer(t *testing.T, m *kubeGenericRuntimeManager, template containerTemplate) *apitest.FakeContainer {
  124. sandboxConfig, err := m.generatePodSandboxConfig(template.pod, template.sandboxAttempt)
  125. assert.NoError(t, err, "generatePodSandboxConfig for container template %+v", template)
  126. containerConfig, _, err := m.generateContainerConfig(template.container, template.pod, template.attempt, "", template.container.Image)
  127. assert.NoError(t, err, "generateContainerConfig for container template %+v", template)
  128. podSandboxID := apitest.BuildSandboxName(sandboxConfig.Metadata)
  129. containerID := apitest.BuildContainerName(containerConfig.Metadata, podSandboxID)
  130. imageRef := containerConfig.Image.Image
  131. return &apitest.FakeContainer{
  132. ContainerStatus: runtimeapi.ContainerStatus{
  133. Id: containerID,
  134. Metadata: containerConfig.Metadata,
  135. Image: containerConfig.Image,
  136. ImageRef: imageRef,
  137. CreatedAt: template.createdAt,
  138. State: template.state,
  139. Labels: containerConfig.Labels,
  140. Annotations: containerConfig.Annotations,
  141. LogPath: filepath.Join(sandboxConfig.GetLogDirectory(), containerConfig.GetLogPath()),
  142. },
  143. SandboxID: podSandboxID,
  144. }
  145. }
  146. // makeFakeContainers creates a group of fake containers based on the container templates.
  147. // The function guarantees the order of the fake containers is the same with the templates.
  148. func makeFakeContainers(t *testing.T, m *kubeGenericRuntimeManager, templates []containerTemplate) []*apitest.FakeContainer {
  149. var fakeContainers []*apitest.FakeContainer
  150. for _, template := range templates {
  151. fakeContainers = append(fakeContainers, makeFakeContainer(t, m, template))
  152. }
  153. return fakeContainers
  154. }
  155. // makeTestContainer creates a test api container.
  156. func makeTestContainer(name, image string) v1.Container {
  157. return v1.Container{
  158. Name: name,
  159. Image: image,
  160. }
  161. }
  162. // makeTestPod creates a test api pod.
  163. func makeTestPod(podName, podNamespace, podUID string, containers []v1.Container) *v1.Pod {
  164. return &v1.Pod{
  165. ObjectMeta: metav1.ObjectMeta{
  166. UID: types.UID(podUID),
  167. Name: podName,
  168. Namespace: podNamespace,
  169. },
  170. Spec: v1.PodSpec{
  171. Containers: containers,
  172. },
  173. }
  174. }
  175. // verifyPods returns true if the two pod slices are equal.
  176. func verifyPods(a, b []*kubecontainer.Pod) bool {
  177. if len(a) != len(b) {
  178. return false
  179. }
  180. // Sort the containers within a pod.
  181. for i := range a {
  182. sort.Sort(containersByID(a[i].Containers))
  183. }
  184. for i := range b {
  185. sort.Sort(containersByID(b[i].Containers))
  186. }
  187. // Sort the pods by UID.
  188. sort.Sort(podsByID(a))
  189. sort.Sort(podsByID(b))
  190. return reflect.DeepEqual(a, b)
  191. }
  192. func verifyFakeContainerList(fakeRuntime *apitest.FakeRuntimeService, expected sets.String) (sets.String, bool) {
  193. actual := sets.NewString()
  194. for _, c := range fakeRuntime.Containers {
  195. actual.Insert(c.Id)
  196. }
  197. return actual, actual.Equal(expected)
  198. }
  199. // Only extract the fields of interests.
  200. type cRecord struct {
  201. name string
  202. attempt uint32
  203. state runtimeapi.ContainerState
  204. }
  205. type cRecordList []*cRecord
  206. func (b cRecordList) Len() int { return len(b) }
  207. func (b cRecordList) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
  208. func (b cRecordList) Less(i, j int) bool {
  209. if b[i].name != b[j].name {
  210. return b[i].name < b[j].name
  211. }
  212. return b[i].attempt < b[j].attempt
  213. }
  214. func verifyContainerStatuses(t *testing.T, runtime *apitest.FakeRuntimeService, expected []*cRecord, desc string) {
  215. actual := []*cRecord{}
  216. for _, cStatus := range runtime.Containers {
  217. actual = append(actual, &cRecord{name: cStatus.Metadata.Name, attempt: cStatus.Metadata.Attempt, state: cStatus.State})
  218. }
  219. sort.Sort(cRecordList(expected))
  220. sort.Sort(cRecordList(actual))
  221. assert.Equal(t, expected, actual, desc)
  222. }
  223. func TestNewKubeRuntimeManager(t *testing.T) {
  224. _, _, _, err := createTestRuntimeManager()
  225. assert.NoError(t, err)
  226. }
  227. func TestVersion(t *testing.T) {
  228. _, _, m, err := createTestRuntimeManager()
  229. assert.NoError(t, err)
  230. version, err := m.Version()
  231. assert.NoError(t, err)
  232. assert.Equal(t, kubeRuntimeAPIVersion, version.String())
  233. }
  234. func TestContainerRuntimeType(t *testing.T) {
  235. _, _, m, err := createTestRuntimeManager()
  236. assert.NoError(t, err)
  237. runtimeType := m.Type()
  238. assert.Equal(t, apitest.FakeRuntimeName, runtimeType)
  239. }
  240. func TestGetPodStatus(t *testing.T) {
  241. fakeRuntime, _, m, err := createTestRuntimeManager()
  242. assert.NoError(t, err)
  243. containers := []v1.Container{
  244. {
  245. Name: "foo1",
  246. Image: "busybox",
  247. ImagePullPolicy: v1.PullIfNotPresent,
  248. },
  249. {
  250. Name: "foo2",
  251. Image: "busybox",
  252. ImagePullPolicy: v1.PullIfNotPresent,
  253. },
  254. }
  255. pod := &v1.Pod{
  256. ObjectMeta: metav1.ObjectMeta{
  257. UID: "12345678",
  258. Name: "foo",
  259. Namespace: "new",
  260. },
  261. Spec: v1.PodSpec{
  262. Containers: containers,
  263. },
  264. }
  265. // Set fake sandbox and faked containers to fakeRuntime.
  266. makeAndSetFakePod(t, m, fakeRuntime, pod)
  267. podStatus, err := m.GetPodStatus(pod.UID, pod.Name, pod.Namespace)
  268. assert.NoError(t, err)
  269. assert.Equal(t, pod.UID, podStatus.ID)
  270. assert.Equal(t, pod.Name, podStatus.Name)
  271. assert.Equal(t, pod.Namespace, podStatus.Namespace)
  272. assert.Equal(t, apitest.FakePodSandboxIP, podStatus.IP)
  273. }
  274. func TestGetPods(t *testing.T) {
  275. fakeRuntime, _, m, err := createTestRuntimeManager()
  276. assert.NoError(t, err)
  277. pod := &v1.Pod{
  278. ObjectMeta: metav1.ObjectMeta{
  279. UID: "12345678",
  280. Name: "foo",
  281. Namespace: "new",
  282. },
  283. Spec: v1.PodSpec{
  284. Containers: []v1.Container{
  285. {
  286. Name: "foo1",
  287. Image: "busybox",
  288. },
  289. {
  290. Name: "foo2",
  291. Image: "busybox",
  292. },
  293. },
  294. },
  295. }
  296. // Set fake sandbox and fake containers to fakeRuntime.
  297. fakeSandbox, fakeContainers := makeAndSetFakePod(t, m, fakeRuntime, pod)
  298. // Convert the fakeContainers to kubecontainer.Container
  299. containers := make([]*kubecontainer.Container, len(fakeContainers))
  300. for i := range containers {
  301. fakeContainer := fakeContainers[i]
  302. c, err := m.toKubeContainer(&runtimeapi.Container{
  303. Id: fakeContainer.Id,
  304. Metadata: fakeContainer.Metadata,
  305. State: fakeContainer.State,
  306. Image: fakeContainer.Image,
  307. ImageRef: fakeContainer.ImageRef,
  308. Labels: fakeContainer.Labels,
  309. Annotations: fakeContainer.Annotations,
  310. })
  311. if err != nil {
  312. t.Fatalf("unexpected error %v", err)
  313. }
  314. containers[i] = c
  315. }
  316. // Convert fakeSandbox to kubecontainer.Container
  317. sandbox, err := m.sandboxToKubeContainer(&runtimeapi.PodSandbox{
  318. Id: fakeSandbox.Id,
  319. Metadata: fakeSandbox.Metadata,
  320. State: fakeSandbox.State,
  321. CreatedAt: fakeSandbox.CreatedAt,
  322. Labels: fakeSandbox.Labels,
  323. Annotations: fakeSandbox.Annotations,
  324. })
  325. if err != nil {
  326. t.Fatalf("unexpected error %v", err)
  327. }
  328. expected := []*kubecontainer.Pod{
  329. {
  330. ID: types.UID("12345678"),
  331. Name: "foo",
  332. Namespace: "new",
  333. Containers: []*kubecontainer.Container{containers[0], containers[1]},
  334. Sandboxes: []*kubecontainer.Container{sandbox},
  335. },
  336. }
  337. actual, err := m.GetPods(false)
  338. assert.NoError(t, err)
  339. if !verifyPods(expected, actual) {
  340. t.Errorf("expected %#v, got %#v", expected, actual)
  341. }
  342. }
  343. func TestKillPod(t *testing.T) {
  344. fakeRuntime, _, m, err := createTestRuntimeManager()
  345. assert.NoError(t, err)
  346. pod := &v1.Pod{
  347. ObjectMeta: metav1.ObjectMeta{
  348. UID: "12345678",
  349. Name: "foo",
  350. Namespace: "new",
  351. },
  352. Spec: v1.PodSpec{
  353. Containers: []v1.Container{
  354. {
  355. Name: "foo1",
  356. Image: "busybox",
  357. },
  358. {
  359. Name: "foo2",
  360. Image: "busybox",
  361. },
  362. },
  363. },
  364. }
  365. // Set fake sandbox and fake containers to fakeRuntime.
  366. fakeSandbox, fakeContainers := makeAndSetFakePod(t, m, fakeRuntime, pod)
  367. // Convert the fakeContainers to kubecontainer.Container
  368. containers := make([]*kubecontainer.Container, len(fakeContainers))
  369. for i := range containers {
  370. fakeContainer := fakeContainers[i]
  371. c, err := m.toKubeContainer(&runtimeapi.Container{
  372. Id: fakeContainer.Id,
  373. Metadata: fakeContainer.Metadata,
  374. State: fakeContainer.State,
  375. Image: fakeContainer.Image,
  376. ImageRef: fakeContainer.ImageRef,
  377. Labels: fakeContainer.Labels,
  378. })
  379. if err != nil {
  380. t.Fatalf("unexpected error %v", err)
  381. }
  382. containers[i] = c
  383. }
  384. runningPod := kubecontainer.Pod{
  385. ID: pod.UID,
  386. Name: pod.Name,
  387. Namespace: pod.Namespace,
  388. Containers: []*kubecontainer.Container{containers[0], containers[1]},
  389. Sandboxes: []*kubecontainer.Container{
  390. {
  391. ID: kubecontainer.ContainerID{
  392. ID: fakeSandbox.Id,
  393. Type: apitest.FakeRuntimeName,
  394. },
  395. },
  396. },
  397. }
  398. err = m.KillPod(pod, runningPod, nil)
  399. assert.NoError(t, err)
  400. assert.Equal(t, 2, len(fakeRuntime.Containers))
  401. assert.Equal(t, 1, len(fakeRuntime.Sandboxes))
  402. for _, sandbox := range fakeRuntime.Sandboxes {
  403. assert.Equal(t, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, sandbox.State)
  404. }
  405. for _, c := range fakeRuntime.Containers {
  406. assert.Equal(t, runtimeapi.ContainerState_CONTAINER_EXITED, c.State)
  407. }
  408. }
  409. func TestSyncPod(t *testing.T) {
  410. fakeRuntime, fakeImage, m, err := createTestRuntimeManager()
  411. assert.NoError(t, err)
  412. containers := []v1.Container{
  413. {
  414. Name: "foo1",
  415. Image: "busybox",
  416. ImagePullPolicy: v1.PullIfNotPresent,
  417. },
  418. {
  419. Name: "foo2",
  420. Image: "alpine",
  421. ImagePullPolicy: v1.PullIfNotPresent,
  422. },
  423. }
  424. pod := &v1.Pod{
  425. ObjectMeta: metav1.ObjectMeta{
  426. UID: "12345678",
  427. Name: "foo",
  428. Namespace: "new",
  429. },
  430. Spec: v1.PodSpec{
  431. Containers: containers,
  432. },
  433. }
  434. backOff := flowcontrol.NewBackOff(time.Second, time.Minute)
  435. result := m.SyncPod(pod, &kubecontainer.PodStatus{}, []v1.Secret{}, backOff)
  436. assert.NoError(t, result.Error())
  437. assert.Equal(t, 2, len(fakeRuntime.Containers))
  438. assert.Equal(t, 2, len(fakeImage.Images))
  439. assert.Equal(t, 1, len(fakeRuntime.Sandboxes))
  440. for _, sandbox := range fakeRuntime.Sandboxes {
  441. assert.Equal(t, runtimeapi.PodSandboxState_SANDBOX_READY, sandbox.State)
  442. }
  443. for _, c := range fakeRuntime.Containers {
  444. assert.Equal(t, runtimeapi.ContainerState_CONTAINER_RUNNING, c.State)
  445. }
  446. }
  447. func TestPruneInitContainers(t *testing.T) {
  448. fakeRuntime, _, m, err := createTestRuntimeManager()
  449. assert.NoError(t, err)
  450. init1 := makeTestContainer("init1", "busybox")
  451. init2 := makeTestContainer("init2", "busybox")
  452. pod := &v1.Pod{
  453. ObjectMeta: metav1.ObjectMeta{
  454. UID: "12345678",
  455. Name: "foo",
  456. Namespace: "new",
  457. },
  458. Spec: v1.PodSpec{
  459. InitContainers: []v1.Container{init1, init2},
  460. },
  461. }
  462. templates := []containerTemplate{
  463. {pod: pod, container: &init1, attempt: 3, createdAt: 3, state: runtimeapi.ContainerState_CONTAINER_EXITED},
  464. {pod: pod, container: &init1, attempt: 2, createdAt: 2, state: runtimeapi.ContainerState_CONTAINER_EXITED},
  465. {pod: pod, container: &init2, attempt: 1, createdAt: 1, state: runtimeapi.ContainerState_CONTAINER_EXITED},
  466. {pod: pod, container: &init1, attempt: 1, createdAt: 1, state: runtimeapi.ContainerState_CONTAINER_UNKNOWN},
  467. {pod: pod, container: &init2, attempt: 0, createdAt: 0, state: runtimeapi.ContainerState_CONTAINER_EXITED},
  468. {pod: pod, container: &init1, attempt: 0, createdAt: 0, state: runtimeapi.ContainerState_CONTAINER_EXITED},
  469. }
  470. fakes := makeFakeContainers(t, m, templates)
  471. fakeRuntime.SetFakeContainers(fakes)
  472. podStatus, err := m.GetPodStatus(pod.UID, pod.Name, pod.Namespace)
  473. assert.NoError(t, err)
  474. m.pruneInitContainersBeforeStart(pod, podStatus)
  475. expectedContainers := sets.NewString(fakes[0].Id, fakes[2].Id)
  476. if actual, ok := verifyFakeContainerList(fakeRuntime, expectedContainers); !ok {
  477. t.Errorf("expected %v, got %v", expectedContainers, actual)
  478. }
  479. }
  480. func TestSyncPodWithInitContainers(t *testing.T) {
  481. fakeRuntime, _, m, err := createTestRuntimeManager()
  482. assert.NoError(t, err)
  483. initContainers := []v1.Container{
  484. {
  485. Name: "init1",
  486. Image: "init",
  487. ImagePullPolicy: v1.PullIfNotPresent,
  488. },
  489. }
  490. containers := []v1.Container{
  491. {
  492. Name: "foo1",
  493. Image: "busybox",
  494. ImagePullPolicy: v1.PullIfNotPresent,
  495. },
  496. {
  497. Name: "foo2",
  498. Image: "alpine",
  499. ImagePullPolicy: v1.PullIfNotPresent,
  500. },
  501. }
  502. pod := &v1.Pod{
  503. ObjectMeta: metav1.ObjectMeta{
  504. UID: "12345678",
  505. Name: "foo",
  506. Namespace: "new",
  507. },
  508. Spec: v1.PodSpec{
  509. Containers: containers,
  510. InitContainers: initContainers,
  511. },
  512. }
  513. backOff := flowcontrol.NewBackOff(time.Second, time.Minute)
  514. // 1. should only create the init container.
  515. podStatus, err := m.GetPodStatus(pod.UID, pod.Name, pod.Namespace)
  516. assert.NoError(t, err)
  517. result := m.SyncPod(pod, podStatus, []v1.Secret{}, backOff)
  518. assert.NoError(t, result.Error())
  519. expected := []*cRecord{
  520. {name: initContainers[0].Name, attempt: 0, state: runtimeapi.ContainerState_CONTAINER_RUNNING},
  521. }
  522. verifyContainerStatuses(t, fakeRuntime, expected, "start only the init container")
  523. // 2. should not create app container because init container is still running.
  524. podStatus, err = m.GetPodStatus(pod.UID, pod.Name, pod.Namespace)
  525. assert.NoError(t, err)
  526. result = m.SyncPod(pod, podStatus, []v1.Secret{}, backOff)
  527. assert.NoError(t, result.Error())
  528. verifyContainerStatuses(t, fakeRuntime, expected, "init container still running; do nothing")
  529. // 3. should create all app containers because init container finished.
  530. // Stop init container instance 0.
  531. sandboxIDs, err := m.getSandboxIDByPodUID(pod.UID, nil)
  532. require.NoError(t, err)
  533. sandboxID := sandboxIDs[0]
  534. initID0, err := fakeRuntime.GetContainerID(sandboxID, initContainers[0].Name, 0)
  535. require.NoError(t, err)
  536. fakeRuntime.StopContainer(initID0, 0)
  537. // Sync again.
  538. podStatus, err = m.GetPodStatus(pod.UID, pod.Name, pod.Namespace)
  539. assert.NoError(t, err)
  540. result = m.SyncPod(pod, podStatus, []v1.Secret{}, backOff)
  541. assert.NoError(t, result.Error())
  542. expected = []*cRecord{
  543. {name: initContainers[0].Name, attempt: 0, state: runtimeapi.ContainerState_CONTAINER_EXITED},
  544. {name: containers[0].Name, attempt: 0, state: runtimeapi.ContainerState_CONTAINER_RUNNING},
  545. {name: containers[1].Name, attempt: 0, state: runtimeapi.ContainerState_CONTAINER_RUNNING},
  546. }
  547. verifyContainerStatuses(t, fakeRuntime, expected, "init container completed; all app containers should be running")
  548. // 4. should restart the init container if needed to create a new podsandbox
  549. // Stop the pod sandbox.
  550. fakeRuntime.StopPodSandbox(sandboxID)
  551. // Sync again.
  552. podStatus, err = m.GetPodStatus(pod.UID, pod.Name, pod.Namespace)
  553. assert.NoError(t, err)
  554. result = m.SyncPod(pod, podStatus, []v1.Secret{}, backOff)
  555. assert.NoError(t, result.Error())
  556. expected = []*cRecord{
  557. // The first init container instance is purged and no longer visible.
  558. // The second (attempt == 1) instance has been started and is running.
  559. {name: initContainers[0].Name, attempt: 1, state: runtimeapi.ContainerState_CONTAINER_RUNNING},
  560. // All containers are killed.
  561. {name: containers[0].Name, attempt: 0, state: runtimeapi.ContainerState_CONTAINER_EXITED},
  562. {name: containers[1].Name, attempt: 0, state: runtimeapi.ContainerState_CONTAINER_EXITED},
  563. }
  564. verifyContainerStatuses(t, fakeRuntime, expected, "kill all app containers, purge the existing init container, and restart a new one")
  565. }
  566. // A helper function to get a basic pod and its status assuming all sandbox and
  567. // containers are running and ready.
  568. func makeBasePodAndStatus() (*v1.Pod, *kubecontainer.PodStatus) {
  569. pod := &v1.Pod{
  570. ObjectMeta: metav1.ObjectMeta{
  571. UID: "12345678",
  572. Name: "foo",
  573. Namespace: "foo-ns",
  574. },
  575. Spec: v1.PodSpec{
  576. Containers: []v1.Container{
  577. {
  578. Name: "foo1",
  579. Image: "busybox",
  580. },
  581. {
  582. Name: "foo2",
  583. Image: "busybox",
  584. },
  585. {
  586. Name: "foo3",
  587. Image: "busybox",
  588. },
  589. },
  590. },
  591. }
  592. status := &kubecontainer.PodStatus{
  593. ID: pod.UID,
  594. Name: pod.Name,
  595. Namespace: pod.Namespace,
  596. SandboxStatuses: []*runtimeapi.PodSandboxStatus{
  597. {
  598. Id: "sandboxID",
  599. State: runtimeapi.PodSandboxState_SANDBOX_READY,
  600. Metadata: &runtimeapi.PodSandboxMetadata{Name: pod.Name, Namespace: pod.Namespace, Uid: "sandboxuid", Attempt: uint32(0)},
  601. Network: &runtimeapi.PodSandboxNetworkStatus{Ip: "10.0.0.1"},
  602. },
  603. },
  604. ContainerStatuses: []*kubecontainer.ContainerStatus{
  605. {
  606. ID: kubecontainer.ContainerID{ID: "id1"},
  607. Name: "foo1", State: kubecontainer.ContainerStateRunning,
  608. Hash: kubecontainer.HashContainer(&pod.Spec.Containers[0]),
  609. },
  610. {
  611. ID: kubecontainer.ContainerID{ID: "id2"},
  612. Name: "foo2", State: kubecontainer.ContainerStateRunning,
  613. Hash: kubecontainer.HashContainer(&pod.Spec.Containers[1]),
  614. },
  615. {
  616. ID: kubecontainer.ContainerID{ID: "id3"},
  617. Name: "foo3", State: kubecontainer.ContainerStateRunning,
  618. Hash: kubecontainer.HashContainer(&pod.Spec.Containers[2]),
  619. },
  620. },
  621. }
  622. return pod, status
  623. }
  624. func TestComputePodActions(t *testing.T) {
  625. _, _, m, err := createTestRuntimeManager()
  626. require.NoError(t, err)
  627. // Createing a pair reference pod and status for the test cases to refer
  628. // the specific fields.
  629. basePod, baseStatus := makeBasePodAndStatus()
  630. noAction := podActions{
  631. SandboxID: baseStatus.SandboxStatuses[0].Id,
  632. ContainersToStart: []int{},
  633. ContainersToKill: map[kubecontainer.ContainerID]containerToKillInfo{},
  634. }
  635. for desc, test := range map[string]struct {
  636. mutatePodFn func(*v1.Pod)
  637. mutateStatusFn func(*kubecontainer.PodStatus)
  638. actions podActions
  639. }{
  640. "everying is good; do nothing": {
  641. actions: noAction,
  642. },
  643. "start pod sandbox and all containers for a new pod": {
  644. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  645. // No container or sandbox exists.
  646. status.SandboxStatuses = []*runtimeapi.PodSandboxStatus{}
  647. status.ContainerStatuses = []*kubecontainer.ContainerStatus{}
  648. },
  649. actions: podActions{
  650. KillPod: true,
  651. CreateSandbox: true,
  652. Attempt: uint32(0),
  653. ContainersToStart: []int{0, 1, 2},
  654. ContainersToKill: getKillMap(basePod, baseStatus, []int{}),
  655. },
  656. },
  657. "restart exited containers if RestartPolicy == Always": {
  658. mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyAlways },
  659. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  660. // The first container completed, restart it,
  661. status.ContainerStatuses[0].State = kubecontainer.ContainerStateExited
  662. status.ContainerStatuses[0].ExitCode = 0
  663. // The second container exited with failure, restart it,
  664. status.ContainerStatuses[1].State = kubecontainer.ContainerStateExited
  665. status.ContainerStatuses[1].ExitCode = 111
  666. },
  667. actions: podActions{
  668. SandboxID: baseStatus.SandboxStatuses[0].Id,
  669. ContainersToStart: []int{0, 1},
  670. ContainersToKill: getKillMap(basePod, baseStatus, []int{}),
  671. },
  672. },
  673. "restart failed containers if RestartPolicy == OnFailure": {
  674. mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyOnFailure },
  675. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  676. // The first container completed, don't restart it,
  677. status.ContainerStatuses[0].State = kubecontainer.ContainerStateExited
  678. status.ContainerStatuses[0].ExitCode = 0
  679. // The second container exited with failure, restart it,
  680. status.ContainerStatuses[1].State = kubecontainer.ContainerStateExited
  681. status.ContainerStatuses[1].ExitCode = 111
  682. },
  683. actions: podActions{
  684. SandboxID: baseStatus.SandboxStatuses[0].Id,
  685. ContainersToStart: []int{1},
  686. ContainersToKill: getKillMap(basePod, baseStatus, []int{}),
  687. },
  688. },
  689. "don't restart containers if RestartPolicy == Never": {
  690. mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyNever },
  691. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  692. // Don't restart any containers.
  693. status.ContainerStatuses[0].State = kubecontainer.ContainerStateExited
  694. status.ContainerStatuses[0].ExitCode = 0
  695. status.ContainerStatuses[1].State = kubecontainer.ContainerStateExited
  696. status.ContainerStatuses[1].ExitCode = 111
  697. },
  698. actions: noAction,
  699. },
  700. "Kill pod and recreate everything if the pod sandbox is dead, and RestartPolicy == Always": {
  701. mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyAlways },
  702. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  703. status.SandboxStatuses[0].State = runtimeapi.PodSandboxState_SANDBOX_NOTREADY
  704. },
  705. actions: podActions{
  706. KillPod: true,
  707. CreateSandbox: true,
  708. SandboxID: baseStatus.SandboxStatuses[0].Id,
  709. Attempt: uint32(1),
  710. ContainersToStart: []int{0, 1, 2},
  711. ContainersToKill: getKillMap(basePod, baseStatus, []int{}),
  712. },
  713. },
  714. "Kill pod and recreate all containers (except for the succeeded one) if the pod sandbox is dead, and RestartPolicy == OnFailure": {
  715. mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyOnFailure },
  716. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  717. status.SandboxStatuses[0].State = runtimeapi.PodSandboxState_SANDBOX_NOTREADY
  718. status.ContainerStatuses[1].State = kubecontainer.ContainerStateExited
  719. status.ContainerStatuses[1].ExitCode = 0
  720. },
  721. actions: podActions{
  722. KillPod: true,
  723. CreateSandbox: true,
  724. SandboxID: baseStatus.SandboxStatuses[0].Id,
  725. Attempt: uint32(1),
  726. ContainersToStart: []int{0, 2},
  727. ContainersToKill: getKillMap(basePod, baseStatus, []int{}),
  728. },
  729. },
  730. "Kill pod and recreate all containers if the PodSandbox does not have an IP": {
  731. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  732. status.SandboxStatuses[0].Network.Ip = ""
  733. },
  734. actions: podActions{
  735. KillPod: true,
  736. CreateSandbox: true,
  737. SandboxID: baseStatus.SandboxStatuses[0].Id,
  738. Attempt: uint32(1),
  739. ContainersToStart: []int{0, 1, 2},
  740. ContainersToKill: getKillMap(basePod, baseStatus, []int{}),
  741. },
  742. },
  743. "Kill and recreate the container if the container's spec changed": {
  744. mutatePodFn: func(pod *v1.Pod) {
  745. pod.Spec.RestartPolicy = v1.RestartPolicyAlways
  746. },
  747. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  748. status.ContainerStatuses[1].Hash = uint64(432423432)
  749. },
  750. actions: podActions{
  751. SandboxID: baseStatus.SandboxStatuses[0].Id,
  752. ContainersToKill: getKillMap(basePod, baseStatus, []int{1}),
  753. ContainersToStart: []int{1},
  754. },
  755. // TODO: Add a test case for containers which failed the liveness
  756. // check. Will need to fake the livessness check result.
  757. },
  758. "Verify we do not create a pod sandbox if no ready sandbox for pod with RestartPolicy=Never and all containers exited": {
  759. mutatePodFn: func(pod *v1.Pod) {
  760. pod.Spec.RestartPolicy = v1.RestartPolicyNever
  761. },
  762. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  763. // no ready sandbox
  764. status.SandboxStatuses[0].State = runtimeapi.PodSandboxState_SANDBOX_NOTREADY
  765. status.SandboxStatuses[0].Metadata.Attempt = uint32(1)
  766. // all containers exited
  767. for i := range status.ContainerStatuses {
  768. status.ContainerStatuses[i].State = kubecontainer.ContainerStateExited
  769. status.ContainerStatuses[i].ExitCode = 0
  770. }
  771. },
  772. actions: podActions{
  773. SandboxID: baseStatus.SandboxStatuses[0].Id,
  774. Attempt: uint32(2),
  775. CreateSandbox: false,
  776. KillPod: true,
  777. ContainersToStart: []int{},
  778. ContainersToKill: map[kubecontainer.ContainerID]containerToKillInfo{},
  779. },
  780. },
  781. "Verify we create a pod sandbox if no ready sandbox for pod with RestartPolicy=Never and no containers have ever been created": {
  782. mutatePodFn: func(pod *v1.Pod) {
  783. pod.Spec.RestartPolicy = v1.RestartPolicyNever
  784. },
  785. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  786. // no ready sandbox
  787. status.SandboxStatuses[0].State = runtimeapi.PodSandboxState_SANDBOX_NOTREADY
  788. status.SandboxStatuses[0].Metadata.Attempt = uint32(2)
  789. // no visible containers
  790. status.ContainerStatuses = []*kubecontainer.ContainerStatus{}
  791. },
  792. actions: podActions{
  793. SandboxID: baseStatus.SandboxStatuses[0].Id,
  794. Attempt: uint32(3),
  795. CreateSandbox: true,
  796. KillPod: true,
  797. ContainersToStart: []int{0, 1, 2},
  798. ContainersToKill: map[kubecontainer.ContainerID]containerToKillInfo{},
  799. },
  800. },
  801. "Kill and recreate the container if the container is in unknown state": {
  802. mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyNever },
  803. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  804. status.ContainerStatuses[1].State = kubecontainer.ContainerStateUnknown
  805. },
  806. actions: podActions{
  807. SandboxID: baseStatus.SandboxStatuses[0].Id,
  808. ContainersToKill: getKillMap(basePod, baseStatus, []int{1}),
  809. ContainersToStart: []int{1},
  810. },
  811. },
  812. } {
  813. pod, status := makeBasePodAndStatus()
  814. if test.mutatePodFn != nil {
  815. test.mutatePodFn(pod)
  816. }
  817. if test.mutateStatusFn != nil {
  818. test.mutateStatusFn(status)
  819. }
  820. actions := m.computePodActions(pod, status)
  821. verifyActions(t, &test.actions, &actions, desc)
  822. }
  823. }
  824. func getKillMap(pod *v1.Pod, status *kubecontainer.PodStatus, cIndexes []int) map[kubecontainer.ContainerID]containerToKillInfo {
  825. m := map[kubecontainer.ContainerID]containerToKillInfo{}
  826. for _, i := range cIndexes {
  827. m[status.ContainerStatuses[i].ID] = containerToKillInfo{
  828. container: &pod.Spec.Containers[i],
  829. name: pod.Spec.Containers[i].Name,
  830. }
  831. }
  832. return m
  833. }
  834. func getKillMapWithInitContainers(pod *v1.Pod, status *kubecontainer.PodStatus, cIndexes []int) map[kubecontainer.ContainerID]containerToKillInfo {
  835. m := map[kubecontainer.ContainerID]containerToKillInfo{}
  836. for _, i := range cIndexes {
  837. m[status.ContainerStatuses[i].ID] = containerToKillInfo{
  838. container: &pod.Spec.InitContainers[i],
  839. name: pod.Spec.InitContainers[i].Name,
  840. }
  841. }
  842. return m
  843. }
  844. func verifyActions(t *testing.T, expected, actual *podActions, desc string) {
  845. if actual.ContainersToKill != nil {
  846. // Clear the message field since we don't need to verify the message.
  847. for k, info := range actual.ContainersToKill {
  848. info.message = ""
  849. actual.ContainersToKill[k] = info
  850. }
  851. }
  852. assert.Equal(t, expected, actual, desc)
  853. }
  854. func TestComputePodActionsWithInitContainers(t *testing.T) {
  855. _, _, m, err := createTestRuntimeManager()
  856. require.NoError(t, err)
  857. // Createing a pair reference pod and status for the test cases to refer
  858. // the specific fields.
  859. basePod, baseStatus := makeBasePodAndStatusWithInitContainers()
  860. noAction := podActions{
  861. SandboxID: baseStatus.SandboxStatuses[0].Id,
  862. ContainersToStart: []int{},
  863. ContainersToKill: map[kubecontainer.ContainerID]containerToKillInfo{},
  864. }
  865. for desc, test := range map[string]struct {
  866. mutatePodFn func(*v1.Pod)
  867. mutateStatusFn func(*kubecontainer.PodStatus)
  868. actions podActions
  869. }{
  870. "initialization completed; start all containers": {
  871. actions: podActions{
  872. SandboxID: baseStatus.SandboxStatuses[0].Id,
  873. ContainersToStart: []int{0, 1, 2},
  874. ContainersToKill: getKillMapWithInitContainers(basePod, baseStatus, []int{}),
  875. },
  876. },
  877. "initialization in progress; do nothing": {
  878. mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyAlways },
  879. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  880. status.ContainerStatuses[2].State = kubecontainer.ContainerStateRunning
  881. },
  882. actions: noAction,
  883. },
  884. "Kill pod and restart the first init container if the pod sandbox is dead": {
  885. mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyAlways },
  886. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  887. status.SandboxStatuses[0].State = runtimeapi.PodSandboxState_SANDBOX_NOTREADY
  888. },
  889. actions: podActions{
  890. KillPod: true,
  891. CreateSandbox: true,
  892. SandboxID: baseStatus.SandboxStatuses[0].Id,
  893. Attempt: uint32(1),
  894. NextInitContainerToStart: &basePod.Spec.InitContainers[0],
  895. ContainersToStart: []int{},
  896. ContainersToKill: getKillMapWithInitContainers(basePod, baseStatus, []int{}),
  897. },
  898. },
  899. "initialization failed; restart the last init container if RestartPolicy == Always": {
  900. mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyAlways },
  901. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  902. status.ContainerStatuses[2].ExitCode = 137
  903. },
  904. actions: podActions{
  905. SandboxID: baseStatus.SandboxStatuses[0].Id,
  906. NextInitContainerToStart: &basePod.Spec.InitContainers[2],
  907. ContainersToStart: []int{},
  908. ContainersToKill: getKillMapWithInitContainers(basePod, baseStatus, []int{}),
  909. },
  910. },
  911. "initialization failed; restart the last init container if RestartPolicy == OnFailure": {
  912. mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyOnFailure },
  913. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  914. status.ContainerStatuses[2].ExitCode = 137
  915. },
  916. actions: podActions{
  917. SandboxID: baseStatus.SandboxStatuses[0].Id,
  918. NextInitContainerToStart: &basePod.Spec.InitContainers[2],
  919. ContainersToStart: []int{},
  920. ContainersToKill: getKillMapWithInitContainers(basePod, baseStatus, []int{}),
  921. },
  922. },
  923. "initialization failed; kill pod if RestartPolicy == Never": {
  924. mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyNever },
  925. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  926. status.ContainerStatuses[2].ExitCode = 137
  927. },
  928. actions: podActions{
  929. KillPod: true,
  930. SandboxID: baseStatus.SandboxStatuses[0].Id,
  931. ContainersToStart: []int{},
  932. ContainersToKill: getKillMapWithInitContainers(basePod, baseStatus, []int{}),
  933. },
  934. },
  935. "init container state unknown; kill and recreate the last init container if RestartPolicy == Always": {
  936. mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyAlways },
  937. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  938. status.ContainerStatuses[2].State = kubecontainer.ContainerStateUnknown
  939. },
  940. actions: podActions{
  941. SandboxID: baseStatus.SandboxStatuses[0].Id,
  942. NextInitContainerToStart: &basePod.Spec.InitContainers[2],
  943. ContainersToStart: []int{},
  944. ContainersToKill: getKillMapWithInitContainers(basePod, baseStatus, []int{2}),
  945. },
  946. },
  947. "init container state unknown; kill and recreate the last init container if RestartPolicy == OnFailure": {
  948. mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyOnFailure },
  949. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  950. status.ContainerStatuses[2].State = kubecontainer.ContainerStateUnknown
  951. },
  952. actions: podActions{
  953. SandboxID: baseStatus.SandboxStatuses[0].Id,
  954. NextInitContainerToStart: &basePod.Spec.InitContainers[2],
  955. ContainersToStart: []int{},
  956. ContainersToKill: getKillMapWithInitContainers(basePod, baseStatus, []int{2}),
  957. },
  958. },
  959. "init container state unknown; kill pod if RestartPolicy == Never": {
  960. mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyNever },
  961. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  962. status.ContainerStatuses[2].State = kubecontainer.ContainerStateUnknown
  963. },
  964. actions: podActions{
  965. KillPod: true,
  966. SandboxID: baseStatus.SandboxStatuses[0].Id,
  967. ContainersToStart: []int{},
  968. ContainersToKill: getKillMapWithInitContainers(basePod, baseStatus, []int{}),
  969. },
  970. },
  971. "Pod sandbox not ready, init container failed, but RestartPolicy == Never; kill pod only": {
  972. mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyNever },
  973. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  974. status.SandboxStatuses[0].State = runtimeapi.PodSandboxState_SANDBOX_NOTREADY
  975. },
  976. actions: podActions{
  977. KillPod: true,
  978. CreateSandbox: false,
  979. SandboxID: baseStatus.SandboxStatuses[0].Id,
  980. Attempt: uint32(1),
  981. ContainersToStart: []int{},
  982. ContainersToKill: getKillMapWithInitContainers(basePod, baseStatus, []int{}),
  983. },
  984. },
  985. "Pod sandbox not ready, and RestartPolicy == Never, but no visible init containers; create a new pod sandbox": {
  986. mutatePodFn: func(pod *v1.Pod) { pod.Spec.RestartPolicy = v1.RestartPolicyNever },
  987. mutateStatusFn: func(status *kubecontainer.PodStatus) {
  988. status.SandboxStatuses[0].State = runtimeapi.PodSandboxState_SANDBOX_NOTREADY
  989. status.ContainerStatuses = []*kubecontainer.ContainerStatus{}
  990. },
  991. actions: podActions{
  992. KillPod: true,
  993. CreateSandbox: true,
  994. SandboxID: baseStatus.SandboxStatuses[0].Id,
  995. Attempt: uint32(1),
  996. NextInitContainerToStart: &basePod.Spec.InitContainers[0],
  997. ContainersToStart: []int{},
  998. ContainersToKill: getKillMapWithInitContainers(basePod, baseStatus, []int{}),
  999. },
  1000. },
  1001. } {
  1002. pod, status := makeBasePodAndStatusWithInitContainers()
  1003. if test.mutatePodFn != nil {
  1004. test.mutatePodFn(pod)
  1005. }
  1006. if test.mutateStatusFn != nil {
  1007. test.mutateStatusFn(status)
  1008. }
  1009. actions := m.computePodActions(pod, status)
  1010. verifyActions(t, &test.actions, &actions, desc)
  1011. }
  1012. }
  1013. func makeBasePodAndStatusWithInitContainers() (*v1.Pod, *kubecontainer.PodStatus) {
  1014. pod, status := makeBasePodAndStatus()
  1015. pod.Spec.InitContainers = []v1.Container{
  1016. {
  1017. Name: "init1",
  1018. Image: "bar-image",
  1019. },
  1020. {
  1021. Name: "init2",
  1022. Image: "bar-image",
  1023. },
  1024. {
  1025. Name: "init3",
  1026. Image: "bar-image",
  1027. },
  1028. }
  1029. // Replace the original statuses of the containers with those for the init
  1030. // containers.
  1031. status.ContainerStatuses = []*kubecontainer.ContainerStatus{
  1032. {
  1033. ID: kubecontainer.ContainerID{ID: "initid1"},
  1034. Name: "init1", State: kubecontainer.ContainerStateExited,
  1035. Hash: kubecontainer.HashContainer(&pod.Spec.InitContainers[0]),
  1036. },
  1037. {
  1038. ID: kubecontainer.ContainerID{ID: "initid2"},
  1039. Name: "init2", State: kubecontainer.ContainerStateExited,
  1040. Hash: kubecontainer.HashContainer(&pod.Spec.InitContainers[0]),
  1041. },
  1042. {
  1043. ID: kubecontainer.ContainerID{ID: "initid3"},
  1044. Name: "init3", State: kubecontainer.ContainerStateExited,
  1045. Hash: kubecontainer.HashContainer(&pod.Spec.InitContainers[0]),
  1046. },
  1047. }
  1048. return pod, status
  1049. }