This repository has been archived by the owner on Mar 9, 2020. It is now read-only.
/
manila_controllerplugin_rbac.go
317 lines (282 loc) · 9.69 KB
/
manila_controllerplugin_rbac.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package manilacsi
import (
"context"
manilacsiv1alpha1 "github.com/Fedosin/csi-driver-manila-operator/pkg/apis/manilacsi/v1alpha1"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
var (
labelsManilaControllerPlugin = map[string]string{
"app": "openstack-manila-csi",
"component": "controllerplugin",
}
)
func (r *ReconcileManilaCSI) handleManilaControllerPluginRBAC(instance *manilacsiv1alpha1.ManilaCSI, reqLogger logr.Logger) error {
reqLogger.Info("Reconciling Manila Controller Plugin RBAC resources")
// Manila Controller Plugin Service Account
err := r.handleManilaControllerPluginServiceAccount(instance, reqLogger)
if err != nil {
return err
}
// Manila Controller Plugin Cluster Role
err = r.handleManilaControllerPluginClusterRole(instance, reqLogger)
if err != nil {
return err
}
// Manila Controller Plugin Cluster Role Binding
err = r.handleManilaControllerPluginClusterRoleBinding(instance, reqLogger)
if err != nil {
return err
}
// Manila Controller Plugin Role
err = r.handleManilaControllerPluginRole(instance, reqLogger)
if err != nil {
return err
}
// Manila Controller Plugin Role Binding
err = r.handleManilaControllerPluginRoleBinding(instance, reqLogger)
if err != nil {
return err
}
return nil
}
func (r *ReconcileManilaCSI) handleManilaControllerPluginServiceAccount(instance *manilacsiv1alpha1.ManilaCSI, reqLogger logr.Logger) error {
reqLogger.Info("Reconciling Manila Controller Plugin Service Account")
// Define a new ServiceAccount object
sa := &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: "openstack-manila-csi-controllerplugin",
Namespace: "manila-csi",
Labels: labelsManilaControllerPlugin,
},
}
// Check if this ServiceAccount already exists
found := &corev1.ServiceAccount{}
err := r.client.Get(context.TODO(), types.NamespacedName{Name: sa.Name, Namespace: sa.Namespace}, found)
if err != nil && errors.IsNotFound(err) {
reqLogger.Info("Creating a new ServiceAccount", "ServiceAccount.Namespace", sa.Namespace, "ServiceAccount.Name", sa.Name)
err = r.client.Create(context.TODO(), sa)
if err != nil {
return err
}
// ServiceAccount created successfully - don't requeue
return nil
} else if err != nil {
return err
}
// ServiceAccount already exists - don't requeue
reqLogger.Info("Skip reconcile: ServiceAccount already exists", "ServiceAccount.Namespace", found.Namespace, "ServiceAccount.Name", found.Name)
return nil
}
func (r *ReconcileManilaCSI) handleManilaControllerPluginClusterRole(instance *manilacsiv1alpha1.ManilaCSI, reqLogger logr.Logger) error {
reqLogger.Info("Reconciling Manila Controller Plugin Cluster Role")
// Define a new ClusterRole object
cr := &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: "openstack-manila-csi-controllerplugin",
Labels: labelsManilaControllerPlugin,
},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{"nodes"},
Verbs: []string{"get", "list", "watch"},
},
{
APIGroups: []string{""},
Resources: []string{"secrets"},
Verbs: []string{"get", "list"},
},
{
APIGroups: []string{""},
Resources: []string{"persistentvolumes"},
Verbs: []string{"get", "list", "watch", "create", "delete"},
},
{
APIGroups: []string{""},
Resources: []string{"persistentvolumeclaims"},
Verbs: []string{"get", "list", "watch", "update"},
},
{
APIGroups: []string{""},
Resources: []string{"events"},
Verbs: []string{"list", "watch", "create", "update", "patch"},
},
{
APIGroups: []string{"storage.k8s.io"},
Resources: []string{"storageclasses"},
Verbs: []string{"get", "list", "watch"},
},
{
APIGroups: []string{"storage.k8s.io"},
Resources: []string{"csinodes"},
Verbs: []string{"get", "list", "watch"},
},
{
APIGroups: []string{"snapshot.storage.k8s.io"},
Resources: []string{"volumesnapshotclasses"},
Verbs: []string{"get", "list", "watch"},
},
{
APIGroups: []string{"snapshot.storage.k8s.io"},
Resources: []string{"volumesnapshotcontents"},
Verbs: []string{"create", "get", "list", "watch", "update", "delete"},
},
{
APIGroups: []string{"snapshot.storage.k8s.io"},
Resources: []string{"volumesnapshots"},
Verbs: []string{"get", "list", "watch", "update"},
},
{
APIGroups: []string{"snapshot.storage.k8s.io"},
Resources: []string{"volumesnapshots/status"},
Verbs: []string{"update"},
},
{
APIGroups: []string{"apiextensions.k8s.io"},
Resources: []string{"customresourcedefinitions"},
Verbs: []string{"create", "list", "watch", "delete", "get", "update"},
},
},
}
// Check if this ClusterRole already exists
found := &rbacv1.ClusterRole{}
err := r.client.Get(context.TODO(), types.NamespacedName{Name: cr.Name, Namespace: ""}, found)
if err != nil && errors.IsNotFound(err) {
reqLogger.Info("Creating a new ClusterRole", "ClusterRole.Name", cr.Name)
err = r.client.Create(context.TODO(), cr)
if err != nil {
return err
}
// ClusterRole created successfully - don't requeue
return nil
} else if err != nil {
return err
}
// ClusterRole already exists - don't requeue
reqLogger.Info("Skip reconcile: ClusterRole already exists", "ClusterRole.Name", found.Name)
return nil
}
func (r *ReconcileManilaCSI) handleManilaControllerPluginClusterRoleBinding(instance *manilacsiv1alpha1.ManilaCSI, reqLogger logr.Logger) error {
reqLogger.Info("Reconciling Manila Controller Plugin Cluster Role Binding")
// Define a new ClusterRoleBinding object
crb := &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "openstack-manila-csi-controllerplugin",
Labels: labelsManilaControllerPlugin,
},
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
Name: "openstack-manila-csi-controllerplugin",
Namespace: "manila-csi",
},
},
RoleRef: rbacv1.RoleRef{
Kind: "ClusterRole",
Name: "openstack-manila-csi-controllerplugin",
APIGroup: "rbac.authorization.k8s.io",
},
}
// Check if this ClusterRoleBinding already exists
found := &rbacv1.ClusterRoleBinding{}
err := r.client.Get(context.TODO(), types.NamespacedName{Name: crb.Name, Namespace: ""}, found)
if err != nil && errors.IsNotFound(err) {
reqLogger.Info("Creating a new ClusterRoleBinding", "ClusterRoleBinding.Name", crb.Name)
err = r.client.Create(context.TODO(), crb)
if err != nil {
return err
}
// ClusterRoleBinding created successfully - don't requeue
return nil
} else if err != nil {
return err
}
// ClusterRoleBinding already exists - don't requeue
reqLogger.Info("Skip reconcile: ClusterRoleBinding already exists", "ClusterRoleBinding.Name", found.Name)
return nil
}
func (r *ReconcileManilaCSI) handleManilaControllerPluginRole(instance *manilacsiv1alpha1.ManilaCSI, reqLogger logr.Logger) error {
reqLogger.Info("Reconciling Manila Controller Plugin Role")
// Define a new Role object
role := &rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: "openstack-manila-csi-controllerplugin",
Namespace: "manila-csi",
Labels: labelsManilaControllerPlugin,
},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{"endpoints"},
Verbs: []string{"get", "watch", "list", "delete", "update", "create"},
},
{
APIGroups: []string{""},
Resources: []string{"configmaps"},
Verbs: []string{"get", "list", "watch", "create", "delete"},
},
},
}
// Check if this Role already exists
found := &rbacv1.Role{}
err := r.client.Get(context.TODO(), types.NamespacedName{Name: role.Name, Namespace: role.Namespace}, found)
if err != nil && errors.IsNotFound(err) {
reqLogger.Info("Creating a new Role", "Role.Namespace", role.Namespace, "Role.Name", role.Name)
err = r.client.Create(context.TODO(), role)
if err != nil {
return err
}
// Role created successfully - don't requeue
return nil
} else if err != nil {
return err
}
// Role already exists - don't requeue
reqLogger.Info("Skip reconcile: Role already exists", "Role.Namespace", found.Namespace, "Role.Name", found.Name)
return nil
}
func (r *ReconcileManilaCSI) handleManilaControllerPluginRoleBinding(instance *manilacsiv1alpha1.ManilaCSI, reqLogger logr.Logger) error {
reqLogger.Info("Reconciling Manila Controller Plugin Role Binding")
// Define a new RoleBinding object
rb := &rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "openstack-manila-csi-controllerplugin",
Namespace: "manila-csi",
Labels: labelsManilaControllerPlugin,
},
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
Name: "openstack-manila-csi-controllerplugin",
Namespace: "manila-csi",
},
},
RoleRef: rbacv1.RoleRef{
Kind: "Role",
Name: "openstack-manila-csi-controllerplugin",
APIGroup: "rbac.authorization.k8s.io",
},
}
// Check if this RoleBinding already exists
found := &rbacv1.RoleBinding{}
err := r.client.Get(context.TODO(), types.NamespacedName{Name: rb.Name, Namespace: rb.Namespace}, found)
if err != nil && errors.IsNotFound(err) {
reqLogger.Info("Creating a new RoleBinding", "RoleBinding.Namespace", rb.Namespace, "RoleBinding.Name", rb.Name)
err = r.client.Create(context.TODO(), rb)
if err != nil {
return err
}
// RoleBinding created successfully - don't requeue
return nil
} else if err != nil {
return err
}
// RoleBinding already exists - don't requeue
reqLogger.Info("Skip reconcile: RoleBinding already exists", "RoleBinding.Namespace", found.Namespace, "RoleBinding.Name", found.Name)
return nil
}