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

provisioner: add json template function #708

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 7 additions & 0 deletions provisioner/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/x509"
"encoding/base64"
"encoding/binary"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
Expand Down Expand Up @@ -113,6 +114,7 @@ func renderTemplate(context *templateContext, file string) (string, error) {
"azID": azID,
"azCount": azCount,
"split": split,
"json": parseJSON,
"mountUnitName": mountUnitName,
"accountID": accountID,
"portRanges": portRanges,
Expand Down Expand Up @@ -294,6 +296,11 @@ func accountID(account string) (string, error) {
return items[1], nil
}

func parseJSON(input string) (result interface{}, err error) {
err = json.Unmarshal([]byte(input), &result)
return
}

type HostPort struct {
Host string
Port string
Expand Down
49 changes: 49 additions & 0 deletions provisioner/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provisioner

import (
"fmt"
"os"
"strconv"
"testing"

Expand Down Expand Up @@ -1164,3 +1165,51 @@ func TestNodePoolGroupsProfile(t *testing.T) {
})
}
}

func TestJson(t *testing.T) {
t.Run("object", func(t *testing.T) {
result, err := renderSingle(
t,
`{{ with json .Values.data }}name={{ .name }} value={{ .value }}{{end}}`,
`{"name": "foo", "value": "bar"}`)

require.NoError(t, err)
require.Equal(t, "name=foo value=bar", result)
})

t.Run("range over array", func(t *testing.T) {
result, err := renderSingle(
t,
`{{ range json .Values.data }}{{ .name }}={{ .value }} {{end}}`,
`[{"name": "foo", "value": "bar"}, {"name": "baz", "value": "qux"}]`)

require.NoError(t, err)
require.Equal(t, "foo=bar baz=qux ", result)
})

t.Run("range over object", func(t *testing.T) {
result, err := renderSingle(
t,
`{{ range $key, $value := json .Values.data }}{{ $key }}={{ $value }} {{end}}`,
`{"name": "foo", "value": "bar"}`)

require.NoError(t, err)
require.Equal(t, "name=foo value=bar ", result)
})

t.Run("example manifest", func(t *testing.T) {
template, err := os.ReadFile("testdata/json.template.yaml")
require.NoError(t, err)

expected, err := os.ReadFile("testdata/json.expected.yaml")
require.NoError(t, err)

result, err := renderSingle(
t,
string(template),
"")

require.NoError(t, err)
require.Equal(t, string(expected), result)
})
}
53 changes: 53 additions & 0 deletions provisioner/testdata/json.expected.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

---
apiVersion: apps/v1
kind: Deployment
metadata:
name: "stable"
namespace: kube-system
labels:
application: app
version: "v1"
spec:

selector:
matchLabels:
deployment: "stable"
template:
spec:
containers:
- name: app
args:
- -log-level=info


---
apiVersion: apps/v1
kind: Deployment
metadata:
name: "alpha"
namespace: kube-system
labels:
application: app
version: "v2"
spec:

replicas: 1

selector:
matchLabels:
deployment: "alpha"
template:
spec:
containers:
- name: app
args:
- -log-level=info


- -foo=bar

- -baz=qux



32 changes: 32 additions & 0 deletions provisioner/testdata/json.template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{{ range json `[
{"name": "stable", "version": "v1"},
{"name": "alpha", "version": "v2", "replicas": 1, "args": ["-foo=bar", "-baz=qux"]}
]` }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: "{{ .name }}"
namespace: kube-system
labels:
application: app
version: "{{ .version }}"
spec:
{{ if index . "replicas" }}
replicas: {{ .replicas }}
{{ end }}
selector:
matchLabels:
deployment: "{{ .name }}"
template:
spec:
containers:
- name: app
args:
- -log-level=info
{{ if index . "args" }}
{{ range .args }}
- {{ . }}
{{ end }}
{{ end }}
{{ end }}
Loading