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

Ordered extraEnv interpolation #152

Open
ChipWolf opened this issue May 14, 2023 · 2 comments
Open

Ordered extraEnv interpolation #152

ChipWolf opened this issue May 14, 2023 · 2 comments

Comments

@ChipWolf
Copy link
Contributor

extraEnv vars sometimes need to be interpolated in a particular order when using variable substitution, as the dependent variables must be specified before their use in the env

@ChipWolf ChipWolf changed the title extraEnv before/after interpolation Ordered extraEnv interpolation May 14, 2023
@itzg
Copy link
Owner

itzg commented May 14, 2023

Can you provide an example since I'm not sure how the chart can be changed to fix anything like that?

@ChipWolf
Copy link
Contributor Author

ChipWolf commented May 15, 2023

Couple of crude examples. Bare in mind the order that env is rendered in the deployment:

{{- range $key, $value := .Values.extraEnv }}
{{- if kindIs "map" $value }}
{{- if hasKey $value "valueFrom" }}
- name: {{ $key }}
valueFrom:
{{- $value.valueFrom | toYaml | nindent 12 }}
{{- end }}
{{- else }}
- name: {{ $key }}
value: {{ $value | quote }}
{{- end }}
{{- end }}

  1. Using the pod name in the JMX options:
extraEnv:
  POD_NAME:
    valueFrom:
      fieldRef:
        fieldPath: metadata.name
minecraftServer:
  jvmOpts: >-
    -Dserver.name=$(POD_NAME)

This doesn't work as .minecraftServer.jvmOpts is rendered before .extraEnv, so the variable remains as-is. There is a workaround for this scenario:

extraEnv:
  POD_NAME:
    valueFrom:
      fieldRef:
        fieldPath: metadata.name
  JVM_OPTS: >-
    -Dserver.name=$(POD_NAME)
  1. Non-deterministic range issues (non-deterministic ordering from "range" results in unnecessary deployments helm/helm#4375 and Helm Template ordering breaks dependent environment variables helm/helm#9165)

Sometimes, the range is non-deterministic, this appears to be an issue when installing or upgrading a chart with the Helm CLI as it does some kind of delta, but doesn't seem to occur when using Flux Helm.

extraEnv:
  MC_VERSION: "1.19.2"
  SRV_BUILD: "37"
  PAPER_DOWNLOAD_URL: "https://example.org/versions/$(MC_VERSION)/builds/$(SRV_BUILD)/downloads/server-$(MC_VERSION)-$(SRV_BUILD).jar"

Sometimes this is rendered unpredictably, where it appears to be in some pseudo-chronological order following on an upgrade if anything in the map has changed, the dependent values are sometimes rendered after the PAPER_DOWNLOAD_URL in this case leaving them uninterpolated.
However, this can be monkey patched by prepending the dependent values with something like A_ and hoping for the best.


Ultimately, any method to guarantee the ordering of these values by weight or something would be great, even if it's just a case of ensuring the list is alphabetised entirely.

Apparently the recommended pattern is to use keys and sortAlpha to be deterministic, then the question just becomes how do you insert extraEnv before or after the minecraftServer helper values without migrating absolutely everything into extraEnv.

Also see https://dastrobu.medium.com/an-advanced-api-for-environment-variables-in-helm-charts-e0bb1e0aa58a
"Kahn's algorithm" is defined as a helper function and maintains the user-provided order in the link above, without some modification this would be a breaking change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants