-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathprofile_controller.go
774 lines (723 loc) · 26.1 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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
/*
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 controllers
import (
"context"
"fmt"
"io/ioutil"
"os"
"reflect"
"time"
"github.com/cenkalti/backoff"
"github.com/fsnotify/fsnotify"
"github.com/ghodss/yaml"
"github.com/go-logr/logr"
profilev1 "github.com/kubeflow/kubeflow/components/profile-controller/api/v1"
"github.com/pkg/errors"
istioSecurity "istio.io/api/security/v1beta1"
istioSecurityClient "istio.io/client-go/pkg/apis/security/v1beta1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
"sigs.k8s.io/controller-runtime/pkg/source"
)
const AUTHZPOLICYISTIO = "ns-owner-access-istio"
// Istio constants
const ISTIOALLOWALL = "allow-all"
const KFQUOTA = "kf-resource-quota"
const PROFILEFINALIZER = "profile-finalizer"
// 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"
istioInjectionLabel = "istio-injection"
)
const DEFAULT_EDITOR = "default-editor"
const DEFAULT_VIEWER = "default-viewer"
type Plugin interface {
// Called when profile CR is created / updated
ApplyPlugin(*ProfileReconciler, *profilev1.Profile) error
// Called when profile CR is being deleted, to cleanup any non-k8s resources created via ApplyPlugin
// RevokePlugin logic need to be IDEMPOTENT
RevokePlugin(*ProfileReconciler, *profilev1.Profile) error
}
// ProfileReconciler reconciles a Profile object
type ProfileReconciler struct {
client.Client
Scheme *runtime.Scheme
Log logr.Logger
UserIdHeader string
UserIdPrefix string
WorkloadIdentity string
DefaultNamespaceLabelsPath string
}
// +kubebuilder:rbac:groups=core,resources=namespaces,verbs="*"
// +kubebuilder:rbac:groups=core,resources=serviceaccounts,verbs="*"
// +kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=rolebindings,verbs="*"
// +kubebuilder:rbac:groups=security.istio.io,resources=authorizationpolicies,verbs="*"
// +kubebuilder:rbac:groups=kubeflow.org,resources=profiles;profiles/status;profiles/finalizers,verbs="*"
// 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
func (r *ProfileReconciler) Reconcile(request ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
logger := r.Log.WithValues("profile", request.NamespacedName)
defaultKubeflowNamespaceLabels := r.readDefaultLabelsFromFile(r.DefaultNamespaceLabelsPath)
// Fetch the Profile instance
instance := &profilev1.Profile{}
logger.Info("Start to Reconcile.", "namespace", request.Namespace, "name", request.Name)
err := r.Get(ctx, request.NamespacedName, instance)
if err != nil {
if apierrors.IsNotFound(err) {
// Object not found, return. Created objects are automatically garbage collected.
// For additional cleanup logic use finalizers.
IncRequestCounter("profile deletion")
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
IncRequestErrorCounter("error reading the profile object", SEVERITY_MAJOR)
logger.Error(err, "error reading the profile object")
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{
istioInjectionLabel: "enabled",
},
Name: instance.Name,
},
}
setNamespaceLabels(ns, defaultKubeflowNamespaceLabels)
logger.Info("List of labels to be added to namespace", "labels", ns.Labels)
if err := controllerutil.SetControllerReference(instance, ns, r.Scheme); err != nil {
IncRequestErrorCounter("error setting ControllerReference", SEVERITY_MAJOR)
logger.Error(err, "error setting ControllerReference")
return reconcile.Result{}, err
}
foundNs := &corev1.Namespace{}
err = r.Get(ctx, types.NamespacedName{Name: ns.Name}, foundNs)
if err != nil {
if apierrors.IsNotFound(err) {
logger.Info("Creating Namespace: " + ns.Name)
err = r.Create(ctx, ns)
if err != nil {
IncRequestErrorCounter("error creating namespace", SEVERITY_MAJOR)
logger.Error(err, "error creating namespace")
return reconcile.Result{}, err
}
// wait 15 seconds for new namespace creation.
err = backoff.Retry(
func() error {
return r.Get(ctx, types.NamespacedName{Name: ns.Name}, foundNs)
},
backoff.WithMaxRetries(backoff.NewConstantBackOff(3*time.Second), 5))
if err != nil {
IncRequestErrorCounter("error namespace create completion", SEVERITY_MAJOR)
logger.Error(err, "error namespace create completion")
return r.appendErrorConditionAndReturn(ctx, instance,
"Owning namespace failed to create within 15 seconds")
}
logger.Info("Created Namespace: "+foundNs.Name, "status", foundNs.Status.Phase)
} else {
IncRequestErrorCounter("error reading namespace", SEVERITY_MAJOR)
logger.Error(err, "error reading namespace")
return reconcile.Result{}, err
}
} else {
// Check exising namespace ownership before move forward
owner, ok := foundNs.Annotations["owner"]
if ok && owner == instance.Spec.Owner.Name {
oldLabels := map[string]string{}
for k, v := range foundNs.Labels {
oldLabels[k] = v
}
setNamespaceLabels(foundNs, defaultKubeflowNamespaceLabels)
logger.Info("List of labels to be added to found namespace", "labels", ns.Labels)
if !reflect.DeepEqual(oldLabels, foundNs.Labels) {
err = r.Update(ctx, foundNs)
if err != nil {
IncRequestErrorCounter("error updating namespace label", SEVERITY_MAJOR)
logger.Error(err, "error updating namespace label")
return reconcile.Result{}, err
}
}
} else {
logger.Info(fmt.Sprintf("namespace already exist, but not owned by profile creator %v",
instance.Spec.Owner.Name))
IncRequestCounter("reject profile taking over existing namespace")
return r.appendErrorConditionAndReturn(ctx, instance, fmt.Sprintf(
"namespace already exist, but not owned by profile creator %v", instance.Spec.Owner.Name))
}
}
// Update Istio AuthorizationPolicy
// Create Istio AuthorizationPolicy in target namespace, which will give ns owner permission to access services in ns.
if err = r.updateIstioAuthorizationPolicy(instance); err != nil {
logger.Error(err, "error Updating Istio AuthorizationPolicy permission", "namespace", instance.Name)
IncRequestErrorCounter("error updating Istio AuthorizationPolicy permission", SEVERITY_MAJOR)
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 {
logger.Error(err, "error Updating ServiceAccount", "namespace", instance.Name, "name",
"defaultEditor")
IncRequestErrorCounter("error updating ServiceAccount", SEVERITY_MAJOR)
return reconcile.Result{}, err
}
// Create service account "default-viewer" in target namespace.
// "default-viewer" would have k8s default "view" permission: view all resources in target namespace.
if err = r.updateServiceAccount(instance, DEFAULT_VIEWER, kubeflowView); err != nil {
logger.Error(err, "error Updating ServiceAccount", "namespace", instance.Name, "name",
"defaultViewer")
IncRequestErrorCounter("error updating ServiceAccount", SEVERITY_MAJOR)
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 {
logger.Error(err, "error Updating Owner Rolebinding", "namespace", instance.Name, "name",
"defaultEdittor")
IncRequestErrorCounter("error updating Owner Rolebinding", SEVERITY_MAJOR)
return reconcile.Result{}, err
}
// Create resource quota for target namespace if resources are specified in profile.
if len(instance.Spec.ResourceQuotaSpec.Hard) > 0 {
resourceQuota := &corev1.ResourceQuota{
ObjectMeta: metav1.ObjectMeta{
Name: KFQUOTA,
Namespace: instance.Name,
},
Spec: instance.Spec.ResourceQuotaSpec,
}
if err = r.updateResourceQuota(instance, resourceQuota); err != nil {
logger.Error(err, "error Updating resource quota", "namespace", instance.Name)
IncRequestErrorCounter("error updating resource quota", SEVERITY_MAJOR)
return reconcile.Result{}, err
}
} else {
logger.Info("No update on resource quota", "spec", instance.Spec.ResourceQuotaSpec.String())
}
if err := r.PatchDefaultPluginSpec(ctx, instance); err != nil {
IncRequestErrorCounter("error patching DefaultPluginSpec", SEVERITY_MAJOR)
logger.Error(err, "Failed patching DefaultPluginSpec", "namespace", instance.Name)
return reconcile.Result{}, err
}
if plugins, err := r.GetPluginSpec(instance); err == nil {
for _, plugin := range plugins {
if err2 := plugin.ApplyPlugin(r, instance); err2 != nil {
logger.Error(err2, "Failed applying plugin", "namespace", instance.Name)
IncRequestErrorCounter("error applying plugin", SEVERITY_MAJOR)
return reconcile.Result{}, err2
}
}
}
// examine DeletionTimestamp to determine if object is under deletion
if instance.ObjectMeta.DeletionTimestamp.IsZero() {
// The object is not being deleted, so if it does not have our finalizer,
// then lets add the finalizer and update the object. This is equivalent
// registering our finalizer.
if !containsString(instance.ObjectMeta.Finalizers, PROFILEFINALIZER) {
instance.ObjectMeta.Finalizers = append(instance.ObjectMeta.Finalizers, PROFILEFINALIZER)
if err := r.Update(ctx, instance); err != nil {
logger.Error(err, "error updating finalizer", "namespace", instance.Name)
IncRequestErrorCounter("error updating finalizer", SEVERITY_MAJOR)
return ctrl.Result{}, err
}
}
} else {
// The object is being deleted
if containsString(instance.ObjectMeta.Finalizers, PROFILEFINALIZER) {
// our finalizer is present, so lets revoke all Plugins to clean up any external dependencies
if plugins, err := r.GetPluginSpec(instance); err == nil {
for _, plugin := range plugins {
if err := plugin.RevokePlugin(r, instance); err != nil {
logger.Error(err, "error revoking plugin", "namespace", instance.Name)
IncRequestErrorCounter("error revoking plugin", SEVERITY_MAJOR)
return reconcile.Result{}, err
}
}
}
// remove our finalizer from the list and update it.
instance.ObjectMeta.Finalizers = removeString(instance.ObjectMeta.Finalizers, PROFILEFINALIZER)
if err := r.Update(ctx, instance); err != nil {
logger.Error(err, "error removing finalizer", "namespace", instance.Name)
IncRequestErrorCounter("error removing finalizer", SEVERITY_MAJOR)
return ctrl.Result{}, err
}
}
}
IncRequestCounter("reconcile")
return ctrl.Result{}, nil
}
// appendErrorConditionAndReturn append failure status to profile CR and mark Reconcile done. If update condition failed, request will be requeued.
func (r *ProfileReconciler) appendErrorConditionAndReturn(ctx context.Context, instance *profilev1.Profile,
message string) (ctrl.Result, error) {
instance.Status.Conditions = append(instance.Status.Conditions, profilev1.ProfileCondition{
Type: profilev1.ProfileFailed,
Message: message,
})
if err := r.Update(ctx, instance); err != nil {
return reconcile.Result{}, err
}
return reconcile.Result{}, nil
}
// ProfileMapper maps an event to a reconciliation of all existing Profiles.
type ProfileMapper struct {
client client.Client
logger logr.Logger
}
var _ inject.Client = (*ProfileMapper)(nil)
func (i *ProfileMapper) InjectClient(c client.Client) error {
i.client = c
return nil
}
var _ inject.Logger = (*ProfileMapper)(nil)
func (i *ProfileMapper) InjectLogger(l logr.Logger) error {
i.logger = l
return nil
}
func (i *ProfileMapper) Map(a handler.MapObject) []reconcile.Request {
req := []reconcile.Request{}
profileList := &profilev1.ProfileList{}
err := i.client.List(context.TODO(), profileList)
if err != nil {
i.logger.Error(err, "Failed to list profiles in order to trigger reconciliation")
return req
}
for _, p := range profileList.Items {
req = append(req, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: p.Name,
Namespace: p.Namespace,
}})
}
return req
}
func (r *ProfileReconciler) SetupWithManager(mgr ctrl.Manager) error {
// Watch config file with namespace labels. If the file changes, trigger
// a reconciliation for all Profiles.
watcher, err := fsnotify.NewWatcher()
if err != nil {
return errors.Wrap(err, "Failed to start file watcher")
}
if err := watcher.Add(r.DefaultNamespaceLabelsPath); err != nil {
return errors.Wrapf(err, "Failed to watch file %s", r.DefaultNamespaceLabelsPath)
}
events := make(chan event.GenericEvent)
go func(watcher *fsnotify.Watcher, reconcileEvents chan event.GenericEvent) {
defer watcher.Close()
for {
select {
case fsEvent := <-watcher.Events:
if fsEvent.Op != fsnotify.Remove && fsEvent.Op != fsnotify.Write {
break
}
// ConfigMaps work with symlinks. See:
// https://martensson.io/go-fsnotify-and-kubernetes-configmaps/
if fsEvent.Op == fsnotify.Remove {
watcher.Remove(fsEvent.Name)
watcher.Add(r.DefaultNamespaceLabelsPath)
}
reconcileEvents <- event.GenericEvent{}
case err := <-watcher.Errors:
r.Log.Error(err, "Error while watching config file", "path",
r.DefaultNamespaceLabelsPath)
}
}
}(watcher, events)
return ctrl.NewControllerManagedBy(mgr).
For(&profilev1.Profile{}).
Owns(&corev1.Namespace{}).
Owns(&istioSecurityClient.AuthorizationPolicy{}).
Owns(&corev1.ServiceAccount{}).
Owns(&rbacv1.RoleBinding{}).
Watches(
&source.Channel{Source: events},
&handler.EnqueueRequestsFromMapFunc{
ToRequests: &ProfileMapper{},
},
).
Complete(r)
}
func (r *ProfileReconciler) getAuthorizationPolicy(profileIns *profilev1.Profile) istioSecurity.AuthorizationPolicy {
return istioSecurity.AuthorizationPolicy{
Action: istioSecurity.AuthorizationPolicy_ALLOW,
// Empty selector == match all workloads in namespace
Selector: nil,
Rules: []*istioSecurity.Rule{
{
When: []*istioSecurity.Condition{
{
// Namespace Owner can access all workloads in the
// namespace
Key: fmt.Sprintf("request.headers[%v]", r.UserIdHeader),
Values: []string{
r.UserIdPrefix + profileIns.Spec.Owner.Name,
},
},
},
},
{
When: []*istioSecurity.Condition{
{
// Workloads in the same namespace can access all other
// workloads in the namespace
Key: fmt.Sprintf("source.namespace"),
Values: []string{profileIns.Name},
},
},
},
{
To: []*istioSecurity.Rule_To{
{
Operation: &istioSecurity.Operation{
// Workloads pathes should be accessible for KNative's
// `activator` and `controller` probes
// See: https://knative.dev/docs/serving/istio-authorization/#allowing-access-from-system-pods-by-paths
Paths: []string{
"/healthz",
"/metrics",
"/wait-for-drain",
},
},
},
},
},
{
// allow the notebook-controller in the kubeflow namespace to access the api/status endpoint of the notebook servers.
From: []*istioSecurity.Rule_From{
{
Source: &istioSecurity.Source{
Principals: []string{"cluster.local/ns/kubeflow/sa/notebook-controller-service-account"},
},
},
},
To: []*istioSecurity.Rule_To{
{
Operation: &istioSecurity.Operation{
Methods: []string{"GET"},
Paths: []string{"*/api/status"}, // wildcard for the name of the notebook server
},
},
},
},
},
}
}
// updateIstioAuthorizationPolicy create or update Istio AuthorizationPolicy
// resources in target namespace owned by "profileIns". The goal is to allow
// service access for profile owner.
func (r *ProfileReconciler) updateIstioAuthorizationPolicy(profileIns *profilev1.Profile) error {
logger := r.Log.WithValues("profile", profileIns.Name)
istioAuth := &istioSecurityClient.AuthorizationPolicy{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{USER: profileIns.Spec.Owner.Name, ROLE: ADMIN},
Name: AUTHZPOLICYISTIO,
Namespace: profileIns.Name,
},
Spec: r.getAuthorizationPolicy(profileIns),
}
if err := controllerutil.SetControllerReference(profileIns, istioAuth, r.Scheme); err != nil {
return err
}
foundAuthorizationPolicy := &istioSecurityClient.AuthorizationPolicy{}
err := r.Get(
context.TODO(),
types.NamespacedName{
Name: istioAuth.ObjectMeta.Name,
Namespace: istioAuth.ObjectMeta.Namespace,
},
foundAuthorizationPolicy,
)
if err != nil {
if apierrors.IsNotFound(err) {
logger.Info("Creating Istio AuthorizationPolicy", "namespace", istioAuth.ObjectMeta.Namespace,
"name", istioAuth.ObjectMeta.Name)
err = r.Create(context.TODO(), istioAuth)
if err != nil {
return err
}
} else {
return err
}
} else {
if !reflect.DeepEqual(istioAuth.Spec, foundAuthorizationPolicy.Spec) {
foundAuthorizationPolicy.Spec = istioAuth.Spec
logger.Info("Updating Istio AuthorizationPolicy", "namespace", istioAuth.ObjectMeta.Namespace,
"name", istioAuth.ObjectMeta.Name)
err = r.Update(context.TODO(), foundAuthorizationPolicy)
if err != nil {
return err
}
}
}
return nil
}
// updateResourceQuota create or update ResourceQuota for target namespace
func (r *ProfileReconciler) updateResourceQuota(profileIns *profilev1.Profile,
resourceQuota *corev1.ResourceQuota) error {
ctx := context.Background()
logger := r.Log.WithValues("profile", profileIns.Name)
if err := controllerutil.SetControllerReference(profileIns, resourceQuota, r.Scheme); err != nil {
return err
}
found := &corev1.ResourceQuota{}
err := r.Get(ctx, types.NamespacedName{Name: resourceQuota.Name, Namespace: resourceQuota.Namespace}, found)
if err != nil {
if apierrors.IsNotFound(err) {
logger.Info("Creating ResourceQuota", "namespace", resourceQuota.Namespace, "name", resourceQuota.Name)
err = r.Create(ctx, resourceQuota)
if err != nil {
return err
}
} else {
return err
}
} else {
if !(reflect.DeepEqual(resourceQuota.Spec, found.Spec)) {
found.Spec = resourceQuota.Spec
logger.Info("Updating ResourceQuota", "namespace", resourceQuota.Namespace, "name", resourceQuota.Name)
err = r.Update(ctx, found)
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 *ProfileReconciler) updateServiceAccount(profileIns *profilev1.Profile, saName string,
ClusterRoleName string) error {
logger := r.Log.WithValues("profile", profileIns.Name)
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 apierrors.IsNotFound(err) {
logger.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 *ProfileReconciler) updateRoleBinding(profileIns *profilev1.Profile,
roleBinding *rbacv1.RoleBinding) error {
logger := r.Log.WithValues("profile", profileIns.Name)
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 apierrors.IsNotFound(err) {
logger.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
logger.Info("Updating RoleBinding", "namespace", roleBinding.Namespace, "name", roleBinding.Name)
err = r.Update(context.TODO(), found)
if err != nil {
return err
}
}
}
return nil
}
// GetPluginSpec will try to unmarshal the plugin spec inside profile for the specified plugin
// Returns an error if the plugin isn't defined or if there is a problem
func (r *ProfileReconciler) GetPluginSpec(profileIns *profilev1.Profile) ([]Plugin, error) {
logger := r.Log.WithValues("profile", profileIns.Name)
plugins := []Plugin{}
for _, p := range profileIns.Spec.Plugins {
var pluginIns Plugin
switch p.Kind {
case KIND_WORKLOAD_IDENTITY:
pluginIns = &GcpWorkloadIdentity{}
case KIND_AWS_IAM_FOR_SERVICE_ACCOUNT:
pluginIns = &AwsIAMForServiceAccount{}
default:
logger.Info("Plugin not recgonized: ", "Kind", p.Kind)
continue
}
// To deserialize it to a specific type we need to first serialize it to bytes
// and then unserialize it.
specBytes, err := yaml.Marshal(p.Spec)
if err != nil {
logger.Info("Could not marshal plugin ", p.Kind, "; error: ", err)
return nil, err
}
err = yaml.Unmarshal(specBytes, pluginIns)
if err != nil {
logger.Info("Could not unmarshal plugin ", p.Kind, "; error: ", err)
return nil, err
}
plugins = append(plugins, pluginIns)
}
return plugins, nil
}
// PatchDefaultPluginSpec patch default plugins to profile CR instance if user doesn't specify plugin of same kind in CR.
func (r *ProfileReconciler) PatchDefaultPluginSpec(ctx context.Context, profileIns *profilev1.Profile) error {
// read existing plugins into map
plugins := make(map[string]profilev1.Plugin)
for _, p := range profileIns.Spec.Plugins {
plugins[p.Kind] = p
}
// Patch default plugins if same kind doesn't exist yet.
if r.WorkloadIdentity != "" {
if _, ok := plugins[KIND_WORKLOAD_IDENTITY]; !ok {
profileIns.Spec.Plugins = append(profileIns.Spec.Plugins, profilev1.Plugin{
TypeMeta: metav1.TypeMeta{
Kind: KIND_WORKLOAD_IDENTITY,
},
Spec: &runtime.RawExtension{
Raw: []byte(fmt.Sprintf(`{"gcpServiceAccount": "%v"}`, r.WorkloadIdentity)),
},
})
}
}
if err := r.Update(ctx, profileIns); err != nil {
return err
}
return nil
}
func containsString(slice []string, s string) bool {
for _, item := range slice {
if item == s {
return true
}
}
return false
}
func removeString(slice []string, s string) (result []string) {
for _, item := range slice {
if item == s {
continue
}
result = append(result, item)
}
return
}
func setNamespaceLabels(ns *corev1.Namespace, newLabels map[string]string) {
if ns.Labels == nil {
ns.Labels = make(map[string]string)
}
for k, v := range newLabels {
_, ok := ns.Labels[k]
if len(v) == 0 {
// When there is an empty value, k should be removed.
if ok {
delete(ns.Labels, k)
}
} else {
if !ok {
// Add label if not exist, otherwise skipping update.
ns.Labels[k] = v
}
}
}
}
func (r *ProfileReconciler) readDefaultLabelsFromFile(path string) map[string]string {
logger := r.Log.WithName("read-config-file").WithValues("path", path)
dat, err := ioutil.ReadFile(path)
if err != nil {
logger.Error(err, "namespace labels properties file doesn't exist")
os.Exit(1)
}
labels := map[string]string{}
err = yaml.Unmarshal(dat, &labels)
if err != nil {
logger.Error(err, "Unable to parse default namespace labels.")
os.Exit(1)
}
return labels
}