Skip to content

Commit

Permalink
Look for all accepted Kustomization filenames
Browse files Browse the repository at this point in the history
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 <hello@hidde.co>
  • Loading branch information
hiddeco committed Jan 15, 2021
1 parent ecff7ea commit 59c24e7
Showing 1 changed file with 39 additions and 36 deletions.
75 changes: 39 additions & 36 deletions controllers/kustomization_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ import (
)

const (
kustomizationFileName = "kustomization.yaml"
transformerFileName = "kustomization-gc-labels.yaml"
transformerFileName = "kustomization-gc-labels.yaml"
)

type KustomizeGenerator struct {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 59c24e7

Please sign in to comment.