Skip to content

Fix null key removal in nested subcharts - #11440

Closed
jamoflaw wants to merge 5 commits into
helm:mainfrom
jamoflaw:fix-null-removal-in-nested-subcharts
Closed

jamoflaw wants to merge 5 commits into
helm:mainfrom
jamoflaw:fix-null-removal-in-nested-subcharts

Conversation

@jamoflaw

@jamoflaw jamoflaw commented Oct 14, 2022 •

Copy link
Copy Markdown

closes #9136
closes #9027

This PR contains two commits, the first replicates the behaviour described in #9136 and the second resolves it.

The original unit-tests did have a case for the subchart values and it was passing, so I spent some time trying to work out why there is a difference between the behaviour of the unit-tests and real world usage. It seems that the bug is related to the way the chart object is mutated by chartutil.ProcessDependencies. During that call the chart objects values are merged with its subcharts leading to a situation where all values are merged together into the parent chart's values.

To replicate this behaviour in the unit tests I just added the following to the parent charts definition:

			"pequod":   map[string]interface{}{
				"ahab":     map[string]interface{}{
					"scope": "ahab",
					"nested": map[string]interface{}{
						"foo": true,
						"bar": true,
					},
				},
			},

Since bar is now defined in that parent chart object, when the call to coalesceValues(printf, ch, dest, prefix) was made, the dest object was mutated and null keys were removed from both the parent chart and subchart values. The subsequent call to coalesceDeps(printf, ch, dest, prefix) then has no more null keys anymore so the subcharts no longer have any keys removed, which causes the subcharts values to be merged back into the parent chart (undo-ing the null removal from the previous call to coalesceValues).

The changes made effectively delay the removal of the null keys until after both the coalesceValues and coalesceDeps calls are completed.

  • this PR contains documentation
  • this PR contains unit tests
  • this PR has been tested for backwards compatibility

@pull-request-size pull-request-size Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Oct 14, 2022
@sabre1041
sabre1041 self-requested a review October 14, 2022 23:04
@sabre1041

Copy link
Copy Markdown
Contributor

@jamoflaw I attempted the following test:

subchart with values

resources:
  limits:
    cpu: 100m
    memory: 128Mi
  requests:
    cpu: 100m
    memory: 128Mi

chart that uses subchart as a dependency:

subchart:
  resources:
    requests:
      cpu: 1000m
      memory: 2Gi
    limits:
      cpu: null
      memory: 2Gi

Resulted in the following to be rendered:

resources:
  limits:
    cpu: 100m
    memory: 2Gi
  requests:
    cpu: 1000m
    memory: 2Gi

Unfortunately, the original value remains

@jamoflaw

Copy link
Copy Markdown
Author

@sabre1041 do you happen to have the chart you tested it on available? I tested this PR on one of my internal ones and it was able to remove mapping values which were set in the subchart from a values file. (ie -f) Were you overriding them in the parent chart? I wonder if there are more issues than I originally found.

So my testing was effectively the equivalent of sub chart with values:

resources:
  limits:
    cpu: 100m
    memory: 128Mi
  requests:
    cpu: 100m
    memory: 128Mi 

Then in the parent chart - nothing above is set

Then I am passing in (with -f) an overrides file with:

subchart:
  resources:
    limits:
      cpu: null
      memory: 128Mi
    requests:
      cpu: 100m
      memory: 128Mi 

Which results in:

subchart:
  resources:
    limits:
      memory: 128Mi
    requests:
      cpu: 100m
      memory: 128Mi 

@sabre1041

Copy link
Copy Markdown
Contributor

@jamoflaw this is unfortunately a large issue where there are multiple scenarios that need to be accounted for.

My test did not explicitly reference an external values file but were referenced in the parent chart that used the chart as a dependency

@jamoflaw

Copy link
Copy Markdown
Author

Agreed! I've written another unit test which covers that scenario and I'll have a look at seeing whats going on

@jamoflaw

Copy link
Copy Markdown
Author

OK yeah I've replicated it - the same case works in the current unit tests though, which isn't ideal!! I'll update the PR with a fix and write a unit test which triggers it. Thanks @sabre1041

@pull-request-size pull-request-size Bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 15, 2022
@jamoflaw

Copy link
Copy Markdown
Author

So this update fixes it for parent chart overrides too. The issue seems to be that CoalesceValues isn't idempotent when it comes to null removal behaviour and it is called a LOT throughout the rendering pipeline basically removing nulls on the first call then remerging values back in from subcharts on subsequent calls.

What I did here was split the CoalesceValues into two (keeping the old names pointing back at the new variant which shares its old behaviour) so it is clear what each variant does, and then delay the removal of null keys even further than previous (basically at the end of the rendering pipeline)

The two variants are *KeepNil and *TrimNil which as their names suggest either keep nils around or trim them. Where possible the Trim variant is used to make sure behaviour isn't needlessly changed, but during initial dependency gathering its changed to the KeepNil variant to avoid early stripping of null keys.

the values. Creating new public function TrimNilValues
which does the final nil value removal. This is called in
ToRenderValues before final schema validation.

Signed-off-by: James Payne <[email protected]>
@jamoflaw

jamoflaw commented Oct 16, 2022 •

Copy link
Copy Markdown
Author

@sabre1041 as an alternative to this PR I have also prepared main...jamoflaw:helm:fix-null-removal-in-nested-subcharts-alt. Both the current state of this PR and this new alternative branch now handle my original case (where overrides in files provided with -f couldn't override nested null keys) and the case you mentioned (where a parent chart defines a null key value to remove a subchart key).

Personally I feel the alternative branch above is a simpler and more effective solution but unlike this PR it means that the current behaviour of CoalesceValues changes slightly. The current behaviour is inconsistently buggy around null removal whereas in my alternative branch above it is always consistently now not removing null keys. Instead this is delegated to a new public method TrimNilValues which has the sole purpose of achieving the removal of null keys. This null removal is then done late in the pipeline in ToRenderValues.

I have purposely not yet raised this as a new PR as wanted to get some feedback on if this approach is viable given it alters the CoalesceValues behaviour slightly.

@sabre1041

Copy link
Copy Markdown
Contributor

@jamoflaw can confirm that the PR above does work as expected with the use case that I brought up. I'll look more into the implementation and any potential implication of the change in functionality within the CoalesceValues function

@Vermyndax

Copy link
Copy Markdown

Any hope of this one getting pushed through for the 11/09 release? Any way I can upvote it?

@sabre1041

Copy link
Copy Markdown
Contributor

@Vermyndax I can take a look again later on this week

@jamoflaw

Copy link
Copy Markdown
Author

For my 2c on the issue:

I prefer this implementation main...jamoflaw:helm:fix-null-removal-in-nested-subcharts-alt as its significantly cleaner IMHO, but replaces a buggy behaviour - where null keys are removed inconsistently, with a consistent behaviour where null keys are instead retained and returned by that function. Internally then keys are removed with a second dedicated function.

I'm happy to rewrite as required to get this one working, maybe taking the alternative approach PR and making it backward compatible, splitting new behaviour into new functions? This does hit us quite a bit as we use nested helm charts a lot, but would like some guidance on the approach just so I don't rewrite it in the wrong direction.

@joejulian joejulian added this to the 3.10.2 milestone Oct 28, 2022
@joejulian joejulian added the bug Categorizes issue or PR as related to a bug. label Oct 28, 2022
@mattfarina mattfarina modified the milestones: 3.10.2, 3.10.3 Nov 10, 2022
@Vermyndax

Copy link
Copy Markdown

...longingly came by to check on all of you... hope you're well... :) Is there anything I can do to help push this along?

@joelio

joelio commented Dec 13, 2022

Copy link
Copy Markdown

Hey, is there anything to help expedite this, it'll be super useful for our work and the sooner the better for us!

@jamoflaw

Copy link
Copy Markdown
Author

For my 2c on the issue:

I prefer this implementation main...jamoflaw:helm:fix-null-removal-in-nested-subcharts-alt as its significantly cleaner IMHO, but replaces a buggy behaviour - where null keys are removed inconsistently, with a consistent behaviour where null keys are instead retained and returned by that function. Internally then keys are removed with a second dedicated function.

I'm happy to rewrite as required to get this one working, maybe taking the alternative approach PR and making it backward compatible, splitting new behaviour into new functions? This does hit us quite a bit as we use nested helm charts a lot, but would like some guidance on the approach just so I don't rewrite it in the wrong direction.

Just a follow up on this comment - I will have time to look at it this weekend - does it seem like something which would be worth following up on as I wouldn't want this issue to become stale

@sabre1041

Copy link
Copy Markdown
Contributor

@jamoflaw I'd like to move this forward. please take a look at it at your convenience and we can push forward as it is an issue that does need to be addressed

@kladiv

kladiv commented Dec 14, 2022

Copy link
Copy Markdown

+1 i follow this PR, useful in scenario when we want to remove (default) resources.limits of kube-prometheus-stack helm chart (made of subcharts)

@mattfarina mattfarina added this to the 3.12.0 milestone Apr 12, 2023
@alice-sawatzky

Copy link
Copy Markdown

can someone please give this a look? As of 2 weeks ago it's been LGTM'd, it's passing CI, and it still missed another release window

@bernardolopes8

Copy link
Copy Markdown

Any chance that this is ready for 3.12.0? This fix would be great to finally remove workarounds.

@sofam

sofam commented Apr 19, 2023

Copy link
Copy Markdown

Any progress on getting this merged in? I just hit this issue with multiple of our Helm charts.

@jamoflaw

Copy link
Copy Markdown
Author

Is there anything I need to do at this stage? Do we need additional approvers given the size/L tag @sabre1041?

@gjenkins8 gjenkins8 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-maintainer (community member), but had a few minorish comments.

Otherwise, agree, we document null-key removal here, and this should apply correctly to subcharts

Comment thread pkg/chartutil/coalesce.go
// the values destined for its dependencies.
//
// Note that CoalesceValues automatically removes nil values
func CoalesceValues(chrt *chart.Chart, vals map[string]interface{}) (Values, error) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't CoalesceValues be written like? (or did I miss something)

func CoalesceValues(chrt *chart.Chart, vals map[string]interface{}) (Values, error) {
    return TrimNilValues(MergeValues(chrt, vals))
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it could actually - currently TrimNilValues doesn't return the err on line 96 which I think could be improved as well, if that was done the sigatures should be compatible.

Ill give this a go tomorrow and test it out

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it works out as:

func CoalesceValues(chrt *chart.Chart, vals map[string]interface{}) (Values, error) {
	valsCopy, err := MergeValues(chrt, vals)
	if err != nil {
		return vals, err
	}
	return TrimNilValues(valsCopy)
}

to avoid passing the error down into TrimNilValues - I'll run against my larger charts which show the original error tomorrow morning

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the bulk of CoalesceValues is MergeValues can we have the code updated to this.

Comment on lines +83 to +102
if val, ok := values["top"]; !ok || val != "exists" {
t.Errorf("Key top was expected to be 'exists' but it is %s", val)
}
if val, ok := values["bottom"]; !ok || val != "exists" {
t.Errorf("Key bottom was expected to be 'exists' but it is %s", val)
}
if val, ok := values["left"]; ok || val != nil {
t.Errorf("Key left was expected to be missing but it is %s", val)
}
if val, ok := values["right"]; !ok || val != "exists" {
t.Errorf("Key right was expected to be 'exists' but it is %s", val)
}

nested := values["nested"].(map[string]interface{})
if val, ok := nested["left"]; !ok || val != true {
t.Errorf("Key nested.left was expected to be true but it is %s", val)
}
if val, ok := nested["right"]; ok || val != nil {
t.Errorf("Key nested.right was expected to be missing but it is %s", val)
}

@gjenkins8 gjenkins8 Apr 21, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

somewhat stylistic, but perhaps a deep equal would be more concise here?

 expectedValues := map[string]interface{}{
	"top":    "exists",
	"bottom": "exists",
	"right":  "exists",
	"nested": map[string]interface{}{
		"left":  true,
	},
}
assert.Equal(t, expectedValues, values) # https://pkg.go.dev/github.com/stretchr/testify/assert#Equal

I think (would need to be vetted) that the deep-equal comparison provided by https://pkg.go.dev/github.com/stretchr/testify/assert#Equal would do the expected behavior here

Comment thread pkg/lint/rules/values.go
// We could change that. For now, though, we retain that strategy, and thus can
// coalesce tables (like reuse-values does) instead of doing the full chart
// CoalesceValues
// MergeValues

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this change intended?

t.Fatal(err)
}
j, _ := json.MarshalIndent(v, "", " ")
t.Logf("Coalesced Values: %s", string(j))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t.Logf("Coalesced Values: %s", string(j))
t.Logf("Merged Values: %s", string(j))

Comment thread pkg/chartutil/values.go
}

