github.com/skyscape-cloud-services/terraform@v0.9.2-0.20170609144644-7ece028a1747/builtin/providers/kubernetes/resource_kubernetes_pod_test.go (about) 1 package kubernetes 2 3 import ( 4 "fmt" 5 "testing" 6 7 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 8 api "k8s.io/kubernetes/pkg/api/v1" 9 kubernetes "k8s.io/kubernetes/pkg/client/clientset_generated/clientset" 10 11 "github.com/hashicorp/terraform/helper/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccKubernetesPod_basic(t *testing.T) { 17 var conf api.Pod 18 19 podName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 20 secretName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 21 configMapName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 22 23 imageName1 := "nginx:1.7.9" 24 imageName2 := "nginx:1.11" 25 26 resource.Test(t, resource.TestCase{ 27 PreCheck: func() { testAccPreCheck(t) }, 28 Providers: testAccProviders, 29 CheckDestroy: testAccCheckKubernetesPodDestroy, 30 Steps: []resource.TestStep{ 31 { 32 Config: testAccKubernetesPodConfigBasic(secretName, configMapName, podName, imageName1), 33 Check: resource.ComposeAggregateTestCheckFunc( 34 testAccCheckKubernetesPodExists("kubernetes_pod.test", &conf), 35 resource.TestCheckResourceAttr("kubernetes_pod.test", "metadata.0.annotations.%", "0"), 36 resource.TestCheckResourceAttr("kubernetes_pod.test", "metadata.0.labels.%", "1"), 37 resource.TestCheckResourceAttr("kubernetes_pod.test", "metadata.0.labels.app", "pod_label"), 38 resource.TestCheckResourceAttr("kubernetes_pod.test", "metadata.0.name", podName), 39 resource.TestCheckResourceAttrSet("kubernetes_pod.test", "metadata.0.generation"), 40 resource.TestCheckResourceAttrSet("kubernetes_pod.test", "metadata.0.resource_version"), 41 resource.TestCheckResourceAttrSet("kubernetes_pod.test", "metadata.0.self_link"), 42 resource.TestCheckResourceAttrSet("kubernetes_pod.test", "metadata.0.uid"), 43 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.env.0.value_from.0.secret_key_ref.0.name", secretName), 44 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.env.1.value_from.0.config_map_key_ref.0.name", configMapName), 45 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.image", imageName1), 46 ), 47 }, 48 { 49 Config: testAccKubernetesPodConfigBasic(secretName, configMapName, podName, imageName2), 50 Check: resource.ComposeAggregateTestCheckFunc( 51 testAccCheckKubernetesPodExists("kubernetes_pod.test", &conf), 52 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.image", imageName2), 53 ), 54 }, 55 }, 56 }) 57 } 58 59 func TestAccKubernetesPod_importBasic(t *testing.T) { 60 resourceName := "kubernetes_pod.test" 61 podName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 62 imageName := "nginx:1.7.9" 63 64 resource.Test(t, resource.TestCase{ 65 PreCheck: func() { testAccPreCheck(t) }, 66 Providers: testAccProviders, 67 CheckDestroy: testAccCheckKubernetesPodDestroy, 68 Steps: []resource.TestStep{ 69 { 70 Config: testAccKubernetesPodConfigWithSecurityContext(podName, imageName), 71 }, 72 { 73 ResourceName: resourceName, 74 ImportState: true, 75 ImportStateVerify: true, 76 ImportStateVerifyIgnore: []string{"metadata.0.resource_version"}, 77 }, 78 }, 79 }) 80 } 81 82 func TestAccKubernetesPod_with_pod_security_context(t *testing.T) { 83 var conf api.Pod 84 85 podName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 86 imageName := "nginx:1.7.9" 87 88 resource.Test(t, resource.TestCase{ 89 PreCheck: func() { testAccPreCheck(t) }, 90 Providers: testAccProviders, 91 CheckDestroy: testAccCheckKubernetesPodDestroy, 92 Steps: []resource.TestStep{ 93 { 94 Config: testAccKubernetesPodConfigWithSecurityContext(podName, imageName), 95 Check: resource.ComposeAggregateTestCheckFunc( 96 testAccCheckKubernetesPodExists("kubernetes_pod.test", &conf), 97 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.security_context.0.run_as_non_root", "true"), 98 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.security_context.0.supplemental_groups.#", "1"), 99 ), 100 }, 101 }, 102 }) 103 } 104 105 func TestAccKubernetesPod_with_container_liveness_probe_using_exec(t *testing.T) { 106 var conf api.Pod 107 108 podName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 109 imageName := "gcr.io/google_containers/busybox" 110 111 resource.Test(t, resource.TestCase{ 112 PreCheck: func() { testAccPreCheck(t) }, 113 Providers: testAccProviders, 114 CheckDestroy: testAccCheckKubernetesPodDestroy, 115 Steps: []resource.TestStep{ 116 { 117 Config: testAccKubernetesPodConfigWithLivenessProbeUsingExec(podName, imageName), 118 Check: resource.ComposeAggregateTestCheckFunc( 119 testAccCheckKubernetesPodExists("kubernetes_pod.test", &conf), 120 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.args.#", "3"), 121 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.liveness_probe.#", "1"), 122 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.liveness_probe.0.exec.#", "1"), 123 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.liveness_probe.0.exec.0.command.#", "2"), 124 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.liveness_probe.0.exec.0.command.0", "cat"), 125 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.liveness_probe.0.exec.0.command.1", "/tmp/healthy"), 126 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.liveness_probe.0.failure_threshold", "3"), 127 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.liveness_probe.0.initial_delay_seconds", "5"), 128 ), 129 }, 130 }, 131 }) 132 } 133 134 func TestAccKubernetesPod_with_container_liveness_probe_using_http_get(t *testing.T) { 135 var conf api.Pod 136 137 podName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 138 imageName := "gcr.io/google_containers/liveness" 139 140 resource.Test(t, resource.TestCase{ 141 PreCheck: func() { testAccPreCheck(t) }, 142 Providers: testAccProviders, 143 CheckDestroy: testAccCheckKubernetesPodDestroy, 144 Steps: []resource.TestStep{ 145 { 146 Config: testAccKubernetesPodConfigWithLivenessProbeUsingHTTPGet(podName, imageName), 147 Check: resource.ComposeAggregateTestCheckFunc( 148 testAccCheckKubernetesPodExists("kubernetes_pod.test", &conf), 149 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.args.#", "1"), 150 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.liveness_probe.#", "1"), 151 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.liveness_probe.0.http_get.#", "1"), 152 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.liveness_probe.0.http_get.0.path", "/healthz"), 153 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.liveness_probe.0.http_get.0.port", "8080"), 154 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.liveness_probe.0.http_get.0.http_header.#", "1"), 155 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.liveness_probe.0.http_get.0.http_header.0.name", "X-Custom-Header"), 156 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.liveness_probe.0.http_get.0.http_header.0.value", "Awesome"), 157 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.liveness_probe.0.initial_delay_seconds", "3"), 158 ), 159 }, 160 }, 161 }) 162 } 163 164 func TestAccKubernetesPod_with_container_liveness_probe_using_tcp(t *testing.T) { 165 var conf api.Pod 166 167 podName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 168 imageName := "gcr.io/google_containers/liveness" 169 170 resource.Test(t, resource.TestCase{ 171 PreCheck: func() { testAccPreCheck(t) }, 172 Providers: testAccProviders, 173 CheckDestroy: testAccCheckKubernetesPodDestroy, 174 Steps: []resource.TestStep{ 175 { 176 Config: testAccKubernetesPodConfigWithLivenessProbeUsingTCP(podName, imageName), 177 Check: resource.ComposeAggregateTestCheckFunc( 178 testAccCheckKubernetesPodExists("kubernetes_pod.test", &conf), 179 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.args.#", "1"), 180 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.liveness_probe.#", "1"), 181 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.liveness_probe.0.tcp_socket.#", "1"), 182 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.liveness_probe.0.tcp_socket.0.port", "8080"), 183 ), 184 }, 185 }, 186 }) 187 } 188 189 func TestAccKubernetesPod_with_container_lifecycle(t *testing.T) { 190 var conf api.Pod 191 192 podName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 193 imageName := "gcr.io/google_containers/liveness" 194 195 resource.Test(t, resource.TestCase{ 196 PreCheck: func() { testAccPreCheck(t) }, 197 Providers: testAccProviders, 198 CheckDestroy: testAccCheckKubernetesPodDestroy, 199 Steps: []resource.TestStep{ 200 { 201 Config: testAccKubernetesPodConfigWithLifeCycle(podName, imageName), 202 Check: resource.ComposeAggregateTestCheckFunc( 203 testAccCheckKubernetesPodExists("kubernetes_pod.test", &conf), 204 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.lifecycle.#", "1"), 205 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.lifecycle.0.post_start.#", "1"), 206 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.lifecycle.0.post_start.0.exec.#", "1"), 207 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.lifecycle.0.post_start.0.exec.0.command.#", "2"), 208 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.lifecycle.0.post_start.0.exec.0.command.0", "ls"), 209 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.lifecycle.0.post_start.0.exec.0.command.1", "-al"), 210 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.lifecycle.0.pre_stop.#", "1"), 211 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.lifecycle.0.pre_stop.0.exec.#", "1"), 212 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.lifecycle.0.pre_stop.0.exec.0.command.#", "1"), 213 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.lifecycle.0.pre_stop.0.exec.0.command.0", "date"), 214 ), 215 }, 216 }, 217 }) 218 } 219 220 func TestAccKubernetesPod_with_container_security_context(t *testing.T) { 221 var conf api.Pod 222 223 podName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 224 imageName := "nginx:1.7.9" 225 226 resource.Test(t, resource.TestCase{ 227 PreCheck: func() { testAccPreCheck(t) }, 228 Providers: testAccProviders, 229 CheckDestroy: testAccCheckKubernetesPodDestroy, 230 Steps: []resource.TestStep{ 231 { 232 Config: testAccKubernetesPodConfigWithContainerSecurityContext(podName, imageName), 233 Check: resource.ComposeAggregateTestCheckFunc( 234 testAccCheckKubernetesPodExists("kubernetes_pod.test", &conf), 235 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.security_context.#", "1"), 236 ), 237 }, 238 }, 239 }) 240 } 241 242 func TestAccKubernetesPod_with_volume_mount(t *testing.T) { 243 var conf api.Pod 244 245 podName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 246 secretName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 247 248 imageName := "nginx:1.7.9" 249 250 resource.Test(t, resource.TestCase{ 251 PreCheck: func() { testAccPreCheck(t) }, 252 Providers: testAccProviders, 253 CheckDestroy: testAccCheckKubernetesPodDestroy, 254 Steps: []resource.TestStep{ 255 { 256 Config: testAccKubernetesPodConfigWithVolumeMounts(secretName, podName, imageName), 257 Check: resource.ComposeAggregateTestCheckFunc( 258 testAccCheckKubernetesPodExists("kubernetes_pod.test", &conf), 259 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.image", imageName), 260 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.volume_mount.#", "1"), 261 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.volume_mount.0.mount_path", "/tmp/my_path"), 262 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.volume_mount.0.name", "db"), 263 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.volume_mount.0.read_only", "false"), 264 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.volume_mount.0.sub_path", ""), 265 ), 266 }, 267 }, 268 }) 269 } 270 271 func TestAccKubernetesPod_with_resource_requirements(t *testing.T) { 272 var conf api.Pod 273 274 podName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 275 276 imageName := "nginx:1.7.9" 277 278 resource.Test(t, resource.TestCase{ 279 PreCheck: func() { testAccPreCheck(t) }, 280 Providers: testAccProviders, 281 CheckDestroy: testAccCheckKubernetesPodDestroy, 282 Steps: []resource.TestStep{ 283 { 284 Config: testAccKubernetesPodConfigWithResourceRequirements(podName, imageName), 285 Check: resource.ComposeAggregateTestCheckFunc( 286 testAccCheckKubernetesPodExists("kubernetes_pod.test", &conf), 287 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.image", imageName), 288 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.resources.0.requests.0.memory", "50Mi"), 289 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.resources.0.requests.0.cpu", "250m"), 290 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.resources.0.limits.0.memory", "512Mi"), 291 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.resources.0.limits.0.cpu", "500m"), 292 ), 293 }, 294 }, 295 }) 296 } 297 298 func TestAccKubernetesPod_with_empty_dir_volume(t *testing.T) { 299 var conf api.Pod 300 301 podName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 302 imageName := "nginx:1.7.9" 303 304 resource.Test(t, resource.TestCase{ 305 PreCheck: func() { testAccPreCheck(t) }, 306 Providers: testAccProviders, 307 CheckDestroy: testAccCheckKubernetesPodDestroy, 308 Steps: []resource.TestStep{ 309 { 310 Config: testAccKubernetesPodConfigWithEmptyDirVolumes(podName, imageName), 311 Check: resource.ComposeAggregateTestCheckFunc( 312 testAccCheckKubernetesPodExists("kubernetes_pod.test", &conf), 313 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.image", imageName), 314 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.volume_mount.#", "1"), 315 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.volume_mount.0.mount_path", "/cache"), 316 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.volume_mount.0.name", "cache-volume"), 317 resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.volume.0.empty_dir.0.medium", "Memory"), 318 ), 319 }, 320 }, 321 }) 322 } 323 324 func testAccCheckKubernetesPodDestroy(s *terraform.State) error { 325 conn := testAccProvider.Meta().(*kubernetes.Clientset) 326 327 for _, rs := range s.RootModule().Resources { 328 if rs.Type != "kubernetes_pod" { 329 continue 330 } 331 namespace, name := idParts(rs.Primary.ID) 332 resp, err := conn.CoreV1().Pods(namespace).Get(name, metav1.GetOptions{}) 333 if err == nil { 334 if resp.Namespace == namespace && resp.Name == name { 335 return fmt.Errorf("Pod still exists: %s: %#v", rs.Primary.ID, resp.Status.Phase) 336 } 337 } 338 } 339 340 return nil 341 } 342 343 func testAccCheckKubernetesPodExists(n string, obj *api.Pod) resource.TestCheckFunc { 344 return func(s *terraform.State) error { 345 rs, ok := s.RootModule().Resources[n] 346 if !ok { 347 return fmt.Errorf("Not found: %s", n) 348 } 349 350 conn := testAccProvider.Meta().(*kubernetes.Clientset) 351 352 namespace, name := idParts(rs.Primary.ID) 353 out, err := conn.CoreV1().Pods(namespace).Get(name, metav1.GetOptions{}) 354 if err != nil { 355 return err 356 } 357 *obj = *out 358 return nil 359 } 360 } 361 362 func testAccKubernetesPodConfigBasic(secretName, configMapName, podName, imageName string) string { 363 return fmt.Sprintf(` 364 365 resource "kubernetes_secret" "test" { 366 metadata { 367 name = "%s" 368 } 369 370 data { 371 one = "first" 372 } 373 } 374 375 resource "kubernetes_config_map" "test" { 376 metadata { 377 name = "%s" 378 } 379 380 data { 381 one = "ONE" 382 } 383 } 384 385 resource "kubernetes_pod" "test" { 386 metadata { 387 labels { 388 app = "pod_label" 389 } 390 391 name = "%s" 392 } 393 394 spec { 395 container { 396 image = "%s" 397 name = "containername" 398 399 env = [{ 400 name = "EXPORTED_VARIBALE_FROM_SECRET" 401 402 value_from { 403 secret_key_ref { 404 name = "${kubernetes_secret.test.metadata.0.name}" 405 key = "one" 406 } 407 } 408 }, 409 { 410 name = "EXPORTED_VARIBALE_FROM_CONFIG_MAP" 411 412 value_from { 413 config_map_key_ref { 414 name = "${kubernetes_config_map.test.metadata.0.name}" 415 key = "one" 416 } 417 } 418 }, 419 ] 420 } 421 volume { 422 name = "db" 423 secret = { 424 secret_name = "${kubernetes_secret.test.metadata.0.name}" 425 } 426 } 427 } 428 } 429 `, secretName, configMapName, podName, imageName) 430 } 431 432 func testAccKubernetesPodConfigWithSecurityContext(podName, imageName string) string { 433 return fmt.Sprintf(` 434 resource "kubernetes_pod" "test" { 435 metadata { 436 labels { 437 app = "pod_label" 438 } 439 440 name = "%s" 441 } 442 spec { 443 security_context { 444 run_as_non_root = true 445 run_as_user = 101 446 supplemental_groups = [101] 447 } 448 container { 449 image = "%s" 450 name = "containername" 451 } 452 } 453 } 454 `, podName, imageName) 455 } 456 457 func testAccKubernetesPodConfigWithLivenessProbeUsingExec(podName, imageName string) string { 458 return fmt.Sprintf(` 459 resource "kubernetes_pod" "test" { 460 metadata { 461 labels { 462 app = "pod_label" 463 } 464 465 name = "%s" 466 } 467 468 spec { 469 container { 470 image = "%s" 471 name = "containername" 472 args = ["/bin/sh", "-c", "touch /tmp/healthy; sleep 300; rm -rf /tmp/healthy; sleep 600"] 473 474 liveness_probe { 475 exec { 476 command = ["cat", "/tmp/healthy"] 477 } 478 479 initial_delay_seconds = 5 480 period_seconds = 5 481 } 482 } 483 } 484 } 485 `, podName, imageName) 486 } 487 488 func testAccKubernetesPodConfigWithLivenessProbeUsingHTTPGet(podName, imageName string) string { 489 return fmt.Sprintf(` 490 resource "kubernetes_pod" "test" { 491 metadata { 492 labels { 493 app = "pod_label" 494 } 495 496 name = "%s" 497 } 498 499 spec { 500 container { 501 image = "%s" 502 name = "containername" 503 args = ["/server"] 504 505 liveness_probe { 506 http_get { 507 path = "/healthz" 508 port = 8080 509 510 http_header { 511 name = "X-Custom-Header" 512 value = "Awesome" 513 } 514 } 515 initial_delay_seconds = 3 516 period_seconds = 3 517 } 518 } 519 } 520 } 521 `, podName, imageName) 522 } 523 524 func testAccKubernetesPodConfigWithLivenessProbeUsingTCP(podName, imageName string) string { 525 return fmt.Sprintf(` 526 resource "kubernetes_pod" "test" { 527 metadata { 528 labels { 529 app = "pod_label" 530 } 531 532 name = "%s" 533 } 534 spec { 535 container { 536 image = "%s" 537 name = "containername" 538 args = ["/server"] 539 540 liveness_probe { 541 tcp_socket { 542 port = 8080 543 } 544 545 initial_delay_seconds = 3 546 period_seconds = 3 547 } 548 } 549 } 550 } 551 `, podName, imageName) 552 } 553 554 func testAccKubernetesPodConfigWithLifeCycle(podName, imageName string) string { 555 return fmt.Sprintf(` 556 resource "kubernetes_pod" "test" { 557 metadata { 558 labels { 559 app = "pod_label" 560 } 561 562 name = "%s" 563 } 564 spec { 565 container { 566 image = "%s" 567 name = "containername" 568 args = ["/server"] 569 570 lifecycle { 571 post_start { 572 exec { 573 command = ["ls", "-al"] 574 } 575 } 576 pre_stop { 577 exec { 578 command = ["date"] 579 } 580 } 581 } 582 } 583 } 584 } 585 586 `, podName, imageName) 587 } 588 589 func testAccKubernetesPodConfigWithContainerSecurityContext(podName, imageName string) string { 590 return fmt.Sprintf(` 591 resource "kubernetes_pod" "test" { 592 metadata { 593 labels { 594 app = "pod_label" 595 } 596 597 name = "%s" 598 } 599 spec { 600 container { 601 image = "%s" 602 name = "containername" 603 604 security_context { 605 privileged = true 606 run_as_user = 1 607 se_linux_options { 608 level = "s0:c123,c456" 609 } 610 } 611 } 612 } 613 } 614 615 616 `, podName, imageName) 617 } 618 619 func testAccKubernetesPodConfigWithVolumeMounts(secretName, podName, imageName string) string { 620 return fmt.Sprintf(` 621 622 resource "kubernetes_secret" "test" { 623 metadata { 624 name = "%s" 625 } 626 627 data { 628 one = "first" 629 } 630 } 631 632 resource "kubernetes_pod" "test" { 633 metadata { 634 labels { 635 app = "pod_label" 636 } 637 638 name = "%s" 639 } 640 641 spec { 642 container { 643 image = "%s" 644 name = "containername" 645 volume_mount { 646 mount_path = "/tmp/my_path" 647 name = "db" 648 } 649 } 650 volume { 651 name = "db" 652 secret = { 653 secret_name = "${kubernetes_secret.test.metadata.0.name}" 654 } 655 } 656 } 657 } 658 `, secretName, podName, imageName) 659 } 660 661 func testAccKubernetesPodConfigWithResourceRequirements(podName, imageName string) string { 662 return fmt.Sprintf(` 663 664 resource "kubernetes_pod" "test" { 665 metadata { 666 labels { 667 app = "pod_label" 668 } 669 670 name = "%s" 671 } 672 673 spec { 674 container { 675 image = "%s" 676 name = "containername" 677 678 resources{ 679 limits{ 680 cpu = "0.5" 681 memory = "512Mi" 682 } 683 requests{ 684 cpu = "250m" 685 memory = "50Mi" 686 } 687 } 688 689 } 690 } 691 } 692 `, podName, imageName) 693 } 694 695 func testAccKubernetesPodConfigWithEmptyDirVolumes(podName, imageName string) string { 696 return fmt.Sprintf(` 697 resource "kubernetes_pod" "test" { 698 metadata { 699 labels { 700 app = "pod_label" 701 } 702 703 name = "%s" 704 } 705 706 spec { 707 container { 708 image = "%s" 709 name = "containername" 710 volume_mount { 711 mount_path = "/cache" 712 name = "cache-volume" 713 } 714 } 715 volume { 716 name = "cache-volume" 717 empty_dir = { 718 medium = "Memory" 719 } 720 } 721 } 722 } 723 `, podName, imageName) 724 }