Skip to content

Commit

Permalink
Include admin ClusterRole and add it to the scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
damsien committed Nov 7, 2024
1 parent 5c8befa commit 7fdf9f2
Show file tree
Hide file tree
Showing 79 changed files with 1,490 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This rule is not used by the project project itself.
# It is provided to allow the cluster admin to help manage permissions for users.
#
# Provides full permissions (create, update, delete) over all custom resource objects
# for the specified API resources across the cluster. In addition to resource management,
# it grants users the ability to modify roles and bindings within the cluster,
# allowing 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,11 @@
# 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.
#
# Allows users to modify all custom resources for project on the cluster.
# This role enables users to create, update, and delete resources,
# making it suitable for team members who need to manage 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 CronJob resources.
# This role is intended for users who need visibility into the resources
# without any permissions to modify them. It’s 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
# 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
# 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,28 @@
# This rule is not used by the project project itself.
# It is provided to allow the cluster admin to help manage permissions for users.
#
# Provides full permissions (create, update, delete) over all custom resource objects
# for the specified API resources across the cluster. In addition to resource management,
# it grants users the ability to modify roles and bindings within the cluster,
# allowing 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,11 @@
# 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.
#
# Allows users to modify all custom resources for project on the cluster.
# This role enables users to create, update, and delete resources,
# making it suitable for team members who need to manage 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 Memcached resources.
# This role is intended for users who need visibility into the resources
# without any permissions to modify them. It’s 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,28 @@
# This rule is not used by the project project itself.
# It is provided to allow the cluster admin to help manage permissions for users.
#
# Provides full permissions (create, update, delete) over all custom resource objects
# for the specified API resources across the cluster. In addition to resource management,
# it grants users the ability to modify roles and bindings within the cluster,
# allowing 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,11 @@
# 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.
#
# Allows users to modify all custom resources for project on the cluster.
# This role enables users to create, update, and delete resources,
# making it suitable for team members who need to manage 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 CronJob resources.
# This role is intended for users who need visibility into the resources
# without any permissions to modify them. It’s 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
# 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
15 changes: 8 additions & 7 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
# if you do not want those helpers be installed with your Project.`
Loading

0 comments on commit 7fdf9f2

Please sign in to comment.