Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Include admin ClusterRole and add it to the scaffold #4299

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This rule is not used by the project project itself.
# It is provided to allow the cluster admin to help manage permissions for users.
#
# Grants full permissions ('*') over batch.tutorial.kubebuilder.io.
# This role is intended for users authorized to modify roles and bindings within the cluster,
# enabling them to delegate specific permissions to other users or groups as needed.

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/name: project
app.kubernetes.io/managed-by: kustomize
name: cronjob-admin-role
rules:
- apiGroups:
- batch.tutorial.kubebuilder.io
resources:
- cronjobs
verbs:
- '*'
- apiGroups:
- batch.tutorial.kubebuilder.io
resources:
- cronjobs/status
verbs:
- get
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# permissions for end users to edit cronjobs.
# This rule is not used by the project project itself.
# It is provided to allow the cluster admin to help manage permissions for users.
#
# Grants permissions to create, update, and delete resources within the batch.tutorial.kubebuilder.io.
# This role is intended for users who need to manage these resources
# but should not control RBAC or manage permissions for others.

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# permissions for end users to view cronjobs.
# This rule is not used by the project project itself.
# It is provided to allow the cluster admin to help manage permissions for users.
#
# Grants read-only access to batch.tutorial.kubebuilder.io resources.
# This role is intended for users who need visibility into these resources
# without permissions to modify them. It is ideal for monitoring purposes and limited-access viewing.

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ resources:
- metrics_auth_role.yaml
- metrics_auth_role_binding.yaml
- metrics_reader_role.yaml
# For each CRD, "Editor" and "Viewer" roles are scaffolded by
# For each CRD, "Admin", "Editor" and "Viewer" roles are scaffolded by
# default, aiding admins in cluster management. Those roles are
# not used by the Project itself. You can comment the following lines
# not used by the {{ .ProjectName }} itself. You can comment the following lines
# if you do not want those helpers be installed with your Project.
- cronjob_admin_role.yaml
- cronjob_editor_role.yaml
- cronjob_viewer_role.yaml

21 changes: 21 additions & 0 deletions docs/book/src/cronjob-tutorial/testdata/project/dist/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3865,6 +3865,27 @@ rules:
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/name: project
name: project-cronjob-admin-role
rules:
- apiGroups:
- batch.tutorial.kubebuilder.io
resources:
- cronjobs
verbs:
- '*'
- apiGroups:
- batch.tutorial.kubebuilder.io
resources:
- cronjobs/status
verbs:
- get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/managed-by: kustomize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ resources:
- metrics_auth_role.yaml
- metrics_auth_role_binding.yaml
- metrics_reader_role.yaml
# For each CRD, "Editor" and "Viewer" roles are scaffolded by
# For each CRD, "Admin", "Editor" and "Viewer" roles are scaffolded by
# default, aiding admins in cluster management. Those roles are
# not used by the Project itself. You can comment the following lines
# not used by the {{ .ProjectName }} itself. You can comment the following lines
# if you do not want those helpers be installed with your Project.
- memcached_admin_role.yaml
- memcached_editor_role.yaml
- memcached_viewer_role.yaml

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This rule is not used by the project project itself.
# It is provided to allow the cluster admin to help manage permissions for users.
#
# Grants full permissions ('*') over cache.example.com.
# This role is intended for users authorized to modify roles and bindings within the cluster,
# enabling them to delegate specific permissions to other users or groups as needed.

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/name: project
app.kubernetes.io/managed-by: kustomize
name: memcached-admin-role
rules:
- apiGroups:
- cache.example.com
resources:
- memcacheds
verbs:
- '*'
- apiGroups:
- cache.example.com
resources:
- memcacheds/status
verbs:
- get
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# permissions for end users to edit memcacheds.
# This rule is not used by the project project itself.
# It is provided to allow the cluster admin to help manage permissions for users.
#
# Grants permissions to create, update, and delete resources within the cache.example.com.
# This role is intended for users who need to manage these resources
# but should not control RBAC or manage permissions for others.

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# permissions for end users to view memcacheds.
# This rule is not used by the project project itself.
# It is provided to allow the cluster admin to help manage permissions for users.
#
# Grants read-only access to cache.example.com resources.
# This role is intended for users who need visibility into these resources
# without permissions to modify them. It is ideal for monitoring purposes and limited-access viewing.

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
Expand Down
21 changes: 21 additions & 0 deletions docs/book/src/getting-started/testdata/project/dist/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,27 @@ rules:
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/name: project
name: project-memcached-admin-role
rules:
- apiGroups:
- cache.example.com
resources:
- memcacheds
verbs:
- '*'
- apiGroups:
- cache.example.com
resources:
- memcacheds/status
verbs:
- get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/managed-by: kustomize
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This rule is not used by the project project itself.
# It is provided to allow the cluster admin to help manage permissions for users.
#
# Grants full permissions ('*') over batch.tutorial.kubebuilder.io.
# This role is intended for users authorized to modify roles and bindings within the cluster,
# enabling them to delegate specific permissions to other users or groups as needed.

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/name: project
app.kubernetes.io/managed-by: kustomize
name: cronjob-admin-role
rules:
- apiGroups:
- batch.tutorial.kubebuilder.io
resources:
- cronjobs
verbs:
- '*'
- apiGroups:
- batch.tutorial.kubebuilder.io
resources:
- cronjobs/status
verbs:
- get
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# permissions for end users to edit cronjobs.
# This rule is not used by the project project itself.
# It is provided to allow the cluster admin to help manage permissions for users.
#
# Grants permissions to create, update, and delete resources within the batch.tutorial.kubebuilder.io.
# This role is intended for users who need to manage these resources
# but should not control RBAC or manage permissions for others.

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# permissions for end users to view cronjobs.
# This rule is not used by the project project itself.
# It is provided to allow the cluster admin to help manage permissions for users.
#
# Grants read-only access to batch.tutorial.kubebuilder.io resources.
# This role is intended for users who need visibility into these resources
# without permissions to modify them. It is ideal for monitoring purposes and limited-access viewing.

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ resources:
- metrics_auth_role.yaml
- metrics_auth_role_binding.yaml
- metrics_reader_role.yaml
# For each CRD, "Editor" and "Viewer" roles are scaffolded by
# For each CRD, "Admin", "Editor" and "Viewer" roles are scaffolded by
# default, aiding admins in cluster management. Those roles are
# not used by the Project itself. You can comment the following lines
# not used by the {{ .ProjectName }} itself. You can comment the following lines
# if you do not want those helpers be installed with your Project.
- cronjob_admin_role.yaml
- cronjob_editor_role.yaml
- cronjob_viewer_role.yaml

