diff --git a/kubernetes/resource_kubernetes_pod_test.go b/kubernetes/resource_kubernetes_pod_test.go index d4a048a918..789aab4953 100644 --- a/kubernetes/resource_kubernetes_pod_test.go +++ b/kubernetes/resource_kubernetes_pod_test.go @@ -268,6 +268,38 @@ func TestAccKubernetesPod_with_volume_mount(t *testing.T) { }) } +func TestAccKubernetesPod_with_cfg_map_volume_mount(t *testing.T) { + var conf api.Pod + + podName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + cfgMap := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + + imageName := "nginx:1.7.9" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckKubernetesPodDestroy, + Steps: []resource.TestStep{ + { + Config: testAccKubernetesPodConfigWithConfigMapVolume(cfgMap, podName, imageName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckKubernetesPodExists("kubernetes_pod.test", &conf), + resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.image", imageName), + resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.volume_mount.#", "1"), + resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.volume_mount.0.mount_path", "/tmp/my_path"), + resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.volume_mount.0.name", "cfg"), + resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.volume_mount.0.read_only", "false"), + resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.volume_mount.0.sub_path", ""), + resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.volume.0.name", "cfg"), + resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.volume.0.config_map.0.name", cfgMap), + resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.volume.0.config_map.0.default_mode", "511"), // 0777 in decimal + ), + }, + }, + }) +} + func TestAccKubernetesPod_with_resource_requirements(t *testing.T) { var conf api.Pod @@ -642,14 +674,14 @@ resource "kubernetes_pod" "test" { container { image = "%s" name = "containername" - volume_mount { - mount_path = "/tmp/my_path" - name = "db" - } + volume_mount { + mount_path = "/tmp/my_path" + name = "db" + } } volume { name = "db" - secret = { + secret { secret_name = "${kubernetes_secret.test.metadata.0.name}" } } @@ -658,6 +690,48 @@ resource "kubernetes_pod" "test" { `, secretName, podName, imageName) } +func testAccKubernetesPodConfigWithConfigMapVolume(secretName, podName, imageName string) string { + return fmt.Sprintf(` +resource "kubernetes_config_map" "test" { + metadata { + name = "%s" + } + + data { + one = "first" + } +} + +resource "kubernetes_pod" "test" { + metadata { + labels { + app = "pod_label" + } + + name = "%s" + } + + spec { + container { + image = "%s" + name = "containername" + volume_mount { + mount_path = "/tmp/my_path" + name = "cfg" + } + } + volume { + name = "cfg" + config_map { + name = "${kubernetes_config_map.test.metadata.0.name}" + default_mode = 0777 + } + } + } +} + `, secretName, podName, imageName) +} + func testAccKubernetesPodConfigWithResourceRequirements(podName, imageName string) string { return fmt.Sprintf(` diff --git a/kubernetes/schema_pod_spec.go b/kubernetes/schema_pod_spec.go index 5bec115d02..674e0e8a19 100644 --- a/kubernetes/schema_pod_spec.go +++ b/kubernetes/schema_pod_spec.go @@ -188,9 +188,10 @@ func volumeSchema() *schema.Resource { }, }, "default_mode": { - Type: schema.TypeInt, - Description: "Optional: mode bits to use on created files by default. Must be a value between 0 and 0777. Defaults to 0644. Directories within the path are not affected by this setting. This might be in conflict with other options that affect the file mode, like fsGroup, and the result can be other mode bits set.", - Optional: true, + Type: schema.TypeInt, + Description: "Optional: mode bits to use on created files by default. Must be a value between 0 and 0777. Defaults to 0644. Directories within the path are not affected by this setting. This might be in conflict with other options that affect the file mode, like fsGroup, and the result can be other mode bits set.", + Optional: true, + ValidateFunc: validateModeBits, }, "name": { Type: schema.TypeString, diff --git a/kubernetes/structures_pod.go b/kubernetes/structures_pod.go index 3cd69f0050..5caeed94ff 100644 --- a/kubernetes/structures_pod.go +++ b/kubernetes/structures_pod.go @@ -473,7 +473,7 @@ func expandConfigMapVolumeSource(l []interface{}) *v1.ConfigMapVolumeSource { } in := l[0].(map[string]interface{}) obj := &v1.ConfigMapVolumeSource{ - DefaultMode: ptrToInt32(int32(in["default_mode "].(int))), + DefaultMode: ptrToInt32(int32(in["default_mode"].(int))), } if v, ok := in["name"].(string); ok { @@ -493,7 +493,7 @@ func expandDownwardAPIVolumeSource(l []interface{}) (*v1.DownwardAPIVolumeSource } in := l[0].(map[string]interface{}) obj := &v1.DownwardAPIVolumeSource{ - DefaultMode: ptrToInt32(int32(in["default_mode "].(int))), + DefaultMode: ptrToInt32(int32(in["default_mode"].(int))), } if v, ok := in["items"].([]interface{}); ok && len(v) > 0 { var err error diff --git a/kubernetes/validators.go b/kubernetes/validators.go index 03dd2cf555..9959d154de 100644 --- a/kubernetes/validators.go +++ b/kubernetes/validators.go @@ -165,6 +165,14 @@ func validateTerminationGracePeriodSeconds(value interface{}, key string) (ws [] return } +func validateModeBits(value interface{}, key string) (ws []string, es []error) { + v := value.(int) + if v < 0 || v > 0777 { + es = append(es, fmt.Errorf("%s (%#o) expects octal notation (a value between 0 and 0777)", key, v)) + } + return +} + func validateAttributeValueDoesNotContain(searchString string) schema.SchemaValidateFunc { return func(v interface{}, k string) (ws []string, errors []error) { input := v.(string) diff --git a/kubernetes/validators_test.go b/kubernetes/validators_test.go new file mode 100644 index 0000000000..78b5699065 --- /dev/null +++ b/kubernetes/validators_test.go @@ -0,0 +1,27 @@ +package kubernetes + +import ( + "testing" +) + +func TestValidateModeBits(t *testing.T) { + validCases := []int{ + 0, 0001, 0644, 0777, + } + for _, mode := range validCases { + _, es := validateModeBits(mode, "mode") + if len(es) > 0 { + t.Fatalf("Expected %#o to be valid: %#v", mode, es) + } + } + + invalidCases := []int{ + -5, -1, 512, 777, + } + for _, mode := range invalidCases { + _, es := validateModeBits(mode, "mode") + if len(es) == 0 { + t.Fatalf("Expected %#o to be invalid", mode) + } + } +}