Skip to content

Commit 32e0fdc

Browse files
authored
Merge pull request #3800 from lunarwhite/add-roles
✨ (kustomize/v2): Add CRD viewer and editor roles in rbac/kustomization.yaml
2 parents edf7ce9 + a1d8c2c commit 32e0fdc

File tree

15 files changed

+1692
-132
lines changed

15 files changed

+1692
-132
lines changed

docs/book/src/component-config-tutorial/testdata/project/config/rbac/kustomization.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ resources:
1616
- auth_proxy_role.yaml
1717
- auth_proxy_role_binding.yaml
1818
- auth_proxy_client_clusterrole.yaml
19+
# For each CRD, "Editor" and "Viewer" roles are scaffolded by
20+
# default, aiding admins in cluster management. Those roles are
21+
# not used by the Project itself. You can comment the following lines
22+
# if you do not want those helpers be installed with your Project.
23+
- projectconfig_editor_role.yaml
24+
- projectconfig_viewer_role.yaml

docs/book/src/cronjob-tutorial/testdata/project/config/rbac/kustomization.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ resources:
1616
- auth_proxy_role.yaml
1717
- auth_proxy_role_binding.yaml
1818
- auth_proxy_client_clusterrole.yaml
19+
# For each CRD, "Editor" and "Viewer" roles are scaffolded by
20+
# default, aiding admins in cluster management. Those roles are
21+
# not used by the Project itself. You can comment the following lines
22+
# if you do not want those helpers be installed with your Project.
23+
- cronjob_editor_role.yaml
24+
- cronjob_viewer_role.yaml

docs/book/src/getting-started.md

-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,6 @@ After making the necessary changes, run the `make generate` command. This will p
464464
<h1>RBAC generate under config/rbac</h1>
465465

466466
For each Kind, Kubebuilder will generate scaffold rules with view and edit permissions. (i.e. `memcached_editor_role.yaml` and `memcached_viewer_role.yaml`)
467-
Those rules are not applied on the cluster when you deploy your solution with `make deploy IMG=myregistery/example:1.0.0`.
468467
Those rules are aimed to help system admins know what to allow when granting permissions to a group of users.
469468

470469
</aside>

docs/book/src/getting-started/testdata/project/config/rbac/kustomization.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ resources:
1616
- auth_proxy_role.yaml
1717
- auth_proxy_role_binding.yaml
1818
- auth_proxy_client_clusterrole.yaml
19+
# For each CRD, "Editor" and "Viewer" roles are scaffolded by
20+
# default, aiding admins in cluster management. Those roles are
21+
# not used by the Project itself. You can comment the following lines
22+
# if you do not want those helpers be installed with your Project.
23+
- memcached_editor_role.yaml
24+
- memcached_viewer_role.yaml

pkg/plugin/util/util.go

+17
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,23 @@ func InsertCode(filename, target, code string) error {
8080
return os.WriteFile(filename, []byte(out), 0644)
8181
}
8282

83+
// InsertCodeIfNotExist insert code if it does not already exists
84+
func InsertCodeIfNotExist(filename, target, code string) error {
85+
// false positive
86+
// nolint:gosec
87+
contents, err := os.ReadFile(filename)
88+
if err != nil {
89+
return err
90+
}
91+
92+
idx := strings.Index(string(contents), code)
93+
if idx != -1 {
94+
return nil
95+
}
96+
97+
return InsertCode(filename, target, code)
98+
}
99+
83100
// UncommentCode searches for target in the file and remove the comment prefix
84101
// of the target content. The target content may span multiple lines.
85102
func UncommentCode(filename, target, prefix string) error {

pkg/plugins/common/kustomize/v2/scaffolds/api.go

+25
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package scaffolds
1818

1919
import (
2020
"fmt"
21+
"strings"
2122

2223
pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
2324
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/crd"
@@ -98,6 +99,30 @@ func (s *apiScaffolder) Scaffold() error {
9899
"%s.", kustomizeFilePath)
99100
}
100101
}
102+
103+
// Add scaffolded CRD Editor and Viewer roles in config/rbac/kustomization.yaml
104+
rbacKustomizeFilePath := "config/rbac/kustomization.yaml"
105+
comment := `
106+
# For each CRD, "Editor" and "Viewer" roles are scaffolded by
107+
# default, aiding admins in cluster management. Those roles are
108+
# not used by the Project itself. You can comment the following lines
109+
# if you do not want those helpers be installed with your Project.`
110+
err = pluginutil.InsertCodeIfNotExist(rbacKustomizeFilePath,
111+
"- auth_proxy_client_clusterrole.yaml", comment)
112+
if err != nil {
113+
log.Errorf("Unable to add a comment in the file "+
114+
"%s.", rbacKustomizeFilePath)
115+
}
116+
crdName := strings.ToLower(s.resource.Kind)
117+
if s.config.IsMultiGroup() && s.resource.Group != "" {
118+
crdName = strings.ToLower(s.resource.Group) + "_" + crdName
119+
}
120+
err = pluginutil.InsertCodeIfNotExist(rbacKustomizeFilePath, comment,
121+
fmt.Sprintf("\n- %[1]s_editor_role.yaml\n- %[1]s_viewer_role.yaml", crdName))
122+
if err != nil {
123+
log.Errorf("Unable to add Editor and Viewer roles in the file "+
124+
"%s.", rbacKustomizeFilePath)
125+
}
101126
}
102127

