Kubernetes: How to set boolean type variable in configMap
Asked Answered
S

2

20

I want to set a boolean variable in configMap (or secret):

apiVersion: v1
kind: ConfigMap
metadata:
  name: env-config
  namespace: mlo-stage
data:
  webpack_dev_server: false

But when I apply it, I get the following error:

The request is invalid: patch: Invalid value: "map[data:map[webpack_dev_server:false] metadata:map[annotations:map[kubectl.kubernetes.io/last-applied-configuration:{ blah blah blah}]]]": unrecognized type: string

I have tried to change the value to Off/No/False, all having the same problem.

It seems that the value of the keys in the data map can only be string, I have tried to change the value to "false", the yaml file is OK, but then the variable becomes a string but not boolean.

what should I do if I want to pass a boolean as value?

Schild answered 15/9, 2020 at 16:10 Comment(0)
R
38

Values in a ConfigMap must be key-value string values or files.

Change:

data:
  webpack_dev_server: false

To:

data:
  webpack_dev_server: "false"

To your question:

what should I do if I want to pass a boolean as value?

You may handle this in the application, transform from string to bool.

Rigging answered 15/9, 2020 at 16:48 Comment(1)
Thank you @Rigging for the answer, so this confirms that only strings can be passed as values in configMap.Schild
A
3

Change the following code like this:

data:
  webpack_dev_server: false

to

data:
  webpack_dev_server: "\"false\""

Below mentioned error can also be solved quickly in the same way.

error when creating "test.yaml": ConfigMap in version "v1" cannot be handled as a ConfigMap: json: cannot unmarshal number into Go struct field ConfigMap.data of type string.

Angarsk answered 24/5, 2023 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.