Helm Chart is a variable declared as a string in the values ​​file, but after configuring the replacement placeholder, it becomes a numeric type.

Translation wujiuye 160 0 2024-04-19

This article is a translation of the original text, which can be found at the following link: https://www.wujiuye.com/article/bb073de1a21e49bdbf38a14788427ad3

Author: wujiuye
Link: https://www.wujiuye.com/article/bb073de1a21e49bdbf38a14788427ad3
Source: 吴就业的网络日记
This article is an original work by the blogger and is not allowed to be reproduced without the blogger's permission.

helm string int

When you define a value in your values.yaml file, Helm uses the encoding/json package to unmarshal the YAML data into a map[string]interface{} structure. This means that the value is stored as an interface{} type, which can hold any type of value (string, int, float, etc.).

When you use the {{.Values.prometheus.username}} syntax in your template, Helm tries to convert the interface{} value to a string using the fmt.Sprintf function. If the value can be converted to an integer (like in your case, where the value is '1491301'), Helm will automatically convert it to an int type.

This is why you’re seeing the single quotes disappear and the value being treated as an integer. This behavior is due to the way Go’s encoding/json package handles YAML unmarshaling.

'{{ toString .Values.prometheus.username }}'

To force the value to be treated as a string, you need to use the toString function. This function explicitly converts the interface{} value to a string, ensuring that it’s treated as a string in the template.

Using single quotes or double quotes around the value in the values.yaml file doesn’t make a difference because Helm will still unmarshal the value as an interface{} type.

So, to summarize: