Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look for all accepted Kustomization filenames #238

Merged
merged 1 commit into from
Jan 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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