Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double quotes removed from strings #4845

Open
uroshercog opened this issue Nov 1, 2022 · 12 comments
Open

Double quotes removed from strings #4845

uroshercog opened this issue Nov 1, 2022 · 12 comments
Labels
kind/bug Categorizes issue or PR as related to a bug. triage/accepted Indicates an issue or PR is ready to be actively worked on.

Comments

@uroshercog
Copy link

Describe the bug
Running kustomize removes double and/or single quotes from strings.

If a value in a config map is a template placeholder and the actual application value is not yet known because the built resources are post-processed by another process, the quotes should not be removed. Removing them can lead to producing invalid config maps. For example if the value of the placeholder is a commit SHA which just happens to be all numeric then the value in the config map is interpreted as a number by Kubernetes and rejected with a validation error.
Values in config maps should always be quoted given that they can only be strings anyways.

Files that can reproduce the issue

config-map.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: example-config-map
data:
  key: "${placeholder}"

kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: example
resources:
  - config-map.yaml

Expected output

apiVersion: v1
kind: ConfigMap
metadata:
  name: example-config-map
data:
  key: "${placeholder}"

Actual output

apiVersion: v1
kind: ConfigMap
metadata:
  name: example-config-map
data:
  key: ${placeholder}

Kustomize version

{Version:kustomize/v4.5.7 GitCommit:56d82a8378dfc8dc3b3b1085e5a6e67b82966bd7 BuildDate:2022-08-02T16:35:54Z GoOs:darwin GoArch:arm64}

Platform

MacOS M1 Monterey 12.6

@uroshercog uroshercog added the kind/bug Categorizes issue or PR as related to a bug. label Nov 1, 2022
@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Nov 1, 2022
@KnVerey
Copy link
Contributor

KnVerey commented Dec 7, 2022

This is highly related to #4146, #3412 and others where the root cause is a possibly-non-string value in what Kustomize theoretically could recognize as a string field. I believe the conclusion so far has been that the quotes are being stripped as superfluous by the YAML library we are using, so although we agree with the behaviour being undesirable, the universal fix isn't obvious or simple (e.g. should we try to force the style to be preserved, and how? or should we use schema knowledge to override after the fact, and where?).

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Dec 7, 2022
@emmaLP
Copy link

emmaLP commented Mar 13, 2023

@KnVerey Any update on this please?

We have just hit this issue on the same version when reading config from an env file and then the value is then used to set some labels.

Using kustomize v4.5.7

If we try wrap the value in double or single quotes to preserve the value as a string, we then get validation errors such as

Deployment.apps "service-name" is invalid: [metadata.labels: Invalid value: "'9079318'": a valid label must be an empty str
ing or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyValue',  or 'my_value',  or '12345', regex used for validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?'), spec.template.labels: Invalid value: "'9079318'": a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyValue',  or 'my_value',  or '12345', regex used for validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?')]  

Update 2023-03-14:
Tried the same config on the following versions and still experiencing the same behaviour:

  1. v5.0.0
  2. v4.5.5
  3. v4.0.5

@gandarez
Copy link

gandarez commented May 3, 2023

Facing the same problem when setting loadBalancerIP for traefik which expects an IP address surrounded by double quotes.

@gandarez
Copy link

gandarez commented Jun 9, 2023

Any update on this?

@CpuID
Copy link

CpuID commented Jul 10, 2023

Also looks related to #3485 - which was closed due to vars deprecation, but the quotes issue looks identical.

@snukone
Copy link

snukone commented Aug 9, 2023

This is still an issue. Tried kustomize build with kustomize version 5.0.0:

configMapGenerator:
- behavior: merge
  literals:
  - ENVIRONMENT='${TEST}'
name: config

Turns into after kustomize build:

apiVersion: v1
data:
  ENVIRONMENT: ${TEST}

After that i do an envsubst and replace ${TEST} with 12345. The resulting YAML isnt possible to apply with kubectl due to conversion error: cannot convert int64 to string (because of ENVIRONMENT: 12345).
Escaping the value 12345 with multiple single quotes or backslahes makes kubectl apply work, but the multiple single quotes or backslash arent replaced (e.g. lead to ENVIRONMENT: \'12345\' in the new configmap)

@snukone
Copy link

snukone commented Aug 9, 2023

Solution: Using block scalar style as mentioned here with Kustomize v5.0.0 works:
#4233 (comment)
https://stackoverflow.com/a/21699210/7746963

configMapGenerator:
- behavior: merge
  literals:
  - |
    ENVIRONMENT=${TEST}
name: config

Now building the Kustomization, subsitute ${TEST} with 12345 and applying it with kubectl results in the following ConfigMap:

apiVersion: v1
data:
ENVIRONMENT: |
    12345

You will have a linebreak at the end of the value (>- or |- doesnt help), but when deploying the application and checking the environment variables within the container of the pod with env the value is without linebreak and the application works as designed:

$ env | grep ENV
ENVIRONMENT=12345
ENVPATH=/dev

@alxndr42
Copy link

The same problem exists in replacements. I need to take a string value and put it in an annotation, which fails because the value gets converted to an int.

@Pradumn99
Copy link

Do we have any solution to above issue as I still observe this behaviour of stripping away the double quotes ?

@krokofant
Copy link

Replacements seems conventionally useless if it breaks quoted numbers 😕

@pastukhov
Copy link

Any news?

@NGPixel
Copy link

NGPixel commented Jun 8, 2024

It's quite annoying that the maintainers don't seem interested in addressing this issue. Dynamically replacing vars in the kustomize output, outside of the "kustomize process", is a totally valid workflow.

A workaround is to use yq (the YAML equivalent to jq) to add back double quotes to values that are environment variables:

kubectl kustomize foobar > manifest.yaml
yq -i --unwrapScalar=false '(.. |= select(. == "$*") style="double")' manifest.yaml

You can then replace vars further down the pipeline, e.g. with envsubst:

kubectl kustomize foobar > manifest.yaml
yq -i --unwrapScalar=false '(.. |= select(. == "$*") style="double")' manifest.yaml
envsubst < manifest.yaml | kubectl apply -f -

or as a single pipeline:

kubectl kustomize foobar | yq --unwrapScalar=false '(.. |= select(. == "$*") style="double")' | envsubst | kubectl apply -f -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

No branches or pull requests