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 <[email protected]>
  • Loading branch information
hiddeco committed Jan 15, 2021
1 parent ecff7ea commit 787ea83
Showing 1 changed file with 38 additions and 39 deletions.
77 changes: 38 additions & 39 deletions controllers/kustomization_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,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 _, kfile := range konfig.RecognizedKustomizationFileNames() {
if _, err := os.Stat(filepath.Join(kfile)); err == nil {
return nil
}
}

scan := func(base string) ([]string, error) {
var paths []string
Expand All @@ -153,64 +160,56 @@ func (kg *KustomizeGenerator) generateKustomization(dirPath string) error {
return nil
}

extension := filepath.Ext(path)
if !containsString([]string{".yaml", ".yml"}, extension) {
return nil
}

fContents, err := fs.ReadFile(path)
if err != nil {
return err
}

if _, err := uf.SliceFromBytes(fContents); err != nil {
return fmt.Errorf("failed to decode Kubernetes YAML from %s: %w", path, err)
return fmt.Errorf("failed to decode Kustomization from %s: %w", path, err)
}
paths = append(paths, path)
return nil
})
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
}
kfile := filepath.Join(dirPath, konfig.DefaultKustomizationFileName())
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,
},
}
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 787ea83

Please sign in to comment.