@@ -124,6 +124,29 @@ 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
+ }
136
+
137
+ var defaultConfig struct {
138
+ NamePrefix string `yaml:"namePrefix"`
139
+ }
140
+
141
+ if err := yaml .Unmarshal (content , & defaultConfig ); err != nil {
142
+ log .Warnf ("failed to parse kustomization.yaml to get namePrefix: %s" , err )
143
+ log .Warnf ("using the project name as a prefix of RBAC and manifests" )
144
+ return s .config .GetProjectName ()
145
+ }
146
+
147
+ return strings .TrimSpace (defaultConfig .NamePrefix )
148
+ }
149
+
127
150
// getDeployImagesEnvVars will return the values to append the envvars for projects
128
151
// which has the APIs scaffolded with DeployImage plugin
129
152
func (s * initScaffolder ) getDeployImagesEnvVars () map [string ]string {
@@ -243,7 +266,7 @@ func (s *initScaffolder) copyConfigFiles() error {
243
266
244
267
for _ , srcFile := range files {
245
268
destFile := filepath .Join (dir .DestDir , filepath .Base (srcFile ))
246
- err : = copyFileWithHelmLogic (srcFile , destFile , dir .SubDir , s .config . GetProjectName ())
269
+ err = copyFileWithHelmLogic (srcFile , destFile , dir .SubDir , s .getNamePrefix ())
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