-
Notifications
You must be signed in to change notification settings - Fork 115
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 upstream's MergeValues with assets #3010
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// Copyright 2016-2022, Pulumi Corporation. | ||
// Copyright 2024, Pulumi Corporation. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
|
@@ -12,55 +12,75 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package helm | ||
package v4 | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/pulumi/pulumi/sdk/v3/go/pulumi" | ||
"gopkg.in/yaml.v3" | ||
"helm.sh/helm/v3/pkg/cli/values" | ||
"helm.sh/helm/v3/pkg/getter" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
// ValueOpts handles merging of chart values from various sources. | ||
type ValueOpts struct { | ||
// ValuesFiles is a list of Helm values files encapsulated as Pulumi assets. | ||
ValuesFiles []pulumi.Asset | ||
// Values is a map of Pulumi values. | ||
Values map[string]any | ||
} | ||
|
||
// MergeValues merges the values in Helm's priority order. | ||
func (opts *ValueOpts) MergeValues(p getter.Providers) (map[string]interface{}, error) { | ||
base := map[string]interface{}{} | ||
// readValues hydrates Assets and persists values on-disk in order to provide | ||
// them to upstream's MergeValues logic. | ||
// | ||
// The returned function cleans up the on-disk values and should always be | ||
// called, even on error. | ||
func readValues(p getter.Providers, v map[string]any, files []pulumi.Asset) (values.Options, func(), error) { | ||
opts := values.Options{} | ||
tmp, err := os.MkdirTemp(os.TempDir(), "pulumi-kubernetes") | ||
if err != nil { | ||
return opts, func() {}, err | ||
} | ||
cleanup := func() { | ||
_ = os.RemoveAll(tmp) | ||
} | ||
|
||
// User specified a values files via -f/--values | ||
for _, asset := range opts.ValuesFiles { | ||
currentMap := map[string]interface{}{} | ||
valuesFiles := make([]string, 0, len(files)+1) | ||
|
||
bytes, err := readAsset(p, asset) | ||
persist := func(out []byte) error { | ||
fname := filepath.Join(tmp, fmt.Sprintf("values-%d.yaml", len(valuesFiles))) | ||
err := os.WriteFile(fname, out, 0o600) | ||
if err != nil { | ||
return nil, err | ||
return err | ||
} | ||
valuesFiles = append(valuesFiles, fname) | ||
return nil | ||
} | ||
|
||
if err := yaml.Unmarshal(bytes, ¤tMap); err != nil { | ||
return nil, err | ||
for _, f := range files { | ||
out, err := readAsset(p, f) | ||
if err != nil { | ||
return opts, cleanup, err | ||
} | ||
err = persist(out) | ||
if err != nil { | ||
return opts, cleanup, err | ||
} | ||
// Merge with the previous map | ||
base = MergeMaps(base, currentMap) | ||
} | ||
|
||
// User specified a literal value map (possibly containing assets) | ||
values, err := marshalValues(p, opts.Values) | ||
values, err := readAssets(p, v) | ||
if err != nil { | ||
return opts, cleanup, err | ||
} | ||
out, err := yaml.Marshal(values) | ||
if err != nil { | ||
return opts, cleanup, err | ||
} | ||
err = persist(out) | ||
if err != nil { | ||
return nil, err | ||
return opts, cleanup, err | ||
} | ||
base = MergeMaps(base, values) | ||
|
||
return base, nil | ||
opts.ValueFiles = valuesFiles | ||
|
||
return opts, cleanup, nil | ||
} | ||
|
||
// readAsset reads the content of a Pulumi asset. | ||
|
@@ -93,14 +113,14 @@ func readAsset(p getter.Providers, asset pulumi.Asset) ([]byte, error) { | |
} | ||
} | ||
|
||
// marshalValues converts Pulumi values to Helm values. | ||
// - Expands assets to their content (to support --set-file). | ||
func marshalValues(p getter.Providers, a map[string]interface{}) (map[string]interface{}, error) { | ||
// readAssets converts Pulumi values to Helm values, hydrating Asset values | ||
// along the way. | ||
func readAssets(p getter.Providers, a map[string]interface{}) (map[string]interface{}, error) { | ||
var err error | ||
Comment on lines
+116
to
+118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like |
||
out := make(map[string]interface{}, len(a)) | ||
for k, v := range a { | ||
if v, ok := v.(map[string]interface{}); ok { | ||
out[k], err = marshalValues(p, v) | ||
out[k], err = readAssets(p, v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,16 +12,18 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package helm | ||
package v4 | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/pulumi/pulumi/sdk/v3/go/pulumi" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"helm.sh/helm/v3/pkg/cli" | ||
"helm.sh/helm/v3/pkg/getter" | ||
) | ||
|
||
|
@@ -37,7 +39,6 @@ func (m *mockGetter) Get(url string, options ...getter.Option) (*bytes.Buffer, e | |
} | ||
|
||
func TestMergeValues(t *testing.T) { | ||
|
||
bitnamiImage := ` | ||
image: | ||
repository: bitnami/nginx | ||
|
@@ -121,7 +122,7 @@ image: | |
}, | ||
want: map[string]interface{}{ | ||
"configuration": map[string]any{ | ||
"backupStorageLocation": []map[string]any{}, | ||
"backupStorageLocation": []any{}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A subtle (probably inconsequential) difference between the previous |
||
}, | ||
}, | ||
}, | ||
|
@@ -143,30 +144,29 @@ image: | |
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
merger := &ValueOpts{ | ||
ValuesFiles: tt.valuesFiles, | ||
Values: tt.values, | ||
} | ||
p := getter.All(cli.New()) | ||
opts, cleanup, err := readValues(p, tt.values, tt.valuesFiles) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use |
||
defer cleanup() | ||
require.NoError(t, err) | ||
|
||
actual, err := merger.MergeValues(getter.Providers{}) | ||
actual, err := opts.MergeValues(p) | ||
require.NoError(t, err) | ||
assert.Equal(t, tt.want, actual) | ||
}) | ||
} | ||
} | ||
|
||
func TestReadAsset(t *testing.T) { | ||
|
||
bitnamiImage := ` | ||
image: | ||
repository: bitnami/nginx | ||
tag: latest | ||
` | ||
bitnamiImageFile, err := os.CreateTemp("", "pulumi-TestReadAsset-*.yaml") | ||
|
||
tmp := t.TempDir() | ||
fname := filepath.Join(tmp, "bitnami.yaml") | ||
err := os.WriteFile(fname, []byte(bitnamiImage), 0o600) | ||
require.NoError(t, err) | ||
_, _ = bitnamiImageFile.WriteString(bitnamiImage) | ||
_ = bitnamiImageFile.Close() | ||
defer os.Remove(bitnamiImageFile.Name()) | ||
|
||
tests := []struct { | ||
name string | ||
|
@@ -182,7 +182,7 @@ image: | |
}, | ||
{ | ||
name: "file asset", | ||
asset: pulumi.NewFileAsset(bitnamiImageFile.Name()), | ||
asset: pulumi.NewFileAsset(fname), | ||
want: []byte(bitnamiImage), | ||
}, | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would still keep
addValueOptionsFlags
to set defaults onValues
and to further resemble:https://github.com/helm/helm/blob/14d0c13e9eefff5b4a1b511cf50643529692ec94/cmd/helm/flags.go#L45-L51