Is it possible to use a template inside a template with go template
Using https://golang.org/pkg/text/template/, I sometimes need to use variables in the accessed path (for kubernetes deployments).
I end up writing something like :
{{ if (eq .Values.cluster "aws" }}{{ .Values.redis.aws.masterHost | quote }}{{else}}{{ .Values.redis.gcp.masterHost | quote }}{{end}}
What I'd really like to write is pretty much {{ .Values.redis.{{.Values.cluster}}.masterHost | quote }}
, which doesn't compile.
Is there a way to write something similar ? (so having a kind of variable in the accessed path).
go kubernetes-helm
add a comment |
Using https://golang.org/pkg/text/template/, I sometimes need to use variables in the accessed path (for kubernetes deployments).
I end up writing something like :
{{ if (eq .Values.cluster "aws" }}{{ .Values.redis.aws.masterHost | quote }}{{else}}{{ .Values.redis.gcp.masterHost | quote }}{{end}}
What I'd really like to write is pretty much {{ .Values.redis.{{.Values.cluster}}.masterHost | quote }}
, which doesn't compile.
Is there a way to write something similar ? (so having a kind of variable in the accessed path).
go kubernetes-helm
Is.Values.redis
a map or a struct? Or something else?
– icza
Nov 16 '18 at 15:21
I believe it's a struct
– C4stor
Nov 16 '18 at 16:35
I'm asking because if it's a map, you may use{{index}}
. If it's a struct, you may add methods to it.
– icza
Nov 16 '18 at 16:47
It's a map andindex
is the right answer.
– David Maze
Nov 16 '18 at 19:20
add a comment |
Using https://golang.org/pkg/text/template/, I sometimes need to use variables in the accessed path (for kubernetes deployments).
I end up writing something like :
{{ if (eq .Values.cluster "aws" }}{{ .Values.redis.aws.masterHost | quote }}{{else}}{{ .Values.redis.gcp.masterHost | quote }}{{end}}
What I'd really like to write is pretty much {{ .Values.redis.{{.Values.cluster}}.masterHost | quote }}
, which doesn't compile.
Is there a way to write something similar ? (so having a kind of variable in the accessed path).
go kubernetes-helm
Using https://golang.org/pkg/text/template/, I sometimes need to use variables in the accessed path (for kubernetes deployments).
I end up writing something like :
{{ if (eq .Values.cluster "aws" }}{{ .Values.redis.aws.masterHost | quote }}{{else}}{{ .Values.redis.gcp.masterHost | quote }}{{end}}
What I'd really like to write is pretty much {{ .Values.redis.{{.Values.cluster}}.masterHost | quote }}
, which doesn't compile.
Is there a way to write something similar ? (so having a kind of variable in the accessed path).
go kubernetes-helm
go kubernetes-helm
asked Nov 16 '18 at 15:09
C4storC4stor
6,12032138
6,12032138
Is.Values.redis
a map or a struct? Or something else?
– icza
Nov 16 '18 at 15:21
I believe it's a struct
– C4stor
Nov 16 '18 at 16:35
I'm asking because if it's a map, you may use{{index}}
. If it's a struct, you may add methods to it.
– icza
Nov 16 '18 at 16:47
It's a map andindex
is the right answer.
– David Maze
Nov 16 '18 at 19:20
add a comment |
Is.Values.redis
a map or a struct? Or something else?
– icza
Nov 16 '18 at 15:21
I believe it's a struct
– C4stor
Nov 16 '18 at 16:35
I'm asking because if it's a map, you may use{{index}}
. If it's a struct, you may add methods to it.
– icza
Nov 16 '18 at 16:47
It's a map andindex
is the right answer.
– David Maze
Nov 16 '18 at 19:20
Is
.Values.redis
a map or a struct? Or something else?– icza
Nov 16 '18 at 15:21
Is
.Values.redis
a map or a struct? Or something else?– icza
Nov 16 '18 at 15:21
I believe it's a struct
– C4stor
Nov 16 '18 at 16:35
I believe it's a struct
– C4stor
Nov 16 '18 at 16:35
I'm asking because if it's a map, you may use
{{index}}
. If it's a struct, you may add methods to it.– icza
Nov 16 '18 at 16:47
I'm asking because if it's a map, you may use
{{index}}
. If it's a struct, you may add methods to it.– icza
Nov 16 '18 at 16:47
It's a map and
index
is the right answer.– David Maze
Nov 16 '18 at 19:20
It's a map and
index
is the right answer.– David Maze
Nov 16 '18 at 19:20
add a comment |
1 Answer
1
active
oldest
votes
You can use _helpers.tpl file to define logic and operate with values.
_helpers.tpl
{{/*
Get redis host based on cluster.
*/}}
{{- define "chart.getRedis" -}}
{{- if eq .Values.cluster "aws" -}}
{{- .Values.redis.aws.masterHost | quote -}}
{{- else -}}
{{- .Values.redis.gcp.masterHost | quote -}}
{{- end -}}
{{- end -}}
values.yaml
cluster: local
redis:
aws:
masterHost: "my-aws-host"
gcp:
masterHost: "my-gcp-host"
And use it in your Deployment (here's a ConfigMap example to keep it shorter)
configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: Configmap
data:
redis: {{ template "chart.getRedis" . }}
Output:
helm install --dry-run --debug mychart
[debug] Created tunnel using local port: '64712'
...
COMPUTED VALUES:
cluster: local
redis:
aws:
masterHost: my-aws-host
gcp:
masterHost: my-gcp-host
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: Configmap
data:
redis: "my-gcp-host"
Set cluster value to aws:
helm install --dry-run --debug mychart --set-string=cluster=aws
[debug] Created tunnel using local port: '64712'
...
COMPUTED VALUES:
cluster: local
redis:
aws:
masterHost: my-aws-host
gcp:
masterHost: my-gcp-host
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: Configmap
data:
redis: "my-aws-host"
Great solution, thanks !
– C4stor
Dec 10 '18 at 10:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53340475%2fis-it-possible-to-use-a-template-inside-a-template-with-go-template%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use _helpers.tpl file to define logic and operate with values.
_helpers.tpl
{{/*
Get redis host based on cluster.
*/}}
{{- define "chart.getRedis" -}}
{{- if eq .Values.cluster "aws" -}}
{{- .Values.redis.aws.masterHost | quote -}}
{{- else -}}
{{- .Values.redis.gcp.masterHost | quote -}}
{{- end -}}
{{- end -}}
values.yaml
cluster: local
redis:
aws:
masterHost: "my-aws-host"
gcp:
masterHost: "my-gcp-host"
And use it in your Deployment (here's a ConfigMap example to keep it shorter)
configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: Configmap
data:
redis: {{ template "chart.getRedis" . }}
Output:
helm install --dry-run --debug mychart
[debug] Created tunnel using local port: '64712'
...
COMPUTED VALUES:
cluster: local
redis:
aws:
masterHost: my-aws-host
gcp:
masterHost: my-gcp-host
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: Configmap
data:
redis: "my-gcp-host"
Set cluster value to aws:
helm install --dry-run --debug mychart --set-string=cluster=aws
[debug] Created tunnel using local port: '64712'
...
COMPUTED VALUES:
cluster: local
redis:
aws:
masterHost: my-aws-host
gcp:
masterHost: my-gcp-host
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: Configmap
data:
redis: "my-aws-host"
Great solution, thanks !
– C4stor
Dec 10 '18 at 10:25
add a comment |
You can use _helpers.tpl file to define logic and operate with values.
_helpers.tpl
{{/*
Get redis host based on cluster.
*/}}
{{- define "chart.getRedis" -}}
{{- if eq .Values.cluster "aws" -}}
{{- .Values.redis.aws.masterHost | quote -}}
{{- else -}}
{{- .Values.redis.gcp.masterHost | quote -}}
{{- end -}}
{{- end -}}
values.yaml
cluster: local
redis:
aws:
masterHost: "my-aws-host"
gcp:
masterHost: "my-gcp-host"
And use it in your Deployment (here's a ConfigMap example to keep it shorter)
configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: Configmap
data:
redis: {{ template "chart.getRedis" . }}
Output:
helm install --dry-run --debug mychart
[debug] Created tunnel using local port: '64712'
...
COMPUTED VALUES:
cluster: local
redis:
aws:
masterHost: my-aws-host
gcp:
masterHost: my-gcp-host
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: Configmap
data:
redis: "my-gcp-host"
Set cluster value to aws:
helm install --dry-run --debug mychart --set-string=cluster=aws
[debug] Created tunnel using local port: '64712'
...
COMPUTED VALUES:
cluster: local
redis:
aws:
masterHost: my-aws-host
gcp:
masterHost: my-gcp-host
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: Configmap
data:
redis: "my-aws-host"
Great solution, thanks !
– C4stor
Dec 10 '18 at 10:25
add a comment |
You can use _helpers.tpl file to define logic and operate with values.
_helpers.tpl
{{/*
Get redis host based on cluster.
*/}}
{{- define "chart.getRedis" -}}
{{- if eq .Values.cluster "aws" -}}
{{- .Values.redis.aws.masterHost | quote -}}
{{- else -}}
{{- .Values.redis.gcp.masterHost | quote -}}
{{- end -}}
{{- end -}}
values.yaml
cluster: local
redis:
aws:
masterHost: "my-aws-host"
gcp:
masterHost: "my-gcp-host"
And use it in your Deployment (here's a ConfigMap example to keep it shorter)
configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: Configmap
data:
redis: {{ template "chart.getRedis" . }}
Output:
helm install --dry-run --debug mychart
[debug] Created tunnel using local port: '64712'
...
COMPUTED VALUES:
cluster: local
redis:
aws:
masterHost: my-aws-host
gcp:
masterHost: my-gcp-host
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: Configmap
data:
redis: "my-gcp-host"
Set cluster value to aws:
helm install --dry-run --debug mychart --set-string=cluster=aws
[debug] Created tunnel using local port: '64712'
...
COMPUTED VALUES:
cluster: local
redis:
aws:
masterHost: my-aws-host
gcp:
masterHost: my-gcp-host
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: Configmap
data:
redis: "my-aws-host"
You can use _helpers.tpl file to define logic and operate with values.
_helpers.tpl
{{/*
Get redis host based on cluster.
*/}}
{{- define "chart.getRedis" -}}
{{- if eq .Values.cluster "aws" -}}
{{- .Values.redis.aws.masterHost | quote -}}
{{- else -}}
{{- .Values.redis.gcp.masterHost | quote -}}
{{- end -}}
{{- end -}}
values.yaml
cluster: local
redis:
aws:
masterHost: "my-aws-host"
gcp:
masterHost: "my-gcp-host"
And use it in your Deployment (here's a ConfigMap example to keep it shorter)
configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: Configmap
data:
redis: {{ template "chart.getRedis" . }}
Output:
helm install --dry-run --debug mychart
[debug] Created tunnel using local port: '64712'
...
COMPUTED VALUES:
cluster: local
redis:
aws:
masterHost: my-aws-host
gcp:
masterHost: my-gcp-host
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: Configmap
data:
redis: "my-gcp-host"
Set cluster value to aws:
helm install --dry-run --debug mychart --set-string=cluster=aws
[debug] Created tunnel using local port: '64712'
...
COMPUTED VALUES:
cluster: local
redis:
aws:
masterHost: my-aws-host
gcp:
masterHost: my-gcp-host
HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: Configmap
data:
redis: "my-aws-host"
answered Nov 23 '18 at 12:39
edbigheadedbighead
600212
600212
Great solution, thanks !
– C4stor
Dec 10 '18 at 10:25
add a comment |
Great solution, thanks !
– C4stor
Dec 10 '18 at 10:25
Great solution, thanks !
– C4stor
Dec 10 '18 at 10:25
Great solution, thanks !
– C4stor
Dec 10 '18 at 10:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53340475%2fis-it-possible-to-use-a-template-inside-a-template-with-go-template%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is
.Values.redis
a map or a struct? Or something else?– icza
Nov 16 '18 at 15:21
I believe it's a struct
– C4stor
Nov 16 '18 at 16:35
I'm asking because if it's a map, you may use
{{index}}
. If it's a struct, you may add methods to it.– icza
Nov 16 '18 at 16:47
It's a map and
index
is the right answer.– David Maze
Nov 16 '18 at 19:20