Questions tagged [go-templates]
Go language supports built-in template functionality. Packages with this support include 1) text/template and; 2) html/template.
642
questions
-2
votes
0
answers
22
views
golang html template if condition when pass a struct [closed]
I made a web app in golang. In the index page, I want to know the logged person as user or admin.
My index.html
''' {{ if .Useraccess=="admin" }}
...
0
votes
1
answer
19
views
How to omit terminating pods from output with go templates
On a project I’m working on at my current job, I’ve come across a problem where I needed to get container versions from all pods with kubectl get pods -o go-template. Then I found bug in code that is ...
1
vote
1
answer
79
views
Go Template compare if string ends in or contains another string
The eq function allows for comparing if two strings are equal
{{if eq .Name "MyName"}}
Is there a way to test if a string ends in (or contains) another string?
0
votes
1
answer
54
views
Passing Dictionary as an argument in Helm Template
I wonder if there is anyway for us to pass dictionary as an argument in Helm Templates. In Python, I could simply create some scripts like this but I am not sure how to translate this script into Helm....
1
vote
1
answer
106
views
Helm 'if' condition in one line
I was trying to put the if condition in a single line of the helm template:
- name: ENV_VARIABLE
value: {{- if .Values.condition }}"Value1"{{- else }}"Value2"{{- end }}
However, ...
1
vote
1
answer
53
views
Helm Error: parse error at invalid syntax
How can I escape () in helm template… I tried as below but getting Error: parse error at yaml:17): invalid syntax..
At the end I need this output "{{ (index . "demo-app-secret") | ...
1
vote
1
answer
50
views
Controlling indents in Go templates
I have the following Go template:
{{ range $job, $steps := .jobs -}}
{{ $job -}}:
{{ range $steps -}}
{{ . }}
{{ end }}
{{- end }}
It's producing output that looks like the ...
0
votes
2
answers
34
views
Creating a configMap from file is critical to file type and lines count in the file
I have a template that creates a configMap from the jmeter-test-data-file-configmap.yaml file in helm chart:
{{- if .Values.env.datafile }}
apiVersion: v1
kind: ConfigMap
metadata:
name: jmeter-...
1
vote
1
answer
57
views
Dynamically parse sub-templates based on those actually required by the main template
Suppose I have a large number of templates with sub-templates, then how do I parse sub-templates based on those required by template pipelines in the?
My idea is to read the current template to be ...
1
vote
1
answer
48
views
Helm iterate over nested list and added output in yaml with decoded value
I have a bunch of secretKey in values.yaml like below. I need to add each value of secretKey as a key and decoded value in template.data like as a value like below.
How can I achieve this ?
{{- range $...
0
votes
1
answer
81
views
helm escape ` in template yaml file
Does anyone know how to espace ` in helm template?
I try to add \ in front doesn't seem to work
original:
{{- if not .Values.istio_cni.chained }}
k8s.v1.cni.cncf.io/networks: '{{ ...
0
votes
0
answers
15
views
Golang Template: Check if variable assigned within a template is set [duplicate]
I have two templates that are combined. In the first one, a variable is set, in the second one it is used - Unfortunately, I can't change that. To test the second one, I need to check if the variable ...
1
vote
0
answers
40
views
How can I parse quotes in golang template such that it doesn't get converted to ascii values on rendering of template?
I have following variable which I want to be rendered in my template:
str := "'~qas\\d{1,2}.abc.com' '~qas\\d{1,2}business.abc.com'"
In a template like this:
const tpl = `
server {
...
0
votes
2
answers
82
views
Usage of variables in Helm
I'm using a Helm chart and I was wondering how I can define a value by default. In my case, I wanna define a date when it isn't defined in values.yaml and I have the following code:
{{- if ne .Value....
1
vote
0
answers
14
views
consul-template file per key-value
I have a use case where i need consul template to watch multiple keys at a specific path. The number of keys at that path are variable. For each key under that path, it needs to grab the value and put ...
2
votes
1
answer
37
views
Dynamically process template map values
I have annotations object like this:
Annotations: map[string]string{
"common": "Some GPM consistency checkers have failed.",
"t_0": "Title1",
...
1
vote
0
answers
52
views
How do I load my CSS while using gohtml templates and a golang webserver
After re-arranging my file structure I can't seem to figure out how to get my CSS to load along with my gohtml pages.
My file structure looks like this:
my-app/
├── src/
│ ├── main.go
│ ├── go.mod
...
2
votes
2
answers
85
views
float division in Go template
In grafana (which uses go templates for Loki line templating), I would like to divide a field (milliseconds) and get a float result.
Here is what I'd like to do :
| logfmt | line_format "{{ ....
2
votes
0
answers
181
views
kubectl substring match using go-template
I am trying to find a way to match a substring in the go-template in kubectl. I am only able to find operators like those listed below, however, none of them is to match a substring between LHS and ...
1
vote
1
answer
43
views
docker -ps --format output with substr
There is a command: docker ps --format "{{.ID}}\t{{.Command}}" --no-trunc (used https://pkg.go.dev/text/template)
which outputs the full text of the '{{.Command}}' (which I need), but ...
2
votes
1
answer
60
views
Why doesn't template.ParseFiles() detect this error?
If I specify a non-existent template in my template file, the error is not detected by ParseFiles() but by ExecuteTemplate(). One would expect parsing to detect any missing templates. Detecting such ...
1
vote
0
answers
51
views
Filter yaml in helmfile
Is there a way to filter yaml files used in a helmfile?
I have a values.yaml:
component-a:
key1: value
key2: value
component-b:
key1: value
key3: value
In the helmfile I load the yaml and ...
0
votes
1
answer
267
views
Helm tpl function pass in file specific variable and global Values
Using the helm function tpl or other similar functions, how do you pass in a file specific variable and the top level Values? Here is a concrete example:
# values
template: "{{ .Values.name }} ...
0
votes
2
answers
302
views
GoLang html template if condition
I made a web app in Go which search for a query in a mysql database. Everything works fine, but I do not know how to obtain following: if there is no result in database print e.g. "there is no ...
0
votes
1
answer
89
views
How to make Sprig function kindIs interpret the string "true" as a string rather than a boolean
The Go template Sprig function kindIs is used to test the type of a value:
kindIs "string" "hello" returns true.
What if the string is actually "true", not the boolean ...
1
vote
1
answer
80
views
Is there any way to run custom commands with go install command?
I've written a simple code generation tool. I've used text/template package in this tool. When I installed it with go build command, it installed successfully. The only problem I'm facing is how to ...
0
votes
0
answers
364
views
Access an input file from vault template injector
I use Vault to retrieve some secrets that I put inside a configuration file. All works fine until this configuration gets bigger and I want it to be saved in sub configs in a folder. The issue is that ...
1
vote
1
answer
259
views
How to solve template: pattern matches no files
How to handle file path when i am accessing file from other go file than main.
in other.go file i am trying to run ParseFS but it's giving the template: pattern matches no files: templates/test.tmpl ...
0
votes
1
answer
171
views
Helm template - dynamic error message for required function
I'm creating a configmap with required parameters. I wanted the error message given by helm to have the key name. So its easy to identify what was missed.
This is the part my template where i'm using ...
1
vote
1
answer
132
views
In Go templates/Sprig, how to modify a captured group in a regular expression before replacing it?
I'd like to write a Go template which takes in a URL with a query parameter earliest which represents a relative time, e.g. earliest=-15m, and converts it to an absolute time relative to now as a Unix ...
0
votes
0
answers
87
views
Handle ambiguous import properly goimports
I am using
go-swagger client template
go-swagger v0.25.0
go version go1.17.1 linux/amd64
makefile with cytopia/goimports:latest-release-0.2
and my repository
└── myRepository
└── go.mod
└── ...
0
votes
1
answer
88
views
How to template Helm to insert templating language from other application?
I have a configMap that has a piece of alerting that looks like this:
alert_output:
outputs:
- type: webhook
enabled: true
template: |
{{- if ne (print .PolicyType) "...
-1
votes
1
answer
163
views
helm - how to loop with complex values
I need to loop over objects with range in a Helm chart.
I have error in network.yaml at line 4. How can I fix it?
My values.yaml contains:
network:
service_shob_server:
- name: shob-server-{{ ....
0
votes
1
answer
611
views
Values context in an included helm named template
We have a helm chart that contains named templates to be used by other templates.
Originally, the helm chart containing the named templates has no "values.yaml" file, as all it has are "...
0
votes
1
answer
362
views
How to get the last element in helm loop?
values.yaml:
products:
apples:
- green
- red
- yellow
I want to convert this into JSON file in chart:
"products": {
"apples": [
"...
1
vote
2
answers
89
views
How to filter even and odd elements?
{{ if eq ($key % 2) 0 }} gives: unexpected "%" in operand
{{ if $key % 2 == 0 }} gives: unexpected "%" in operand
So how do I find even and odd keys ?
1
vote
0
answers
216
views
Helm chart template: Require that values (in values.yaml) for nodeSelector exists as labels on nodes
I was trying to make sure that users configuring nodeSelector values in a chart will fill in labels that actually match the ones on the nodes in the kubernetes cluster and was wondering if anyone ...
0
votes
1
answer
149
views
Helm templating.. values match
Wondering whether it's possible to use a port if it exists in the values, else use http.. something like this;
svc:
app:
ports:
- port: 8080
name: http
- port: 8090
...
0
votes
0
answers
108
views
Helm Kubernetes ConfigMap unexpected "\\" in define clause
I'm getting the following error triying to import a JSON into a ConfigMap YAML and then creating the chart.
[ERROR] templates/: parse error at (mychart/templates/provision.yml:50): unexpected "\\&...
1
vote
0
answers
43
views
breadcrumbs navigation in Go application
I'm very new to Golang and I'm trying right now to dynamically generate breadcrumbs navigation in my go app.
Right now I have been able to make it in a static way in a go template:
{{ define "...
0
votes
1
answer
56
views
Proper way to query in Hugo using goTemplates with multiple nested arguments, is it possible?
I'm working with Hugo and have a question regarding where clause. Currently I am doing the following and it works fine. I attempted to add one more where argument and I got the error below:
Question: ...
1
vote
1
answer
739
views
Escape dot in Helm template
In values.yaml I have:
key1:
key2.yaml:
key3:
- name
value
- name
value
Inside a template, how can I get to key3? {{ .Values.key1.key2.yaml.key3 }} does not work because ...
1
vote
0
answers
165
views
Replacing strings in templates in Go
I have a block of variables in my templates that repeats a lot.
To fix that, i figured that i could
Create a unique string inside a template (like __VARIABLE_BLOCK__)
While parsing the template, ...
2
votes
1
answer
55
views
How do you left align a printf on Docker format
I am trying to align a set of docker service ps calls together using printf but it seems to align things on the right when I use printf
for service in $(command docker service ls --format "{{....
4
votes
0
answers
219
views
Creating yaml list using the go template
I am looking to create a yaml file in the following format. Using Go template
Custom_listeners:
Config1 : config_value
Config2 : config_value
copy_files:
- source_path: /path/...
1
vote
1
answer
59
views
Go Template Extra line on the Range
I have Following go Template code
custom_listeners: {{ range $cl := $.Vars.CustomListeners }}
{{ $cl.ListenerType }}:
{{ range $k,$v := $cl.Values }}{{ $k }}: "{{ $v }}"
...
4
votes
1
answer
196
views
html/template is not showing a float value with e+07 notation in exact decimal places
I'm trying an example where I'm passing a value 1.8e+07 to the TestVal field of struct Student. Now I want this 1.8e+07 value to be in exact decimal places but it is not doing so. It is able to show ...
2
votes
1
answer
57
views
changing a forms action attribute dynamically in a go template
Currently the forms action attribute is hardcoded to the id=2
How would I dynamically set it to the ID chosen by the user when pressing submit.
<p>Select a group</p>
<form ...
0
votes
1
answer
151
views
Helm: the range function is not keeping the input values in order as expected?
I know, the dictionary is processed based on order. However, for some process purpose I need to run the certain process in some order. (Since native k8s wont support cronjobs in order, I am doing it ...