Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to fill default namespace in manifest if no other namespace set #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions tfk8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think instead of doing this mutation here, we can just add this conditional to stripServerSideFields and just not delete the namespace key. I think doing it this way will add a namespace to resources that are not namespaced?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my first intent as well, but then I realized that stripServerSideFields is only being called when -s is passed on the CLI.
The two operations felt kind of orthogonal so I went like this. Do you mean I should drop the -n flag and just do this as part of stripServerSideFields?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well -n will only do something if -s is set anyway, because the default namespace always gets sent back and won't get stripped.

I think maybe we can just remove the delete namespace line from stripServerSideFields

// 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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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)
Expand Down
79 changes: 74 additions & 5 deletions tfk8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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))
}