Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kubernetes/data_source_kubernetes_persistent_volume.go #2118

Merged
merged 22 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2118.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
`kubernetes/data_source_kubernetes_persistent_volume.go`: Add data source for Kubernetes Persistent Volume Resource
```
151 changes: 151 additions & 0 deletions kubernetes/data_source_kubernetes_persistent_volume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package kubernetes

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func dataSourceKubernetesPersistentVolume() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceKubernetesPersistentVolumeRead,

Schema: map[string]*schema.Schema{
"metadata": metadataSchema("persistent volume", false),
"spec": {
Type: schema.TypeList,
Description: "Spec of the persistent volume owned by the cluster",
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"access_modes": {
Type: schema.TypeSet,
Description: "Contains all ways the volume can be mounted. More info: http://kubernetes.io/docs/user-guide/persistent-volumes#access-modes",
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
"ReadWriteOnce",
"ReadOnlyMany",
"ReadWriteMany",
}, false),
},
Set: schema.HashString,
},
"capacity": {
Type: schema.TypeMap,
Description: "A description of the persistent volume's resources and capacity. More info: http://kubernetes.io/docs/user-guide/persistent-volumes#capacity",
Required: true,
Elem: schema.TypeString,
ValidateFunc: validateResourceList,
DiffSuppressFunc: suppressEquivalentResourceQuantity,
},
"persistent_volume_reclaim_policy": {
Type: schema.TypeString,
Description: "What happens to a persistent volume when released from its claim. Valid options are Retain (default) and Recycle. Recycling must be supported by the volume plugin underlying this persistent volume. More info: http://kubernetes.io/docs/user-guide/persistent-volumes#recycling-policy",
Optional: true,
Default: "Retain",
ValidateFunc: validation.StringInSlice([]string{
"Recycle",
"Delete",
"Retain",
}, false),
},
"claim_ref": {
Type: schema.TypeList,
Description: "A reference to the persistent volume claim details for statically managed PVs. More Info: https://kubernetes.io/docs/concepts/storage/persistent-volumes/#binding",
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"namespace": {
Type: schema.TypeString,
Description: "The namespace of the PersistentVolumeClaim. Uses 'default' namespace if none is specified.",
Elem: schema.TypeString,
Optional: true,
Default: "default",
},
"name": {
Type: schema.TypeString,
Description: "The name of the PersistentVolumeClaim",
Elem: schema.TypeString,
Required: true,
},
},
},
},
"persistent_volume_source": {
Type: schema.TypeList,
Description: "The specification of a persistent volume.",
Required: true,
MaxItems: 1,
Elem: persistentVolumeSourceSchema(),
},
"storage_class_name": {
Type: schema.TypeString,
Description: "A description of the persistent volume's class. More info: https://kubernetes.io/docs/concepts/storage/persistent-volumes/#class",
Optional: true,
},
"node_affinity": {
Type: schema.TypeList,
Description: "A description of the persistent volume's node affinity. More info: https://kubernetes.io/docs/concepts/storage/volumes/#local",
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"required": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"node_selector_term": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: nodeSelectorTermFields(),
},
},
},
},
},
},
},
},
"mount_options": {
Type: schema.TypeSet,
Description: "A list of mount options, e.g. [\"ro\", \"soft\"]. Not validated - mount will simply fail if one is invalid.",
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"volume_mode": {
Type: schema.TypeString,
Description: "Defines if a volume is intended to be used with a formatted filesystem. or to remain in raw block state.",
Optional: true,
ForceNew: true,
Default: "Filesystem",
ValidateFunc: validation.StringInSlice([]string{
"Block",
"Filesystem",
}, false),
},
},
},
},
},
}
}

func dataSourceKubernetesPersistentVolumeRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
name := d.Get("metadata.0.name").(string)
d.SetId(name)
return resourceKubernetesPersistentVolumeRead(ctx, d, meta)
}
75 changes: 75 additions & 0 deletions kubernetes/data_source_kubernetes_persistent_volume_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package kubernetes

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccKubernetesDataSourcePV_basic(t *testing.T) {
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccKubernetesDataSourcePVConfig_basic(name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.kubernetes_persistent_volume_v1.test", "metadata.0.name", name),
resource.TestCheckResourceAttrSet("data.kubernetes_persistent_volume_v1.test", "metadata.0.generation"),
resource.TestCheckResourceAttrSet("data.kubernetes_persistent_volume_v1.test", "metadata.0.resource_version"),
resource.TestCheckResourceAttrSet("data.kubernetes_persistent_volume_v1.test", "metadata.0.uid"),
resource.TestCheckResourceAttr("data.kubernetes_persistent_volume_v1.test", "spec.0.access_modes.#", "1"),
resource.TestCheckResourceAttr("data.kubernetes_persistent_volume_v1.test", "spec.0.access_modes.0", "ReadWriteOnce"),
resource.TestCheckResourceAttr("data.kubernetes_persistent_volume_v1.test", "spec.0.capacity.%", "1"),
resource.TestCheckResourceAttr("data.kubernetes_persistent_volume_v1.test", "spec.0.capacity.storage", "5Gi"),
resource.TestCheckResourceAttr("kubernetes_persistent_volume_v1.test", "metadata.0.name", name),
resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_v1.test", "metadata.0.generation"),
resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_v1.test", "metadata.0.resource_version"),
resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_v1.test", "metadata.0.uid"),
resource.TestCheckResourceAttr("kubernetes_persistent_volume_v1.test", "spec.0.access_modes.#", "1"),
resource.TestCheckResourceAttr("kubernetes_persistent_volume_v1.test", "spec.0.access_modes.0", "ReadWriteOnce"),
resource.TestCheckResourceAttr("kubernetes_persistent_volume_v1.test", "spec.0.capacity.%", "1"),
resource.TestCheckResourceAttr("kubernetes_persistent_volume_v1.test", "spec.0.capacity.storage", "5Gi"),
),
},
},
})
}

func testAccKubernetesDataSourcePVConfig_basic(name string) string {
return fmt.Sprintf(`
resource "kubernetes_persistent_volume_v1" "test" {
metadata {
name = "%s"
}

spec {

capacity = {
storage = "5Gi"
}

access_modes = ["ReadWriteOnce"]
persistent_volume_source {
vsphere_volume {
volume_path = "/absolute/path"
}
}

}
}

data "kubernetes_persistent_volume_v1" "test" {
metadata {
name = "${kubernetes_persistent_volume_v1.test.metadata.0.name}"
}
}
`, name)
}
1 change: 1 addition & 0 deletions kubernetes/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func Provider() *schema.Provider {
"kubernetes_pod_v1": dataSourceKubernetesPod(),
"kubernetes_service_account": dataSourceKubernetesServiceAccount(),
"kubernetes_service_account_v1": dataSourceKubernetesServiceAccount(),
"kubernetes_persistent_volume_v1": dataSourceKubernetesPersistentVolume(),
"kubernetes_persistent_volume_claim": dataSourceKubernetesPersistentVolumeClaim(),
"kubernetes_persistent_volume_claim_v1": dataSourceKubernetesPersistentVolumeClaim(),
"kubernetes_nodes": dataSourceKubernetesNodes(),
Expand Down
Loading