Skip to content

Commit 7ffe184

Browse files
committed
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 <[email protected]>
1 parent ecff7ea commit 7ffe184

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

controllers/kustomization_generator.go

+37-33
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,14 @@ func checkKustomizeImageExists(images []kustypes.Image, imageName string) (bool,
129129

130130
func (kg *KustomizeGenerator) generateKustomization(dirPath string) error {
131131
fs := filesys.MakeFsOnDisk()
132-
kfile := filepath.Join(dirPath, kustomizationFileName)
132+
133+
// Determine if there already is a Kustomization file at the root,
134+
// as this means we do not have to generate one.
135+
for _, kfile := range konfig.RecognizedKustomizationFileNames() {
136+
if _, err := os.Stat(filepath.Join(kfile)); err == nil {
137+
return nil
138+
}
139+
}
133140

134141
scan := func(base string) ([]string, error) {
135142
var paths []string
@@ -172,45 +179,42 @@ func (kg *KustomizeGenerator) generateKustomization(dirPath string) error {
172179
return paths, err
173180
}
174181

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-
}
182+
abs, err := filepath.Abs(dirPath)
183+
if err != nil {
184+
return err
185+
}
185186

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

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

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

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

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

213-
return nil
217+
return ioutil.WriteFile(kfile, kd, os.ModePerm)
214218
}
215219

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

0 commit comments

Comments
 (0)