// Remove nil values from the coalesced map
vals = TrimNilValues(vals)

@gjenkins8 gjenkins8 Apr 21, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup its basically done last as the previous issue was due to CoalesceValues basically being non-idempotent but being called as if it was. This delays that call to the last moment before its needed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, thanks for clarifying.

@mattfarina mattfarina left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR and for putting work into this, @jamoflaw

I did a brief run though and have some questions (and one comment). A big thing is to look at all the consequences of the change. Including how output changes.

I'll circle back with more feedback asap but wanted to get these questions added before I walk away from the PR for the day.

Comment thread pkg/chartutil/coalesce.go
// the values destined for its dependencies.
// Note that MergeValues does NOT automatically remove nil values, it is up
// to the caller to ensure that TrimNilValues is called at the appropriate time
func MergeValues(chrt *chart.Chart, vals map[string]interface{}) (Values, error) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helm is both a client (as in the CLI - the helm tool) and an SDK. Other applications can use the public functions in their own code. CoalesceValues is an example of a function used by others. I'm reviewing CoalesceValues and MergeValues with that in mind.

MergeValues is the exact same code as CoalesceValues was before this change. Is that because you think trimming should be part of the exported code used by other applications? CoalesceValues is no longer used by Helm itself.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its the opposite so MergeValues is called internally but leaves null values as is - CoalesceValues maintains exactly the same external behaviour - in that nulls are removed albeit maintaining that incorrect behaviour if its called multiple times such as how Helm internally does it

