Skip to content

Commit 1039c8f

Browse files
bancekdak1n1
andauthored
Add secret_namespace to volume_source azure_file (#1204)
* Add secret_namespace to volume_source azure_file * Update kubernetes/schema_volume_source.go Co-authored-by: Stef Forrester <[email protected]>
1 parent e84777a commit 1039c8f

File tree

6 files changed

+159
-7
lines changed

6 files changed

+159
-7
lines changed

kubernetes/resource_kubernetes_persistent_volume_test.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,50 @@ func TestAccKubernetesPersistentVolume_azure_blobStorageDisk(t *testing.T) {
137137
})
138138
}
139139

140+
func TestAccKubernetesPersistentVolume_azure_file(t *testing.T) {
141+
var conf1, conf2 api.PersistentVolume
142+
// name must not contain dashes, due to the Azure API requirements for storage accounts.
143+
name := fmt.Sprintf("tfacctest%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
144+
namespace := fmt.Sprintf("tfacctest%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
145+
secretName := fmt.Sprintf("tfacctest%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
146+
location := os.Getenv("TF_VAR_location")
147+
148+
resource.Test(t, resource.TestCase{
149+
PreCheck: func() { testAccPreCheck(t); skipIfNotRunningInAks(t) },
150+
IDRefreshName: "kubernetes_persistent_volume.test",
151+
ProviderFactories: testAccProviderFactories,
152+
ExternalProviders: testAccExternalProviders,
153+
CheckDestroy: testAccCheckKubernetesPersistentVolumeDestroy,
154+
Steps: []resource.TestStep{
155+
{ // Create a PV using the existing Azure storage share (without secret_namespace).
156+
Config: testAccKubernetesPersistentVolumeConfig_azure_file(name, location) +
157+
testAccKubernetesPersistentVolumeConfig_azure_PersistentVolumeAzureFile(name, secretName),
158+
Check: resource.ComposeAggregateTestCheckFunc(
159+
testAccCheckKubernetesPersistentVolumeExists("kubernetes_persistent_volume.test", &conf1),
160+
resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.name", name),
161+
resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.azure_file.#", "1"),
162+
resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.azure_file.0.share_name", name),
163+
resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.azure_file.0.secret_name", secretName),
164+
resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.azure_file.0.secret_namespace", ""),
165+
),
166+
},
167+
{ // Create a PV using the existing Azure storage share (with secret_namespace).
168+
Config: testAccKubernetesPersistentVolumeConfig_azure_file(name, location) +
169+
testAccKubernetesPersistentVolumeConfig_azure_PersistentVolumeAzureFileNamespace(name, namespace, secretName),
170+
Check: resource.ComposeAggregateTestCheckFunc(
171+
testAccCheckKubernetesPersistentVolumeExists("kubernetes_persistent_volume.test", &conf2),
172+
resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.name", name),
173+
resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.azure_file.#", "1"),
174+
resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.azure_file.0.share_name", name),
175+
resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.azure_file.0.secret_name", secretName),
176+
resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.azure_file.0.secret_namespace", namespace),
177+
testAccCheckKubernetesPersistentVolumeForceNew(&conf1, &conf2, true),
178+
),
179+
},
180+
},
181+
})
182+
}
183+
140184
func TestAccKubernetesPersistentVolume_googleCloud_basic(t *testing.T) {
141185
var conf api.PersistentVolume
142186
randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
@@ -1357,6 +1401,99 @@ resource "azurerm_storage_container" "test" {
13571401
`, name, location)
13581402
}
13591403

1404+
func testAccKubernetesPersistentVolumeConfig_azure_PersistentVolumeAzureFile(name, secretName string) string {
1405+
return fmt.Sprintf(`resource "kubernetes_persistent_volume" "test" {
1406+
metadata {
1407+
name = %[1]q
1408+
}
1409+
spec {
1410+
capacity = {
1411+
storage = "1Gi"
1412+
}
1413+
access_modes = ["ReadWriteOnce"]
1414+
persistent_volume_source {
1415+
azure_file {
1416+
secret_name = %[2]q
1417+
share_name = %[1]q
1418+
read_only = false
1419+
}
1420+
}
1421+
}
1422+
}`, name, secretName)
1423+
}
1424+
1425+
func testAccKubernetesPersistentVolumeConfig_azure_PersistentVolumeAzureFileNamespace(name, namespace, secretName string) string {
1426+
return fmt.Sprintf(`resource "kubernetes_namespace" "test" {
1427+
metadata {
1428+
name = %[2]q
1429+
}
1430+
}
1431+
1432+
resource "kubernetes_secret" "test" {
1433+
metadata {
1434+
name = %[3]q
1435+
namespace = %[2]q
1436+
}
1437+
1438+
data = {
1439+
azurestorageaccountname = azurerm_storage_account.test.name
1440+
azurestorageaccountkey = azurerm_storage_account.test.primary_access_key
1441+
}
1442+
}
1443+
1444+
resource "kubernetes_persistent_volume" "test" {
1445+
metadata {
1446+
name = %[1]q
1447+
}
1448+
spec {
1449+
capacity = {
1450+
storage = "1Gi"
1451+
}
1452+
access_modes = ["ReadWriteOnce"]
1453+
persistent_volume_source {
1454+
azure_file {
1455+
secret_name = %[3]q
1456+
secret_namespace = %[2]q
1457+
share_name = %[1]q
1458+
read_only = false
1459+
}
1460+
}
1461+
}
1462+
}`, name, namespace, secretName)
1463+
}
1464+
1465+
func testAccKubernetesPersistentVolumeConfig_azure_file(name, location string) string {
1466+
return fmt.Sprintf(`provider "azurerm" {
1467+
features {}
1468+
}
1469+
resource "azurerm_resource_group" "test" {
1470+
name = "%s"
1471+
location = "%s"
1472+
tags = {
1473+
environment = "terraform-provider-kubernetes-test"
1474+
}
1475+
}
1476+
resource "azurerm_storage_account" "test" {
1477+
name = %[1]q
1478+
resource_group_name = azurerm_resource_group.test.name
1479+
location = azurerm_resource_group.test.location
1480+
account_tier = "Standard"
1481+
account_replication_type = "LRS"
1482+
account_kind = "StorageV2"
1483+
# needed for Azure File kubernetes cifs mount
1484+
enable_https_traffic_only = false
1485+
tags = {
1486+
environment = "terraform-provider-kubernetes-test"
1487+
}
1488+
}
1489+
resource "azurerm_storage_share" "test" {
1490+
name = %[1]q
1491+
storage_account_name = azurerm_storage_account.test.name
1492+
quota = 1
1493+
}
1494+
`, name, location)
1495+
}
1496+
13601497
func testAccKubernetesPersistentVolumeConfig_csi_basic(name string) string {
13611498
return fmt.Sprintf(`resource "kubernetes_persistent_volume" "test" {
13621499
metadata {

kubernetes/schema_volume_source.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ func commonVolumeSources() map[string]*schema.Schema {
139139
Description: "The name of secret that contains Azure Storage Account Name and Key",
140140
Required: true,
141141
},
142+
"secret_namespace": {
143+
Type: schema.TypeString,
144+
Description: "The namespace of the secret that contains Azure Storage Account Name and Key. For Kubernetes up to 1.18.x the default is the same as the Pod. For Kubernetes 1.19.x and later the default is \"default\" namespace.",
145+
Optional: true,
146+
Computed: false,
147+
ForceNew: true,
148+
},
142149
"share_name": {
143150
Type: schema.TypeString,
144151
Description: "Share Name",

kubernetes/structure_persistent_volume_spec.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ func flattenAzureFilePersistentVolumeSource(in *v1.AzureFilePersistentVolumeSour
5757
if in.ReadOnly != false {
5858
att["read_only"] = in.ReadOnly
5959
}
60+
if in.SecretNamespace != nil {
61+
att["secret_namespace"] = *in.SecretNamespace
62+
}
6063
return []interface{}{att}
6164
}
6265

@@ -583,6 +586,9 @@ func expandAzureFilePersistentVolumeSource(l []interface{}) *v1.AzureFilePersist
583586
if v, ok := in["read_only"].(bool); ok {
584587
obj.ReadOnly = v
585588
}
589+
if v, ok := in["secret_namespace"].(string); ok && v != "" {
590+
obj.SecretNamespace = &v
591+
}
586592
return obj
587593
}
588594

kubernetes/test-infra/aks/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ In addition, you will need the following environment variables to be set.
99
- `TF_VAR_aks_client_secret`
1010
- `TF_VAR_location`
1111

12-
Obtaining the values for ***client id*** and ***client secret*** is detailed in the documentation linked above.
12+
Obtaining the values for ***client id*** and ***client secret*** is detailed in the documentation linked above.
1313

1414
## Versions
1515

website/docs/guides/getting-started.html.markdown

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,10 @@ Terraform will perform the following actions:
350350
}
351351
352352
+ azure_file {
353-
+ read_only = (known after apply)
354-
+ secret_name = (known after apply)
355-
+ share_name = (known after apply)
353+
+ read_only = (known after apply)
354+
+ secret_name = (known after apply)
355+
+ share_name = (known after apply)
356+
+ secret_namespace = (known after apply)
356357
}
357358
358359
+ ceph_fs {

website/docs/r/persistent_volume.html.markdown

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ The following arguments are supported:
180180
#### Arguments
181181

182182
* `read_only` - (Optional) Whether to force the read-only setting in VolumeMounts. Defaults to false (read/write).
183-
* `secret_name` - (Required) The name of secret that contains Azure Storage Account Name and Key
183+
* `secret_name` - (Required) The name of secret that contains Azure Storage Account Name and Key.
184+
* `secret_namespace` - (Optional) The namespace of the secret that contains Azure Storage Account Name and Key. For Kubernetes up to 1.18.x the default is the same as the Pod. For Kubernetes 1.19.x and later the default is \"default\" namespace.
184185
* `share_name` - (Required) Share Name
185186

186187
### `ceph_fs`
@@ -287,11 +288,11 @@ The following arguments are supported:
287288

288289
#### Arguments
289290

290-
* `annotations` - (Optional) An unstructured key value map stored with the persistent volume that may be used to store arbitrary metadata.
291+
* `annotations` - (Optional) An unstructured key value map stored with the persistent volume that may be used to store arbitrary metadata.
291292

292293
~> By default, the provider ignores any annotations whose key names end with *kubernetes.io*. This is necessary because such annotations can be mutated by server-side components and consequently cause a perpetual diff in the Terraform plan output. If you explicitly specify any such annotations in the configuration template then Terraform will consider these as normal resource attributes and manage them as expected (while still avoiding the perpetual diff problem). For more info info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/annotations)
293294

294-
* `labels` - (Optional) Map of string keys and values that can be used to organize and categorize (scope and select) the persistent volume. May match selectors of replication controllers and services.
295+
* `labels` - (Optional) Map of string keys and values that can be used to organize and categorize (scope and select) the persistent volume. May match selectors of replication controllers and services.
295296

296297
~> By default, the provider ignores any labels whose key names end with *kubernetes.io*. This is necessary because such labels can be mutated by server-side components and consequently cause a perpetual diff in the Terraform plan output. If you explicitly specify any such labels in the configuration template then Terraform will consider these as normal resource attributes and manage them as expected (while still avoiding the perpetual diff problem). For more info info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/labels)
297298

0 commit comments

Comments
 (0)