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

api: add CSIProvisionerRBAC functions for the NFS-provisioner #4395

Merged
merged 4 commits into from
Mar 14, 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
48 changes: 48 additions & 0 deletions api/deploy/kubernetes/nfs/csi-provisioner-rbac-cr.yaml
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"]
13 changes: 13 additions & 0 deletions api/deploy/kubernetes/nfs/csi-provisioner-rbac-crb.yaml
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"
Copy link
Collaborator

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?

Copy link
Member Author

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?

Copy link
Collaborator

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.

subjects:
- kind: ServiceAccount
name: "{{ .ServiceAccount }}"
namespace: "{{ .Namespace }}"
roleRef:
kind: ClusterRole
name: nfs-external-provisioner-runner
apiGroup: rbac.authorization.k8s.io
14 changes: 14 additions & 0 deletions api/deploy/kubernetes/nfs/csi-provisioner-rbac-r.yaml
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"]
14 changes: 14 additions & 0 deletions api/deploy/kubernetes/nfs/csi-provisioner-rbac-rb.yaml
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"
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
220 changes: 220 additions & 0 deletions api/deploy/kubernetes/nfs/csi-provisioner-rbac.go
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
}
67 changes: 67 additions & 0 deletions api/deploy/kubernetes/nfs/csi-provisioner-rbac_test.go
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)
}
Loading