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

Fix deep merge of ValuesYamlFiles #2963

Merged
merged 7 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

- Fix Release behavior to deep merge `valueYamlFiles` to match Helm. (https://github.com/pulumi/pulumi-kubernetes/pull/2963)

## 4.11.0 (April 17, 2024)

- [dotnet] Unknowns for previews involving an uninitialized provider (https://github.com/pulumi/pulumi-kubernetes/pull/2957)
Expand Down
38 changes: 38 additions & 0 deletions provider/pkg/helm/upstream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright The Helm Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// package helm contains code vendored from the upstream Helm project.
package helm

// https://github.com/helm/helm/blob/14d0c13e9eefff5b4a1b511cf50643529692ec94/pkg/cli/values/options.go#L108-L125
func MergeMaps(a, b map[string]interface{}) map[string]interface{} {
out := make(map[string]interface{}, len(a))
for k, v := range a {
out[k] = v
}
for k, v := range b {
if v, ok := v.(map[string]interface{}); ok {
if bv, ok := out[k]; ok {
if bv, ok := bv.(map[string]interface{}); ok {
out[k] = MergeMaps(bv, v)
continue
}
}
}
out[k] = v
}
return out
}
5 changes: 4 additions & 1 deletion provider/pkg/provider/helm_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/mitchellh/mapstructure"
pkgerrors "github.com/pkg/errors"
"github.com/pulumi/pulumi-kubernetes/provider/v4/pkg/clients"
"github.com/pulumi/pulumi-kubernetes/provider/v4/pkg/helm"
"github.com/pulumi/pulumi-kubernetes/provider/v4/pkg/host"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
Expand Down Expand Up @@ -299,9 +300,11 @@ func decodeRelease(pm resource.PropertyMap, label string) (*Release, error) {
if err != nil {
return nil, err
}
if err = yaml.Unmarshal(b, &values); err != nil {
valuesMap := map[string]any{}
if err = yaml.Unmarshal(b, &valuesMap); err != nil {
return nil, err
}
values = helm.MergeMaps(values, valuesMap)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we also merge the literal values using helm.MergeMaps rather than our own mergeMaps (see line 319)? Perhaps use helm.MergeMaps when AllowNullValues is true (since the flag was intended to improve compatibility). Are there any differences in the logic in that case? Or we could leave it for the v4 implementation of Release.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a good question. I added some test cases and I think this basically boils down to whether we also want to fix #2731 as part of this PR, because I think that's essentially what your suggestion would do.

I'm OK to leave line 319 as-is just to keep scope and risk down. I can follow up with #2731.

default:
return nil, fmt.Errorf("unsupported type for 'valueYamlFiles' arg: %T", v)
}
Expand Down
114 changes: 114 additions & 0 deletions provider/pkg/provider/helm_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package provider
import (
"testing"

"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/asset"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -138,3 +140,115 @@ func Test_MergeMaps(t *testing.T) {
})
}
}

func TestDecodeRelease(t *testing.T) {
bitnamiImage := `
image:
repository: bitnami/nginx
tag: latest
`

tests := []struct {
name string
given resource.PropertyMap
want *Release
}{
{
name: "valueYamlFiles layering",
given: resource.PropertyMap{
"valueYamlFiles": resource.NewArrayProperty([]resource.PropertyValue{
blampe marked this conversation as resolved.
Show resolved Hide resolved
resource.NewAssetProperty(&asset.Asset{Text: `
image:
repository: bitnami/nginx
`}),
resource.NewAssetProperty(&asset.Asset{Text: `
image:
tag: "1.25.0"
`}),
}),
},
want: &Release{
Values: map[string]any{
"image": map[string]any{
"tag": "1.25.0",
"repository": "bitnami/nginx",
},
},
},
},
{
name: "valueYamlFiles with literals",
given: resource.PropertyMap{
"values": resource.NewObjectProperty(resource.PropertyMap{
"image": resource.NewObjectProperty(resource.PropertyMap{
"tag": resource.NewStringProperty("patched"),
}),
},
),
"valueYamlFiles": resource.NewArrayProperty([]resource.PropertyValue{
resource.NewAssetProperty(&asset.Asset{Text: bitnamiImage}),
}),
},
want: &Release{
Values: map[string]any{
"image": map[string]any{
"repository": "bitnami/nginx",
"tag": "patched",
},
},
},
},
{
name: "valueYamlFiles with literals and allowNullValues=true",
given: resource.PropertyMap{
"allowNullValues": resource.NewBoolProperty(true),
"values": resource.NewObjectProperty(resource.PropertyMap{
"image": resource.NewObjectProperty(resource.PropertyMap{
"tag": resource.NewNullProperty(),
}),
},
),
"valueYamlFiles": resource.NewArrayProperty([]resource.PropertyValue{
resource.NewAssetProperty(&asset.Asset{Text: bitnamiImage}),
}),
},
want: &Release{
AllowNullValues: true,
Values: map[string]any{
"image": map[string]any{
"repository": "bitnami/nginx",
"tag": nil,
},
},
},
},
{
name: "valueYamlFiles with literals and allowNullValues=false",
given: resource.PropertyMap{
"values": resource.NewObjectProperty(resource.PropertyMap{
"image": resource.NewObjectProperty(resource.PropertyMap{
"tag": resource.NewNullProperty(),
}),
},
),
"valueYamlFiles": resource.NewArrayProperty([]resource.PropertyValue{
resource.NewAssetProperty(&asset.Asset{Text: bitnamiImage}),
}),
},
want: &Release{
Values: map[string]any{
"image": map[string]any{
"repository": "bitnami/nginx",
"tag": "latest", // Not removed.
},
},
},
},
}

for _, tt := range tests {
actual, err := decodeRelease(tt.given, "")
assert.NoError(t, err)
assert.Equal(t, tt.want, actual)
}
}