Kubernetes create a Map and get Values Dynamically - kubernetes-helm

I would like to create a map in value file and get the value depending of another variable.
For example :
resource[belgium] : 4G
resource[canada] : 4G
Then by passing the value belgium in environment variable COUNTRY, something like:
{{ .Values.resource[$COUNTRY] }}
I don't find anything in the documentation.
Tx for help,
Gegko

Related

How to understand this Helm usage

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?

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}}
...

Fill Grafana Dashboard Variable From LoqQL

I have a Grafana dashboard and I'd like to define a variable for this dashboard. I'd like the values of this variable will come from LogQL query. To be more specific - in each log I have a field called "site_ids", and I want the values of the variable to be all the different "site_ids" (longs).
So I wrote this query:
{_namespace_="namespace",_schema_="schema"} | logfmt | line_format "{{.site_ids}}"
Which seems to work when I just run it in the query executor, this is the output (the actual site_ids):
0
-1
196
2
3
...
But when putting it as a query when I try to configure a new variable, I see nothing in the Preview of values:
Unfortunately I can barely find documentation about this..
Any suggestions?
Thanks!
Use label_values like this Query Variable section

Select value from custom key/value variable in Grafana

I want dome devices connected to a Grafana dashboard. Each device has a name. I wanted to "attatch" an IP to each device, but I can't modify the database, so I figured I could use custom variables.
The name of the device is saved on the "name" variable and I used a key/value custom variable called "list" with the name of the device as the key, and the IP as the value, like this:
DEVICE 1 : 192.168.0.26, DEVICE 2 : 192.168.0.27
Now I want to display the correct IP when the user select the name of the device but I haven't figured out the right way to do it.
I've tried the following:
Use the "Repeat by variable option" in the panel where I want to display the IP in, and select the variable "name".
Tried to access the value using this sintax: ${list[name]} and ${list.${name}}
Using this sintax ${list.name} seems to work, but it only displays the first value.
I can't make it happened. Please help me with the right sintax or the right way to do this.
Correct syntax for custom variables is ${variable:text} and ${variable:value}. So in your case ${variable:value} will print IP.

How do I map one variable's values to another variable in Grafana?

In my Grafana dashboard (with Prometheus as a data source), I have a custom $tier variable, which allows the user to pick the tier from a dropdown. It's defined as:
Values separated by comma: production, stage, development
I need to filter a Prometheus metric by a label which contains a shortened version of the tier name:
"foo-dev"
"foo-stage"
"foo-prod"
I was thinking that I'd create a hidden variable $shortened_tier so I could use that in my query filter, like this:
my_label=~"foo-$shortened_tier"
I'd like to define it based on the value of $tier:
"development" -> "dev"
"stage" -> "stage"
"production" -> "prod"
How do I do that?
I figured out a workaround for this, but it is suuuuper hacky:
Name: shortened_tier
Type: Query
Data Source: Prometheus
Query: label_values(up{env="$tier"}, env)
Regex: (dev|stage|prod).*
What I wanted to do was simply Query: $tier, but since Grafana wouldn't let me do that, I had to use a completely different metric (up) where I could pass in $tier and get back the same exact value as a string. Then I use regex to just look for dev|stage|prod at the beginning of the string, capture that part, and throw away the rest.
This has the result that I'm looking for, with the value of $shortened_tier dynamically changing based on the value that's selected and assigned to $tier. But man I wish Grafana had a less hacky way to do this.