103128
return nil

test/e2e/v4/plugin_cluster_test.go

-15
Original file line numberDiff line numberDiff line change
@@ -269,21 +269,6 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller bool) {
269269
return err
270270
}, time.Minute, time.Second).Should(Succeed())
271271

272-
By("applying the CRD Editor Role")
273-
crdEditorRole := filepath.Join("config", "rbac",
274-
fmt.Sprintf("%s_editor_role.yaml", strings.ToLower(kbc.Kind)))
275-
EventuallyWithOffset(1, func() error {
276-
_, err = kbc.Kubectl.Apply(true, "-f", crdEditorRole)
277-
return err
278-
}, time.Minute, time.Second).Should(Succeed())
279-
280-
By("applying the CRD Viewer Role")
281-
crdViewerRole := filepath.Join("config", "rbac", fmt.Sprintf("%s_viewer_role.yaml", strings.ToLower(kbc.Kind)))
282-
EventuallyWithOffset(1, func() error {
283-
_, err = kbc.Kubectl.Apply(true, "-f", crdViewerRole)
284-
return err
285-
}, time.Minute, time.Second).Should(Succeed())
286-
287272
By("validating that the created resource object gets reconciled in the controller")
288273
metricsOutput := curlMetrics(kbc)
289274
ExpectWithOffset(1, metricsOutput).To(ContainSubstring(fmt.Sprintf(

testdata/project-v4-multigroup-with-deploy-image/config/rbac/kustomization.yaml

+24
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,27 @@ resources:
1616
- auth_proxy_role.yaml
1717
- auth_proxy_role_binding.yaml
1818
- auth_proxy_client_clusterrole.yaml
19+
# For each CRD, "Editor" and "Viewer" roles are scaffolded by
20+
# default, aiding admins in cluster management. Those roles are
21+
# not used by the Project itself. You can comment the following lines
22+
# if you do not want those helpers be installed with your Project.
23+
- lakers_editor_role.yaml
24+
- lakers_viewer_role.yaml
25+
- fiz_bar_editor_role.yaml
26+
- fiz_bar_viewer_role.yaml
27+
- foo_bar_editor_role.yaml
28+
- foo_bar_viewer_role.yaml
29+
- foo.policy_healthcheckpolicy_editor_role.yaml
30+
- foo.policy_healthcheckpolicy_viewer_role.yaml
31+
- sea-creatures_leviathan_editor_role.yaml
32+
- sea-creatures_leviathan_viewer_role.yaml
33+
- sea-creatures_kraken_editor_role.yaml
34+
- sea-creatures_kraken_viewer_role.yaml
35+
- ship_cruiser_editor_role.yaml
36+
- ship_cruiser_viewer_role.yaml
37+
- ship_destroyer_editor_role.yaml
38+
- ship_destroyer_viewer_role.yaml
39+
- ship_frigate_editor_role.yaml
40+
- ship_frigate_viewer_role.yaml
41+
- crew_captain_editor_role.yaml
42+
- crew_captain_viewer_role.yaml

0 commit comments

Comments
 (0)