Mohammed Idris
Mohammed Idris

Reputation: 938

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

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.

Upvotes: 15

Views: 29172

Answers (3)

Mohammed Idris
Mohammed Idris

Reputation: 938

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

Upvotes: 27

David Maze
David Maze

Reputation: 158647

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

Upvotes: 5

Vit
Vit

Reputation: 8411

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

Upvotes: 3

Related Questions