Skip to content

Commit

Permalink
Adding opt-in kustomized options consistent with the ones used by flu…
Browse files Browse the repository at this point in the history
…x v2 (#453)

Co-authored-by: Jeff McCoy <[email protected]>
  • Loading branch information
mahomedalid and jeff-mccoy authored Apr 27, 2022
1 parent f89c429 commit 7c52e71
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
23 changes: 17 additions & 6 deletions src/internal/kustomize/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,35 @@ 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)
if err != nil {
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)
}
2 changes: 1 addition & 1 deletion src/internal/packager/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/internal/packager/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions src/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions zarf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@
"name": {
"type": "string"
},
"kustomizeAllowAnyDirectory": {
"type": "boolean"
},
"namespace": {
"type": "string"
},
Expand Down

0 comments on commit 7c52e71

Please sign in to comment.