diff --git a/.changelog/2200.txt b/.changelog/2200.txt new file mode 100644 index 0000000000..e91d72ccbe --- /dev/null +++ b/.changelog/2200.txt @@ -0,0 +1,7 @@ +```release-note:enhancement +`resource/kubernetes_pod`: add a new attribute `target_state` to specify the Pod phase(s) that indicate whether it was successfully created. +``` + +```release-note:enhancement +`resource/kubernetes_pod_v1`: add a new attribute `target_state` to specify the Pod phase(s) that indicate whether it was successfully created. +``` diff --git a/__debug_bin b/__debug_bin new file mode 100755 index 0000000000..a841db8ec9 Binary files /dev/null and b/__debug_bin differ diff --git a/kubernetes/resource_kubernetes_pod.go b/kubernetes/resource_kubernetes_pod.go index fec2a2d42a..47cb30506f 100644 --- a/kubernetes/resource_kubernetes_pod.go +++ b/kubernetes/resource_kubernetes_pod.go @@ -13,7 +13,8 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - api "k8s.io/api/core/v1" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" pkgApi "k8s.io/apimachinery/pkg/types" @@ -56,11 +57,21 @@ func resourceKubernetesPodSchemaV1() map[string]*schema.Schema { Schema: podSpecFields(false, false), }, }, - "legacy_lifecycle_states": { - Type: schema.TypeBool, - Description: "Setting this attribute to `false` would set the target pod lifecycle state as [\"Running\", \"Succeeded\", \"Failed\"]. The default value of `true` would leave the target pod lifecycle state as [\"Running\"]`.", + "target_state": { + Type: schema.TypeList, + Description: fmt.Sprintf("A list of the pod phases that indicate whether it was successfully created. Options: %q, %q, %q, %q, %q. Default: %q. More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-phase", corev1.PodPending, corev1.PodRunning, corev1.PodSucceeded, corev1.PodFailed, corev1.PodUnknown, corev1.PodRunning), Optional: true, - Default: true, + MinItems: 1, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validation.StringInSlice([]string{ + string(corev1.PodPending), + string(corev1.PodRunning), + string(corev1.PodSucceeded), + string(corev1.PodFailed), + string(corev1.PodUnknown), + }, false), + }, }, } } @@ -77,7 +88,7 @@ func resourceKubernetesPodCreate(ctx context.Context, d *schema.ResourceData, me return diag.FromErr(err) } - pod := api.Pod{ + pod := corev1.Pod{ ObjectMeta: metadata, Spec: *spec, } @@ -92,15 +103,9 @@ func resourceKubernetesPodCreate(ctx context.Context, d *schema.ResourceData, me d.SetId(buildId(out.ObjectMeta)) - target := []string{"Running"} - - if d.Get("legacy_lifecycle_states").(bool) != true { - target = []string{"Running", "Succeeded", "Failed"} - } - stateConf := &resource.StateChangeConf{ - Target: target, - Pending: []string{"Pending"}, + Target: expandPodTargetState(d.Get("target_state").([]interface{})), + Pending: []string{string(corev1.PodPending)}, Timeout: d.Timeout(schema.TimeoutCreate), Refresh: func() (interface{}, string, error) { out, err := conn.CoreV1().Pods(metadata.Namespace).Get(ctx, metadata.Name, metav1.GetOptions{}) diff --git a/kubernetes/resource_kubernetes_pod_test.go b/kubernetes/resource_kubernetes_pod_test.go index 131a8a70e2..135cd5f28e 100644 --- a/kubernetes/resource_kubernetes_pod_test.go +++ b/kubernetes/resource_kubernetes_pod_test.go @@ -1534,6 +1534,34 @@ func TestAccKubernetesPod_with_ephemeral_storage(t *testing.T) { }) } +func TestAccKubernetesPod_phase(t *testing.T) { + name := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "kubernetes_pod_v1.test" + image := "this-fake-image-has-never-exist" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccCheckKubernetesPodDestroy, + Steps: []resource.TestStep{ + { + Config: testAccKubernetesPodConfigPhase(name, image), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrSet(resourceName, "metadata.0.generation"), + resource.TestCheckResourceAttrSet(resourceName, "metadata.0.resource_version"), + resource.TestCheckResourceAttrSet(resourceName, "metadata.0.uid"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"metadata.0.resource_version", "target_state"}, + }, + }, + }) +} + func createRuncRuntimeClass(rn string) error { conn, err := testAccProvider.Meta().(KubeClientsets).MainClientset() if err != nil { @@ -3400,3 +3428,19 @@ func testAccKubernetesPodEphemeralStorageClass(name string) string { } `, name) } + +func testAccKubernetesPodConfigPhase(name, imageName string) string { + return fmt.Sprintf(`resource "kubernetes_pod_v1" "test" { + metadata { + name = "%s" + } + spec { + container { + image = "%s" + name = "containername" + } + } + target_state = ["Pending"] +} +`, name, imageName) +} diff --git a/kubernetes/structures_pod.go b/kubernetes/structures_pod.go index 97188b591c..86221bbc60 100644 --- a/kubernetes/structures_pod.go +++ b/kubernetes/structures_pod.go @@ -694,6 +694,18 @@ func flattenPodEphemeralVolumeSource(in *v1.EphemeralVolumeSource) []interface{} // Expanders +func expandPodTargetState(p []interface{}) []string { + if len(p) > 0 { + t := make([]string, len(p)) + for i, v := range p { + t[i] = v.(string) + } + return t + } + + return []string{string(v1.PodRunning)} +} + func expandPodSpec(p []interface{}) (*v1.PodSpec, error) { obj := &v1.PodSpec{} if len(p) == 0 || p[0] == nil { diff --git a/website/docs/r/pod.html.markdown b/website/docs/r/pod.html.markdown index 96e95e18bf..33bbecb108 100644 --- a/website/docs/r/pod.html.markdown +++ b/website/docs/r/pod.html.markdown @@ -169,7 +169,7 @@ The following arguments are supported: * `metadata` - (Required) Standard pod's metadata. For more info see [Kubernetes reference](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#metadata) * `spec` - (Required) Spec of the pod owned by the cluster -* `legacy_lifecycle_states` - (Optional) Setting this attribute to `false` would set the target pod lifecycle state as `["Running", "Succeeded", "Failed"]`. The default value of `true` would leave the target pod lifecycle state as `["Running"]`. +* `target_state` - (Optional) A list of the pod phases that indicate whether it was successfully created. Options: "Pending", "Running", "Succeeded", "Failed", "Unknown". Default: "Running". For more info see [Kubernetes reference](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-phase") ## Nested Blocks diff --git a/website/docs/r/pod_v1.html.markdown b/website/docs/r/pod_v1.html.markdown index 15715fb644..bb71a56963 100644 --- a/website/docs/r/pod_v1.html.markdown +++ b/website/docs/r/pod_v1.html.markdown @@ -169,6 +169,7 @@ The following arguments are supported: * `metadata` - (Required) Standard pod's metadata. For more info see [Kubernetes reference](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#metadata) * `spec` - (Required) Spec of the pod owned by the cluster +* `target_state` - (Optional) A list of the pod phases that indicate whether it was successfully created. Options: "Pending", "Running", "Succeeded", "Failed", "Unknown". Default: "Running". For more info see [Kubernetes reference](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-phase") ## Nested Blocks