Original file line number Diff line number Diff line change
Expand Up @@ -7676,6 +7676,27 @@ rules:
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/name: project
name: project-cronjob-admin-role
rules:
- apiGroups:
- batch.tutorial.kubebuilder.io
resources:
- cronjobs
verbs:
- '*'
- apiGroups:
- batch.tutorial.kubebuilder.io
resources:
- cronjobs/status
verbs:
- get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/managed-by: kustomize
Expand Down
17 changes: 9 additions & 8 deletions pkg/plugins/common/kustomize/v2/scaffolds/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (s *apiScaffolder) Scaffold() error {
if s.resource.HasAPI() {
if err := scaffold.Execute(
&samples.CRDSample{Force: s.force},
&rbac.CRDAdminRole{},
&rbac.CRDEditorRole{},
&rbac.CRDViewerRole{},
&crd.Kustomization{},
Expand All @@ -100,22 +101,22 @@ func (s *apiScaffolder) Scaffold() error {
}
}

// Add scaffolded CRD Editor and Viewer roles in config/rbac/kustomization.yaml
// Add scaffolded CRD Admin, Editor and Viewer roles in config/rbac/kustomization.yaml
rbacKustomizeFilePath := "config/rbac/kustomization.yaml"
err = pluginutil.AppendCodeIfNotExist(rbacKustomizeFilePath,
editViewRulesCommentFragment)
adminEditViewRulesCommentFragment)
if err != nil {
log.Errorf("Unable to append the edit/view roles comment in the file "+
log.Errorf("Unable to append the admin/edit/view roles comment in the file "+
"%s.", rbacKustomizeFilePath)
}
crdName := strings.ToLower(s.resource.Kind)
if s.config.IsMultiGroup() && s.resource.Group != "" {
crdName = strings.ToLower(s.resource.Group) + "_" + crdName
}
err = pluginutil.InsertCodeIfNotExist(rbacKustomizeFilePath, editViewRulesCommentFragment,
fmt.Sprintf("\n- %[1]s_editor_role.yaml\n- %[1]s_viewer_role.yaml", crdName))
err = pluginutil.InsertCodeIfNotExist(rbacKustomizeFilePath, adminEditViewRulesCommentFragment,
fmt.Sprintf("\n- %[1]s_admin_role.yaml\n- %[1]s_editor_role.yaml\n- %[1]s_viewer_role.yaml", crdName))
if err != nil {
log.Errorf("Unable to add Editor and Viewer roles in the file "+
log.Errorf("Unable to add Admin, Editor and Viewer roles in the file "+
"%s.", rbacKustomizeFilePath)
}
// Add an empty line at the end of the file
Expand All @@ -132,7 +133,7 @@ func (s *apiScaffolder) Scaffold() error {
return nil
}

const editViewRulesCommentFragment = `# For each CRD, "Editor" and "Viewer" roles are scaffolded by
const adminEditViewRulesCommentFragment = `# For each CRD, "Admin", "Editor" and "Viewer" roles are scaffolded by
# default, aiding admins in cluster management. Those roles are
# not used by the Project itself. You can comment the following lines
# not used by the {{ .ProjectName }} itself. You can comment the following lines
# if you do not want those helpers be installed with your Project.`
Loading
Loading