Skip to content

Commit

Permalink
Fix deep merge of ValuesYamlFiles (#2963)
Browse files Browse the repository at this point in the history
Stop relying on `yaml.Unmarshal` to merge `ValuesYaml` as it cannot do a
deep merge. Instead we Unmarshal the values in a temporary map and use
the `mergeMap` function that already exists to merge.

With this the behavior of the provider is aligned with the deep merge
made by Helm helm/helm#1620

Fixes #2958 .

---------

Co-authored-by: Bryce Lampe <bryce@pulumi.com>
  • Loading branch information
KevinFairise2 and blampe committed Apr 24, 2024
1 parent b753ee1 commit 67947e4
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 1 deletion.
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)
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{
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)
}
}

0 comments on commit 67947e4

Please sign in to comment.