Skip to content

Commit

Permalink
Make target pod state / phase configurable (hashicorp#2200)
Browse files Browse the repository at this point in the history
  • Loading branch information
arybolovlev authored Jul 28, 2023
1 parent 67ae35a commit e0afbe9
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 15 deletions.
7 changes: 7 additions & 0 deletions .changelog/2200.txt
Original file line number Diff line number Diff line change
@@ -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.
```
Binary file added __debug_bin
Binary file not shown.
33 changes: 19 additions & 14 deletions kubernetes/resource_kubernetes_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
},
},
}
}
Expand All @@ -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,
}
Expand All @@ -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{})
Expand Down
44 changes: 44 additions & 0 deletions kubernetes/resource_kubernetes_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
12 changes: 12 additions & 0 deletions kubernetes/structures_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/pod.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions website/docs/r/pod_v1.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit e0afbe9

Please sign in to comment.