Skip to content

Commit 80c2169

Browse files
authored
Merge pull request #238 from fluxcd/accept-all-kustomization-filenames
Look for all accepted Kustomization filenames
2 parents ecff7ea + 59c24e7 commit 80c2169

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

controllers/kustomization_generator.go

+39-36
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ import (
3535
)
3636

3737
const (
38-
kustomizationFileName = "kustomization.yaml"
39-
transformerFileName = "kustomization-gc-labels.yaml"
38+
transformerFileName = "kustomization-gc-labels.yaml"
4039
)
4140

4241
type KustomizeGenerator struct {
@@ -50,7 +49,7 @@ func NewGenerator(kustomization kustomizev1.Kustomization) *KustomizeGenerator {
5049
}
5150

5251
func (kg *KustomizeGenerator) WriteFile(dirPath string) (string, error) {
53-
kfile := filepath.Join(dirPath, kustomizationFileName)
52+
kfile := filepath.Join(dirPath, konfig.DefaultKustomizationFileName())
5453

5554
checksum, err := kg.checksum(dirPath)
5655
if err != nil {
@@ -129,7 +128,14 @@ func checkKustomizeImageExists(images []kustypes.Image, imageName string) (bool,
129128

130129
func (kg *KustomizeGenerator) generateKustomization(dirPath string) error {
131130
fs := filesys.MakeFsOnDisk()
132-
kfile := filepath.Join(dirPath, kustomizationFileName)
131+
132+
// Determine if there already is a Kustomization file at the root,
133+
// as this means we do not have to generate one.
134+
for _, kfilename := range konfig.RecognizedKustomizationFileNames() {
135+
if kpath := filepath.Join(dirPath, kfilename); fs.Exists(kpath) && !fs.IsDir(kpath) {
136+
return nil
137+
}
138+
}
133139

134140
scan := func(base string) ([]string, error) {
135141
var paths []string
@@ -172,45 +178,42 @@ func (kg *KustomizeGenerator) generateKustomization(dirPath string) error {
172178
return paths, err
173179
}
174180

175-
if _, err := os.Stat(kfile); err != nil {
176-
abs, err := filepath.Abs(dirPath)
177-
if err != nil {
178-
return err
179-
}
180-
181-
files, err := scan(abs)
182-
if err != nil {
183-
return err
184-
}
181+
abs, err := filepath.Abs(dirPath)
182+
if err != nil {
183+
return err
184+
}
185185

186-
f, err := fs.Create(kfile)
187-
if err != nil {
188-
return err
189-
}
190-
f.Close()
186+
files, err := scan(abs)
187+
if err != nil {
188+
return err
189+
}
191190

192-
kus := kustypes.Kustomization{
193-
TypeMeta: kustypes.TypeMeta{
194-
APIVersion: kustypes.KustomizationVersion,
195-
Kind: kustypes.KustomizationKind,
196-
},
197-
}
191+
kfile := filepath.Join(dirPath, konfig.DefaultKustomizationFileName())
192+
f, err := fs.Create(kfile)
193+
if err != nil {
194+
return err
195+
}
196+
f.Close()
198197

199-
var resources []string
200-
for _, file := range files {
201-
resources = append(resources, strings.Replace(file, abs, ".", 1))
202-
}
198+
kus := kustypes.Kustomization{
199+
TypeMeta: kustypes.TypeMeta{
200+
APIVersion: kustypes.KustomizationVersion,
201+
Kind: kustypes.KustomizationKind,
202+
},
203+
}
203204

204-
kus.Resources = resources
205-
kd, err := yaml.Marshal(kus)
206-
if err != nil {
207-
return err
208-
}
205+
var resources []string
206+
for _, file := range files {
207+
resources = append(resources, strings.Replace(file, abs, ".", 1))
208+
}
209209

210-
return ioutil.WriteFile(kfile, kd, os.ModePerm)
210+
kus.Resources = resources
211+
kd, err := yaml.Marshal(kus)
212+
if err != nil {
213+
return err
211214
}
212215

213-
return nil
216+
return ioutil.WriteFile(kfile, kd, os.ModePerm)
214217
}
215218

216219
func (kg *KustomizeGenerator) checksum(dirPath string) (string, error) {

0 commit comments

Comments
 (0)