Skip to content

Commit 9327d51

Browse files
committed
✨ Parametrize filename for generated RBAC
Before this change, the file name of the role/clusterrole generated from `+rbac` markers was hardcoded to `role.yaml`. This change introduces a parameter `fileName` to the RBAC generator, so that one can do ``` controller-gen rbac:fileName=my-rbac-my-choice.yaml ``` and have the file be `my-rbac-my-choice.yaml` instead. (Given no specified `output` in the above example, the full path of the output file will be `/config/rbac/my-rbac-my-choice.yaml`.) This is useful e.g. in cases where the target directory structure is not the standard Kustomize-packaged Kubebuilder one, but instead e.g. a Helm chart where multiple sources of information may contribute to the final set of files.
1 parent 0894cec commit 9327d51

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

pkg/rbac/parser.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ type Generator struct {
161161
// RoleName sets the name of the generated ClusterRole.
162162
RoleName string
163163

164+
// FileName sets the file name for the generated manifest(s). If not set, defaults to "role.yaml".
165+
FileName string `marker:",optional"`
166+
164167
// HeaderFile specifies the header text (e.g. license) to prepend to generated files.
165168
HeaderFile string `marker:",optional"`
166169

@@ -383,5 +386,10 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
383386
}
384387
headerText = strings.ReplaceAll(headerText, " YEAR", " "+g.Year)
385388

386-
return ctx.WriteYAML("role.yaml", headerText, objs, genall.WithTransform(genall.TransformRemoveCreationTimestamp))
389+
fileName := "role.yaml"
390+
if g.FileName != "" {
391+
fileName = g.FileName
392+
}
393+
394+
return ctx.WriteYAML(fileName, headerText, objs, genall.WithTransform(genall.TransformRemoveCreationTimestamp))
387395
}

pkg/rbac/zz_generated.markerhelp.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)