Skip to content

Commit

Permalink
Merge pull request stefanprodan#117 from stefanprodan/fix-bundle-defa…
Browse files Browse the repository at this point in the history
…ult-values

Merge the bundle values with the module defaults
  • Loading branch information
stefanprodan authored May 29, 2023
2 parents 025d044 + 34e75ba commit 27b3a61
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
8 changes: 4 additions & 4 deletions cmd/timoni/bundle_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strings"
"time"

"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"github.com/fluxcd/pkg/ssa"
"github.com/go-logr/logr"
Expand Down Expand Up @@ -143,7 +144,7 @@ func runBundleApplyCmd(cmd *cobra.Command, _ []string) error {

for _, instance := range bundle.Instances {
log.Info(fmt.Sprintf("applying instance %s", instance.Name))
if err := applyBundleInstance(logr.NewContext(ctx, log), instance); err != nil {
if err := applyBundleInstance(logr.NewContext(ctx, log), cuectx, instance); err != nil {
return err
}
}
Expand All @@ -157,7 +158,7 @@ func runBundleApplyCmd(cmd *cobra.Command, _ []string) error {
return nil
}

func applyBundleInstance(ctx context.Context, instance engine.BundleInstance) error {
func applyBundleInstance(ctx context.Context, cuectx *cue.Context, instance engine.BundleInstance) error {
moduleVersion := instance.Module.Version
sourceURL := fmt.Sprintf("%s:%s", instance.Module.Repository, instance.Module.Version)

Expand Down Expand Up @@ -195,7 +196,6 @@ func applyBundleInstance(ctx context.Context, instance engine.BundleInstance) er
mod.Digest, instance.Module.Version, instance.Module.Digest)
}

cuectx := cuecontext.New()
builder := engine.NewModuleBuilder(
cuectx,
instance.Name,
Expand All @@ -215,7 +215,7 @@ func applyBundleInstance(ctx context.Context, instance engine.BundleInstance) er

log.Info(fmt.Sprintf("using module %s version %s", mod.Name, mod.Version))

err = builder.WriteValuesFile(instance.Values)
err = builder.WriteValuesFileWithDefaults(instance.Values)
if err != nil {
return err
}
Expand Down
23 changes: 23 additions & 0 deletions internal/engine/module_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,29 @@ func (b *ModuleBuilder) WriteValuesFile(val cue.Value) error {
return os.WriteFile(defaultFile, []byte(cueGen), 0644)
}

// WriteValuesFileWithDefaults merges the module's root values.cue with supplied ones.
func (b *ModuleBuilder) WriteValuesFileWithDefaults(val cue.Value) error {
defaultFile := filepath.Join(b.pkgPath, defaultValuesFile)

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

finalVal, err := MergeValue(val, baseVal)
if err != nil {
return fmt.Errorf("merging values failed: %w", err)
}

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

// overwrite the values.cue file with the merged values
if err := os.MkdirAll(b.moduleRoot, os.ModePerm); err != nil {
return err
}
return os.WriteFile(defaultFile, []byte(cueGen), 0644)
}

// WriteSchemaFile generates the module's instance schema.
func (b *ModuleBuilder) WriteSchemaFile() error {
if fs, err := os.Stat(b.pkgPath); err != nil || !fs.IsDir() {
Expand Down

0 comments on commit 27b3a61

Please sign in to comment.