Skip to content

Commit c187f9d

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 c187f9d

File tree

1 file changed

+38
-39
lines changed

1 file changed

+38
-39
lines changed

controllers/kustomization_generator.go

+38-39
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 in 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
@@ -153,64 +160,56 @@ func (kg *KustomizeGenerator) generateKustomization(dirPath string) error {
153160
return nil
154161
}
155162

156-
extension := filepath.Ext(path)
157-
if !containsString([]string{".yaml", ".yml"}, extension) {
158-
return nil
159-
}
160-
161163
fContents, err := fs.ReadFile(path)
162164
if err != nil {
163165
return err
164166
}
165167

166168
if _, err := uf.SliceFromBytes(fContents); err != nil {
167-
return fmt.Errorf("failed to decode Kubernetes YAML from %s: %w", path, err)
169+
return fmt.Errorf("failed to decode Kustomization from %s: %w", path, err)
168170
}
169171
paths = append(paths, path)
170172
return nil
171173
})
172174
return paths, err
173175
}
174176

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-
}
177+
kfile := filepath.Join(dirPath, konfig.DefaultKustomizationFileName())
178+
abs, err := filepath.Abs(dirPath)
179+
if err != nil {
180+
return err
181+
}
185182

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

192-
kus := kustypes.Kustomization{
193-
TypeMeta: kustypes.TypeMeta{
194-
APIVersion: kustypes.KustomizationVersion,
195-
Kind: kustypes.KustomizationKind,
196-
},
197-
}
188+
f, err := fs.Create(kfile)
189+
if err != nil {
190+
return err
191+
}
192+
f.Close()
198193

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

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

210-
return ioutil.WriteFile(kfile, kd, os.ModePerm)
206+
kus.Resources = resources
207+
kd, err := yaml.Marshal(kus)
208+
if err != nil {
209+
return err
211210
}
212211

213-
return nil
212+
return ioutil.WriteFile(kfile, kd, os.ModePerm)
214213
}
215214

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

0 commit comments

Comments
 (0)