How to understand this Helm usage - kubernetes-helm

I saw the following Helm from time to time, but I didn't understand how it works.
{{- include "common.tplvalues.render" ( dict "value" .Values.abc.service.annotations "context" $) | nindent 4 }}
First, where is the definition of common.tplvalues.render? I searched all over the internet, and seems that this is related to Bitnami? Or it is a built-in function provided by Helm?
Second, I understand how to create a dictionary by reading this doc about template functions. However, why create a dictionary named value in this situation?
Third, the usage of $ sign. In this official doc about variables, it stated that '$ - this variable will always point to the root context'. What is 'root context'? Is this 'root context' related to the 'context' parameter of the 'value' dictionary?

Related

Whats the meaning of CHANGEME in K8s helm charts?

While checking the values of yaml files for a helm chart, one often encounters
changeme passed as a value. E.g.:
rabbitmq.conf: |-
##username and password
default_user={{.Values.rabbitmq.username}}
default_pass=CHANGEME
or:
config:
accumuloSite:
instance.secret: "changeme"
userManagement:
rootPassword: "changeme"
What is the meaning of "changeme"?
Is it just a word that needs to be replaced? If so, what will happen if it is not? A security hole, or hopefully an error?
Or is it a keyword that lets the system to replace this with a secure password? If so, how does the system know what type of password to produce?
In either case, how does the chart connect this value with other places where this value might be needed? ( e.g. if this is a password another -dependent to the first- service needs, how is the manually assigned / derived password propagated to the second service? )
(*mainly interested about helm v3 if this is important)
I'd almost always expect this to be just a placeholder that needs to be filled in. In many cases YAML can wind up having inconsistent types if a value is actually absent, so it can be useful to have some value in the chart's values.yaml, but for things like passwords there's not a "right default value" you could include.
Nothing will automatically replace these for you or warn if you're using the default values. Nothing bad will obviously happen if you do deploy with these values, but I'm sure changeme is up there with passw0rd on the short list of default passwords to try if you're actively trying to break into a system.
If you were writing your own chart, you could also test if a value is present using required and explain what's missing, and this approach might be more secure than having a well-known default password.

has function in the helm template is not returning true

I have a values.yaml file containing list variable like:
excludePort: [32104, 30119]
I am trying to use that in the helm template like:
{{ if has 32104 .Values.excludePort }}
But it seems to be not returning true. (The block after the condition is not executing. Any reason for it?
This is a problem caused by a variable type mismatch.
{{ kindOf (first .Values.excludePort) }}
output:
float64
You need to understand that the essence of helm rendering templates is to first parse the values.yaml file into map through golang, and the number will be deserialized to float64 type by default, which is determined by the underlying implementation of the golang language.
See this: Go Decode 、json package
So the elements in the excludePort array is of type float64 and 32104 is of type int
In order to get the desired result you need to implement this:
{{- if has 32104.0 .Values.excludePort}}
Of course, this is not a good implementation, because there is a precision problem caused by float, it is best to use a string to solve it.
Lisk this:
values.yaml
excludePort: ["32104", "30119"]
template/xxx.yaml
{{- if has "32104" .Values.excludePort}}
...

Extracting value dynamically from dictionary on helm2

I'm trying to run a bit of a complicated helm chart, and need some help.
I'm trying to get value from a dictionary like that:
get .myDictionary .Values.dictionaryKey
Unfortunately, looks like get function is not supported in helm 2 which I'm using.
Do you have any advice on how to get the dictionary value with dynamic value coming from a variable on helm2?
Apparently helm v2 uses sprig version 1 which doesn't have the get function (which was added only on v3).
The found solution in helm v2 is to use pluck "keyName" $dict | first.
The core Go text/template language contains a index function that does what you're looking for.
{{ index .myDictionary .Values.dictionaryKey }}

Helmcharts- how to access values nested inside a key that is a number?

I am trying to pull values out of a kubernetes helm chart values.yaml that has a number as one of the keys and I'm getting a parse error unexpected ".1" in operand. How can I access values that contain a number in its path?
Let's say my values.yaml looks like this:
global:
foo:
bar1: 1
1:
bar2: 2
Using helm charts, I can access bar1 by typing: {{ .Values.global.foo.bar1 }}.
If I try to do the same with accessing bar2 by typing: {{ .Values.global.1.bar2 }} I receive a parse error. It doesn't get better if I try to use brackets {{ .Values.global[1].bar2 }}, quotes {{ .Values.global."1".bar2 }}, or brackets and quotes: {{ .Values.global["1"].bar2 }}.
I know that helm charts utilize golang templates under the hood, is there some kind of template I could create to extract this information?
Thanks so much!
The easy option is to just quote it in your values file so it's a string but:
{{ index .Values.globals 1 "bar2"}}
is probably what you want.

Using where() node to filter empty tags in Kapacitor

Using Kapacitor 1.3 and I am trying to use the following where node to keep measurements with an empty tag. Nothing is passing through and I get the same result with ==''.
| where(lambda: 'process-cpu__process-name' =~ /^$/)
I can workaround this issue using a default value for missing tags and filter on this default tag, in the following node but I am wondering if there is a better way structure the initial where statement and avoid an extra node.
| default()
.tag('process-cpu__process-name','system')
| where(lambda: \"process-cpu__process-name\" == 'system' )
Sure it doesn't pass, 'cause this
'process-cpu__process-name'
is a string literal it TICKScript, not a reference to a field, which is
"process-cpu__process-name"
You obviously got the condition always false in this case.
Quite common mistake though, especially for someone with previous experience with the languages that tolerates both single & double quote for mere string. :-)
Also, there's a function in TICKScript lambda called strLength(), find the doc here, please.