github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/test/e2e/generate_kube_test.go (about) 1 package integration 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "strconv" 8 "strings" 9 10 "github.com/hanks177/podman/v4/libpod/define" 11 12 v1 "github.com/hanks177/podman/v4/pkg/k8s.io/api/core/v1" 13 "github.com/hanks177/podman/v4/pkg/util" 14 . "github.com/hanks177/podman/v4/test/utils" 15 "github.com/ghodss/yaml" 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 . "github.com/onsi/gomega/gexec" 19 ) 20 21 var _ = Describe("Podman generate kube", func() { 22 var ( 23 tempdir string 24 err error 25 podmanTest *PodmanTestIntegration 26 ) 27 28 BeforeEach(func() { 29 tempdir, err = CreateTempDirInTempDir() 30 if err != nil { 31 os.Exit(1) 32 } 33 podmanTest = PodmanTestCreate(tempdir) 34 podmanTest.Setup() 35 }) 36 37 AfterEach(func() { 38 podmanTest.Cleanup() 39 f := CurrentGinkgoTestDescription() 40 processTestResult(f) 41 42 }) 43 44 It("podman generate pod kube on bogus object", func() { 45 session := podmanTest.Podman([]string{"generate", "kube", "foobar"}) 46 session.WaitWithDefaultTimeout() 47 Expect(session).To(ExitWithError()) 48 }) 49 50 It("podman generate service kube on bogus object", func() { 51 session := podmanTest.Podman([]string{"generate", "kube", "-s", "foobar"}) 52 session.WaitWithDefaultTimeout() 53 Expect(session).To(ExitWithError()) 54 }) 55 56 It("podman generate kube on container", func() { 57 session := podmanTest.RunTopContainer("top") 58 session.WaitWithDefaultTimeout() 59 Expect(session).Should(Exit(0)) 60 61 kube := podmanTest.Podman([]string{"generate", "kube", "top"}) 62 kube.WaitWithDefaultTimeout() 63 Expect(kube).Should(Exit(0)) 64 65 pod := new(v1.Pod) 66 err := yaml.Unmarshal(kube.Out.Contents(), pod) 67 Expect(err).To(BeNil()) 68 Expect(pod.Spec).To(HaveField("HostNetwork", false)) 69 Expect(pod.Spec.SecurityContext).To(BeNil()) 70 Expect(pod.Spec.DNSConfig).To(BeNil()) 71 Expect(pod.Spec.Containers[0]).To(HaveField("WorkingDir", "")) 72 Expect(pod.Spec.Containers[0].Env).To(BeNil()) 73 Expect(pod).To(HaveField("Name", "top-pod")) 74 75 numContainers := 0 76 for range pod.Spec.Containers { 77 numContainers++ 78 } 79 Expect(numContainers).To(Equal(1)) 80 }) 81 82 It("podman generate service kube on container with --security-opt level", func() { 83 session := podmanTest.Podman([]string{"create", "--name", "test", "--security-opt", "label=level:s0:c100,c200", "alpine"}) 84 session.WaitWithDefaultTimeout() 85 Expect(session).Should(Exit(0)) 86 87 kube := podmanTest.Podman([]string{"generate", "kube", "test"}) 88 kube.WaitWithDefaultTimeout() 89 Expect(kube).Should(Exit(0)) 90 91 pod := new(v1.Pod) 92 err := yaml.Unmarshal(kube.Out.Contents(), pod) 93 Expect(err).To(BeNil()) 94 Expect(kube.OutputToString()).To(ContainSubstring("level: s0:c100,c200")) 95 }) 96 97 It("podman generate service kube on container with --security-opt disable", func() { 98 session := podmanTest.Podman([]string{"create", "--name", "test-disable", "--security-opt", "label=disable", "alpine"}) 99 session.WaitWithDefaultTimeout() 100 Expect(session).Should(Exit(0)) 101 102 kube := podmanTest.Podman([]string{"generate", "kube", "test-disable"}) 103 kube.WaitWithDefaultTimeout() 104 Expect(kube).Should(Exit(0)) 105 106 pod := new(v1.Pod) 107 err = yaml.Unmarshal(kube.Out.Contents(), pod) 108 Expect(err).To(BeNil()) 109 Expect(kube.OutputToString()).To(ContainSubstring("type: spc_t")) 110 111 }) 112 113 It("podman generate service kube on container with --security-opt type", func() { 114 session := podmanTest.Podman([]string{"create", "--name", "test", "--security-opt", "label=type:foo_bar_t", "alpine"}) 115 session.WaitWithDefaultTimeout() 116 Expect(session).Should(Exit(0)) 117 118 kube := podmanTest.Podman([]string{"generate", "kube", "test"}) 119 kube.WaitWithDefaultTimeout() 120 Expect(kube).Should(Exit(0)) 121 122 pod := new(v1.Pod) 123 err = yaml.Unmarshal(kube.Out.Contents(), pod) 124 Expect(err).To(BeNil()) 125 Expect(kube.OutputToString()).To(ContainSubstring("type: foo_bar_t")) 126 }) 127 128 It("podman generate service kube on container - targetPort should match port name", func() { 129 session := podmanTest.Podman([]string{"create", "--name", "test-ctr", "-p", "3890:3890", ALPINE, "ls"}) 130 session.WaitWithDefaultTimeout() 131 Expect(session).Should(Exit(0)) 132 133 kube := podmanTest.Podman([]string{"generate", "kube", "-s", "test-ctr"}) 134 kube.WaitWithDefaultTimeout() 135 Expect(kube).Should(Exit(0)) 136 137 // Separate out the Service and Pod yaml 138 arr := strings.Split(string(kube.Out.Contents()), "---") 139 Expect(arr).To(HaveLen(2)) 140 141 svc := new(v1.Service) 142 err := yaml.Unmarshal([]byte(arr[0]), svc) 143 Expect(err).To(BeNil()) 144 Expect(svc.Spec.Ports).To(HaveLen(1)) 145 Expect(svc.Spec.Ports[0].TargetPort.IntValue()).To(Equal(3890)) 146 147 pod := new(v1.Pod) 148 err = yaml.Unmarshal([]byte(arr[1]), pod) 149 Expect(err).To(BeNil()) 150 }) 151 152 It("podman generate kube on pod", func() { 153 _, rc, _ := podmanTest.CreatePod(map[string][]string{"--name": {"toppod"}}) 154 Expect(rc).To(Equal(0)) 155 156 session := podmanTest.RunTopContainerInPod("topcontainer", "toppod") 157 session.WaitWithDefaultTimeout() 158 Expect(session).Should(Exit(0)) 159 160 kube := podmanTest.Podman([]string{"generate", "kube", "toppod"}) 161 kube.WaitWithDefaultTimeout() 162 Expect(kube).Should(Exit(0)) 163 164 pod := new(v1.Pod) 165 err := yaml.Unmarshal(kube.Out.Contents(), pod) 166 Expect(err).To(BeNil()) 167 Expect(pod.Spec).To(HaveField("HostNetwork", false)) 168 169 numContainers := 0 170 for range pod.Spec.Containers { 171 numContainers++ 172 } 173 Expect(numContainers).To(Equal(1)) 174 }) 175 176 It("podman generate kube multiple pods", func() { 177 pod1 := podmanTest.Podman([]string{"run", "-dt", "--pod", "new:pod1", ALPINE, "top"}) 178 pod1.WaitWithDefaultTimeout() 179 Expect(pod1).Should(Exit(0)) 180 181 pod2 := podmanTest.Podman([]string{"run", "-dt", "--pod", "new:pod2", ALPINE, "top"}) 182 pod2.WaitWithDefaultTimeout() 183 Expect(pod2).Should(Exit(0)) 184 185 kube := podmanTest.Podman([]string{"generate", "kube", "pod1", "pod2"}) 186 kube.WaitWithDefaultTimeout() 187 Expect(kube).Should(Exit(0)) 188 189 Expect(string(kube.Out.Contents())).To(ContainSubstring(`name: pod1`)) 190 Expect(string(kube.Out.Contents())).To(ContainSubstring(`name: pod2`)) 191 }) 192 193 It("podman generate kube on pod with init containers", func() { 194 session := podmanTest.Podman([]string{"create", "--pod", "new:toppod", "--init-ctr", "always", ALPINE, "echo", "hello"}) 195 session.WaitWithDefaultTimeout() 196 Expect(session).Should(Exit(0)) 197 198 session = podmanTest.Podman([]string{"create", "--pod", "toppod", "--init-ctr", "always", ALPINE, "echo", "world"}) 199 session.WaitWithDefaultTimeout() 200 Expect(session).Should(Exit(0)) 201 202 session = podmanTest.Podman([]string{"create", "--pod", "toppod", ALPINE, "top"}) 203 session.WaitWithDefaultTimeout() 204 Expect(session).Should(Exit(0)) 205 206 kube := podmanTest.Podman([]string{"generate", "kube", "toppod"}) 207 kube.WaitWithDefaultTimeout() 208 Expect(kube).Should(Exit(0)) 209 210 pod := new(v1.Pod) 211 err := yaml.Unmarshal(kube.Out.Contents(), pod) 212 Expect(err).To(BeNil()) 213 Expect(pod.Spec).To(HaveField("HostNetwork", false)) 214 215 numContainers := len(pod.Spec.Containers) + len(pod.Spec.InitContainers) 216 Expect(numContainers).To(Equal(3)) 217 218 // Init container should be in the generated kube yaml if created with "once" type and the pod has not been started 219 session = podmanTest.Podman([]string{"create", "--pod", "new:toppod-2", "--init-ctr", "once", ALPINE, "echo", "using once type"}) 220 session.WaitWithDefaultTimeout() 221 Expect(session).Should(Exit(0)) 222 223 session = podmanTest.Podman([]string{"create", "--pod", "toppod-2", ALPINE, "top"}) 224 session.WaitWithDefaultTimeout() 225 Expect(session).Should(Exit(0)) 226 227 kube = podmanTest.Podman([]string{"generate", "kube", "toppod-2"}) 228 kube.WaitWithDefaultTimeout() 229 Expect(kube).Should(Exit(0)) 230 231 pod = new(v1.Pod) 232 err = yaml.Unmarshal(kube.Out.Contents(), pod) 233 Expect(err).To(BeNil()) 234 Expect(pod.Spec).To(HaveField("HostNetwork", false)) 235 236 numContainers = len(pod.Spec.Containers) + len(pod.Spec.InitContainers) 237 Expect(numContainers).To(Equal(2)) 238 239 // Init container should not be in the generated kube yaml if created with "once" type and the pod has been started 240 session = podmanTest.Podman([]string{"create", "--pod", "new:toppod-3", "--init-ctr", "once", ALPINE, "echo", "using once type"}) 241 session.WaitWithDefaultTimeout() 242 Expect(session).Should(Exit(0)) 243 244 session = podmanTest.Podman([]string{"create", "--pod", "toppod-3", ALPINE, "top"}) 245 session.WaitWithDefaultTimeout() 246 Expect(session).Should(Exit(0)) 247 248 session = podmanTest.Podman([]string{"pod", "start", "toppod-3"}) 249 session.WaitWithDefaultTimeout() 250 Expect(session).Should(Exit(0)) 251 252 kube = podmanTest.Podman([]string{"generate", "kube", "toppod-3"}) 253 kube.WaitWithDefaultTimeout() 254 Expect(kube).Should(Exit(0)) 255 256 pod = new(v1.Pod) 257 err = yaml.Unmarshal(kube.Out.Contents(), pod) 258 Expect(err).To(BeNil()) 259 Expect(pod.Spec).To(HaveField("HostNetwork", false)) 260 261 numContainers = len(pod.Spec.Containers) + len(pod.Spec.InitContainers) 262 Expect(numContainers).To(Equal(1)) 263 }) 264 265 It("podman generate kube on pod with host network", func() { 266 podSession := podmanTest.Podman([]string{"pod", "create", "--name", "testHostNetwork", "--network", "host"}) 267 podSession.WaitWithDefaultTimeout() 268 Expect(podSession).Should(Exit(0)) 269 270 session := podmanTest.Podman([]string{"create", "--name", "topcontainer", "--pod", "testHostNetwork", "--network", "host", ALPINE, "top"}) 271 session.WaitWithDefaultTimeout() 272 Expect(session).Should(Exit(0)) 273 274 kube := podmanTest.Podman([]string{"generate", "kube", "testHostNetwork"}) 275 kube.WaitWithDefaultTimeout() 276 Expect(kube).Should(Exit(0)) 277 278 pod := new(v1.Pod) 279 err := yaml.Unmarshal(kube.Out.Contents(), pod) 280 Expect(err).To(BeNil()) 281 Expect(pod.Spec).To(HaveField("HostNetwork", true)) 282 }) 283 284 It("podman generate kube on container with host network", func() { 285 session := podmanTest.RunTopContainerWithArgs("topcontainer", []string{"--network", "host"}) 286 session.WaitWithDefaultTimeout() 287 Expect(session).Should(Exit(0)) 288 289 kube := podmanTest.Podman([]string{"generate", "kube", "topcontainer"}) 290 kube.WaitWithDefaultTimeout() 291 Expect(kube).Should(Exit(0)) 292 293 pod := new(v1.Pod) 294 err := yaml.Unmarshal(kube.Out.Contents(), pod) 295 Expect(err).To(BeNil()) 296 Expect(pod.Spec).To(HaveField("HostNetwork", true)) 297 }) 298 299 It("podman generate kube on pod with hostAliases", func() { 300 podName := "testHost" 301 testIP := "127.0.0.1" 302 podSession := podmanTest.Podman([]string{"pod", "create", "--name", podName, 303 "--add-host", "test1.podman.io" + ":" + testIP, 304 "--add-host", "test2.podman.io" + ":" + testIP, 305 }) 306 podSession.WaitWithDefaultTimeout() 307 Expect(podSession).Should(Exit(0)) 308 309 ctr1Name := "ctr1" 310 ctr1Session := podmanTest.Podman([]string{"create", "--name", ctr1Name, "--pod", podName, ALPINE, "top"}) 311 ctr1Session.WaitWithDefaultTimeout() 312 Expect(ctr1Session).Should(Exit(0)) 313 314 ctr2Name := "ctr2" 315 ctr2Session := podmanTest.Podman([]string{"create", "--name", ctr2Name, "--pod", podName, ALPINE, "top"}) 316 ctr2Session.WaitWithDefaultTimeout() 317 Expect(ctr2Session).Should(Exit(0)) 318 319 kube := podmanTest.Podman([]string{"generate", "kube", podName}) 320 kube.WaitWithDefaultTimeout() 321 Expect(kube).Should(Exit(0)) 322 323 pod := new(v1.Pod) 324 err := yaml.Unmarshal(kube.Out.Contents(), pod) 325 Expect(err).To(BeNil()) 326 Expect(pod.Spec.HostAliases).To(HaveLen(2)) 327 Expect(pod.Spec.HostAliases[0]).To(HaveField("IP", testIP)) 328 Expect(pod.Spec.HostAliases[1]).To(HaveField("IP", testIP)) 329 }) 330 331 It("podman generate service kube on pod", func() { 332 session := podmanTest.Podman([]string{"create", "--pod", "new:test-pod", "-p", "4000:4000/udp", ALPINE, "ls"}) 333 session.WaitWithDefaultTimeout() 334 Expect(session).Should(Exit(0)) 335 336 kube := podmanTest.Podman([]string{"generate", "kube", "-s", "test-pod"}) 337 kube.WaitWithDefaultTimeout() 338 Expect(kube).Should(Exit(0)) 339 340 // Separate out the Service and Pod yaml 341 arr := strings.Split(string(kube.Out.Contents()), "---") 342 Expect(arr).To(HaveLen(2)) 343 344 svc := new(v1.Service) 345 err := yaml.Unmarshal([]byte(arr[0]), svc) 346 Expect(err).To(BeNil()) 347 Expect(svc.Spec.Ports).To(HaveLen(1)) 348 Expect(svc.Spec.Ports[0].TargetPort.IntValue()).To(Equal(4000)) 349 Expect(svc.Spec.Ports[0]).To(HaveField("Protocol", v1.ProtocolUDP)) 350 351 pod := new(v1.Pod) 352 err = yaml.Unmarshal([]byte(arr[1]), pod) 353 Expect(err).To(BeNil()) 354 }) 355 356 It("podman generate kube on pod with restartPolicy", func() { 357 // podName, set, expect 358 testSli := [][]string{ 359 {"testPod1", "", "Never"}, // some pod create from cmdline, so set it to Never 360 {"testPod2", "always", "Always"}, 361 {"testPod3", "on-failure", "OnFailure"}, 362 {"testPod4", "no", "Never"}, 363 } 364 365 for k, v := range testSli { 366 podName := v[0] 367 podSession := podmanTest.Podman([]string{"pod", "create", "--name", podName}) 368 podSession.WaitWithDefaultTimeout() 369 Expect(podSession).Should(Exit(0)) 370 371 ctrName := "ctr" + strconv.Itoa(k) 372 ctr1Session := podmanTest.Podman([]string{"create", "--name", ctrName, "--pod", podName, 373 "--restart", v[1], ALPINE, "top"}) 374 ctr1Session.WaitWithDefaultTimeout() 375 Expect(ctr1Session).Should(Exit(0)) 376 377 kube := podmanTest.Podman([]string{"generate", "kube", podName}) 378 kube.WaitWithDefaultTimeout() 379 Expect(kube).Should(Exit(0)) 380 381 pod := new(v1.Pod) 382 err := yaml.Unmarshal(kube.Out.Contents(), pod) 383 Expect(err).To(BeNil()) 384 385 Expect(string(pod.Spec.RestartPolicy)).To(Equal(v[2])) 386 } 387 }) 388 389 It("podman generate kube on pod with memory limit", func() { 390 podName := "testMemoryLimit" 391 podSession := podmanTest.Podman([]string{"pod", "create", "--name", podName}) 392 podSession.WaitWithDefaultTimeout() 393 Expect(podSession).Should(Exit(0)) 394 395 ctr1Name := "ctr1" 396 ctr1Session := podmanTest.Podman([]string{"create", "--name", ctr1Name, "--pod", podName, "--memory", "10Mi", ALPINE, "top"}) 397 ctr1Session.WaitWithDefaultTimeout() 398 Expect(ctr1Session).Should(Exit(0)) 399 400 kube := podmanTest.Podman([]string{"generate", "kube", podName}) 401 kube.WaitWithDefaultTimeout() 402 Expect(kube).Should(Exit(0)) 403 404 pod := new(v1.Pod) 405 err := yaml.Unmarshal(kube.Out.Contents(), pod) 406 Expect(err).To(BeNil()) 407 408 for _, ctr := range pod.Spec.Containers { 409 memoryLimit, _ := ctr.Resources.Limits.Memory().AsInt64() 410 Expect(memoryLimit).To(Equal(int64(10 * 1024 * 1024))) 411 } 412 }) 413 414 It("podman generate kube on pod with cpu limit", func() { 415 podName := "testCpuLimit" 416 podSession := podmanTest.Podman([]string{"pod", "create", "--name", podName}) 417 podSession.WaitWithDefaultTimeout() 418 Expect(podSession).Should(Exit(0)) 419 420 ctr1Name := "ctr1" 421 ctr1Session := podmanTest.Podman([]string{"create", "--name", ctr1Name, "--pod", podName, 422 "--cpus", "0.5", ALPINE, "top"}) 423 ctr1Session.WaitWithDefaultTimeout() 424 Expect(ctr1Session).Should(Exit(0)) 425 426 ctr2Name := "ctr2" 427 ctr2Session := podmanTest.Podman([]string{"create", "--name", ctr2Name, "--pod", podName, 428 "--cpu-period", "100000", "--cpu-quota", "50000", ALPINE, "top"}) 429 ctr2Session.WaitWithDefaultTimeout() 430 Expect(ctr2Session).Should(Exit(0)) 431 432 kube := podmanTest.Podman([]string{"generate", "kube", podName}) 433 kube.WaitWithDefaultTimeout() 434 Expect(kube).Should(Exit(0)) 435 436 pod := new(v1.Pod) 437 err := yaml.Unmarshal(kube.Out.Contents(), pod) 438 Expect(err).To(BeNil()) 439 440 for _, ctr := range pod.Spec.Containers { 441 cpuLimit := ctr.Resources.Limits.Cpu().MilliValue() 442 Expect(cpuLimit).To(Equal(int64(500))) 443 } 444 }) 445 446 It("podman generate kube on pod with ports", func() { 447 podName := "test" 448 podSession := podmanTest.Podman([]string{"pod", "create", "--name", podName, "-p", "4000:4000", "-p", "5000:5000"}) 449 podSession.WaitWithDefaultTimeout() 450 Expect(podSession).Should(Exit(0)) 451 452 ctr1Name := "ctr1" 453 ctr1Session := podmanTest.Podman([]string{"create", "--name", ctr1Name, "--pod", podName, ALPINE, "top"}) 454 ctr1Session.WaitWithDefaultTimeout() 455 Expect(ctr1Session).Should(Exit(0)) 456 457 ctr2Name := "ctr2" 458 ctr2Session := podmanTest.Podman([]string{"create", "--name", ctr2Name, "--pod", podName, ALPINE, "top"}) 459 ctr2Session.WaitWithDefaultTimeout() 460 Expect(ctr2Session).Should(Exit(0)) 461 462 kube := podmanTest.Podman([]string{"generate", "kube", podName}) 463 kube.WaitWithDefaultTimeout() 464 Expect(kube).Should(Exit(0)) 465 466 pod := new(v1.Pod) 467 err := yaml.Unmarshal(kube.Out.Contents(), pod) 468 Expect(err).To(BeNil()) 469 470 foundPort4000 := 0 471 foundPort5000 := 0 472 foundOtherPort := 0 473 for _, ctr := range pod.Spec.Containers { 474 for _, port := range ctr.Ports { 475 // Since we are using tcp here, the generated kube yaml shouldn't 476 // have anything for protocol under the ports as tcp is the default 477 // for k8s 478 Expect(port.Protocol).To(BeEmpty()) 479 if port.HostPort == 4000 { 480 foundPort4000++ 481 } else if port.HostPort == 5000 { 482 foundPort5000++ 483 } else { 484 foundOtherPort++ 485 } 486 } 487 } 488 Expect(foundPort4000).To(Equal(1)) 489 Expect(foundPort5000).To(Equal(1)) 490 Expect(foundOtherPort).To(Equal(0)) 491 492 // Create container with UDP port and check the generated kube yaml 493 ctrWithUDP := podmanTest.Podman([]string{"create", "--pod", "new:test-pod", "-p", "6666:66/udp", ALPINE, "top"}) 494 ctrWithUDP.WaitWithDefaultTimeout() 495 Expect(ctrWithUDP).Should(Exit(0)) 496 497 kube = podmanTest.Podman([]string{"generate", "kube", "test-pod"}) 498 kube.WaitWithDefaultTimeout() 499 Expect(kube).Should(Exit(0)) 500 501 pod = new(v1.Pod) 502 err = yaml.Unmarshal(kube.Out.Contents(), pod) 503 Expect(err).To(BeNil()) 504 505 containers := pod.Spec.Containers 506 Expect(containers).To(HaveLen(1)) 507 Expect(containers[0].Ports).To(HaveLen(1)) 508 Expect(containers[0].Ports[0]).To(HaveField("Protocol", v1.ProtocolUDP)) 509 }) 510 511 It("podman generate and reimport kube on pod", func() { 512 podName := "toppod" 513 _, rc, _ := podmanTest.CreatePod(map[string][]string{"--name": {podName}}) 514 Expect(rc).To(Equal(0)) 515 516 session := podmanTest.Podman([]string{"create", "--pod", podName, "--name", "test1", ALPINE, "top"}) 517 session.WaitWithDefaultTimeout() 518 Expect(session).Should(Exit(0)) 519 520 session2 := podmanTest.Podman([]string{"create", "--pod", podName, "--name", "test2", ALPINE, "top"}) 521 session2.WaitWithDefaultTimeout() 522 Expect(session2).Should(Exit(0)) 523 524 outputFile := filepath.Join(podmanTest.RunRoot, "pod.yaml") 525 kube := podmanTest.Podman([]string{"generate", "kube", "-f", outputFile, podName}) 526 kube.WaitWithDefaultTimeout() 527 Expect(kube).Should(Exit(0)) 528 529 session3 := podmanTest.Podman([]string{"pod", "rm", "-af"}) 530 session3.WaitWithDefaultTimeout() 531 Expect(session3).Should(Exit(0)) 532 533 session4 := podmanTest.Podman([]string{"play", "kube", outputFile}) 534 session4.WaitWithDefaultTimeout() 535 Expect(session4).Should(Exit(0)) 536 537 session5 := podmanTest.Podman([]string{"pod", "ps"}) 538 session5.WaitWithDefaultTimeout() 539 Expect(session5).Should(Exit(0)) 540 Expect(session5.OutputToString()).To(ContainSubstring(podName)) 541 542 session6 := podmanTest.Podman([]string{"ps", "-a"}) 543 session6.WaitWithDefaultTimeout() 544 Expect(session6).Should(Exit(0)) 545 psOut := session6.OutputToString() 546 Expect(psOut).To(ContainSubstring("test1")) 547 Expect(psOut).To(ContainSubstring("test2")) 548 }) 549 550 It("podman generate with user and reimport kube on pod", func() { 551 podName := "toppod" 552 _, rc, _ := podmanTest.CreatePod(map[string][]string{"--name": {podName}}) 553 Expect(rc).To(Equal(0)) 554 555 session := podmanTest.Podman([]string{"create", "--pod", podName, "--name", "test1", "--user", "100:200", ALPINE, "top"}) 556 session.WaitWithDefaultTimeout() 557 Expect(session).Should(Exit(0)) 558 559 inspect := podmanTest.Podman([]string{"inspect", "--format", "{{.Config.User}}", "test1"}) 560 inspect.WaitWithDefaultTimeout() 561 Expect(inspect).Should(Exit(0)) 562 Expect(inspect.OutputToString()).To(ContainSubstring("100:200")) 563 564 outputFile := filepath.Join(podmanTest.RunRoot, "pod.yaml") 565 kube := podmanTest.Podman([]string{"generate", "kube", "-f", outputFile, podName}) 566 kube.WaitWithDefaultTimeout() 567 Expect(kube).Should(Exit(0)) 568 569 session = podmanTest.Podman([]string{"pod", "rm", "-af"}) 570 session.WaitWithDefaultTimeout() 571 Expect(session).Should(Exit(0)) 572 573 podmanTest.AddImageToRWStore(ALPINE) 574 session = podmanTest.Podman([]string{"play", "kube", outputFile}) 575 session.WaitWithDefaultTimeout() 576 Expect(session).Should(Exit(0)) 577 578 // container name in pod is <podName>-<ctrName> 579 inspect1 := podmanTest.Podman([]string{"inspect", "--format", "{{.Config.User}}", "toppod-test1"}) 580 inspect1.WaitWithDefaultTimeout() 581 Expect(inspect1).Should(Exit(0)) 582 Expect(inspect1.OutputToString()).To(ContainSubstring(inspect.OutputToString())) 583 }) 584 585 It("podman generate kube with volume", func() { 586 vol1 := filepath.Join(podmanTest.TempDir, "vol-test1") 587 err := os.MkdirAll(vol1, 0755) 588 Expect(err).To(BeNil()) 589 590 // we need a container name because IDs don't persist after rm/play 591 ctrName := "test-ctr" 592 ctrNameInKubePod := "test1-test-ctr" 593 594 session1 := podmanTest.Podman([]string{"run", "-d", "--pod", "new:test1", "--name", ctrName, "-v", vol1 + ":/volume/:z", "alpine", "top"}) 595 session1.WaitWithDefaultTimeout() 596 Expect(session1).Should(Exit(0)) 597 598 outputFile := filepath.Join(podmanTest.RunRoot, "pod.yaml") 599 kube := podmanTest.Podman([]string{"generate", "kube", "test1", "-f", outputFile}) 600 kube.WaitWithDefaultTimeout() 601 Expect(kube).Should(Exit(0)) 602 603 b, err := ioutil.ReadFile(outputFile) 604 Expect(err).ShouldNot(HaveOccurred()) 605 pod := new(v1.Pod) 606 err = yaml.Unmarshal(b, pod) 607 Expect(err).To(BeNil()) 608 Expect(pod.Annotations).To(HaveKeyWithValue(define.BindMountPrefix+vol1, HaveSuffix("z"))) 609 610 rm := podmanTest.Podman([]string{"pod", "rm", "-t", "0", "-f", "test1"}) 611 rm.WaitWithDefaultTimeout() 612 Expect(rm).Should(Exit(0)) 613 614 play := podmanTest.Podman([]string{"play", "kube", outputFile}) 615 play.WaitWithDefaultTimeout() 616 Expect(play).Should(Exit(0)) 617 618 inspect := podmanTest.Podman([]string{"inspect", ctrNameInKubePod}) 619 inspect.WaitWithDefaultTimeout() 620 Expect(inspect).Should(Exit(0)) 621 Expect(inspect.OutputToString()).To(ContainSubstring(vol1)) 622 }) 623 624 It("podman generate kube when bind-mounting '/' and '/root' at the same time ", func() { 625 // Fixes https://github.com/containers/podman/issues/9764 626 627 ctrName := "mount-root-ctr" 628 session1 := podmanTest.Podman([]string{"run", "-d", "--pod", "new:mount-root-conflict", "--name", ctrName, 629 "-v", "/:/volume1/", 630 "-v", "/root:/volume2/", 631 "alpine", "top"}) 632 session1.WaitWithDefaultTimeout() 633 Expect(session1).Should(Exit(0)) 634 635 kube := podmanTest.Podman([]string{"generate", "kube", "mount-root-conflict"}) 636 kube.WaitWithDefaultTimeout() 637 Expect(kube).Should(Exit(0)) 638 639 pod := new(v1.Pod) 640 err := yaml.Unmarshal(kube.Out.Contents(), pod) 641 Expect(err).To(BeNil()) 642 643 Expect(pod.Spec.Volumes).To(HaveLen(2)) 644 645 }) 646 647 It("podman generate kube with persistent volume claim", func() { 648 vol := "vol-test-persistent-volume-claim" 649 650 // we need a container name because IDs don't persist after rm/play 651 ctrName := "test-persistent-volume-claim" 652 ctrNameInKubePod := "test1-test-persistent-volume-claim" 653 654 session := podmanTest.Podman([]string{"run", "-d", "--pod", "new:test1", "--name", ctrName, "-v", vol + ":/volume/:z", "alpine", "top"}) 655 session.WaitWithDefaultTimeout() 656 Expect(session).Should(Exit(0)) 657 658 outputFile := filepath.Join(podmanTest.RunRoot, "pod.yaml") 659 kube := podmanTest.Podman([]string{"generate", "kube", "test1", "-f", outputFile}) 660 kube.WaitWithDefaultTimeout() 661 Expect(kube).Should(Exit(0)) 662 663 rm := podmanTest.Podman([]string{"pod", "rm", "-t", "0", "-f", "test1"}) 664 rm.WaitWithDefaultTimeout() 665 Expect(rm).Should(Exit(0)) 666 667 play := podmanTest.Podman([]string{"play", "kube", outputFile}) 668 play.WaitWithDefaultTimeout() 669 Expect(play).Should(Exit(0)) 670 671 inspect := podmanTest.Podman([]string{"inspect", ctrNameInKubePod}) 672 inspect.WaitWithDefaultTimeout() 673 Expect(inspect).Should(Exit(0)) 674 Expect(inspect.OutputToString()).To(ContainSubstring(vol)) 675 }) 676 677 It("podman generate kube sharing pid namespace", func() { 678 podName := "test" 679 podSession := podmanTest.Podman([]string{"pod", "create", "--name", podName, "--share", "pid"}) 680 podSession.WaitWithDefaultTimeout() 681 Expect(podSession).Should(Exit(0)) 682 683 session := podmanTest.Podman([]string{"create", "--pod", podName, "--name", "test1", ALPINE, "top"}) 684 session.WaitWithDefaultTimeout() 685 Expect(session).Should(Exit(0)) 686 687 outputFile := filepath.Join(podmanTest.RunRoot, "pod.yaml") 688 kube := podmanTest.Podman([]string{"generate", "kube", podName, "-f", outputFile}) 689 kube.WaitWithDefaultTimeout() 690 Expect(kube).Should(Exit(0)) 691 692 rm := podmanTest.Podman([]string{"pod", "rm", "-t", "0", "-f", podName}) 693 rm.WaitWithDefaultTimeout() 694 Expect(rm).Should(Exit(0)) 695 696 play := podmanTest.Podman([]string{"play", "kube", outputFile}) 697 play.WaitWithDefaultTimeout() 698 Expect(play).Should(Exit(0)) 699 700 inspect := podmanTest.Podman([]string{"pod", "inspect", podName}) 701 inspect.WaitWithDefaultTimeout() 702 Expect(inspect).Should(Exit(0)) 703 Expect(inspect.OutputToString()).To(ContainSubstring(`"pid"`)) 704 }) 705 706 It("podman generate kube with pods and containers", func() { 707 pod1 := podmanTest.Podman([]string{"run", "-dt", "--pod", "new:pod1", ALPINE, "top"}) 708 pod1.WaitWithDefaultTimeout() 709 Expect(pod1).Should(Exit(0)) 710 711 pod2 := podmanTest.Podman([]string{"run", "-dt", "--name", "top", ALPINE, "top"}) 712 pod2.WaitWithDefaultTimeout() 713 Expect(pod2).Should(Exit(0)) 714 715 kube := podmanTest.Podman([]string{"generate", "kube", "pod1", "top"}) 716 kube.WaitWithDefaultTimeout() 717 Expect(kube).Should(Exit(0)) 718 }) 719 720 It("podman generate kube with containers in a pod should fail", func() { 721 pod1 := podmanTest.Podman([]string{"pod", "create", "--name", "pod1"}) 722 pod1.WaitWithDefaultTimeout() 723 Expect(pod1).Should(Exit(0)) 724 725 con := podmanTest.Podman([]string{"run", "-dt", "--pod", "pod1", "--name", "top", ALPINE, "top"}) 726 con.WaitWithDefaultTimeout() 727 Expect(con).Should(Exit(0)) 728 729 kube := podmanTest.Podman([]string{"generate", "kube", "top"}) 730 kube.WaitWithDefaultTimeout() 731 Expect(kube).To(ExitWithError()) 732 }) 733 734 It("podman generate kube with multiple containers", func() { 735 con1 := podmanTest.Podman([]string{"run", "-dt", "--name", "con1", ALPINE, "top"}) 736 con1.WaitWithDefaultTimeout() 737 Expect(con1).Should(Exit(0)) 738 739 con2 := podmanTest.Podman([]string{"run", "-dt", "--name", "con2", ALPINE, "top"}) 740 con2.WaitWithDefaultTimeout() 741 Expect(con2).Should(Exit(0)) 742 743 kube := podmanTest.Podman([]string{"generate", "kube", "con1", "con2"}) 744 kube.WaitWithDefaultTimeout() 745 Expect(kube).Should(Exit(0)) 746 }) 747 748 It("podman generate kube with containers in pods should fail", func() { 749 pod1 := podmanTest.Podman([]string{"run", "-dt", "--pod", "new:pod1", "--name", "top1", ALPINE, "top"}) 750 pod1.WaitWithDefaultTimeout() 751 Expect(pod1).Should(Exit(0)) 752 753 pod2 := podmanTest.Podman([]string{"run", "-dt", "--pod", "new:pod2", "--name", "top2", ALPINE, "top"}) 754 pod2.WaitWithDefaultTimeout() 755 Expect(pod2).Should(Exit(0)) 756 757 kube := podmanTest.Podman([]string{"generate", "kube", "top1", "top2"}) 758 kube.WaitWithDefaultTimeout() 759 Expect(kube).To(ExitWithError()) 760 }) 761 762 It("podman generate kube on a container with dns options", func() { 763 top := podmanTest.Podman([]string{"run", "-dt", "--name", "top", "--dns", "8.8.8.8", "--dns-search", "foobar.com", "--dns-opt", "color:blue", ALPINE, "top"}) 764 top.WaitWithDefaultTimeout() 765 Expect(top).Should(Exit(0)) 766 767 kube := podmanTest.Podman([]string{"generate", "kube", "top"}) 768 kube.WaitWithDefaultTimeout() 769 Expect(kube).Should(Exit(0)) 770 771 pod := new(v1.Pod) 772 err := yaml.Unmarshal(kube.Out.Contents(), pod) 773 Expect(err).To(BeNil()) 774 775 Expect(pod.Spec.DNSConfig.Nameservers).To(ContainElement("8.8.8.8")) 776 Expect(pod.Spec.DNSConfig.Searches).To(ContainElement("foobar.com")) 777 Expect(len(pod.Spec.DNSConfig.Options)).To(BeNumerically(">", 0)) 778 Expect(pod.Spec.DNSConfig.Options[0]).To(HaveField("Name", "color")) 779 s := "blue" 780 Expect(pod.Spec.DNSConfig.Options[0]).To(HaveField("Value", &s)) 781 }) 782 783 It("podman generate kube multiple container dns servers and options are cumulative", func() { 784 top1 := podmanTest.Podman([]string{"run", "-dt", "--name", "top1", "--dns", "8.8.8.8", "--dns-search", "foobar.com", ALPINE, "top"}) 785 top1.WaitWithDefaultTimeout() 786 Expect(top1).Should(Exit(0)) 787 788 top2 := podmanTest.Podman([]string{"run", "-dt", "--name", "top2", "--dns", "8.7.7.7", "--dns-search", "homer.com", ALPINE, "top"}) 789 top2.WaitWithDefaultTimeout() 790 Expect(top2).Should(Exit(0)) 791 792 kube := podmanTest.Podman([]string{"generate", "kube", "top1", "top2"}) 793 kube.WaitWithDefaultTimeout() 794 Expect(kube).Should(Exit(0)) 795 796 pod := new(v1.Pod) 797 err := yaml.Unmarshal(kube.Out.Contents(), pod) 798 Expect(err).To(BeNil()) 799 800 Expect(pod.Spec.DNSConfig.Nameservers).To(ContainElement("8.8.8.8")) 801 Expect(pod.Spec.DNSConfig.Nameservers).To(ContainElement("8.7.7.7")) 802 Expect(pod.Spec.DNSConfig.Searches).To(ContainElement("foobar.com")) 803 Expect(pod.Spec.DNSConfig.Searches).To(ContainElement("homer.com")) 804 }) 805 806 It("podman generate kube on a pod with dns options", func() { 807 top := podmanTest.Podman([]string{"run", "--pod", "new:pod1", "-dt", "--name", "top", "--dns", "8.8.8.8", "--dns-search", "foobar.com", "--dns-opt", "color:blue", ALPINE, "top"}) 808 top.WaitWithDefaultTimeout() 809 Expect(top).Should(Exit(0)) 810 811 kube := podmanTest.Podman([]string{"generate", "kube", "pod1"}) 812 kube.WaitWithDefaultTimeout() 813 Expect(kube).Should(Exit(0)) 814 815 pod := new(v1.Pod) 816 err := yaml.Unmarshal(kube.Out.Contents(), pod) 817 Expect(err).To(BeNil()) 818 819 Expect(pod.Spec.DNSConfig.Nameservers).To(ContainElement("8.8.8.8")) 820 Expect(pod.Spec.DNSConfig.Searches).To(ContainElement("foobar.com")) 821 Expect(len(pod.Spec.DNSConfig.Options)).To(BeNumerically(">", 0)) 822 Expect(pod.Spec.DNSConfig.Options[0]).To(HaveField("Name", "color")) 823 s := "blue" 824 Expect(pod.Spec.DNSConfig.Options[0]).To(HaveField("Value", &s)) 825 }) 826 827 It("podman generate kube - set entrypoint as command", func() { 828 session := podmanTest.Podman([]string{"create", "--pod", "new:testpod", "--entrypoint", "/bin/sleep", ALPINE, "10s"}) 829 session.WaitWithDefaultTimeout() 830 Expect(session).Should(Exit(0)) 831 832 kube := podmanTest.Podman([]string{"generate", "kube", "testpod"}) 833 kube.WaitWithDefaultTimeout() 834 Expect(kube).Should(Exit(0)) 835 836 // Now make sure that the container's command is set to the 837 // entrypoint and it's arguments to "10s". 838 pod := new(v1.Pod) 839 err := yaml.Unmarshal(kube.Out.Contents(), pod) 840 Expect(err).To(BeNil()) 841 842 containers := pod.Spec.Containers 843 Expect(containers).To(HaveLen(1)) 844 845 Expect(containers[0]).To(HaveField("Command", []string{"/bin/sleep"})) 846 Expect(containers[0]).To(HaveField("Args", []string{"10s"})) 847 }) 848 849 It("podman generate kube - use command from image unless explicitly set in the podman command", func() { 850 session := podmanTest.Podman([]string{"create", "--name", "test", ALPINE}) 851 session.WaitWithDefaultTimeout() 852 Expect(session).Should(Exit(0)) 853 854 kube := podmanTest.Podman([]string{"generate", "kube", "test"}) 855 kube.WaitWithDefaultTimeout() 856 Expect(kube).Should(Exit(0)) 857 858 // Now make sure that the container's command in the kube yaml is not set to the 859 // image command. 860 pod := new(v1.Pod) 861 err := yaml.Unmarshal(kube.Out.Contents(), pod) 862 Expect(err).To(BeNil()) 863 864 containers := pod.Spec.Containers 865 Expect(containers).To(HaveLen(1)) 866 Expect(containers[0].Command).To(BeEmpty()) 867 868 cmd := []string{"echo", "hi"} 869 session = podmanTest.Podman(append([]string{"create", "--name", "test1", ALPINE}, cmd...)) 870 session.WaitWithDefaultTimeout() 871 Expect(session).Should(Exit(0)) 872 873 kube = podmanTest.Podman([]string{"generate", "kube", "test1"}) 874 kube.WaitWithDefaultTimeout() 875 Expect(kube).Should(Exit(0)) 876 877 // Now make sure that the container's command in the kube yaml is set to the 878 // command passed via the cli to podman create. 879 pod = new(v1.Pod) 880 err = yaml.Unmarshal(kube.Out.Contents(), pod) 881 Expect(err).To(BeNil()) 882 883 containers = pod.Spec.Containers 884 Expect(containers).To(HaveLen(1)) 885 Expect(containers[0]).To(HaveField("Command", cmd)) 886 }) 887 888 It("podman generate kube - use entrypoint from image unless --entrypoint is set", func() { 889 // Build an image with an entrypoint. 890 containerfile := `FROM quay.io/libpod/alpine:latest 891 ENTRYPOINT ["sleep"]` 892 893 targetPath, err := CreateTempDirInTempDir() 894 Expect(err).To(BeNil()) 895 containerfilePath := filepath.Join(targetPath, "Containerfile") 896 err = ioutil.WriteFile(containerfilePath, []byte(containerfile), 0644) 897 Expect(err).To(BeNil()) 898 899 image := "generatekube:test" 900 session := podmanTest.Podman([]string{"build", "--pull-never", "-f", containerfilePath, "-t", image}) 901 session.WaitWithDefaultTimeout() 902 Expect(session).Should(Exit(0)) 903 904 session = podmanTest.Podman([]string{"create", "--pod", "new:testpod", image, "10s"}) 905 session.WaitWithDefaultTimeout() 906 Expect(session).Should(Exit(0)) 907 908 kube := podmanTest.Podman([]string{"generate", "kube", "testpod"}) 909 kube.WaitWithDefaultTimeout() 910 Expect(kube).Should(Exit(0)) 911 912 // Now make sure that the container's command in the kube yaml is NOT set to the 913 // entrypoint but the arguments should be set to "10s". 914 pod := new(v1.Pod) 915 err = yaml.Unmarshal(kube.Out.Contents(), pod) 916 Expect(err).To(BeNil()) 917 918 containers := pod.Spec.Containers 919 Expect(containers).To(HaveLen(1)) 920 Expect(containers[0]).To(HaveField("Args", []string{"10s"})) 921 922 session = podmanTest.Podman([]string{"create", "--pod", "new:testpod-2", "--entrypoint", "echo", image, "hello"}) 923 session.WaitWithDefaultTimeout() 924 Expect(session).Should(Exit(0)) 925 926 kube = podmanTest.Podman([]string{"generate", "kube", "testpod-2"}) 927 kube.WaitWithDefaultTimeout() 928 Expect(kube).Should(Exit(0)) 929 930 // Now make sure that the container's command in the kube yaml is set to the 931 // entrypoint defined by the --entrypoint flag and the arguments should be set to "hello". 932 pod = new(v1.Pod) 933 err = yaml.Unmarshal(kube.Out.Contents(), pod) 934 Expect(err).To(BeNil()) 935 936 containers = pod.Spec.Containers 937 Expect(containers).To(HaveLen(1)) 938 Expect(containers[0]).To(HaveField("Command", []string{"echo"})) 939 Expect(containers[0]).To(HaveField("Args", []string{"hello"})) 940 }) 941 942 It("podman generate kube - --privileged container", func() { 943 session := podmanTest.Podman([]string{"create", "--pod", "new:testpod", "--privileged", ALPINE, "ls"}) 944 session.WaitWithDefaultTimeout() 945 Expect(session).Should(Exit(0)) 946 947 kube := podmanTest.Podman([]string{"generate", "kube", "testpod"}) 948 kube.WaitWithDefaultTimeout() 949 Expect(kube).Should(Exit(0)) 950 951 // Now make sure that the capabilities aren't set. 952 pod := new(v1.Pod) 953 err := yaml.Unmarshal(kube.Out.Contents(), pod) 954 Expect(err).To(BeNil()) 955 956 containers := pod.Spec.Containers 957 Expect(containers).To(HaveLen(1)) 958 Expect(containers[0].SecurityContext.Capabilities).To(BeNil()) 959 960 // Now make sure we can also `play` it. 961 kubeFile := filepath.Join(podmanTest.TempDir, "kube.yaml") 962 963 kube = podmanTest.Podman([]string{"generate", "kube", "testpod", "-f", kubeFile}) 964 kube.WaitWithDefaultTimeout() 965 Expect(kube).Should(Exit(0)) 966 967 // Remove the pod so play can recreate it. 968 kube = podmanTest.Podman([]string{"pod", "rm", "-t", "0", "-f", "testpod"}) 969 kube.WaitWithDefaultTimeout() 970 Expect(kube).Should(Exit(0)) 971 972 kube = podmanTest.Podman([]string{"play", "kube", kubeFile}) 973 kube.WaitWithDefaultTimeout() 974 Expect(kube).Should(Exit(0)) 975 }) 976 977 It("podman generate kube based on user in container", func() { 978 // Build an image with an entrypoint. 979 containerfile := `FROM quay.io/libpod/alpine:latest 980 RUN adduser -u 10001 -S test1 981 USER test1` 982 983 targetPath, err := CreateTempDirInTempDir() 984 Expect(err).To(BeNil()) 985 containerfilePath := filepath.Join(targetPath, "Containerfile") 986 err = ioutil.WriteFile(containerfilePath, []byte(containerfile), 0644) 987 Expect(err).To(BeNil()) 988 989 image := "generatekube:test" 990 session := podmanTest.Podman([]string{"build", "--pull-never", "-f", containerfilePath, "-t", image}) 991 session.WaitWithDefaultTimeout() 992 Expect(session).Should(Exit(0)) 993 994 session = podmanTest.Podman([]string{"create", "--pod", "new:testpod", image, "test1"}) 995 session.WaitWithDefaultTimeout() 996 Expect(session).Should(Exit(0)) 997 998 kube := podmanTest.Podman([]string{"generate", "kube", "testpod"}) 999 kube.WaitWithDefaultTimeout() 1000 Expect(kube).Should(Exit(0)) 1001 1002 pod := new(v1.Pod) 1003 err = yaml.Unmarshal(kube.Out.Contents(), pod) 1004 Expect(err).To(BeNil()) 1005 Expect(pod.Spec.Containers[0].SecurityContext.RunAsUser).To(BeNil()) 1006 }) 1007 1008 It("podman generate kube on named volume", func() { 1009 vol := "simple-named-volume" 1010 1011 session := podmanTest.Podman([]string{"volume", "create", vol}) 1012 session.WaitWithDefaultTimeout() 1013 Expect(session).Should(Exit(0)) 1014 1015 kube := podmanTest.Podman([]string{"generate", "kube", vol}) 1016 kube.WaitWithDefaultTimeout() 1017 Expect(kube).Should(Exit(0)) 1018 1019 pvc := new(v1.PersistentVolumeClaim) 1020 err := yaml.Unmarshal(kube.Out.Contents(), pvc) 1021 Expect(err).To(BeNil()) 1022 Expect(pvc).To(HaveField("Name", vol)) 1023 Expect(pvc.Spec.AccessModes[0]).To(Equal(v1.ReadWriteOnce)) 1024 Expect(pvc.Spec.Resources.Requests.Storage().String()).To(Equal("1Gi")) 1025 }) 1026 1027 It("podman generate kube on named volume with options", func() { 1028 vol := "complex-named-volume" 1029 volDevice := "tmpfs" 1030 volType := "tmpfs" 1031 volOpts := "nodev,noexec" 1032 1033 session := podmanTest.Podman([]string{"volume", "create", "--opt", "device=" + volDevice, "--opt", "type=" + volType, "--opt", "o=" + volOpts, vol}) 1034 session.WaitWithDefaultTimeout() 1035 Expect(session).Should(Exit(0)) 1036 1037 kube := podmanTest.Podman([]string{"generate", "kube", vol}) 1038 kube.WaitWithDefaultTimeout() 1039 Expect(kube).Should(Exit(0)) 1040 1041 pvc := new(v1.PersistentVolumeClaim) 1042 err := yaml.Unmarshal(kube.Out.Contents(), pvc) 1043 Expect(err).To(BeNil()) 1044 Expect(pvc).To(HaveField("Name", vol)) 1045 Expect(pvc.Spec.AccessModes[0]).To(Equal(v1.ReadWriteOnce)) 1046 Expect(pvc.Spec.Resources.Requests.Storage().String()).To(Equal("1Gi")) 1047 1048 for k, v := range pvc.Annotations { 1049 switch k { 1050 case util.VolumeDeviceAnnotation: 1051 Expect(v).To(Equal(volDevice)) 1052 case util.VolumeTypeAnnotation: 1053 Expect(v).To(Equal(volType)) 1054 case util.VolumeMountOptsAnnotation: 1055 Expect(v).To(Equal(volOpts)) 1056 } 1057 } 1058 }) 1059 1060 It("podman generate kube on container with auto update labels", func() { 1061 top := podmanTest.Podman([]string{"run", "-dt", "--name", "top", "--label", "io.containers.autoupdate=local", ALPINE, "top"}) 1062 top.WaitWithDefaultTimeout() 1063 Expect(top).Should(Exit(0)) 1064 1065 kube := podmanTest.Podman([]string{"generate", "kube", "top"}) 1066 kube.WaitWithDefaultTimeout() 1067 Expect(kube).Should(Exit(0)) 1068 1069 pod := new(v1.Pod) 1070 err := yaml.Unmarshal(kube.Out.Contents(), pod) 1071 Expect(err).To(BeNil()) 1072 1073 Expect(pod.Annotations).To(HaveKeyWithValue("io.containers.autoupdate/top", "local")) 1074 }) 1075 1076 It("podman generate kube on pod with auto update labels in all containers", func() { 1077 pod1 := podmanTest.Podman([]string{"pod", "create", "--name", "pod1"}) 1078 pod1.WaitWithDefaultTimeout() 1079 Expect(pod1).Should(Exit(0)) 1080 1081 top1 := podmanTest.Podman([]string{"run", "-dt", "--name", "top1", "--pod", "pod1", "--label", "io.containers.autoupdate=registry", "--label", "io.containers.autoupdate.authfile=/some/authfile.json", ALPINE, "top"}) 1082 top1.WaitWithDefaultTimeout() 1083 Expect(top1).Should(Exit(0)) 1084 1085 top2 := podmanTest.Podman([]string{"run", "-dt", "--name", "top2", "--workdir", "/root", "--pod", "pod1", "--label", "io.containers.autoupdate=registry", "--label", "io.containers.autoupdate.authfile=/some/authfile.json", ALPINE, "top"}) 1086 top2.WaitWithDefaultTimeout() 1087 Expect(top2).Should(Exit(0)) 1088 1089 kube := podmanTest.Podman([]string{"generate", "kube", "pod1"}) 1090 kube.WaitWithDefaultTimeout() 1091 Expect(kube).Should(Exit(0)) 1092 1093 pod := new(v1.Pod) 1094 err := yaml.Unmarshal(kube.Out.Contents(), pod) 1095 Expect(err).To(BeNil()) 1096 Expect(pod.Spec.Containers[0]).To(HaveField("WorkingDir", "")) 1097 Expect(pod.Spec.Containers[1]).To(HaveField("WorkingDir", "/root")) 1098 1099 for _, ctr := range []string{"top1", "top2"} { 1100 Expect(pod.Annotations).To(HaveKeyWithValue("io.containers.autoupdate/"+ctr, "registry")) 1101 Expect(pod.Annotations).To(HaveKeyWithValue("io.containers.autoupdate.authfile/"+ctr, "/some/authfile.json")) 1102 } 1103 }) 1104 1105 It("podman generate kube can export env variables correctly", func() { 1106 // Fixes https://github.com/containers/podman/issues/12647 1107 // PR https://github.com/containers/podman/pull/12648 1108 1109 ctrName := "gen-kube-env-ctr" 1110 podName := "gen-kube-env" 1111 session1 := podmanTest.Podman([]string{"run", "-d", "--pod", "new:" + podName, "--name", ctrName, 1112 "-e", "FOO=bar", 1113 "-e", "HELLO=WORLD", 1114 "alpine", "top"}) 1115 session1.WaitWithDefaultTimeout() 1116 Expect(session1).Should(Exit(0)) 1117 1118 kube := podmanTest.Podman([]string{"generate", "kube", podName}) 1119 kube.WaitWithDefaultTimeout() 1120 Expect(kube).Should(Exit(0)) 1121 1122 pod := new(v1.Pod) 1123 err := yaml.Unmarshal(kube.Out.Contents(), pod) 1124 Expect(err).To(BeNil()) 1125 1126 Expect(pod.Spec.Containers[0].Env).To(HaveLen(2)) 1127 }) 1128 })