Skip to content

Commit b7421c2

Browse files
dvulpealexsomesan
andauthored
add support for setting the persistent volume claimRef (#1020)
* add support for setting the persistent volume claimRef * Adapt to recent test framework changes Co-authored-by: Alex Somesan <[email protected]>
1 parent 645772c commit b7421c2

6 files changed

+184
-1
lines changed

kubernetes/resource_kubernetes_persistent_volume.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,28 @@ func resourceKubernetesPersistentVolume() *schema.Resource {
114114
"Retain",
115115
}, false),
116116
},
117+
"claim_ref": {
118+
Type: schema.TypeList,
119+
Description: "A reference to the persistent volume claim details for statically managed PVs. More Info: https://kubernetes.io/docs/concepts/storage/persistent-volumes/#binding",
120+
Optional: true,
121+
MaxItems: 1,
122+
Elem: &schema.Resource{
123+
Schema: map[string]*schema.Schema{
124+
"namespace": {
125+
Type: schema.TypeString,
126+
Description: "The namespace of the PersistentVolumeClaim",
127+
Elem: schema.TypeString,
128+
Required: true,
129+
},
130+
"name": {
131+
Type: schema.TypeString,
132+
Description: "The name of the PersistentVolumeClaim",
133+
Elem: schema.TypeString,
134+
Required: true,
135+
},
136+
},
137+
},
138+
},
117139
"persistent_volume_source": {
118140
Type: schema.TypeList,
119141
Description: "The specification of a persistent volume.",

kubernetes/resource_kubernetes_persistent_volume_claim_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,11 @@ resource "kubernetes_persistent_volume" "test" {
716716
}
717717
}
718718
}
719+
lifecycle {
720+
ignore_changes = [
721+
spec[0].claim_ref,
722+
]
723+
}
719724
}
720725
resource "kubernetes_persistent_volume_claim" "test" {
721726
wait_until_bound = true

kubernetes/resource_kubernetes_persistent_volume_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,50 @@ func TestAccKubernetesPersistentVolume_regression(t *testing.T) {
879879
})
880880
}
881881

