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