diff --git a/cmd/flux/build_kustomization.go b/cmd/flux/build_kustomization.go index dbbf7abc..96fb5b9b 100644 --- a/cmd/flux/build_kustomization.go +++ b/cmd/flux/build_kustomization.go @@ -63,6 +63,7 @@ type buildKsFlags struct { path string ignorePaths []string dryRun bool + strictSubst bool } var buildKsArgs buildKsFlags @@ -72,6 +73,8 @@ func init() { buildKsCmd.Flags().StringVar(&buildKsArgs.kustomizationFile, "kustomization-file", "", "Path to the Flux Kustomization YAML file.") buildKsCmd.Flags().StringSliceVar(&buildKsArgs.ignorePaths, "ignore-paths", nil, "set paths to ignore in .gitignore format") buildKsCmd.Flags().BoolVar(&buildKsArgs.dryRun, "dry-run", false, "Dry run mode.") + buildKsCmd.Flags().BoolVar(&buildKsArgs.strictSubst, "strict-substitute", false, + "When enabled, the post build substitutions will fail if a var without a default value is declared in files but is missing from the input vars.") buildCmd.AddCommand(buildKsCmd) } @@ -107,6 +110,7 @@ func buildKsCmdRun(cmd *cobra.Command, args []string) (err error) { build.WithDryRun(buildKsArgs.dryRun), build.WithNamespace(*kubeconfigArgs.Namespace), build.WithIgnore(buildKsArgs.ignorePaths), + build.WithStrictSubstitute(buildKsArgs.strictSubst), ) } else { builder, err = build.NewBuilder(name, buildKsArgs.path, @@ -114,6 +118,7 @@ func buildKsCmdRun(cmd *cobra.Command, args []string) (err error) { build.WithTimeout(rootArgs.timeout), build.WithKustomizationFile(buildKsArgs.kustomizationFile), build.WithIgnore(buildKsArgs.ignorePaths), + build.WithStrictSubstitute(buildKsArgs.strictSubst), ) } diff --git a/cmd/flux/diff_kustomization.go b/cmd/flux/diff_kustomization.go index 0c4968eb..0b85a2b1 100644 --- a/cmd/flux/diff_kustomization.go +++ b/cmd/flux/diff_kustomization.go @@ -23,8 +23,9 @@ import ( "github.com/spf13/cobra" - "github.com/fluxcd/flux2/v2/internal/build" kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1" + + "github.com/fluxcd/flux2/v2/internal/build" ) var diffKsCmd = &cobra.Command{ @@ -53,6 +54,7 @@ type diffKsFlags struct { path string ignorePaths []string progressBar bool + strictSubst bool } var diffKsArgs diffKsFlags @@ -62,6 +64,8 @@ func init() { diffKsCmd.Flags().BoolVar(&diffKsArgs.progressBar, "progress-bar", true, "Boolean to set the progress bar. The default value is true.") diffKsCmd.Flags().StringSliceVar(&diffKsArgs.ignorePaths, "ignore-paths", nil, "set paths to ignore in .gitignore format") diffKsCmd.Flags().StringVar(&diffKsArgs.kustomizationFile, "kustomization-file", "", "Path to the Flux Kustomization YAML file.") + diffKsCmd.Flags().BoolVar(&diffKsArgs.strictSubst, "strict-substitute", false, + "When enabled, the post build substitutions will fail if a var without a default value is declared in files but is missing from the input vars.") diffCmd.AddCommand(diffKsCmd) } @@ -96,6 +100,7 @@ func diffKsCmdRun(cmd *cobra.Command, args []string) error { build.WithKustomizationFile(diffKsArgs.kustomizationFile), build.WithProgressBar(), build.WithIgnore(diffKsArgs.ignorePaths), + build.WithStrictSubstitute(diffKsArgs.strictSubst), ) } else { builder, err = build.NewBuilder(name, diffKsArgs.path, @@ -103,6 +108,7 @@ func diffKsCmdRun(cmd *cobra.Command, args []string) error { build.WithTimeout(rootArgs.timeout), build.WithKustomizationFile(diffKsArgs.kustomizationFile), build.WithIgnore(diffKsArgs.ignorePaths), + build.WithStrictSubstitute(diffKsArgs.strictSubst), ) } diff --git a/go.mod b/go.mod index b070de4b..b23f0a52 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/fluxcd/pkg/apis/meta v1.4.0 github.com/fluxcd/pkg/git v0.18.0 github.com/fluxcd/pkg/git/gogit v0.18.0 - github.com/fluxcd/pkg/kustomize v1.8.0 + github.com/fluxcd/pkg/kustomize v1.9.0 github.com/fluxcd/pkg/oci v0.36.0 github.com/fluxcd/pkg/runtime v0.46.0 github.com/fluxcd/pkg/sourceignore v0.6.0 @@ -108,7 +108,6 @@ require ( github.com/docker/docker-credential-helpers v0.7.0 // indirect github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect github.com/docker/go-metrics v0.0.1 // indirect - github.com/drone/envsubst v1.0.3 // indirect github.com/emicklei/go-restful/v3 v3.11.0 // indirect github.com/emirpasic/gods v1.18.1 // indirect github.com/evanphx/json-patch v5.7.0+incompatible // indirect @@ -118,6 +117,7 @@ require ( github.com/felixge/httpsnoop v1.0.4 // indirect github.com/fluxcd/pkg/apis/acl v0.2.0 // indirect github.com/fluxcd/pkg/apis/kustomize v1.4.0 // indirect + github.com/fluxcd/pkg/envsubst v1.0.0 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/go-errors/errors v1.5.1 // indirect github.com/go-fed/httpsig v1.1.0 // indirect diff --git a/go.sum b/go.sum index 26674dec..b9fab90b 100644 --- a/go.sum +++ b/go.sum @@ -130,8 +130,6 @@ github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c h1:+pKlWGMw7gf6bQ github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c/go.mod h1:Uw6UezgYA44ePAFQYUehOuCzmy5zmg/+nl2ZfMWGkpA= github.com/docker/go-metrics v0.0.1 h1:AgB/0SvBxihN0X8OR4SjsblXkbMvalQ8cjmtKQ2rQV8= github.com/docker/go-metrics v0.0.1/go.mod h1:cG1hvH2utMXtqgqqYE9plW6lDxS3/5ayHzueweSI3Vw= -github.com/drone/envsubst v1.0.3 h1:PCIBwNDYjs50AsLZPYdfhSATKaRg/FJmDc2D6+C2x8g= -github.com/drone/envsubst v1.0.3/go.mod h1:N2jZmlMufstn1KEqvbHjw40h1KyTmnVzHcSc9bFiJ2g= github.com/elazarl/goproxy v0.0.0-20231117061959-7cc037d33fb5 h1:m62nsMU279qRD9PQSWD1l66kmkXzuYcnVJqL4XLeV2M= github.com/elazarl/goproxy v0.0.0-20231117061959-7cc037d33fb5/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g= @@ -173,14 +171,16 @@ github.com/fluxcd/pkg/apis/kustomize v1.4.0 h1:SXoGN9M31fW5tO+wpKMnyHXbjxGUqDo7Y github.com/fluxcd/pkg/apis/kustomize v1.4.0/go.mod h1:bZklVWB11tELMss89qYzgg4ClzhFzp0Hm4/8EiHgKew= github.com/fluxcd/pkg/apis/meta v1.4.0 h1:nNdgB6FFHP3cubxZCViaCFDUVlAbpq9+hvKEIveOGMg= github.com/fluxcd/pkg/apis/meta v1.4.0/go.mod h1:81sZ01ShTuLc1C3M1dFJNkINareBysvmrO1b8zJFFKs= +github.com/fluxcd/pkg/envsubst v1.0.0 h1:LD86BRNSCGJrvyrH2aX5/pit7RfbFpkzRXogwcazLVk= +github.com/fluxcd/pkg/envsubst v1.0.0/go.mod h1:VAcb4OxcRdsDix1TRtr/mtTqFGHmNQaOvXQO2REArFQ= github.com/fluxcd/pkg/git v0.18.0 h1:t6o/eaLeoXtcWX1GpLMjkY/b91nx+6VKcrW+MxWJ4JI= github.com/fluxcd/pkg/git v0.18.0/go.mod h1:BqcoBcuGH+AFNjdYLgNNwTUdvXwNRL+tukD85kiL9dk= github.com/fluxcd/pkg/git/gogit v0.18.0 h1:tvYNKhwzRtpAkWvanyfoOOdtghuJe0W57EIQQHHZ8+g= github.com/fluxcd/pkg/git/gogit v0.18.0/go.mod h1:I+wp/Aw6+Rnw/zI3JmctirHI63mXLapyyHJ8QEV35fU= github.com/fluxcd/pkg/gittestserver v0.11.0 h1:CSGgzR1PfSucPn2X+hkR4qv3j0uvf7tcdFY1Av5FBXI= github.com/fluxcd/pkg/gittestserver v0.11.0/go.mod h1:ELar3OZhsKML0pX+v+xxtGdejaUQJUbAWBf5F3tZ7eA= -github.com/fluxcd/pkg/kustomize v1.8.0 h1:Vf1UwnoP3yScaLi/QrDjgN2d2nI6LcmX4tNRoH+sypY= -github.com/fluxcd/pkg/kustomize v1.8.0/go.mod h1:yszv9tkYrnC01mcGPct8+bdxpTyxf69k1kmSvk7w0zs= +github.com/fluxcd/pkg/kustomize v1.9.0 h1:bqS3mXiK1q5TpUtIO5I5b+v/0r96NGJBiearKGUhicA= +github.com/fluxcd/pkg/kustomize v1.9.0/go.mod h1:PBerk0KzZN/IXaGociVp4MSMvsUQB0jR1P2SqSdixz0= github.com/fluxcd/pkg/oci v0.36.0 h1:bC+Qkm6p8rN72McArNwYFjI9bt7M2vn6mnt7i/vOWv0= github.com/fluxcd/pkg/oci v0.36.0/go.mod h1:ocWsg1tjhjlIpdpdw0xZz7q9YQH8YzcrN1wi+Az0t7E= github.com/fluxcd/pkg/runtime v0.46.0 h1:+pxFwTk8j8lZIS9Vyc8EJbgvmFp9JqeT6pfLo/0iP98= @@ -270,7 +270,6 @@ github.com/google/btree v1.1.2 h1:xf4v41cLI2Z6FxbKm+8Bu+m8ifhj15JuZ9sa0jZCMUU= github.com/google/btree v1.1.2/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4= github.com/google/gnostic-models v0.6.8 h1:yo/ABAfM5IMRsS1VnXjTBvUb61tFIHozhlYvRgGre9I= github.com/google/gnostic-models v0.6.8/go.mod h1:5n7qKqH0f5wFt+aWF8CW6pZLLNOfYuF5OpfBSENuI8U= -github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= diff --git a/internal/build/build.go b/internal/build/build.go index 30784b6a..d8a8c381 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -80,6 +80,7 @@ type Builder struct { timeout time.Duration spinner *yacspin.Spinner dryRun bool + strictSubst bool } // BuilderOptionFunc is a function that configures a Builder @@ -158,6 +159,14 @@ func WithDryRun(dryRun bool) BuilderOptionFunc { } } +// WithStrictSubstitute sets the strict substitute flag +func WithStrictSubstitute(strictSubstitute bool) BuilderOptionFunc { + return func(b *Builder) error { + b.strictSubst = strictSubstitute + return nil + } +} + // WithIgnore sets ignore field func WithIgnore(ignore []string) BuilderOptionFunc { return func(b *Builder) error { @@ -391,7 +400,13 @@ func (b *Builder) do(ctx context.Context, kustomization kustomizev1.Kustomizatio if err != nil { return nil, err } - outRes, err := kustomize.SubstituteVariables(ctx, b.client, unstructured.Unstructured{Object: data}, res, b.dryRun) + outRes, err := kustomize.SubstituteVariables(ctx, + b.client, + unstructured.Unstructured{Object: data}, + res, + kustomize.SubstituteWithDryRun(b.dryRun), + kustomize.SubstituteWithStrict(b.strictSubst), + ) if err != nil { return nil, fmt.Errorf("var substitution failed for '%s': %w", res.GetName(), err) }