882+
func TestAccKubernetesPersistentVolume_hostPath_claimRef(t *testing.T) {
883+
var conf api.PersistentVolume
884+
randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
885+
name := fmt.Sprintf("tf-acc-test-%s", randString)
886+
claimNamespace := "default"
887+
claimName := "expected-claim-name"
888+
889+
const resourceName = "kubernetes_persistent_volume.test"
890+
resource.Test(t, resource.TestCase{
891+
PreCheck: func() { testAccPreCheck(t) },
892+
IDRefreshName: resourceName,
893+
ProviderFactories: testAccProviderFactories,
894+
CheckDestroy: testAccCheckKubernetesPersistentVolumeDestroy,
895+
Steps: []resource.TestStep{
896+
// create a volume without a claimRef
897+
{
898+
Config: testAccKubernetesPersistentVolumeConfig_hostPath_basic(name),
899+
Check: resource.ComposeAggregateTestCheckFunc(
900+
testAccCheckKubernetesPersistentVolumeExists(resourceName, &conf),
901+
resource.TestCheckResourceAttr(resourceName, "spec.0.claim_ref.#", "0"),
902+
),
903+
},
904+
// set the claimRef and assert it's present
905+
{
906+
Config: testAccKubernetesPersistentVolumeConfig_hostPath_claimRef(name, claimNamespace, claimName),
907+
Check: resource.ComposeAggregateTestCheckFunc(
908+
testAccCheckKubernetesPersistentVolumeExists(resourceName, &conf),
909+
resource.TestCheckResourceAttr(resourceName, "spec.0.claim_ref.#", "1"),
910+
resource.TestCheckResourceAttr(resourceName, "spec.0.claim_ref.0.namespace", claimNamespace),
911+
resource.TestCheckResourceAttr(resourceName, "spec.0.claim_ref.0.name", claimName),
912+
),
913+
},
914+
// unset the claimRef and assert it's absent
915+
{
916+
Config: testAccKubernetesPersistentVolumeConfig_hostPath_basic(name),
917+
Check: resource.ComposeAggregateTestCheckFunc(
918+
testAccCheckKubernetesPersistentVolumeExists(resourceName, &conf),
919+
resource.TestCheckResourceAttr(resourceName, "spec.0.claim_ref.#", "0"),
920+
),
921+
},
922+
},
923+
})
924+
}
925+
882926
func testAccCheckKubernetesPersistentVolumeForceNew(old, new *api.PersistentVolume, wantNew bool) resource.TestCheckFunc {
883927
return func(s *terraform.State) error {
884928
if wantNew {
@@ -1718,6 +1762,52 @@ func testAccKubernetesPersistentVolumeConfig_hostPath_mountOptions(name string)
17181762
}`, name)
17191763
}
17201764

1765+
func testAccKubernetesPersistentVolumeConfig_hostPath_basic(name string) string {
1766+
return fmt.Sprintf(`resource "kubernetes_persistent_volume" "test" {
1767+
metadata {
1768+
name = "%s"
1769+
}
1770+
spec {
1771+
capacity = {
1772+
storage = "1Gi"
1773+
}
1774+
access_modes = ["ReadWriteMany"]
1775+
mount_options = ["foo"]
1776+
1777+
persistent_volume_source {
1778+
host_path {
1779+
path = "/mnt/local-volume"
1780+
}
1781+
}
1782+
}
1783+
}`, name)
1784+
}
1785+
1786+
func testAccKubernetesPersistentVolumeConfig_hostPath_claimRef(name string, claimNamespace string, claimName string) string {
1787+
return fmt.Sprintf(`resource "kubernetes_persistent_volume" "test" {
1788+
metadata {
1789+
name = "%s"
1790+
}
1791+
spec {
1792+
capacity = {
1793+
storage = "1Gi"
1794+
}
1795+
access_modes = ["ReadWriteMany"]
1796+
mount_options = ["foo"]
1797+
claim_ref {
1798+
name = "%s"
1799+
namespace = "%s"
1800+
}
1801+
1802+
persistent_volume_source {
1803+
host_path {
1804+
path = "/mnt/local-volume"
1805+
}
1806+
}
1807+
}
1808+
}`, name, claimName, claimNamespace)
1809+
}
1810+
17211811
func testAccKubernetesPersistentVolume_regression(provider, name, path, typ string) string {
17221812
return fmt.Sprintf(`resource "kubernetes_persistent_volume" "test" {
17231813
provider = %s

kubernetes/structure_persistent_volume_spec.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ func flattenPersistentVolumeSpec(in v1.PersistentVolumeSpec) []interface{} {
394394
if in.VolumeMode != nil {
395395
att["volume_mode"] = in.VolumeMode
396396
}
397+
if in.ClaimRef != nil {
398+
att["claim_ref"] = flattenObjectRef(in.ClaimRef)
399+
}
397400
return []interface{}{att}
398401
}
399402

@@ -1010,9 +1013,23 @@ func expandPersistentVolumeSpec(l []interface{}) (*v1.PersistentVolumeSpec, erro
10101013
volumeMode := v1.PersistentVolumeMode(v)
10111014
obj.VolumeMode = &volumeMode
10121015
}
1016+
if v, ok := in["claim_ref"].([]interface{}); ok && len(v) > 0 {
1017+
obj.ClaimRef = expandClaimRef(v)
1018+
}
10131019
return obj, nil
10141020
}
10151021

1022+
func expandClaimRef(v []interface{}) *v1.ObjectReference {
1023+
if len(v) == 0 || v[0] == nil {
1024+
return nil
1025+
}
1026+
o := v[0].(map[string]interface{})
1027+
return &v1.ObjectReference{
1028+
Name: o["name"].(string),
1029+
Namespace: o["namespace"].(string),
1030+
}
1031+
}
1032+
10161033
func expandPhotonPersistentDiskVolumeSource(l []interface{}) *v1.PhotonPersistentDiskVolumeSource {
10171034
if len(l) == 0 || l[0] == nil {
10181035
return &v1.PhotonPersistentDiskVolumeSource{}
@@ -1222,7 +1239,13 @@ func patchPersistentVolumeSpec(pathPrefix, prefix string, d *schema.ResourceData
12221239
Value: expandPersistentVolumeAccessModes(v.List()),
12231240
})
12241241
}
1225-
1242+
if d.HasChange(prefix + "claim_ref") {
1243+
v := d.Get(prefix + "claim_ref").([]interface{})
1244+
ops = append(ops, &ReplaceOperation{
1245+
Path: pathPrefix + "/claimRef",
1246+
Value: expandClaimRef(v),
1247+
})
1248+
}
12261249
return ops, nil
12271250
}
12281251

kubernetes/structures_container.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,3 +1028,14 @@ func expandContainerResourceRequirements(l []interface{}) (*v1.ResourceRequireme
10281028

10291029
return obj, nil
10301030
}
1031+
1032+
func flattenObjectRef(in *v1.ObjectReference) []interface{} {
1033+
att := make(map[string]interface{})
1034+
if in.Name != "" {
1035+
att["name"] = in.Name
1036+
}
1037+
if in.Namespace != "" {
1038+
att["namespace"] = in.Namespace
1039+
}
1040+
return []interface{}{att}
1041+
}

kubernetes/structures_container_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,35 @@ func TestExpandConfigMapKeyRef(t *testing.T) {
212212
}
213213
}
214214
}
215+
216+
func TestFlattenObjectRef(t *testing.T) {
217+
cases := []struct {
218+
Input *v1.ObjectReference
219+
ExpectedOutput []interface{}
220+
}{
221+
{
222+
&v1.ObjectReference{
223+
Name: "demo",
224+
Namespace: "default",
225+
},
226+
[]interface{}{
227+
map[string]interface{}{
228+
"name": "demo",
229+
"namespace": "default",
230+
},
231+
},
232+
},
233+
{
234+
&v1.ObjectReference{},
235+
[]interface{}{map[string]interface{}{}},
236+
},
237+
}
238+
239+
for _, tc := range cases {
240+
output := flattenObjectRef(tc.Input)
241+
if !reflect.DeepEqual(output, tc.ExpectedOutput) {
242+
t.Fatalf("Unexpected output from flattener.\nExpected: %#v\nGiven: %#v",
243+
tc.ExpectedOutput, output)
244+
}
245+
}
246+
}

0 commit comments

Comments
 (0)