|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "go.uber.org/multierr" |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | + "k8s.io/apimachinery/pkg/api/resource" |
| 12 | + "k8s.io/apimachinery/pkg/runtime" |
| 13 | + "k8s.io/client-go/tools/record" |
| 14 | + ctrl "sigs.k8s.io/controller-runtime" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/builder" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 19 | +) |
| 20 | + |
| 21 | +// LegacyResourceQuotaReconciler reconciles namespaces and synchronizes their resource quotas |
| 22 | +type LegacyResourceQuotaReconciler struct { |
| 23 | + client.Client |
| 24 | + Scheme *runtime.Scheme |
| 25 | + Recorder record.EventRecorder |
| 26 | + |
| 27 | + OrganizationLabel string |
| 28 | + |
| 29 | + ResourceQuotaAnnotationBase string |
| 30 | + DefaultResourceQuotas map[string]corev1.ResourceQuotaSpec |
| 31 | + |
| 32 | + LimitRangeName string |
| 33 | + DefaultLimitRange corev1.LimitRangeSpec |
| 34 | +} |
| 35 | + |
| 36 | +func (r *LegacyResourceQuotaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 37 | + l := log.FromContext(ctx) |
| 38 | + l.Info("Reconciling Namespace") |
| 39 | + |
| 40 | + var ns corev1.Namespace |
| 41 | + if err := r.Get(ctx, req.NamespacedName, &ns); err != nil { |
| 42 | + return ctrl.Result{}, client.IgnoreNotFound(err) |
| 43 | + } |
| 44 | + if ns.DeletionTimestamp != nil { |
| 45 | + l.Info("Namespace is being deleted, skipping reconciliation") |
| 46 | + return ctrl.Result{}, nil |
| 47 | + } |
| 48 | + |
| 49 | + if _, ok := ns.Labels[r.OrganizationLabel]; !ok { |
| 50 | + l.Info("Namespace does not have organization label, skipping reconciliation") |
| 51 | + return ctrl.Result{}, nil |
| 52 | + } |
| 53 | + |
| 54 | + var errs []error |
| 55 | + for name, s := range r.DefaultResourceQuotas { |
| 56 | + spec := *s.DeepCopy() |
| 57 | + |
| 58 | + var storageQuotas corev1.ResourceList |
| 59 | + if sqa := ns.Annotations[fmt.Sprintf("%s/%s.storageclasses", r.ResourceQuotaAnnotationBase, name)]; sqa != "" { |
| 60 | + err := json.Unmarshal([]byte(ns.Annotations[fmt.Sprintf("%s/%s.storageclasses", r.ResourceQuotaAnnotationBase, name)]), &storageQuotas) |
| 61 | + if err != nil { |
| 62 | + errs = append(errs, fmt.Errorf("failed to unmarshal storage classes: %w", err)) |
| 63 | + storageQuotas = make(corev1.ResourceList) |
| 64 | + } |
| 65 | + } else { |
| 66 | + storageQuotas = make(corev1.ResourceList) |
| 67 | + } |
| 68 | + |
| 69 | + rq := &corev1.ResourceQuota{ |
| 70 | + ObjectMeta: ctrl.ObjectMeta{ |
| 71 | + Name: name, |
| 72 | + Namespace: ns.Name, |
| 73 | + }, |
| 74 | + } |
| 75 | + op, err := controllerutil.CreateOrUpdate(ctx, r.Client, rq, func() error { |
| 76 | + for k := range spec.Hard { |
| 77 | + an := fmt.Sprintf("%s/%s.%s", r.ResourceQuotaAnnotationBase, name, strings.ReplaceAll(string(k), "/", "_")) |
| 78 | + if strings.Contains(string(k), "storageclass.storage.k8s.io") { |
| 79 | + if _, ok := storageQuotas[k]; ok { |
| 80 | + spec.Hard[k] = storageQuotas[k] |
| 81 | + } |
| 82 | + } else if a := ns.Annotations[an]; a != "" { |
| 83 | + po, err := resource.ParseQuantity(a) |
| 84 | + if err != nil { |
| 85 | + errs = append(errs, fmt.Errorf("failed to parse quantity %s=%s: %w", an, a, err)) |
| 86 | + continue |
| 87 | + } |
| 88 | + spec.Hard[k] = po |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + rq.Spec = spec |
| 93 | + return controllerutil.SetControllerReference(&ns, rq, r.Scheme) |
| 94 | + }) |
| 95 | + if err != nil { |
| 96 | + errs = append(errs, fmt.Errorf("failed to reconcile ResourceQuota %s: %w", name, err)) |
| 97 | + } |
| 98 | + if op != controllerutil.OperationResultNone { |
| 99 | + l.Info("Reconciled ResourceQuota", "name", name, "operation", op) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + lr := &corev1.LimitRange{ |
| 104 | + ObjectMeta: ctrl.ObjectMeta{ |
| 105 | + Name: r.LimitRangeName, |
| 106 | + Namespace: ns.Name, |
| 107 | + }, |
| 108 | + } |
| 109 | + op, err := controllerutil.CreateOrUpdate(ctx, r.Client, lr, func() error { |
| 110 | + lr.Spec = *r.DefaultLimitRange.DeepCopy() |
| 111 | + return controllerutil.SetControllerReference(&ns, lr, r.Scheme) |
| 112 | + }) |
| 113 | + if err != nil { |
| 114 | + errs = append(errs, fmt.Errorf("failed to reconcile LimitRange %s: %w", r.LimitRangeName, err)) |
| 115 | + } |
| 116 | + if op != controllerutil.OperationResultNone { |
| 117 | + l.Info("Reconciled LimitRange", "name", r.LimitRangeName, "operation", op) |
| 118 | + } |
| 119 | + |
| 120 | + if err := multierr.Combine(errs...); err != nil { |
| 121 | + r.Recorder.Eventf(&ns, corev1.EventTypeWarning, "ReconcileError", "Failed to reconcile ResourceQuotas and LimitRanges: %s", err.Error()) |
| 122 | + return ctrl.Result{}, fmt.Errorf("failed to reconcile ResourceQuotas and LimitRanges: %w", err) |
| 123 | + } |
| 124 | + |
| 125 | + return ctrl.Result{}, nil |
| 126 | +} |
| 127 | + |
| 128 | +// SetupWithManager sets up the controller with the Manager. |
| 129 | +func (r *LegacyResourceQuotaReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 130 | + orgPredicate, err := labelExistsPredicate(r.OrganizationLabel) |
| 131 | + if err != nil { |
| 132 | + return fmt.Errorf("failed to create organization label predicate: %w", err) |
| 133 | + } |
| 134 | + return ctrl.NewControllerManagedBy(mgr). |
| 135 | + For(&corev1.Namespace{}, builder.WithPredicates(orgPredicate)). |
| 136 | + Owns(&corev1.ResourceQuota{}). |
| 137 | + Owns(&corev1.LimitRange{}). |
| 138 | + Complete(r) |
| 139 | +} |
0 commit comments