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

Escape shell vars in HCL output #28

Merged
merged 1 commit into from
Aug 30, 2021
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.1.7

- Escape shell vars in HCL output

# 0.1.6

- Fix crash when trying to use List resources
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: build docker docker-push release install test clean

VERSION := 0.1.6
VERSION := 0.1.7
DOCKER_IMAGE_NAME := jrhouston/tfk8s

build:
Expand Down
7 changes: 7 additions & 0 deletions tfk8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ func snakify(s string) string {
return strings.ToLower(re.ReplaceAllString(s, "_"))
}

// escape incidences of ${} with $${} to prevent Terraform trying to interpolate them
func escapeShellVars(s string) string {
r := regexp.MustCompile(`(\${.*?})`)
Copy link
Owner Author

Choose a reason for hiding this comment

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

The .*? here is to make the regexp not greedy – otherwise it would match ${VAR1} ${VAR2} as one group.

return r.ReplaceAllString(s, `$$$1`)
Copy link
Owner Author

Choose a reason for hiding this comment

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

There is three $ because $1 uses the first group, then we need to escape the $ we want to put in front of it 😅

}

// yamlToHCL converts a single YAML document Terraform HCL
func yamlToHCL(doc cty.Value, providerAlias string, stripServerSide bool, mapOnly bool) (string, error) {
m := doc.AsValueMap()
Expand Down Expand Up @@ -118,6 +124,7 @@ func yamlToHCL(doc cty.Value, providerAlias string, stripServerSide bool, mapOnl
doc = stripServerSideFields(doc)
}
s := terraform.FormatValue(doc, 0)
s = escapeShellVars(s)

if mapOnly {
hcl += fmt.Sprintf("%v\n", s)
Expand Down
34 changes: 34 additions & 0 deletions tfk8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,40 @@ resource "kubernetes_manifest" "configmap_test" {
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(output))
}

func TestYAMLToTerraformResourcesEscapeShell(t *testing.T) {
yaml := `---
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
SCRIPT: |
echo Hello, ${USER} your homedir is ${HOME}`

r := strings.NewReader(yaml)
output, err := YAMLToTerraformResources(r, "", false, false)

if err != nil {
t.Fatal("Converting to HCL failed:", err)
}

expected := `
resource "kubernetes_manifest" "configmap_test" {
manifest = {
"apiVersion" = "v1"
"data" = {
"SCRIPT" = "echo Hello, $${USER} your homedir is $${HOME}"
}
"kind" = "ConfigMap"
"metadata" = {
"name" = "test"
}
}
}`

assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(output))
}

func TestYAMLToTerraformResourcesMultiple(t *testing.T) {
yaml := `---
apiVersion: v1
Expand Down