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

Use Global Values when local values doesn't exist #5676

Closed
mcortinas opened this issue May 6, 2019 · 8 comments
Closed

Use Global Values when local values doesn't exist #5676

mcortinas opened this issue May 6, 2019 · 8 comments

Comments

@mcortinas
Copy link

I want use local values from alias dependency more priority than a common values. If the local value exist use this instead of global value.
Let me share an example of values.yaml in the parent chart

stable:
  key1: local_value1_stable_alias
global:
  key1: global_value1

The only option to do that I think is templating the subchart with this code

{{- if .Values.key1 }}
  key1: {{ .Values.key1 }}
{{- else }}
  key1: {{ .Values.global.key1 }}
{{- end }}

Basically, we want use a global values when local values is not set.

I tried the workaround commented on #2076 using the global inside a local valuas from alias (called stable in this example)

stable:
  global:
    key4: value4_from_megachart_stable_global
global:
  key4: value4_from_megachart

This is not a perfect workaround due to the global variables has more priority than local variables, let me define a common section in the same values file but with more priority.

Do you know some go functions in order to be subchart template code more simple???

@mcortinas
Copy link
Author

I also tried use default from http://masterminds.github.io/sprig/defaults.html with this code

key3: {{ default .Values.key3 .Values.global.key3 }}

and this:

key3: {{ default .Values.global.key3 .Values.key3 }}

without lucky, It seems only print local value if global is not defined.

@mattfarina
Copy link
Collaborator

@mcortinas If I understand it right, you want the local variable to have precedence over the global one. Two things...

First, I would be very curious to know your use case so we can better understand the need. We're always curious to understand what people are doing and why they're doing it.

Second, you could create a function in the _helpers.tpl file to pick the local one if it has a value or fall back to a global one. This might provide a quick solution you can use now.

@jgleonard
Copy link
Contributor

jgleonard commented May 7, 2019

@mattfarina I was thinking about a similar functionality.

I have a few standalone charts that can also be grouped together under a parent chart. Two scenarios here...

  1. In the case that they are being called as subcharts, I would want to use a global rather than duplicate values for each child, but I wouldn't want to be forced into using globals in the children when they're being deployed standalone.

  2. Additionally, I could envision a scenario where a value passed to a subchart would be assumed to override the global at that same level, but since Helm doesn't appear to make that distinction, that level of precedence isn't possible.

# chartA
food: ham
vessel: plate
# chartB
food: eggs 
vessel: bowl
# parent
chartA:
  food: bacon

chartB:
  vessel: plate

global:
  food: sausage

Deployed with the parent, I could see arguments for expecting a plate of bacon and a plate of sausage, but given the way that helm sets values, you'd end up with 2 plates of sausage. Apologies for the poor example, but tried to keep it simple.

@jgleonard
Copy link
Contributor

Also reminds me of #2492

@mcortinas
Copy link
Author

mcortinas commented May 8, 2019

@mattfarina , let me explain my use case.... my team develop charts and this is used by other teams teams as a subchart several times in a mega chart with helm dependencies using aliases.
We want the other teams can define common values for all the subcharts and use a common section, I think in global section.
@Do you think some tpl functions in order to do this king of logic?

{{ if .Values.parent.child.net }}
  {{ .Values.parent.child.net }}
{{ else }}
  {{ .Values.global.parent.child.net  }}
{{ end }}

I tried use sprig functions like coalesce or add the in templa a simple if clause but I found this issue with nested values like this:
Error: render error in "templates/svc.yaml": template: templates/svc.yaml:16:25: executing "templates/svc.yaml" at <.Values.parent.child...>: can't evaluate field child in type interface {}
when the values of the parent chart has a nested values like this

parent:
  child:
    net: "joan"

Finally, I think I'm following applying KISS and apply the solution adding global inside each aliases in the parent chart values.yaml commented in my first comment, I don't like due to the global parent has more priority than the global inside each alias... but is more trustly.

@jfcoz
Copy link

jfcoz commented Nov 15, 2019

This could also be usefull to pass the nodeSelector to all dependency, with helm --set global.nodeSelector=…

Each dependency would use it, unless dependency.nodeSelector is defined

@flutt13
Copy link

flutt13 commented Aug 12, 2020

Don't know if the question is still valid, but I think I solved this problem by using merge function

values.yaml:

global:
  defaults:
    key1: value1
    key2: value2

subchart_name:
  defaultOverrides:
    key1: test

And then in any of the subchart templates

{{- $properties := merge .Values.defaultOverrides $.Values.global.defaults }}
{{ toYaml $properties }}

which should print

key1: test
key2: value2

This will make subchart value override global value (this is because we passed local .Values as a first argument to merge which takes higher precedence).
As a bonus no need to use the ugly if structure or even default function. The value set in global section acts as a default if it's not overridden in subchart values.

@bacongobbler
Copy link
Member

thanks @flutt13 for the solution. I guess we can consider this resolved.

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

No branches or pull requests

6 participants