Skip to content

Commit

Permalink
Add Empty and Apply function to HelmValues
Browse files Browse the repository at this point in the history
  • Loading branch information
jgustie committed Oct 3, 2024
1 parent 750c5d8 commit 98029d9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pkg/pipes/helmvalues.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/thestormforge/konjure/pkg/pipes/internal/strvals"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/yaml"
"sigs.k8s.io/kustomize/kyaml/yaml/merge2"
"sigs.k8s.io/kustomize/kyaml/yaml/walk"
)

// HelmValues is a reader that emits resource nodes representing Helm values.
Expand All @@ -40,6 +42,11 @@ type HelmValues struct {
FS fs.FS
}

// Empty returns true if there are no values configured on the this Helm values instance.
func (r *HelmValues) Empty() bool {
return len(r.ValueFiles)+len(r.Values)+len(r.StringValues)+len(r.FileValues) == 0
}

// AsMap converts the configured user specified values into a map of values.
func (r *HelmValues) AsMap() (map[string]any, error) {
base := map[string]any{}
Expand Down Expand Up @@ -112,6 +119,26 @@ func (r *HelmValues) readFile(spec string) (string, error) {
return string(data), err
}

// Apply merges these Helm values into the filtered nodes without a schema.
func (r *HelmValues) Apply() yaml.Filter {
return yaml.FilterFunc(func(node *yaml.RNode) (*yaml.RNode, error) {
if r.Empty() {
return node, nil
}

w := walk.Walker{
Visitor: merge2.Merger{},
Sources: []*yaml.RNode{node},
}
if nodes, err := r.Read(); err != nil {
return nil, err
} else {
w.Sources = append(w.Sources, nodes...)
}
return w.Walk()
})
}

// MergeMaps is used to combine results from multiple values.
func (r *HelmValues) MergeMaps(a, b map[string]any) map[string]any {
out := make(map[string]any, len(a))
Expand Down
19 changes: 19 additions & 0 deletions pkg/pipes/helmvalues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ c: d`),
}
}

func TestHelmValues_Apply(t *testing.T) {
nodes, err := (&filters.Pipeline{
Inputs: []kio.Reader{
&HelmValues{Values: []string{"a=b"}},
},
Filters: []kio.Filter{
kio.FilterAll((&HelmValues{Values: []string{
"c.d=e",
"a=z",
}}).Apply()),
},
}).Read()
assert.NoError(t, err, "failed to apply")

actual, err := nodes[0].MarshalJSON()
assert.NoError(t, err, "failed to produce JSON")
assert.JSONEq(t, `{"a":"z","c":{"d":"e"}}`, string(actual))
}

func TestHelmValues_Flatten(t *testing.T) {
flattened, err := (&filters.Pipeline{
Inputs: []kio.Reader{
Expand Down

0 comments on commit 98029d9

Please # to comment.