From 7c52e7163a0fa76cc4d9a3bdbdf2661e7f398b3e Mon Sep 17 00:00:00 2001 From: mahomedalid Date: Tue, 26 Apr 2022 17:55:47 -0700 Subject: [PATCH] Adding opt-in kustomized options consistent with the ones used by flux v2 (#453) Co-authored-by: Jeff McCoy --- src/internal/kustomize/build.go | 23 +++++++++++++++++------ src/internal/packager/create.go | 2 +- src/internal/packager/prepare.go | 2 +- src/types/types.go | 9 +++++---- zarf.schema.json | 3 +++ 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/internal/kustomize/build.go b/src/internal/kustomize/build.go index ab37ac4633..9a38031af8 100644 --- a/src/internal/kustomize/build.go +++ b/src/internal/kustomize/build.go @@ -5,14 +5,23 @@ import ( "github.com/defenseunicorns/zarf/src/internal/utils" "sigs.k8s.io/kustomize/api/krusty" + kustypes "sigs.k8s.io/kustomize/api/types" "sigs.k8s.io/kustomize/kyaml/filesys" ) -// BuildKustomization reads a kustomization and builds it into a single yaml file -func BuildKustomization(path string, destination string) error { +// BuildKustomization reads a kustomization and builds it into a single yaml file. +func BuildKustomization(path string, destination string, kustomizeAllowAnyDirectory bool) error { // Kustomize has to write to the filesystem on-disk fSys := filesys.MakeFsOnDisk() - kustomizer := krusty.MakeKustomizer(krusty.MakeDefaultOptions()) + + // flux2 build options for consistency, load restrictions none applies only to local files + buildOptions := krusty.MakeDefaultOptions() + + if kustomizeAllowAnyDirectory { + buildOptions.LoadRestrictions = kustypes.LoadRestrictionsNone + } + + kustomizer := krusty.MakeKustomizer(buildOptions) // Try to build the kustomization resources, err := kustomizer.Run(fSys, path) @@ -20,9 +29,11 @@ func BuildKustomization(path string, destination string) error { return err } - if yaml, err := resources.AsYaml(); err != nil { + yaml, err := resources.AsYaml() + + if err != nil { return fmt.Errorf("problem converting kustomization to yaml: %w", err) - } else { - return utils.WriteFile(destination, yaml) } + + return utils.WriteFile(destination, yaml) } diff --git a/src/internal/packager/create.go b/src/internal/packager/create.go index c788caa7ee..fa6228b2ac 100644 --- a/src/internal/packager/create.go +++ b/src/internal/packager/create.go @@ -157,7 +157,7 @@ func addComponent(tempPath tempPaths, component types.ZarfComponent) { // Generate manifests from kustomizations and place in the package spinner.Updatef("Building kustomization for %s", kustomization) destination := fmt.Sprintf("%s/kustomization-%s-%d.yaml", componentPath.manifests, manifest.Name, idx) - if err := kustomize.BuildKustomization(kustomization, destination); err != nil { + if err := kustomize.BuildKustomization(kustomization, destination, manifest.KustomizeAllowAnyDirectory); err != nil { spinner.Fatalf(err, "unable to build the kustomization for %s", kustomization) } } diff --git a/src/internal/packager/prepare.go b/src/internal/packager/prepare.go index a94fe4c789..e5876485f9 100644 --- a/src/internal/packager/prepare.go +++ b/src/internal/packager/prepare.go @@ -128,7 +128,7 @@ func FindImages(repoHelmChartPath string) { for idx, kustomization := range manifest.Kustomizations { // Generate manifests from kustomizations and place in the package destination := fmt.Sprintf("%s/kustomization-%s-%d.yaml", componentPath.manifests, manifest.Name, idx) - if err := kustomize.BuildKustomization(kustomization, destination); err != nil { + if err := kustomize.BuildKustomization(kustomization, destination, manifest.KustomizeAllowAnyDirectory); err != nil { message.Errorf(err, "unable to build the kustomization for %s", kustomization) } else { manifest.Files = append(manifest.Files, destination) diff --git a/src/types/types.go b/src/types/types.go index 23c9dc5365..e72a6772cb 100644 --- a/src/types/types.go +++ b/src/types/types.go @@ -66,10 +66,11 @@ type ZarfComponent struct { // ZarfManifest defines raw manifests Zarf will deploy as a helm chart type ZarfManifest struct { - Name string `yaml:"name"` - DefaultNamespace string `yaml:"namespace,omitempty"` - Files []string `yaml:"files,omitempty"` - Kustomizations []string `yaml:"kustomizations,omitempty"` + Name string `yaml:"name"` + DefaultNamespace string `yaml:"namespace,omitempty"` + Files []string `yaml:"files,omitempty"` + KustomizeAllowAnyDirectory bool `yaml:"kustomizeAllowAnyDirectory,omitempty"` + Kustomizations []string `yaml:"kustomizations,omitempty"` } // ZarfComponentScripts are scripts that run before or after a component is deployed diff --git a/zarf.schema.json b/zarf.schema.json index a9183870a4..1b7817dd1e 100644 --- a/zarf.schema.json +++ b/zarf.schema.json @@ -256,6 +256,9 @@ "name": { "type": "string" }, + "kustomizeAllowAnyDirectory": { + "type": "boolean" + }, "namespace": { "type": "string" },