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

Document how to use numbers and booleans in post build substitutions #1129

Merged
merged 2 commits into from
Apr 8, 2024

Conversation

stefanprodan
Copy link
Member

When using post build substitutions with vars that are numbers or booleans, the reconciliation may fail if the substitution
is for a field that must be of type string. This PR adds a section to the API docs on how to use double quotes vars, and adds unit tests for this procedure.

Fix: fluxcd/flux2#4154
Fix: fluxcd/flux2#1839
Fix: #1096
Fix: #728
Fix: #554

Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
@stefanprodan stefanprodan added area/docs Documentation related issues and pull requests area/testing Testing related issues and pull requests labels Apr 6, 2024
@stefanprodan stefanprodan requested a review from souleb April 6, 2024 11:25
@stefanprodan stefanprodan merged commit b2daff1 into main Apr 8, 2024
8 checks passed
@stefanprodan stefanprodan deleted the substitute-int-bool branch April 8, 2024 14:49
@ahothan
Copy link

ahothan commented Apr 8, 2024

Stefan, we use quoted int and bool and it works fine with latest flux version (ie it renders as a double quoted string containing the numeric or bool value)

enabled: "'true'"
count: "'40'"

@stefanprodan
Copy link
Member Author

The problem with this "' is that when the var comes as a string it will be invalid, for example some AWS IDs can be int or string.

@ahothan
Copy link

ahothan commented Apr 8, 2024

not really a problem because for string, the variable value should just be defined in ConfigMap as usual without need for any quoting (or with the regular quoting)

@stefanprodan
Copy link
Member Author

stefanprodan commented Apr 9, 2024

@ahothan your solution doesn't work, using "'${number}'" for metadata labels results in: Invalid value: \"'123'\": a valid label must be a string that consist of alphanumeric characters, '-', '_' or '.'. I think it works for you, because you only do substitutions for Helm values.

@ahothan
Copy link

ahothan commented Apr 10, 2024

we would not be using "'${number}'" at all

example of definitions in configmap that should all result in a string being rendered

number: "'100'"
condition: "'true'"
text: "anytext_that_is_not_int_or_bool"

and the reference from any flux yaml file (can be in values of HelmRelease or in any part of k8s manifest):

text1: ${text}
text2: ${number:='100'}
text3: ${cond}

rendered:

text1: anytext_that_is_not_int_or_bool
text2: "100"
text3: "true"

I have not tried it on a metadata label but it should work

@ruiengana
Copy link

ruiengana commented Apr 24, 2024

I think this works better...

var="100"
value: ${v="${var}"}

renders

value: "100"

better than having ${quote}${var}${quote} I guess...

@nick4fake
Copy link

So... there is still no clean solution? This doesn't work for many cases, like substitutions from configmaps

@stefanprodan
Copy link
Member Author

@nick4fake I don't see why substitutions from ConfigMaps would not work with the solution present in the docs. You can configure it like this:

spec:
  postBuild:
    substitute:
      quote: '"'
    substituteFrom:
      - kind: ConfigMap
        name: cluster-vars

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment