Skip to content

Commit

Permalink
Merge pull request #42 from sl1pm4t/fix-node-selectors
Browse files Browse the repository at this point in the history
Fix pod template node_selectors
  • Loading branch information
radeksimko authored Aug 11, 2017
2 parents 891220e + 19a9697 commit 2826a5e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
46 changes: 46 additions & 0 deletions kubernetes/resource_kubernetes_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kubernetes

import (
"fmt"
"os"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
10 changes: 8 additions & 2 deletions kubernetes/structures_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2826a5e

Please sign in to comment.