Comment thread pkg/chartutil/coalesce.go
// - Scalar values and arrays are replaced, maps are merged
// - A chart has access to all of the variables for it, as well as all of
// the values destined for its dependencies.
// Note that MergeValues does NOT automatically remove nil values, it is up

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put a blank link before the Note. So that it appears separately from the final bullet.

Comment thread pkg/chartutil/coalesce.go
Comment on lines +90 to +92
// TrimNilValues removes nil/null values from maps.
//
// To be used after CoalesceValues to remove keys as needed

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function comment is a little unclear here. These comments are for external users of the SDK.

CoalesceValues uses TrimNilValues. If an end user uses CoalesceValues there is no need to use TrimNilValues.

Can you please update the comment for clarity.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah this is true it should mention MergeValues here instead

Comment thread pkg/chartutil/coalesce.go
// the values destined for its dependencies.
//
// Note that CoalesceValues automatically removes nil values
func CoalesceValues(chrt *chart.Chart, vals map[string]interface{}) (Values, error) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the bulk of CoalesceValues is MergeValues can we have the code updated to this.

Comment thread cmd/helm/status.go
fmt.Fprintln(out)

cfg, err := chartutil.CoalesceValues(s.release.Chart, s.release.Config)
cfg, err := chartutil.MergeValues(s.release.Chart, s.release.Config)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section of code is used by helm get all, helm --debug install, and possibly other places. Before this change values that were null were not displayed in the output. After this change they are displayed with a value of null in the computed values. Is this intentional?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imo that is what the output should explicitly show, because that is the whole purpose of this pr. Nill values should be propagated. So any result output should list that this is what is happening. Explicitly.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I was mentioning here #11440 (comment)

Basically the original behaviour of the public function CoalesceValues is maintained in this PR and a new public function MergeValues is also created which does not trim nil values from the resulting output. This is to allow those nils to persist until the last call to TrimNilValues where they will be removed.

In short:

  • MergeValues is effectively a "fixed" CoalesceValues - but it leaves nulls in place and returns them
  • CoalesceValues maintains its previous potentially buggy behaviour but doesn't return null ever - I say potentially as its only when its called in succession as helm does internally - externally this behaviour may not have been seen

I have made the assumtion in the original comment that CoalesceValues should never return null - this may not be the case and in which case we could simplify this PR and the potential annoyance of wording between Merge and Coalesce - but it is potentially changing external behaviour which this current PR fixes albeit with the aforementioned naming challenges.

Before I make additional changes its probably worth clearing up if my assumption is valid or if we can make CoalesceValues return nulls

