-
Notifications
You must be signed in to change notification settings - Fork 151
/
transformer_rbac.go
355 lines (316 loc) · 12.1 KB
/
transformer_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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package apps
import (
"fmt"
"time"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/constant"
"github.com/apecloud/kubeblocks/pkg/controller/component"
"github.com/apecloud/kubeblocks/pkg/controller/factory"
"github.com/apecloud/kubeblocks/pkg/controller/graph"
"github.com/apecloud/kubeblocks/pkg/controller/model"
ictrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
dptypes "github.com/apecloud/kubeblocks/pkg/dataprotection/types"
viper "github.com/apecloud/kubeblocks/pkg/viperx"
)
// RBACTransformer puts the rbac at the beginning of the DAG
type RBACTransformer struct{}
var _ graph.Transformer = &RBACTransformer{}
func (c *RBACTransformer) Transform(ctx graph.TransformContext, dag *graph.DAG) error {
transCtx, _ := ctx.(*clusterTransformContext)
cluster := transCtx.Cluster
graphCli, _ := transCtx.Client.(model.GraphClient)
componentSpecs, err := getComponentSpecs(transCtx)
if err != nil {
return err
}
serviceAccounts, serviceAccountsNeedCrb, err := buildServiceAccounts(transCtx, componentSpecs)
if err != nil {
return err
}
if !viper.GetBool(constant.EnableRBACManager) {
transCtx.Logger.V(1).Info("rbac manager is disabled")
saNotExist := false
for saName := range serviceAccounts {
if !isServiceAccountExist(transCtx, saName) {
transCtx.EventRecorder.Event(transCtx.Cluster, corev1.EventTypeWarning,
string(ictrlutil.ErrorTypeNotFound), saName+" ServiceAccount is not exist")
saNotExist = true
}
}
if saNotExist {
return ictrlutil.NewRequeueError(time.Second, "RBAC manager is disabed, but service account is not exist")
}
return nil
}
var parent client.Object
rb := buildRoleBinding(cluster, serviceAccounts)
graphCli.Create(dag, rb)
parent = rb
if len(serviceAccountsNeedCrb) > 0 {
crb := buildClusterRoleBinding(cluster, serviceAccountsNeedCrb)
graphCli.Create(dag, crb)
graphCli.DependOn(dag, parent, crb)
parent = crb
}
sas := createServiceAccounts(serviceAccounts, graphCli, dag, parent)
stsList := graphCli.FindAll(dag, &appsv1.StatefulSet{})
for _, sts := range stsList {
// serviceaccount must be created before statefulset
graphCli.DependOn(dag, sts, sas...)
}
deployList := graphCli.FindAll(dag, &appsv1.Deployment{})
for _, deploy := range deployList {
// serviceaccount must be created before deployment
graphCli.DependOn(dag, deploy, sas...)
}
return nil
}
func isProbesEnabled(clusterDef *appsv1alpha1.ClusterDefinition, compSpec *appsv1alpha1.ClusterComponentSpec) bool {
for _, compDef := range clusterDef.Spec.ComponentDefs {
if compDef.Name == compSpec.ComponentDefRef && compDef.Probes != nil {
return true
}
}
return false
}
func isDataProtectionEnabled(backupTpl *appsv1alpha1.BackupPolicyTemplate, compSpec *appsv1alpha1.ClusterComponentSpec) bool {
if backupTpl == nil {
return false
}
for _, policy := range backupTpl.Spec.BackupPolicies {
if policy.ComponentDefRef == compSpec.ComponentDefRef {
return true
}
}
return false
}
func isVolumeProtectionEnabled(clusterDef *appsv1alpha1.ClusterDefinition, compSpec *appsv1alpha1.ClusterComponentSpec) bool {
for _, compDef := range clusterDef.Spec.ComponentDefs {
if compDef.Name == compSpec.ComponentDefRef && compDef.VolumeProtectionSpec != nil {
return true
}
}
return false
}
func isServiceAccountExist(transCtx *clusterTransformContext, serviceAccountName string) bool {
cluster := transCtx.Cluster
namespaceName := types.NamespacedName{
Namespace: cluster.Namespace,
Name: serviceAccountName,
}
sa := &corev1.ServiceAccount{}
if err := transCtx.Client.Get(transCtx.Context, namespaceName, sa); err != nil {
// KubeBlocks will create a rolebinding only if it has RBAC access priority and
// the rolebinding is not already present.
if errors.IsNotFound(err) {
transCtx.Logger.V(1).Info("ServiceAccount not exists", "namespaceName", namespaceName)
return false
}
transCtx.Logger.Error(err, "get ServiceAccount failed")
return false
}
return true
}
func isClusterRoleBindingExist(transCtx *clusterTransformContext, serviceAccountName string) bool {
cluster := transCtx.Cluster
namespaceName := types.NamespacedName{
Namespace: cluster.Namespace,
Name: "kb-" + cluster.Name,
}
crb := &rbacv1.ClusterRoleBinding{}
if err := transCtx.Client.Get(transCtx.Context, namespaceName, crb); err != nil {
// KubeBlocks will create a cluster role binding only if it has RBAC access priority and
// the cluster role binding is not already present.
if errors.IsNotFound(err) {
transCtx.Logger.V(1).Info("ClusterRoleBinding not exists", "namespaceName", namespaceName)
return false
}
transCtx.Logger.Error(err, fmt.Sprintf("get cluster role binding failed: %s", namespaceName))
return false
}
if crb.RoleRef.Name != constant.RBACClusterRoleName {
transCtx.Logger.V(1).Info("rbac manager: ClusterRole not match", "ClusterRole",
constant.RBACClusterRoleName, "clusterrolebinding.RoleRef", crb.RoleRef.Name)
}
isServiceAccountMatch := false
for _, sub := range crb.Subjects {
if sub.Kind == rbacv1.ServiceAccountKind && sub.Name == serviceAccountName {
isServiceAccountMatch = true
break
}
}
if !isServiceAccountMatch {
transCtx.Logger.V(1).Info("rbac manager: ServiceAccount not match", "ServiceAccount",
serviceAccountName, "clusterrolebinding.Subjects", crb.Subjects)
}
return true
}
func isRoleBindingExist(transCtx *clusterTransformContext, serviceAccountName string) bool {
cluster := transCtx.Cluster
namespaceName := types.NamespacedName{
Namespace: cluster.Namespace,
Name: "kb-" + cluster.Name,
}
rb := &rbacv1.RoleBinding{}
if err := transCtx.Client.Get(transCtx.Context, namespaceName, rb); err != nil {
// KubeBlocks will create a role binding only if it has RBAC access priority and
// the role binding is not already present.
if errors.IsNotFound(err) {
transCtx.Logger.V(1).Info("RoleBinding not exists", "namespaceName", namespaceName)
return false
}
transCtx.Logger.Error(err, fmt.Sprintf("get role binding failed: %s", namespaceName))
return false
}
if rb.RoleRef.Name != constant.RBACClusterRoleName {
transCtx.Logger.V(1).Info("rbac manager: ClusterRole not match", "ClusterRole",
constant.RBACRoleName, "rolebinding.RoleRef", rb.RoleRef.Name)
}
isServiceAccountMatch := false
for _, sub := range rb.Subjects {
if sub.Kind == rbacv1.ServiceAccountKind && sub.Name == serviceAccountName {
isServiceAccountMatch = true
break
}
}
if !isServiceAccountMatch {
transCtx.Logger.V(1).Info("rbac manager: ServiceAccount not match", "ServiceAccount",
serviceAccountName, "rolebinding.Subjects", rb.Subjects)
}
return true
}
func getComponentSpecs(transCtx *clusterTransformContext) ([]appsv1alpha1.ClusterComponentSpec, error) {
cluster := transCtx.Cluster
clusterDef := transCtx.ClusterDef
componentSpecs := make([]appsv1alpha1.ClusterComponentSpec, 0, 1)
compSpecMap := cluster.Spec.GetDefNameMappingComponents()
for _, compDef := range clusterDef.Spec.ComponentDefs {
comps := compSpecMap[compDef.Name]
if len(comps) == 0 {
// if componentSpecs is empty, it may be generated from the cluster template and cluster.
reqCtx := ictrlutil.RequestCtx{
Ctx: transCtx.Context,
Log: log.Log.WithName("rbac"),
}
synthesizedComponent, err := component.BuildComponent(reqCtx, nil, cluster, transCtx.ClusterDef, &compDef, nil, nil)
if err != nil {
return nil, err
}
if synthesizedComponent == nil {
continue
}
comps = []appsv1alpha1.ClusterComponentSpec{{
ServiceAccountName: synthesizedComponent.ServiceAccountName,
ComponentDefRef: compDef.Name,
}}
}
componentSpecs = append(componentSpecs, comps...)
}
return componentSpecs, nil
}
func getDefaultBackupPolicyTemplate(transCtx *clusterTransformContext, clusterDefName string) (*appsv1alpha1.BackupPolicyTemplate, error) {
backupPolicyTPLs := &appsv1alpha1.BackupPolicyTemplateList{}
if err := transCtx.Client.List(transCtx.Context, backupPolicyTPLs, client.MatchingLabels{constant.ClusterDefLabelKey: clusterDefName}); err != nil {
return nil, err
}
if len(backupPolicyTPLs.Items) == 0 {
return nil, nil
}
for _, item := range backupPolicyTPLs.Items {
if item.Annotations[dptypes.DefaultBackupPolicyTemplateAnnotationKey] == trueVal {
return &item, nil
}
}
return &backupPolicyTPLs.Items[0], nil
}
func buildServiceAccounts(transCtx *clusterTransformContext, componentSpecs []appsv1alpha1.ClusterComponentSpec) (map[string]*corev1.ServiceAccount, map[string]*corev1.ServiceAccount, error) {
serviceAccounts := map[string]*corev1.ServiceAccount{}
serviceAccountsNeedCrb := map[string]*corev1.ServiceAccount{}
clusterDef := transCtx.ClusterDef
cluster := transCtx.Cluster
backupPolicyTPL, err := getDefaultBackupPolicyTemplate(transCtx, clusterDef.Name)
if err != nil {
return serviceAccounts, serviceAccountsNeedCrb, err
}
for _, compSpec := range componentSpecs {
serviceAccountName := compSpec.ServiceAccountName
if serviceAccountName == "" {
if !isProbesEnabled(clusterDef, &compSpec) && !isVolumeProtectionEnabled(clusterDef, &compSpec) && !isDataProtectionEnabled(backupPolicyTPL, &compSpec) {
continue
}
serviceAccountName = "kb-" + cluster.Name
}
if isRoleBindingExist(transCtx, serviceAccountName) && isServiceAccountExist(transCtx, serviceAccountName) {
if !isVolumeProtectionEnabled(clusterDef, &compSpec) || isClusterRoleBindingExist(transCtx, serviceAccountName) {
continue
}
}
if _, ok := serviceAccounts[serviceAccountName]; ok {
continue
}
serviceAccount := factory.BuildServiceAccount(cluster)
serviceAccount.Name = serviceAccountName
serviceAccounts[serviceAccountName] = serviceAccount
if isVolumeProtectionEnabled(clusterDef, &compSpec) {
serviceAccountsNeedCrb[serviceAccountName] = serviceAccount
}
}
return serviceAccounts, serviceAccountsNeedCrb, nil
}
func buildRoleBinding(cluster *appsv1alpha1.Cluster, serviceAccounts map[string]*corev1.ServiceAccount) *rbacv1.RoleBinding {
roleBinding := factory.BuildRoleBinding(cluster)
roleBinding.Subjects = []rbacv1.Subject{}
for saName := range serviceAccounts {
subject := rbacv1.Subject{
Name: saName,
Namespace: cluster.Namespace,
Kind: rbacv1.ServiceAccountKind,
}
roleBinding.Subjects = append(roleBinding.Subjects, subject)
}
return roleBinding
}
func buildClusterRoleBinding(cluster *appsv1alpha1.Cluster, serviceAccounts map[string]*corev1.ServiceAccount) *rbacv1.ClusterRoleBinding {
clusterRoleBinding := factory.BuildClusterRoleBinding(cluster)
clusterRoleBinding.Subjects = []rbacv1.Subject{}
for saName := range serviceAccounts {
subject := rbacv1.Subject{
Name: saName,
Namespace: cluster.Namespace,
Kind: rbacv1.ServiceAccountKind,
}
clusterRoleBinding.Subjects = append(clusterRoleBinding.Subjects, subject)
}
return clusterRoleBinding
}
func createServiceAccounts(serviceAccounts map[string]*corev1.ServiceAccount, graphCli model.GraphClient, dag *graph.DAG, parent client.Object) []client.Object {
var sas []client.Object
for _, sa := range serviceAccounts {
// serviceaccount must be created before rolebinding and clusterrolebinding
graphCli.Create(dag, sa)
graphCli.DependOn(dag, parent, sa)
sas = append(sas, sa)
}
return sas
}