/
profile_controller.go
435 lines (406 loc) · 14.7 KB
/
profile_controller.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
Copyright 2019 The Kubeflow 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 profile
import (
"context"
"fmt"
"reflect"
istiorbac "github.com/kubeflow/kubeflow/components/profile-controller/v2/pkg/apis/istiorbac/v1alpha1"
kubeflowv1alpha1 "github.com/kubeflow/kubeflow/components/profile-controller/v2/pkg/apis/kubeflow/v1alpha1"
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/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/source"
)
var log = logf.Log.WithName("controller")
const USERIDHEADER = "userid-header"
const USERIDPREFIX = "userid-prefix"
const SERVICEROLEISTIO = "ns-access-istio"
const SERVICEROLEBINDINGISTIO = "owner-binding-istio"
// annotation key, consumed by kfam API
const USER = "user"
const ROLE = "role"
const ADMIN = "admin"
// Kubeflow default role names
// TODO: Make kubeflow roles configurable (krishnadurai)
// This will enable customization of roles.
const(
kubeflowAdmin = "kubeflow-admin"
kubeflowEdit = "kubeflow-edit"
kubeflowView = "kubeflow-view"
)
// Add creates a new Profile Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(mgr manager.Manager, args map[string]string) error {
if _, ok := args[USERIDHEADER]; !ok {
return fmt.Errorf("%v not set!", USERIDHEADER)
}
if _, ok := args[USERIDPREFIX]; !ok {
return fmt.Errorf("%v not set!", USERIDPREFIX)
}
return add(mgr, newReconciler(mgr, args[USERIDHEADER], args[USERIDPREFIX]))
}
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager, userIdHeader string, userIdPrefix string) reconcile.Reconciler {
return &ReconcileProfile{
Client: mgr.GetClient(),
scheme: mgr.GetScheme(),
userIdHeader: userIdHeader,
userIdPrefix: userIdPrefix,
}
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler) error {
// Create a new controller
c, err := controller.New("profile-controller", mgr, controller.Options{Reconciler: r})
if err != nil {
return err
}
// Watch for changes to Profile
err = c.Watch(&source.Kind{Type: &kubeflowv1alpha1.Profile{}}, &handler.EnqueueRequestForObject{})
if err != nil {
return err
}
err = c.Watch(&source.Kind{Type: &istiorbac.ServiceRole{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &kubeflowv1alpha1.Profile{},
})
if err != nil {
return err
}
err = c.Watch(&source.Kind{Type: &istiorbac.ServiceRoleBinding{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &kubeflowv1alpha1.Profile{},
})
if err != nil {
return err
}
err = c.Watch(&source.Kind{Type: &corev1.Namespace{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &kubeflowv1alpha1.Profile{},
})
if err != nil {
return err
}
err = c.Watch(&source.Kind{Type: &corev1.ServiceAccount{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &kubeflowv1alpha1.Profile{},
})
if err != nil {
return err
}
err = c.Watch(&source.Kind{Type: &rbacv1.RoleBinding{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &kubeflowv1alpha1.Profile{},
})
if err != nil {
return err
}
return nil
}
var _ reconcile.Reconciler = &ReconcileProfile{}
// ReconcileProfile reconciles a Profile object
type ReconcileProfile struct {
client.Client
scheme *runtime.Scheme
userIdHeader string
userIdPrefix string
}
// Reconcile reads that state of the cluster for a Profile object and makes changes based on the state read
// and what is in the Profile.Spec
// Automatically generate RBAC rules to allow the Controller to read and write Deployments
// +kubebuilder:rbac:groups=core,resources=namespaces,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core,resources=serviceaccount,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=rbac,resources=rolebindings,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=kubeflow.org,resources=profiles,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=kubeflow.org,resources=profiles/status,verbs=get;update;patch
func (r *ReconcileProfile) Reconcile(request reconcile.Request) (reconcile.Result, error) {
// Fetch the Profile instance
instance := &kubeflowv1alpha1.Profile{}
err := r.Get(context.TODO(), request.NamespacedName, instance)
if err != nil {
if errors.IsNotFound(err) {
// Object not found, return. Created objects are automatically garbage collected.
// For additional cleanup logic use finalizers.
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
return reconcile.Result{}, err
}
// Update namespace
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"owner": instance.Spec.Owner.Name},
// inject istio sidecar to all pods in target namespace by default.
Labels: map[string]string{"istio-injection": "enabled"},
Name: instance.Name,
},
}
if err := controllerutil.SetControllerReference(instance, ns, r.scheme); err != nil {
return reconcile.Result{}, err
}
foundNs := &corev1.Namespace{}
err = r.Get(context.TODO(), types.NamespacedName{Name: ns.Name}, foundNs)
if err != nil {
if errors.IsNotFound(err) {
log.Info("Creating Namespace: " + ns.Name)
err = r.Create(context.TODO(), ns)
if err != nil {
return reconcile.Result{}, err
}
} else {
return reconcile.Result{}, err
}
} else {
// Check exising namespace ownership before move forward
val, ok := foundNs.Annotations["owner"]
if (!ok) || val != instance.Spec.Owner.Name {
log.Info(fmt.Sprintf("namespace already exist, but not owned by profile creator %v",
instance.Spec.Owner.Name))
instance.Status = kubeflowv1alpha1.ProfileStatus{
Status: kubeflowv1alpha1.ProfileFailed,
Message: fmt.Sprintf("namespace already exist, but not owned by profile creator %v",
instance.Spec.Owner.Name),
}
if err := r.Update(context.TODO(), instance); err != nil {
return reconcile.Result{}, err
}
return reconcile.Result{}, nil
}
}
// Update Istio Rbac
// Create Istio ServiceRole and ServiceRoleBinding in target namespace; which will give ns owner permission to access services in ns.
if err = r.updateIstioRbac(instance); err != nil {
log.Info("Failed Updating Istio rbac permission", "namespace", instance.Name, "error", err)
return reconcile.Result{}, err
}
// Update service accounts
// Create service account "default-editor" in target namespace.
// "default-editor" would have kubeflowEdit permission: edit all resources in target namespace except rbac.
if err = r.updateServiceAccount(instance, "default-editor", kubeflowEdit); err != nil {
log.Info("Failed Updating ServiceAccount", "namespace", instance.Name, "name",
"defaultEdittor", "error", err)
return reconcile.Result{}, err
}
// Create service account "default-viewer" in target namespace.
// "default-viewer" would have kubeflowView permission: view all resources in target namespace.
if err = r.updateServiceAccount(instance, "default-viewer", kubeflowView); err != nil {
log.Info("Failed Updating ServiceAccount", "namespace", instance.Name, "name",
"defaultViewer", "error", err)
return reconcile.Result{}, err
}
// TODO: add role for impersonate permission
// Update owner rbac permission
// When ClusterRole was referred by namespaced roleBinding, the result permission will be namespaced as well.
roleBinding := &rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{USER: instance.Spec.Owner.Name, ROLE: ADMIN},
Name: "namespaceAdmin",
Namespace: instance.Name,
},
// Use default ClusterRole 'admin' for profile/namespace owner
RoleRef: rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "ClusterRole",
Name: kubeflowAdmin,
},
Subjects: []rbacv1.Subject{
instance.Spec.Owner,
},
}
if err = r.updateRoleBinding(instance, roleBinding); err != nil {
log.Info("Failed Updating Owner Rolebinding", "namespace", instance.Name, "name",
"defaultEdittor", "error", err)
return reconcile.Result{}, err
}
return reconcile.Result{}, nil
}
// updateIstioRbac create or update Istio rbac resources in target namespace owned by "profileIns". The goal is to allow service access for profile owner
func (r *ReconcileProfile) updateIstioRbac(profileIns *kubeflowv1alpha1.Profile) error {
istioServiceRole := &istiorbac.ServiceRole{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{USER: profileIns.Spec.Owner.Name, ROLE: ADMIN},
Name: SERVICEROLEISTIO,
Namespace: profileIns.Name,
},
Spec: istiorbac.ServiceRoleSpec{
Rules: []*istiorbac.AccessRule{
{
Services: []string{"*"},
},
},
},
}
if err := controllerutil.SetControllerReference(profileIns, istioServiceRole, r.scheme); err != nil {
return err
}
foundSr := &istiorbac.ServiceRole{}
err := r.Get(context.TODO(), types.NamespacedName{Name: istioServiceRole.Name,
Namespace: istioServiceRole.Namespace}, foundSr)
if err != nil {
if errors.IsNotFound(err) {
log.Info("Creating Istio ServiceRole", "namespace", istioServiceRole.Namespace,
"name", istioServiceRole.Name)
err = r.Create(context.TODO(), istioServiceRole)
if err != nil {
return err
}
} else {
return err
}
} else {
if !reflect.DeepEqual(istioServiceRole.Spec, foundSr.Spec) {
foundSr.Spec = istioServiceRole.Spec
log.Info("Updating Istio ServiceRole", "namespace", istioServiceRole.Namespace,
"name", istioServiceRole.Name)
err = r.Update(context.TODO(), foundSr)
if err != nil {
return err
}
}
}
istioServiceRoleBinding := &istiorbac.ServiceRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{USER: profileIns.Spec.Owner.Name, ROLE: ADMIN},
Name: SERVICEROLEBINDINGISTIO,
Namespace: profileIns.Name,
},
Spec: istiorbac.ServiceRoleBindingSpec{
Subjects: []*istiorbac.Subject{
{
Properties: map[string]string{fmt.Sprintf("request.headers[%v]", r.userIdHeader): r.userIdPrefix + profileIns.Spec.Owner.Name},
},
},
RoleRef: &istiorbac.RoleRef{
Kind: "ServiceRole",
Name: SERVICEROLEISTIO,
},
},
}
if err := controllerutil.SetControllerReference(profileIns, istioServiceRoleBinding, r.scheme); err != nil {
return err
}
foundSrb := &istiorbac.ServiceRoleBinding{}
err = r.Get(context.TODO(), types.NamespacedName{Name: istioServiceRoleBinding.Name,
Namespace: istioServiceRoleBinding.Namespace}, foundSrb)
if err != nil {
if errors.IsNotFound(err) {
log.Info("Creating Istio ServiceRoleBinding", "namespace", istioServiceRoleBinding.Namespace,
"name", istioServiceRoleBinding.Name)
err = r.Create(context.TODO(), istioServiceRoleBinding)
if err != nil {
return err
}
} else {
return err
}
} else {
if !reflect.DeepEqual(istioServiceRoleBinding.Spec, foundSrb.Spec) {
foundSrb.Spec = istioServiceRoleBinding.Spec
log.Info("Updating Istio ServiceRoleBinding", "namespace", istioServiceRoleBinding.Namespace,
"name", istioServiceRoleBinding.Name)
err = r.Update(context.TODO(), foundSrb)
if err != nil {
return err
}
}
}
return nil
}
// updateServiceAccount create or update service account "saName" with role "ClusterRoleName" in target namespace owned by "profileIns"
func (r *ReconcileProfile) updateServiceAccount(profileIns *kubeflowv1alpha1.Profile, saName string, ClusterRoleName string) error {
serviceAccount := &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: saName,
Namespace: profileIns.Name,
},
}
if err := controllerutil.SetControllerReference(profileIns, serviceAccount, r.scheme); err != nil {
return err
}
found := &corev1.ServiceAccount{}
err := r.Get(context.TODO(), types.NamespacedName{Name: serviceAccount.Name, Namespace: serviceAccount.Namespace}, found)
if err != nil {
if errors.IsNotFound(err) {
log.Info("Creating ServiceAccount", "namespace", serviceAccount.Namespace,
"name", serviceAccount.Name)
err = r.Create(context.TODO(), serviceAccount)
if err != nil {
return err
}
} else {
return err
}
}
roleBinding := &rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: saName,
Namespace: profileIns.Name,
},
// Use default ClusterRole 'admin' for profile/namespace owner
RoleRef: rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "ClusterRole",
Name: ClusterRoleName,
},
Subjects: []rbacv1.Subject{
{
Kind: rbacv1.ServiceAccountKind,
Name: saName,
Namespace: profileIns.Name,
},
},
}
return r.updateRoleBinding(profileIns, roleBinding)
}
// updateRoleBinding create or update roleBinding "roleBinding" in target namespace owned by "profileIns"
func (r *ReconcileProfile) updateRoleBinding(profileIns *kubeflowv1alpha1.Profile,
roleBinding *rbacv1.RoleBinding) error {
if err := controllerutil.SetControllerReference(profileIns, roleBinding, r.scheme); err != nil {
return err
}
found := &rbacv1.RoleBinding{}
err := r.Get(context.TODO(), types.NamespacedName{Name: roleBinding.Name, Namespace: roleBinding.Namespace}, found)
if err != nil {
if errors.IsNotFound(err) {
log.Info("Creating RoleBinding", "namespace", roleBinding.Namespace, "name", roleBinding.Name)
err = r.Create(context.TODO(), roleBinding)
if err != nil {
return err
}
} else {
return err
}
} else {
if !(reflect.DeepEqual(roleBinding.RoleRef, found.RoleRef) && reflect.DeepEqual(roleBinding.Subjects, found.Subjects)) {
found.RoleRef = roleBinding.RoleRef
found.Subjects = roleBinding.Subjects
log.Info("Updating RoleBinding", "namespace", roleBinding.Namespace, "name", roleBinding.Name)
err = r.Update(context.TODO(), found)
if err != nil {
return err
}
}
}
return nil
}