From 34e75bae119bd9b095f668376a243456fe4e8584 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Mon, 29 May 2023 18:30:36 +0300 Subject: [PATCH] Merge the bundle values with the module defaults Signed-off-by: Stefan Prodan --- cmd/timoni/bundle_apply.go | 8 ++++---- internal/engine/module_builder.go | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/cmd/timoni/bundle_apply.go b/cmd/timoni/bundle_apply.go index 23785d87..d980fe8d 100644 --- a/cmd/timoni/bundle_apply.go +++ b/cmd/timoni/bundle_apply.go @@ -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" @@ -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 } } @@ -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) @@ -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, @@ -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 } diff --git a/internal/engine/module_builder.go b/internal/engine/module_builder.go index 80bfb576..a5ee3c7d 100644 --- a/internal/engine/module_builder.go +++ b/internal/engine/module_builder.go @@ -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() {