|
| 1 | +/* |
| 2 | +Copyright 2020 Clastix Labs. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package service_labels |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/go-logr/logr" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + "k8s.io/apimachinery/pkg/fields" |
| 26 | + "k8s.io/apimachinery/pkg/runtime" |
| 27 | + "k8s.io/apimachinery/pkg/types" |
| 28 | + ctrl "sigs.k8s.io/controller-runtime" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/builder" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 34 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 35 | + |
| 36 | + "github.com/clastix/capsule/api/v1alpha1" |
| 37 | +) |
| 38 | + |
| 39 | +type abstractServiceLabelsReconciler struct { |
| 40 | + obj client.Object |
| 41 | + client client.Client |
| 42 | + log logr.Logger |
| 43 | + scheme *runtime.Scheme |
| 44 | +} |
| 45 | + |
| 46 | +func (r *abstractServiceLabelsReconciler) InjectClient(c client.Client) error { |
| 47 | + r.client = c |
| 48 | + return nil |
| 49 | +} |
| 50 | + |
| 51 | +func (r *abstractServiceLabelsReconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) { |
| 52 | + tenant, err := r.getTenant(ctx, request.NamespacedName, r.client) |
| 53 | + if err != nil { |
| 54 | + switch err.(type) { |
| 55 | + case *NonTenantObject, *NoServicesMetadata: |
| 56 | + return reconcile.Result{}, nil |
| 57 | + default: |
| 58 | + r.log.Error(err, fmt.Sprintf("Cannot sync %t labels", r.obj)) |
| 59 | + return reconcile.Result{}, err |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + err = r.client.Get(ctx, request.NamespacedName, r.obj) |
| 64 | + if err != nil { |
| 65 | + return reconcile.Result{}, err |
| 66 | + } |
| 67 | + |
| 68 | + _, err = controllerutil.CreateOrUpdate(ctx, r.client, r.obj, func() (err error) { |
| 69 | + r.obj.SetLabels(r.sync(r.obj.GetLabels(), tenant.Spec.ServicesMetadata.AdditionalLabels)) |
| 70 | + r.obj.SetAnnotations(r.sync(r.obj.GetAnnotations(), tenant.Spec.ServicesMetadata.AdditionalAnnotations)) |
| 71 | + return nil |
| 72 | + }) |
| 73 | + |
| 74 | + return reconcile.Result{}, err |
| 75 | +} |
| 76 | + |
| 77 | +func (r *abstractServiceLabelsReconciler) getTenant(ctx context.Context, namespacedName types.NamespacedName, client client.Client) (*v1alpha1.Tenant, error) { |
| 78 | + ns := &corev1.Namespace{} |
| 79 | + tenant := &v1alpha1.Tenant{} |
| 80 | + |
| 81 | + if err := client.Get(ctx, types.NamespacedName{Name: namespacedName.Namespace}, ns); err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + capsuleLabel, _ := v1alpha1.GetTypeLabel(&v1alpha1.Tenant{}) |
| 86 | + if _, ok := ns.GetLabels()[capsuleLabel]; !ok { |
| 87 | + return nil, NewNonTenantObject(namespacedName.Name) |
| 88 | + } |
| 89 | + |
| 90 | + if err := client.Get(ctx, types.NamespacedName{Name: ns.Labels[capsuleLabel]}, tenant); err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + |
| 94 | + if tenant.Spec.ServicesMetadata.AdditionalLabels == nil && tenant.Spec.ServicesMetadata.AdditionalAnnotations == nil { |
| 95 | + return nil, NewNoServicesMetadata(namespacedName.Name) |
| 96 | + } |
| 97 | + |
| 98 | + return tenant, nil |
| 99 | +} |
| 100 | + |
| 101 | +func (r *abstractServiceLabelsReconciler) sync(available map[string]string, tenantSpec map[string]string) map[string]string { |
| 102 | + if tenantSpec != nil { |
| 103 | + if available == nil { |
| 104 | + available = tenantSpec |
| 105 | + } else { |
| 106 | + for key, value := range tenantSpec { |
| 107 | + if available[key] != value { |
| 108 | + available[key] = value |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + return available |
| 114 | +} |
| 115 | + |
| 116 | +func (r *abstractServiceLabelsReconciler) forOptionPerInstanceName() builder.ForOption { |
| 117 | + return builder.WithPredicates(predicate.Funcs{ |
| 118 | + CreateFunc: func(event event.CreateEvent) bool { |
| 119 | + return r.IsNamespaceInTenant(event.Object.GetNamespace()) |
| 120 | + }, |
| 121 | + DeleteFunc: func(deleteEvent event.DeleteEvent) bool { |
| 122 | + return r.IsNamespaceInTenant(deleteEvent.Object.GetNamespace()) |
| 123 | + }, |
| 124 | + UpdateFunc: func(updateEvent event.UpdateEvent) bool { |
| 125 | + return r.IsNamespaceInTenant(updateEvent.ObjectNew.GetNamespace()) |
| 126 | + }, |
| 127 | + GenericFunc: func(genericEvent event.GenericEvent) bool { |
| 128 | + return r.IsNamespaceInTenant(genericEvent.Object.GetNamespace()) |
| 129 | + }, |
| 130 | + }) |
| 131 | +} |
| 132 | + |
| 133 | +func (r *abstractServiceLabelsReconciler) IsNamespaceInTenant(namespace string) bool { |
| 134 | + tl := &v1alpha1.TenantList{} |
| 135 | + if err := r.client.List(context.Background(), tl, client.MatchingFieldsSelector{ |
| 136 | + Selector: fields.OneTermEqualSelector(".status.namespaces", namespace), |
| 137 | + }); err != nil { |
| 138 | + return false |
| 139 | + } |
| 140 | + return len(tl.Items) > 0 |
| 141 | +} |
0 commit comments