/
resourcequota_controller.go
245 lines (206 loc) · 8.57 KB
/
resourcequota_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
// SPDX-FileCopyrightText: 2024 Axel Christ and Spheric contributors
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0
package core
import (
"context"
"fmt"
"github.com/go-logr/logr"
"github.com/ironcore-dev/controller-utils/metautils"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
corev1alpha1 "spheric.cloud/spheric/api/core/v1alpha1"
sphericclient "spheric.cloud/spheric/utils/client"
"spheric.cloud/spheric/utils/quota"
)
type ResourceQuotaReconciler struct {
client.Client
APIReader client.Reader
Scheme *runtime.Scheme
Registry quota.Registry
}
//+kubebuilder:rbac:groups=core.spheric.cloud,resources=resourcequotas,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core.spheric.cloud,resources=resourcequotas/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=core.spheric.cloud,resources=resourcequotas/finalizers,verbs=update
//+kubebuilder:rbac:groups="",resources=namespaces,verbs=get;list;watch;update;patch
//+kubebuilder:rbac:groups=compute.spheric.cloud,resources=machines,verbs=get;list;watch
//+kubebuilder:rbac:groups=compute.spheric.cloud,resources=machineclasses,verbs=get;list;watch
//+kubebuilder:rbac:groups=storage.spheric.cloud,resources=volumes,verbs=get;list;watch
//+kubebuilder:rbac:groups=storage.spheric.cloud,resources=volumeclasses,verbs=get;list;watch
func (r *ResourceQuotaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := ctrl.LoggerFrom(ctx)
resourceQuota := &corev1alpha1.ResourceQuota{}
if err := r.Get(ctx, req.NamespacedName, resourceQuota); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
return r.reconcileExists(ctx, log, resourceQuota)
}
func (r *ResourceQuotaReconciler) reconcileExists(ctx context.Context, log logr.Logger, resourceQuota *corev1alpha1.ResourceQuota) (ctrl.Result, error) {
if !resourceQuota.DeletionTimestamp.IsZero() {
return r.delete(ctx, log, resourceQuota)
}
return r.reconcile(ctx, log, resourceQuota)
}
func (r *ResourceQuotaReconciler) delete(ctx context.Context, log logr.Logger, resourceQuota *corev1alpha1.ResourceQuota) (ctrl.Result, error) {
return ctrl.Result{}, nil
}
func (r *ResourceQuotaReconciler) reconcile(ctx context.Context, log logr.Logger, resourceQuota *corev1alpha1.ResourceQuota) (ctrl.Result, error) {
log.V(1).Info("Reconcile")
log.V(1).Info("Gathering matching evaluators")
matchingEvaluators, coveredResourceNames := r.getMatchingEvaluators(resourceQuota)
log.V(1).Info("Calculating resource usage")
used, err := r.calculateUsage(ctx, log, resourceQuota, matchingEvaluators, coveredResourceNames)
if err != nil {
return ctrl.Result{}, fmt.Errorf("error calculating usage: %w", err)
}
log.V(1).Info("Updating resource quota status", "Used", used)
if err := r.updateStatus(ctx, resourceQuota, resourceQuota.Spec.Hard, used); err != nil {
return ctrl.Result{}, fmt.Errorf("error updating resource quota status: %w", err)
}
log.V(1).Info("Reconciled")
return ctrl.Result{}, nil
}
func (r *ResourceQuotaReconciler) getMatchingEvaluators(resourceQuota *corev1alpha1.ResourceQuota) ([]quota.Evaluator, sets.Set[corev1alpha1.ResourceName]) {
var (
evaluators []quota.Evaluator
hardResourceNames = quota.ResourceNames(resourceQuota.Spec.Hard)
coveredResourceNames = sets.New[corev1alpha1.ResourceName]()
)
for _, evaluator := range r.Registry.List() {
var matches bool
for resourceName := range hardResourceNames {
if evaluator.MatchesResourceName(resourceName) {
matches = true
coveredResourceNames.Insert(resourceName)
}
}
if matches {
evaluators = append(evaluators, evaluator)
}
}
return evaluators, coveredResourceNames
}
func (r *ResourceQuotaReconciler) calculateUsage(
ctx context.Context,
log logr.Logger,
resourceQuota *corev1alpha1.ResourceQuota,
evaluators []quota.Evaluator,
coveredResourceNames sets.Set[corev1alpha1.ResourceName],
) (corev1alpha1.ResourceList, error) {
usage := make(corev1alpha1.ResourceList, len(coveredResourceNames))
zero := resource.MustParse("0")
for resourceName := range coveredResourceNames {
usage[resourceName] = zero
}
for _, evaluator := range evaluators {
gvk, list, err := metautils.NewListForObject(r.Scheme, evaluator.Type())
if err != nil {
return nil, fmt.Errorf("error creating list for type %T: %w", evaluator.Type, err)
}
// We use the APIReader to list here since using the cached reader might cause objects created slightly before
// a new resource quota to be missed.
// TODO: Re-evaluate how we can avoid APIReader here.
log := log.WithValues("GVK", gvk)
log.V(1).Info("Listing resources")
if err := r.APIReader.List(ctx, list, client.InNamespace(resourceQuota.Namespace)); err != nil {
return nil, fmt.Errorf("[%s] error listing objects: %w", gvk, err)
}
log.V(1).Info("Listed resources", "NoOfResources", meta.LenList(list))
log.V(1).Info("Calculating usage for type")
typeUsage := corev1alpha1.ResourceList{}
if err := metautils.EachListItem(list, func(obj client.Object) error {
matches, err := quota.EvaluatorMatchesResourceScopeSelector(evaluator, obj, resourceQuota.Spec.ScopeSelector)
if err != nil {
return fmt.Errorf("error matching resource scope selector: %w", err)
}
if !matches {
return nil
}
itemUsage, err := evaluator.Usage(ctx, obj)
if err != nil {
return fmt.Errorf("error computing usage: %w", err)
}
typeUsage = quota.Add(typeUsage, itemUsage)
return nil
}); err != nil {
return nil, fmt.Errorf("[%s] error iterating list: %w", gvk, err)
}
log.V(1).Info("Calculated type usage", "TypeUsage", typeUsage)
usage = quota.Add(usage, typeUsage)
}
usage = quota.Mask(usage, coveredResourceNames)
return usage, nil
}
func (r *ResourceQuotaReconciler) updateStatus(
ctx context.Context,
resourceQuota *corev1alpha1.ResourceQuota,
hard, used corev1alpha1.ResourceList,
) error {
base := resourceQuota.DeepCopy()
resourceQuota.Status.Hard = hard
resourceQuota.Status.Used = used
if err := r.Status().Patch(ctx, resourceQuota, client.MergeFrom(base)); err != nil {
return fmt.Errorf("error patching resource quota status: %w", err)
}
return nil
}
func (r *ResourceQuotaReconciler) enqueueResourceQuotasByNamespace() handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, obj client.Object) []ctrl.Request {
namespace := obj.(*corev1.Namespace)
log := ctrl.LoggerFrom(ctx, "Namespace", namespace.Name)
resourceQuotaList := &corev1alpha1.ResourceQuotaList{}
if err := r.List(ctx, resourceQuotaList,
client.InNamespace(namespace.Name),
); err != nil {
log.Error(err, "Error listing resource quotas in namespace")
return nil
}
// Clean up replenishment annotation to mark it as 'received'
go func() {
log.V(1).Info("Removing replenish resource quota annotation from namespace")
if _, err := PatchRemoveReplenishResourceQuotaAnnotation(ctx, r.Client, namespace); err != nil {
log.Error(err, "Error removing replenish resource quota annotation from namespace")
}
}()
return sphericclient.ReconcileRequestsFromObjectStructSlice(resourceQuotaList.Items)
})
}
var resourceQuotaDirtyPredicate = predicate.NewPredicateFuncs(func(obj client.Object) bool {
resourceQuota := obj.(*corev1alpha1.ResourceQuota)
// If we did not calculate any usage yet (i.e. hard being unset), we need to recalc.
if resourceQuota.Status.Hard == nil {
return true
}
// If the spec enforced quota does not match with the status enforced quota, we also need to recalc.
if !quota.Equals(resourceQuota.Spec.Hard, resourceQuota.Status.Hard) {
return true
}
// If we're deleting, we have to enqueue.
if !resourceQuota.DeletionTimestamp.IsZero() {
return true
}
// Ignore any other case. We're triggered by watching namespaces when resource changes occur.
return false
})
func (r *ResourceQuotaReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(
&corev1alpha1.ResourceQuota{},
builder.WithPredicates(resourceQuotaDirtyPredicate),
).
Watches(
&corev1.Namespace{},
r.enqueueResourceQuotasByNamespace(),
builder.WithPredicates(HasReplenishResourceQuotaPredicate),
).
Complete(r)
}