From 5c41c3a6f979dff5dfaaffb1608d1cbf57b1ae19 Mon Sep 17 00:00:00 2001 From: mmorrison Date: Thu, 10 Aug 2017 14:46:00 +1200 Subject: [PATCH 1/2] Fix pod template node_selectors Prior to this fix the provider did not correct set the nodeSelectors attribute of a pod template. This was because it silently failed the type conversion ( map[string]string != map[string]interface{} ). --- kubernetes/structures_pod.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/kubernetes/structures_pod.go b/kubernetes/structures_pod.go index 5caeed94ff..0b5e7f8664 100644 --- a/kubernetes/structures_pod.go +++ b/kubernetes/structures_pod.go @@ -335,8 +335,14 @@ func expandPodSpec(p []interface{}) (v1.PodSpec, error) { obj.NodeName = v.(string) } - if v, ok := in["node_selector"].(map[string]string); ok { - obj.NodeSelector = v + if v, ok := in["node_selector"].(map[string]interface{}); ok { + nodeSelectors := make(map[string]string) + for k, v := range v { + if val, ok := v.(string); ok { + nodeSelectors[k] = val + } + } + obj.NodeSelector = nodeSelectors } if v, ok := in["restart_policy"].(string); ok { From 19a96970b07e5406676a66dc27692874c21b2dae Mon Sep 17 00:00:00 2001 From: mmorrison Date: Fri, 11 Aug 2017 11:09:36 +1200 Subject: [PATCH 2/2] Add acc tests --- kubernetes/resource_kubernetes_pod_test.go | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/kubernetes/resource_kubernetes_pod_test.go b/kubernetes/resource_kubernetes_pod_test.go index 789aab4953..cd42faeeb9 100644 --- a/kubernetes/resource_kubernetes_pod_test.go +++ b/kubernetes/resource_kubernetes_pod_test.go @@ -2,6 +2,7 @@ package kubernetes import ( "fmt" + "os" "testing" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -353,6 +354,31 @@ func TestAccKubernetesPod_with_empty_dir_volume(t *testing.T) { }) } +func TestAccKubernetesPod_with_nodeSelector(t *testing.T) { + var conf api.Pod + + podName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + imageName := "nginx:1.7.9" + region := os.Getenv("GOOGLE_REGION") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckKubernetesPodDestroy, + Steps: []resource.TestStep{ + { + Config: testAccKubernetesPodConfigNodeSelector(podName, imageName, region), + 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.node_selector.%", "1"), + resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.node_selector.failure-domain.beta.kubernetes.io/region", region), + ), + }, + }, + }) +} + func testAccCheckKubernetesPodDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*kubernetes.Clientset) @@ -796,3 +822,23 @@ resource "kubernetes_pod" "test" { } `, podName, imageName) } + +func testAccKubernetesPodConfigNodeSelector(podName, imageName, region string) string { + return fmt.Sprintf(` +resource "kubernetes_pod" "test" { + metadata { + name = "%s" + } + + spec { + container { + image = "%s" + name = "containername" + } + node_selector { + "failure-domain.beta.kubernetes.io/region" = "%s" + } + } +} +`, podName, imageName, region) +}