@@ -124,6 +124,24 @@ func (s *initScaffolder) Scaffold() error {
124
124
return nil
125
125
}
126
126
127
+ func (s * initScaffolder ) getNamePrefix () (string , error ) {
128
+ filePath := "config/default/kustomization.yaml"
129
+ content , err := os .ReadFile (filePath )
130
+ if err != nil {
131
+ return "" , fmt .Errorf ("failed to read kustomization.yaml: %w" , err )
132
+ }
133
+
134
+ var kustomization struct {
135
+ NamePrefix string `yaml:"namePrefix"`
136
+ }
137
+
138
+ if err := yaml .Unmarshal (content , & kustomization ); err != nil {
139
+ return "" , fmt .Errorf ("failed to parse kustomization.yaml: %w" , err )
140
+ }
141
+
142
+ return strings .TrimSpace (kustomization .NamePrefix ), nil
143
+ }
144
+
127
145
// getDeployImagesEnvVars will return the values to append the envvars for projects
128
146
// which has the APIs scaffolded with DeployImage plugin
129
147
func (s * initScaffolder ) getDeployImagesEnvVars () map [string ]string {
@@ -243,7 +261,12 @@ func (s *initScaffolder) copyConfigFiles() error {
243
261
244
262
for _ , srcFile := range files {
245
263
destFile := filepath .Join (dir .DestDir , filepath .Base (srcFile ))
246
- err := copyFileWithHelmLogic (srcFile , destFile , dir .SubDir , s .config .GetProjectName ())
264
+ namePrefix , err := s .getNamePrefix ()
265
+ if err != nil {
266
+ return err
267
+ }
268
+
269
+ err = copyFileWithHelmLogic (srcFile , destFile , dir .SubDir , namePrefix )
247
270
if err != nil {
248
271
return err
249
272
}
@@ -255,7 +278,7 @@ func (s *initScaffolder) copyConfigFiles() error {
255
278
256
279
// copyFileWithHelmLogic reads the source file, modifies the content for Helm, applies patches
257
280
// to spec.conversion if applicable, and writes it to the destination
258
- func copyFileWithHelmLogic (srcFile , destFile , subDir , projectName string ) error {
281
+ func copyFileWithHelmLogic (srcFile , destFile , subDir , namePrefix string ) error {
259
282
if _ , err := os .Stat (srcFile ); os .IsNotExist (err ) {
260
283
log .Printf ("Source file does not exist: %s" , srcFile )
261
284
return err
@@ -282,14 +305,14 @@ func copyFileWithHelmLogic(srcFile, destFile, subDir, projectName string) error
282
305
"name: {{ .Values.controllerManager.serviceAccountName }}" , - 1 )
283
306
contentStr = strings .Replace (contentStr ,
284
307
"name: metrics-reader" ,
285
- fmt .Sprintf ("name: %s-metrics- reader" , projectName ), 1 )
308
+ fmt .Sprintf ("name: %smetrics- reader" , namePrefix ), 1 )
286
309
287
310
contentStr = strings .Replace (contentStr ,
288
311
"name: metrics-auth-role" ,
289
- fmt .Sprintf ("name: %s-metrics- auth-role" , projectName ), - 1 )
312
+ fmt .Sprintf ("name: %smetrics- auth-role" , namePrefix ), - 1 )
290
313
contentStr = strings .Replace (contentStr ,
291
314
"name: metrics-auth-rolebinding" ,
292
- fmt .Sprintf ("name: %s-metrics- auth-rolebinding" , projectName ), 1 )
315
+ fmt .Sprintf ("name: %smetrics- auth-rolebinding" , namePrefix ), 1 )
293
316
294
317
if strings .Contains (contentStr , ".Values.controllerManager.serviceAccountName" ) &&
295
318
strings .Contains (contentStr , "kind: ServiceAccount" ) &&
@@ -306,16 +329,16 @@ func copyFileWithHelmLogic(srcFile, destFile, subDir, projectName string) error
306
329
}
307
330
contentStr = strings .Replace (contentStr ,
308
331
"name: leader-election-role" ,
309
- fmt .Sprintf ("name: %s-leader- election-role" , projectName ), - 1 )
332
+ fmt .Sprintf ("name: %sleader- election-role" , namePrefix ), - 1 )
310
333
contentStr = strings .Replace (contentStr ,
311
334
"name: leader-election-rolebinding" ,
312
- fmt .Sprintf ("name: %s-leader- election-rolebinding" , projectName ), 1 )
335
+ fmt .Sprintf ("name: %sleader- election-rolebinding" , namePrefix ), 1 )
313
336
contentStr = strings .Replace (contentStr ,
314
337
"name: manager-role" ,
315
- fmt .Sprintf ("name: %s-manager- role" , projectName ), - 1 )
338
+ fmt .Sprintf ("name: %smanager- role" , namePrefix ), - 1 )
316
339
contentStr = strings .Replace (contentStr ,
317
340
"name: manager-rolebinding" ,
318
- fmt .Sprintf ("name: %s-manager- rolebinding" , projectName ), 1 )
341
+ fmt .Sprintf ("name: %smanager- rolebinding" , namePrefix ), 1 )
319
342
320
343
// The generated files do not include the namespace
321
344
if strings .Contains (contentStr , "leader-election-rolebinding" ) ||
0 commit comments