diff --git a/tfk8s.go b/tfk8s.go index d1f675b..525e833 100644 --- a/tfk8s.go +++ b/tfk8s.go @@ -89,13 +89,23 @@ func stripServerSideFields(m map[string]interface{}) { } } -func toHCL(doc map[interface{}]interface{}, providerAlias string, stripServerSide bool, mapOnly bool) (string, error) { +func toHCL(doc map[interface{}]interface{}, providerAlias string, stripServerSide bool, mapOnly bool, defaultNamespace bool) (string, error) { formattable := fixMap(doc) if stripServerSide { stripServerSideFields(formattable) } + if defaultNamespace { + // Add "namespace" = "default" to metadata if no namespace specified + metadata, ok := formattable["metadata"].(map[string]interface{}) + if !ok { + return "", fmt.Errorf("manifest is missing metadata") + } + if _, ok := metadata["namespace"]; !ok { + metadata["namespace"] = "default" + } + } // TODO need to find a way of ordering the fields in the output s, err := repl.FormatResult(formattable) if err != nil { @@ -131,7 +141,7 @@ func toHCL(doc map[interface{}]interface{}, providerAlias string, stripServerSid // ToHCL converts a file containing one or more Kubernetes configs // and converts it to resources that can be used by the Terraform Kubernetes Provider -func ToHCL(r io.Reader, providerAlias string, stripServerSide bool, mapOnly bool) (string, error) { +func ToHCL(r io.Reader, providerAlias string, stripServerSide bool, mapOnly bool, defaultNamespace bool) (string, error) { hcl := "" decoder := yaml.NewDecoder(r) @@ -150,7 +160,7 @@ func ToHCL(r io.Reader, providerAlias string, stripServerSide bool, mapOnly bool } } - formatted, err := toHCL(doc, providerAlias, stripServerSide, mapOnly) + formatted, err := toHCL(doc, providerAlias, stripServerSide, mapOnly, defaultNamespace) if err != nil { return "", fmt.Errorf("error converting YAML to HCL: %s", err) @@ -170,6 +180,7 @@ func main() { infile := flag.StringP("file", "f", "-", "Input file containing Kubernetes YAML manifests") outfile := flag.StringP("output", "o", "-", "Output file to write Terraform config") providerAlias := flag.StringP("provider", "p", "", "Provider alias to populate the `provider` attribute") + defaultNamespace := flag.BoolP("namespace", "n", false, "Fill in default namespace when none is set") stripServerSide := flag.BoolP("strip", "s", false, "Strip out server side fields - use if you are piping from kubectl get") version := flag.BoolP("version", "V", false, "Show tool version") mapOnly := flag.BoolP("map-only", "M", false, "Output only an HCL map structure") @@ -192,7 +203,7 @@ func main() { } } - hcl, err := ToHCL(file, *providerAlias, *stripServerSide, *mapOnly) + hcl, err := ToHCL(file, *providerAlias, *stripServerSide, *mapOnly, *defaultNamespace) if err != nil { fmt.Println(err) os.Exit(1) diff --git a/tfk8s_test.go b/tfk8s_test.go index 23c595e..4cdcb96 100644 --- a/tfk8s_test.go +++ b/tfk8s_test.go @@ -17,7 +17,7 @@ data: TEST: test` r := strings.NewReader(yaml) - output, err := ToHCL(r, "", false, false) + output, err := ToHCL(r, "", false, false, false) if err != nil { t.Fatal("Converting to HCL failed:", err) @@ -57,7 +57,7 @@ data: TEST: two` r := strings.NewReader(yaml) - output, err := ToHCL(r, "", false, false) + output, err := ToHCL(r, "", false, false, false) if err != nil { t.Fatal("Converting to HCL failed:", err) @@ -103,7 +103,7 @@ data: TEST: test` r := strings.NewReader(yaml) - output, err := ToHCL(r, "kubernetes-alpha", false, false) + output, err := ToHCL(r, "kubernetes-alpha", false, false, false) if err != nil { t.Fatal("Converting to HCL failed:", err) @@ -148,7 +148,7 @@ metadata: - test` r := strings.NewReader(yaml) - output, err := ToHCL(r, "", true, false) + output, err := ToHCL(r, "", true, false, false) if err != nil { t.Fatal("Converting to HCL failed:", err) @@ -185,7 +185,7 @@ metadata: uid: bea6500b-0637-4d2d-b726-e0bda0b595dd` r := strings.NewReader(yaml) - output, err := ToHCL(r, "", true, true) + output, err := ToHCL(r, "", true, true, false) if err != nil { t.Fatal("Converting to HCL failed:", err) @@ -204,3 +204,72 @@ metadata: assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(output)) } + +func TestToHCLDefaultNamespace(t *testing.T) { + yaml := `--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: test +data: + TEST: test` + + r := strings.NewReader(yaml) + output, err := ToHCL(r, "", false, false, true) + + if err != nil { + t.Fatal("Converting to HCL failed:", err) + } + + expected := ` +resource "kubernetes_manifest" "configmap_test" { + manifest = { + "apiVersion" = "v1" + "data" = { + "TEST" = "test" + } + "kind" = "ConfigMap" + "metadata" = { + "name" = "test" + "namespace" = "default" + } + } +}` + + assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(output)) +} + +func TestToHCLOthertNamespace(t *testing.T) { + yaml := `--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: test + namespace: foobar +data: + TEST: test` + + r := strings.NewReader(yaml) + output, err := ToHCL(r, "", false, false, true) + + if err != nil { + t.Fatal("Converting to HCL failed:", err) + } + + expected := ` +resource "kubernetes_manifest" "configmap_test" { + manifest = { + "apiVersion" = "v1" + "data" = { + "TEST" = "test" + } + "kind" = "ConfigMap" + "metadata" = { + "name" = "test" + "namespace" = "foobar" + } + } +}` + + assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(output)) +}