Skip to content

Commit

Permalink
Merge pull request #189 from stefanprodan/fix-bundle-defaults
Browse files Browse the repository at this point in the history
bundle: Fix values merge with module defaults
  • Loading branch information
stefanprodan authored Sep 10, 2023
2 parents f3f3cb3 + 0da4b21 commit 4b047b0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
1 change: 1 addition & 0 deletions cmd/timoni/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func TestBuild(t *testing.T) {
g.Expect(len(objects)).To(BeEquivalentTo(1))
g.Expect(objects[0].GetName()).To(BeEquivalentTo(name + "-client"))
g.Expect(objects[0].GetAnnotations()).To(HaveKeyWithValue("scope", "external"))
g.Expect(objects[0].GetLabels()).To(HaveKeyWithValue("app.kubernetes.io/team", "test"))

val, _, err := unstructured.NestedString(objects[0].Object, "data", "server")
g.Expect(err).ToNot(HaveOccurred())
Expand Down
3 changes: 3 additions & 0 deletions cmd/timoni/testdata/module/templates/config.cue
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package templates

#Config: {
team!: string

metadata: {
name: *"test" | string
namespace: *"default" | string
labels: *{
"app.kubernetes.io/name": metadata.name
"app.kubernetes.io/version": moduleVersion
"app.kubernetes.io/kube": kubeVersion
"app.kubernetes.io/team": team
} | {[ string]: string}
annotations?: {[ string]: string}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/testdata/module/values.cue
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
package main

values: {
// Placeholder for user-supplied values
team: "test"
}
12 changes: 5 additions & 7 deletions internal/engine/module_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,14 @@ func (b *ModuleBuilder) MergeValuesFile(overlays [][]byte) error {

// WriteValuesFileWithDefaults merges the module's root values.cue with the supplied value.
func (b *ModuleBuilder) WriteValuesFileWithDefaults(val cue.Value) error {
defaultFile := filepath.Join(b.pkgPath, defaultValuesFile)
valData := []byte(fmt.Sprintf("%s: %v", apiv1.ValuesSelector.String(), val))

baseVal, err := ExtractValueFromFile(b.ctx, defaultFile, apiv1.ValuesSelector.String())
if err != nil {
return fmt.Errorf("loading default values from module failed: %w", err)
}
vb := NewValuesBuilder(b.ctx)
defaultFile := filepath.Join(b.pkgPath, defaultValuesFile)

finalVal, err := MergeValue(val, baseVal)
finalVal, err := vb.MergeValues([][]byte{valData}, defaultFile)
if err != nil {
return fmt.Errorf("merging values failed: %w", err)
return err
}

cueGen := fmt.Sprintf("package %s\n%s: %v", b.pkgName, apiv1.ValuesSelector, finalVal)
Expand Down

0 comments on commit 4b047b0

Please sign in to comment.