-
Notifications
You must be signed in to change notification settings - Fork 559
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
api: add CSIProvisionerRBAC functions for the NFS-provisioner #4395
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4b3d9ef
vendor: include k8s.io/api/rbac/v1
nixpanic c060aa6
api: add CSIProvisionerRBAC functions for the NFS-provisioner
nixpanic cb416a6
build: add NFS provisioner RBAC to generated artifacts
nixpanic c9eea08
rebase: update vendored Ceph-CSI API
nixpanic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
kind: ClusterRole | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
metadata: | ||
name: nfs-external-provisioner-runner | ||
rules: | ||
- apiGroups: [""] | ||
resources: ["nodes"] | ||
verbs: ["get", "list", "watch"] | ||
- apiGroups: [""] | ||
resources: ["secrets"] | ||
verbs: ["get", "list"] | ||
- apiGroups: [""] | ||
resources: ["events"] | ||
verbs: ["list", "watch", "create", "update", "patch"] | ||
- apiGroups: [""] | ||
resources: ["persistentvolumes"] | ||
verbs: ["get", "list", "watch", "create", "update", "delete", "patch"] | ||
- apiGroups: [""] | ||
resources: ["persistentvolumeclaims"] | ||
verbs: ["get", "list", "watch", "update"] | ||
- apiGroups: ["storage.k8s.io"] | ||
resources: ["storageclasses"] | ||
verbs: ["get", "list", "watch"] | ||
- apiGroups: ["storage.k8s.io"] | ||
resources: ["volumeattachments"] | ||
verbs: ["get", "list", "watch", "update", "patch"] | ||
- apiGroups: ["storage.k8s.io"] | ||
resources: ["volumeattachments/status"] | ||
verbs: ["patch"] | ||
- apiGroups: [""] | ||
resources: ["persistentvolumeclaims/status"] | ||
verbs: ["update", "patch"] | ||
- apiGroups: ["storage.k8s.io"] | ||
resources: ["csinodes"] | ||
verbs: ["get", "list", "watch"] | ||
- apiGroups: ["snapshot.storage.k8s.io"] | ||
resources: ["volumesnapshotclasses"] | ||
verbs: ["get", "list", "watch"] | ||
- apiGroups: ["snapshot.storage.k8s.io"] | ||
resources: ["volumesnapshotcontents"] | ||
verbs: ["get", "list", "watch", "update", "patch"] | ||
- apiGroups: ["snapshot.storage.k8s.io"] | ||
resources: ["volumesnapshotcontents/status"] | ||
verbs: ["update", "patch"] | ||
- apiGroups: ["snapshot.storage.k8s.io"] | ||
resources: ["volumesnapshots"] | ||
verbs: ["get", "list"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
kind: ClusterRoleBinding | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
metadata: | ||
name: "{{ .ServiceAccount }}-role" | ||
subjects: | ||
- kind: ServiceAccount | ||
name: "{{ .ServiceAccount }}" | ||
namespace: "{{ .Namespace }}" | ||
roleRef: | ||
kind: ClusterRole | ||
name: nfs-external-provisioner-runner | ||
apiGroup: rbac.authorization.k8s.io |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
kind: Role | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
metadata: | ||
namespace: "{{ .Namespace }}" | ||
name: nfs-external-provisioner-cfg | ||
rules: | ||
# remove this once we stop supporting v1.0.0 | ||
- apiGroups: [""] | ||
resources: ["configmaps"] | ||
verbs: ["get", "list", "create", "delete"] | ||
- apiGroups: ["coordination.k8s.io"] | ||
resources: ["leases"] | ||
verbs: ["get", "watch", "list", "delete", "update", "create"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
kind: RoleBinding | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
metadata: | ||
name: "{{ .ServiceAccount }}-role-cfg" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here as well |
||
namespace: "{{ .Namespace }}" | ||
subjects: | ||
- kind: ServiceAccount | ||
name: "{{ .ServiceAccount }}" | ||
namespace: "{{ .Namespace }}" | ||
roleRef: | ||
kind: Role | ||
name: nfs-external-provisioner-cfg | ||
apiGroup: rbac.authorization.k8s.io |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
/* | ||
Copyright 2024 The Ceph-CSI Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package nfs | ||
|
||
import ( | ||
"bytes" | ||
_ "embed" | ||
"fmt" | ||
"text/template" | ||
"strings" | ||
|
||
"github.com/ghodss/yaml" | ||
corev1 "k8s.io/api/core/v1" | ||
rbacv1 "k8s.io/api/rbac/v1" | ||
|
||
"github.com/ceph/ceph-csi/api/deploy/kubernetes" | ||
) | ||
|
||
//go:embed csi-provisioner-rbac-cr.yaml | ||
var csiProvisionerClusterRole string | ||
|
||
//go:embed csi-provisioner-rbac-crb.yaml | ||
var csiProvisionerClusterRoleBinding string | ||
|
||
//go:embed csi-provisioner-rbac-r.yaml | ||
var csiProvisionerRole string | ||
|
||
//go:embed csi-provisioner-rbac-rb.yaml | ||
var csiProvisionerRoleBinding string | ||
|
||
var CSIProvisionerRBACDefaults = kubernetes.CSIProvisionerRBACValues{ | ||
Namespace: "default", | ||
ServiceAccount: "nfs-csi-provisioner", | ||
} | ||
|
||
// NewCSIProvisionerRBAC takes a driver name from the CSIProvisionerRBACValues | ||
// struct and replaces the value in the template. A CSIProvisionerRBAC object | ||
// is returned which can be used to create permissions for the provisioner in | ||
// the Kubernetes cluster. | ||
func NewCSIProvisionerRBAC(values kubernetes.CSIProvisionerRBACValues) (kubernetes.CSIProvisionerRBAC, error) { | ||
sa := &corev1.ServiceAccount{} | ||
sa.Namespace = values.Namespace | ||
sa.Name = values.ServiceAccount | ||
|
||
cr, err := newClusterRole(values) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
crb, err := newClusterRoleBinding(values) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
r, err := newRole(values) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rb, err := newRoleBinding(values) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &csiProvisionerRBAC{ | ||
serviceAccount: sa, | ||
clusterRole: cr, | ||
clusterRoleBinding: crb, | ||
role: r, | ||
roleBinding: rb, | ||
}, nil | ||
} | ||
|
||
func NewCSIProvisionerRBACYAML(values kubernetes.CSIProvisionerRBACValues) (string, error) { | ||
docs := []string{} | ||
|
||
data, err := newYAML("csiProvisionerClusterRole", csiProvisionerClusterRole, values) | ||
if err != nil { | ||
return "", err | ||
} | ||
docs = append(docs, data) | ||
|
||
data, err = newYAML("csiProvisionerClusterRoleBinding", csiProvisionerClusterRoleBinding, values) | ||
if err != nil { | ||
return "", err | ||
} | ||
docs = append(docs, data) | ||
|
||
data, err = newYAML("csiProvisionerRole", csiProvisionerRole, values) | ||
if err != nil { | ||
return "", err | ||
} | ||
docs = append(docs, data) | ||
|
||
data, err = newYAML("csiProvisionerRoleBinding", csiProvisionerRoleBinding, values) | ||
if err != nil { | ||
return "", err | ||
} | ||
docs = append(docs, data) | ||
|
||
return strings.Join(docs, "\n"), nil | ||
} | ||
|
||
|
||
func newYAML(name, data string, values kubernetes.CSIProvisionerRBACValues) (string, error) { | ||
var buf bytes.Buffer | ||
|
||
tmpl, err := template.New(name).Parse(data) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to parse template: %w", err) | ||
} | ||
err = tmpl.Execute(&buf, values) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to replace values in template: %w", err) | ||
} | ||
|
||
return buf.String(), nil | ||
} | ||
|
||
func newClusterRole(values kubernetes.CSIProvisionerRBACValues) (*rbacv1.ClusterRole, error) { | ||
data, err := newYAML("csiProvisionerClusterRole", csiProvisionerClusterRole, values) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cr := &rbacv1.ClusterRole{} | ||
err = yaml.Unmarshal([]byte(data), cr) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed convert YAML to %T: %w", cr, err) | ||
} | ||
|
||
return cr, nil | ||
} | ||
|
||
func newClusterRoleBinding(values kubernetes.CSIProvisionerRBACValues) (*rbacv1.ClusterRoleBinding, error) { | ||
data, err := newYAML("csiProvisionerClusterRoleBinding", csiProvisionerClusterRoleBinding, values) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
crb := &rbacv1.ClusterRoleBinding{} | ||
err = yaml.Unmarshal([]byte(data), crb) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed convert YAML to %T: %w", crb, err) | ||
} | ||
|
||
return crb, nil | ||
} | ||
|
||
func newRole(values kubernetes.CSIProvisionerRBACValues) (*rbacv1.Role, error) { | ||
data, err := newYAML("csiProvisionerRole", csiProvisionerRole, values) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
r := &rbacv1.Role{} | ||
err = yaml.Unmarshal([]byte(data), r) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed convert YAML to %T: %w", r, err) | ||
} | ||
|
||
return r, nil | ||
} | ||
|
||
func newRoleBinding(values kubernetes.CSIProvisionerRBACValues) (*rbacv1.RoleBinding, error) { | ||
data, err := newYAML("csiProvisionerRoleBinding", csiProvisionerRoleBinding, values) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rb := &rbacv1.RoleBinding{} | ||
err = yaml.Unmarshal([]byte(data), rb) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed convert YAML to %T: %w", rb, err) | ||
} | ||
|
||
return rb, nil | ||
} | ||
|
||
type csiProvisionerRBAC struct { | ||
serviceAccount *corev1.ServiceAccount | ||
clusterRole *rbacv1.ClusterRole | ||
clusterRoleBinding *rbacv1.ClusterRoleBinding | ||
role *rbacv1.Role | ||
roleBinding *rbacv1.RoleBinding | ||
} | ||
|
||
func (rbac *csiProvisionerRBAC) GetServiceAccount() *corev1.ServiceAccount { | ||
return rbac.serviceAccount | ||
} | ||
|
||
func (rbac *csiProvisionerRBAC) GetClusterRole() *rbacv1.ClusterRole { | ||
return rbac.clusterRole | ||
} | ||
|
||
func (rbac *csiProvisionerRBAC) GetClusterRoleBinding() *rbacv1.ClusterRoleBinding { | ||
return rbac.clusterRoleBinding | ||
} | ||
|
||
func (rbac *csiProvisionerRBAC) GetRole() *rbacv1.Role { | ||
return rbac.role | ||
} | ||
|
||
func (rbac *csiProvisionerRBAC) GetRoleBinding() *rbacv1.RoleBinding { | ||
return rbac.roleBinding | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright 2024 The Ceph-CSI Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package nfs | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewCSIProvisionerRBAC(t *testing.T) { | ||
rbac, err := NewCSIProvisionerRBAC(CSIProvisionerRBACDefaults) | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, rbac) | ||
require.Equal(t, rbac.GetServiceAccount().Namespace, CSIProvisionerRBACDefaults.Namespace) | ||
require.Equal(t, rbac.GetServiceAccount().Name, CSIProvisionerRBACDefaults.ServiceAccount) | ||
} | ||
|
||
func TestNewCSIProvisionerRBACYAML(t *testing.T) { | ||
yaml, err := NewCSIProvisionerRBACYAML(CSIProvisionerRBACDefaults) | ||
|
||
require.NoError(t, err) | ||
require.NotEqual(t, "", yaml) | ||
} | ||
|
||
func TestNewClusterRole(t *testing.T) { | ||
cr, err := newClusterRole(CSIProvisionerRBACDefaults) | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, cr) | ||
} | ||
|
||
func TestNewClusterRoleBinding(t *testing.T) { | ||
crb, err := newClusterRoleBinding(CSIProvisionerRBACDefaults) | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, crb) | ||
} | ||
|
||
func TestNewRole(t *testing.T) { | ||
r, err := newRole(CSIProvisionerRBACDefaults) | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, r) | ||
} | ||
|
||
func TestNewRoleBinding(t *testing.T) { | ||
rb, err := newRoleBinding(CSIProvisionerRBACDefaults) | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, rb) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why no hardcoded name for clusterRoleBinding?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we allow the user to pick a name for the service account. I think it is nice to have names of roles easily related with service accounts. If you prefer a different naming convention, can you give a suggestion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i dont have any preference but just wanted to know why this flexibility for users here but for Role/ClusterRole names.