From 59c24e7d38e37248348cff2cf5a6261926e5d3c3 Mon Sep 17 00:00:00 2001 From: Hidde Beydals Date: Fri, 15 Jan 2021 14:37:04 +0100 Subject: [PATCH] Look for all accepted Kustomization filenames Before this commit we only checked if a `kustomization.yaml` existed at the root of the given directory, this caused problems when people for example used `.yml` as the extension, as the generated `kustomization.yaml` would conflict with the `.yml` file. After this commit all recognized Kustomization filenames as listed by Kustomize itself are accepted, including files _without_ an extension (`Kustomization`). Signed-off-by: Hidde Beydals --- controllers/kustomization_generator.go | 75 +++++++++++++------------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/controllers/kustomization_generator.go b/controllers/kustomization_generator.go index 18e0edd0..66929c16 100644 --- a/controllers/kustomization_generator.go +++ b/controllers/kustomization_generator.go @@ -35,8 +35,7 @@ import ( ) const ( - kustomizationFileName = "kustomization.yaml" - transformerFileName = "kustomization-gc-labels.yaml" + transformerFileName = "kustomization-gc-labels.yaml" ) type KustomizeGenerator struct { @@ -50,7 +49,7 @@ func NewGenerator(kustomization kustomizev1.Kustomization) *KustomizeGenerator { } func (kg *KustomizeGenerator) WriteFile(dirPath string) (string, error) { - kfile := filepath.Join(dirPath, kustomizationFileName) + kfile := filepath.Join(dirPath, konfig.DefaultKustomizationFileName()) checksum, err := kg.checksum(dirPath) if err != nil { @@ -129,7 +128,14 @@ func checkKustomizeImageExists(images []kustypes.Image, imageName string) (bool, func (kg *KustomizeGenerator) generateKustomization(dirPath string) error { fs := filesys.MakeFsOnDisk() - kfile := filepath.Join(dirPath, kustomizationFileName) + + // Determine if there already is a Kustomization file at the root, + // as this means we do not have to generate one. + for _, kfilename := range konfig.RecognizedKustomizationFileNames() { + if kpath := filepath.Join(dirPath, kfilename); fs.Exists(kpath) && !fs.IsDir(kpath) { + return nil + } + } scan := func(base string) ([]string, error) { var paths []string @@ -172,45 +178,42 @@ func (kg *KustomizeGenerator) generateKustomization(dirPath string) error { return paths, err } - if _, err := os.Stat(kfile); err != nil { - abs, err := filepath.Abs(dirPath) - if err != nil { - return err - } - - files, err := scan(abs) - if err != nil { - return err - } + abs, err := filepath.Abs(dirPath) + if err != nil { + return err + } - f, err := fs.Create(kfile) - if err != nil { - return err - } - f.Close() + files, err := scan(abs) + if err != nil { + return err + } - kus := kustypes.Kustomization{ - TypeMeta: kustypes.TypeMeta{ - APIVersion: kustypes.KustomizationVersion, - Kind: kustypes.KustomizationKind, - }, - } + kfile := filepath.Join(dirPath, konfig.DefaultKustomizationFileName()) + f, err := fs.Create(kfile) + if err != nil { + return err + } + f.Close() - var resources []string - for _, file := range files { - resources = append(resources, strings.Replace(file, abs, ".", 1)) - } + kus := kustypes.Kustomization{ + TypeMeta: kustypes.TypeMeta{ + APIVersion: kustypes.KustomizationVersion, + Kind: kustypes.KustomizationKind, + }, + } - kus.Resources = resources - kd, err := yaml.Marshal(kus) - if err != nil { - return err - } + var resources []string + for _, file := range files { + resources = append(resources, strings.Replace(file, abs, ".", 1)) + } - return ioutil.WriteFile(kfile, kd, os.ModePerm) + kus.Resources = resources + kd, err := yaml.Marshal(kus) + if err != nil { + return err } - return nil + return ioutil.WriteFile(kfile, kd, os.ModePerm) } func (kg *KustomizeGenerator) checksum(dirPath string) (string, error) {