Kubernetes: Cannot convert int64 to string. Kubernetes fails to interpret integer value in helmchart values.yaml file
Asked Answered
T

3

12

I have a values.yaml file in which I have given spring_datasource_hikari_maximum_pool_size: "10"

In deployment yaml I have used this value as

 - name: SPRING_DATASOURCE_HIKARI_MAXIMUM-POOL-SIZE
    value: {{ .Values.spring_datasource_hikari_maximum_pool_size }}

However, when used inside the deployment.yaml file it fails with the below error.


Deploy failed: The request is invalid: patch: Invalid value: "map[metadata:map[annotations:map[kubectl.kubernetes.io/last-applied-configuration:{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":
{
(helm values etc)
`{"name":"SPRING_DATASOURCE_HIKARI_MAXIMUM-POOL-SIZE","value":10}]` **(this is the incorrect value)** 
}
cannot convert int64 to string

What is the correct format of using an integer value from values.yaml file in a deployment.yaml file?

I have also tried multiple combinations with quotes "" but nothing seems to be working.

Any help is appreciated, Thanks in advance.

Tuchun answered 17/6, 2021 at 5:34 Comment(1)
just double-quote your value. Dealing with numeric values (env vars, args, resources allocation) => yaml to json conversion would cast those as integers, rather than strings..Appel
T
24

I was able to resolve this by using double quotes on the value itself in deployment.yaml file

- name: SPRING_DATASOURCE_HIKARI_MAXIMUM-POOL-SIZE
  value: "{{ .Values.spring_datasource_hikari_maximum_pool_size }}"

Since this was a production instance I could not check with @David Maze and Vit's solution.

Edit:

Tried with quote option and it worked too.

 - name: SPRING_DATASOURCE_HIKARI_MAXIMUMPOOLSIZE 
   value: {{ quote .Values.spring_datasource_hikari_maximum_pool_size }}
Tuchun answered 17/6, 2021 at 12:45 Comment(0)
T
4

YAML values have types, and the standard rule is to consider a string of digits like 10 to be a number. In the Kubernetes YAML format, though, the names and values of environment variables have to be strings.

The easiest way to do this is to use the Helm (Sprig) quote function, which will wrap its parameter in double quotes:

- name: SPRING_DATASOURCE_HIKARI_MAXIMUMPOOLSIZE
  value: {{ quote .Values.spring_datasource_hikari_maximum_pool_size }}
{{/*        ^^^^^                                                  */}}

quote isn't especially intelligent; it's the same as value: "{{ .Values...}}". There's a similar squote that would wrap the value in single quotes.

If you wanted a really robust solution, you could use print to convert an arbitrary value to a string, then the lightly-documented toJson function to convert that to JSON. By design, valid JSON is valid YAML, and "converting a string to JSON" will mean double-quoting it and otherwise escaping it as needed.

value: {{ .Values...pool_size | print | toJson }}
Trifacial answered 17/6, 2021 at 11:4 Comment(3)
Thanks for the quick response. I was able to use quotes ("") in the deployment.yaml file like value: "{{ .Values.spring_datasource_hikari_maximum_pool_size }}" Although I didn't think this would work as I thought it will take the literal value inside the quotes. However, it worked like a charm, Thanks again @David MazeTuchun
Do you have a reference for the first statement?Pusillanimity
Are you looking for the grammars in the YAML Recommended Schemas, that say that 10 is an integer?Trifacial
P
3

Check solutions from Helm Environment Variables with Booleans and Integers

We can use !!str to convert the output to a string, Alternatively we can also use a undefined !! and get the same behaviour giving later developers nice hints of what we intended !!booleanEnv or !!integerEnv will cast the values to string (or even just !!boolean)

- name: SPRING_DATASOURCE_HIKARI_MAXIMUM-POOL-SIZE
    value: !!integerEnv {{ .Values.spring_datasource_hikari_maximum_pool_size }}
- name: FAVORITE_DRINK
    value: !!stringEnv {{ .Values.favoriteDrink }}
- name: TAKES_SUGAR
    value: !!booleanEnv {{ .Values.takesSugar }}
Prostrate answered 17/6, 2021 at 9:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.