@joejulian joejulian modified the milestones: 3.12.0, 3.13.0 May 5, 2023
@alice-sawatzky

Copy link
Copy Markdown

any updates? @jamoflaw if you're burned out on the back and forth i'd be happy to pick this up

@prune998

Copy link
Copy Markdown

@alice-sawatzky you should take over as this is 3 years old now and we still can't "remove" value in a subchart... :/

@jamoflaw

Copy link
Copy Markdown
Author

@alice-sawatzky you should take over as this is 3 years old now and we still can't "remove" value in a subchart... :/

I'm happy to move this forward but was waiting on feedback from the questions I've asked a couple times

#11440 (comment)
#11440 (comment)

Specifically on direction - do we need to split these functions or can this PR be simplified as we could return null from the original function?

@jamoflaw

Copy link
Copy Markdown
Author

@alice-sawatzky you should take over as this is 3 years old now and we still can't "remove" value in a subchart... :/

I'm happy to move this forward but was waiting on feedback from the questions I've asked a couple times

#11440 (comment) #11440 (comment)

Specifically on direction - do we need to split these functions or can this PR be simplified as we could return null from the original function?

basically I don't want to unnecessarily make this PR more complex than it needs to be - specifically changing the language used for the function names - its an annoying change to have to make - and I'm seeking advice on if that is wanted

@mattfarina

Copy link
Copy Markdown
Collaborator

I have a few things to add here...

First, we can't change the behavior of a public function. If it removed null values before and that behavior is removed from it, that's a problem because it breaks SemVer. People use the Helm SDK and we need to avoid breaking changes until Helm v4 comes out. It appears that means the "simpler change" won't work.

I'm skipping the second question because I was looking at #9027 and found the difference is in where ProcessDependencies executes. When it runs it alters the parent charts Values property to import the child chart values. I'm looking through that part of the process for a possible bug.

@jamoflaw

Copy link
Copy Markdown
Author

First, we can't change the behavior of a public function. If it removed null values before and that behavior is removed from it, that's a problem because it breaks SemVer. People use the Helm SDK and we need to avoid breaking changes until Helm v4 comes out. It appears that means the "simpler change" won't work.

Yeah that does make sense

Ref ProcessDependencies I tried:

	newvals, _ := copystructure.Copy(vals)
	if err := chartutil.ProcessDependencies(chrt, newvals.(map[string]interface{})); err != nil {
		return nil, err
	}

to ensure it works with a copy and it didn't seem to make any difference.

I did notice though that the chart.Values value gets worked on a lot in those calls via a ptr and that passes through the same CoalesceValues functions multiple times. It seems that some of those calls are intended to mutate that object though?

@mattfarina

Copy link
Copy Markdown
Collaborator

@jamoflaw I'm fairly deep into this one now. I might have a fix. More testing required. I found two problems...

  1. When processing dependencies there is a place where the values are being manipulated. A copy of them fixes that issue.
  2. Coalescing the values removes null values. During the dependency phase this happens. Later, they are coalesced again but without the null values. A merge that does not remove nulls may handle this situation. Creating merge here might be a little more complicated than trimming the nulls. There are times, in the current code and tests, that they are left and the "delete" step is skipped.

I'm testing some ideas out right now to better deal with the 2 issues. Also need to make the changes in a backward compatible manner. I'll have more to share soon.

@mattfarina

Copy link
Copy Markdown
Collaborator

@jamoflaw Can you take a look at #12162? This is my attempt to fix this and some other related problems that needed to be fixed to make this all work.

@jamoflaw

Copy link
Copy Markdown
Author

Thanks @mattfarina - should this PR be closed up with a link to the new one in that case?

@shazada

shazada commented Jul 17, 2023

Copy link
Copy Markdown

When will this be released? This has been an issue since december 2020....

@joejulian

Copy link
Copy Markdown
Contributor

Based on my tests, the issues attached have both been fixed by #12162. Closing.

@joejulian joejulian closed this Aug 1, 2023
@mattfarina

Copy link
Copy Markdown
Collaborator

@shazada a fix for this problem is scheduled for release on September 13th.

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

Labels

bug Categorizes issue or PR as related to a bug. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nested null values do not remove keys from sub-charts Could not delete default keys from subcharts if subchart is defined in dependencies