forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreconciler.go
More file actions
438 lines (367 loc) · 16.6 KB
/
reconciler.go
File metadata and controls
438 lines (367 loc) · 16.6 KB
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
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package stale
import (
"context"
"fmt"
"strconv"
"time"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/clock"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
controllermanagerconfigv1alpha1 "github.com/gardener/gardener/pkg/apis/config/controllermanager/v1alpha1"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
securityv1alpha1 "github.com/gardener/gardener/pkg/apis/security/v1alpha1"
gardenerutils "github.com/gardener/gardener/pkg/utils/gardener"
kubernetesutils "github.com/gardener/gardener/pkg/utils/kubernetes"
)
// Reconciler reconciles Projects, marks them as stale and auto-deletes them after a certain time if not in-use.
type Reconciler struct {
Client client.Client
Config controllermanagerconfigv1alpha1.ProjectControllerConfiguration
Clock clock.Clock
}
// Reconcile reconciles Projects, marks them as stale and auto-deletes them after a certain time if not in-use.
func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
log := logf.FromContext(ctx)
project := &gardencorev1beta1.Project{}
if err := r.Client.Get(ctx, request.NamespacedName, project); err != nil {
if apierrors.IsNotFound(err) {
log.V(1).Info("Object is gone, stop reconciling")
return reconcile.Result{}, nil
}
return reconcile.Result{}, fmt.Errorf("error retrieving object from store: %w", err)
}
if err := r.reconcile(ctx, log, project); err != nil {
return reconcile.Result{}, err
}
return reconcile.Result{RequeueAfter: r.Config.StaleSyncPeriod.Duration}, nil
}
func (r *Reconciler) reconcile(ctx context.Context, log logr.Logger, project *gardencorev1beta1.Project) error {
if project.DeletionTimestamp != nil || project.Spec.Namespace == nil {
return nil
}
// Skip projects whose namespace is annotated with the skip-stale-check annotation.
namespace := &corev1.Namespace{}
if err := r.Client.Get(ctx, client.ObjectKey{Name: *project.Spec.Namespace}, namespace); err != nil {
return err
}
log = log.WithValues("namespaceName", namespace.Name)
var skipStaleCheck bool
if value, ok := namespace.Annotations[v1beta1constants.ProjectSkipStaleCheck]; ok {
skipStaleCheck, _ = strconv.ParseBool(value)
}
if skipStaleCheck {
log.Info("Namespace is marked to skip the stale check, marking Project as not stale")
return r.markProjectAsNotStale(ctx, project)
}
// Skip projects that are not older than the configured minimum lifetime in days. This allows having Projects for a
// certain period of time until they are checked whether they got stale.
if project.CreationTimestamp.UTC().Add(time.Hour * 24 * time.Duration(*r.Config.MinimumLifetimeDays)).After(r.Clock.Now().UTC()) {
log.Info("Project is not older than the configured minimum lifetime, marking Project as not stale", "minimumLifetimeDays", *r.Config.MinimumLifetimeDays, "creationTimestamp", project.CreationTimestamp.UTC())
return r.markProjectAsNotStale(ctx, project)
}
// Skip projects that have been used recently
if project.Status.LastActivityTimestamp != nil && project.Status.LastActivityTimestamp.UTC().Add(time.Hour*24*time.Duration(*r.Config.MinimumLifetimeDays)).After(r.Clock.Now().UTC()) {
log.Info("Project was used recently and it is not exceeding the configured minimum lifetime, marking Project as not stale", "minimumLifetimeDays", *r.Config.MinimumLifetimeDays, "lastActivityTimestamp", project.Status.LastActivityTimestamp.UTC())
return r.markProjectAsNotStale(ctx, project)
}
for _, check := range []struct {
resource string
checkFunc func(context.Context, string) (bool, error)
}{
{"Shoots", r.projectInUseDueToShoots},
{"BackupEntries", r.projectInUseDueToBackupEntries},
{"Secrets", r.projectInUseDueToSecrets},
{"InternalSecrets", r.projectInUseDueToInternalSecrets},
{"WorkloadIdentities", r.projectInUseDueToWorkloadIdentities},
{"Quotas", r.projectInUseDueToQuotas},
} {
projectInUse, err := check.checkFunc(ctx, *project.Spec.Namespace)
if err != nil {
return err
}
if projectInUse {
log.Info("Project is in use by resource, marking Project as not stale", "resource", check.resource)
return r.markProjectAsNotStale(ctx, project)
}
}
log.Info("Project is not in use by any resource, marking Project as stale")
if err := r.markProjectAsStale(ctx, project); err != nil {
return err
}
log = log.WithValues("staleSinceTimestamp", (*project.Status.StaleSinceTimestamp).Time)
if project.Status.StaleAutoDeleteTimestamp != nil {
log = log.WithValues("staleAutoDeleteTimestamp", (*project.Status.StaleAutoDeleteTimestamp).Time)
}
if project.Status.StaleAutoDeleteTimestamp == nil || r.Clock.Now().UTC().Before(project.Status.StaleAutoDeleteTimestamp.UTC()) {
log.Info("Project is stale, but will not be deleted now")
return nil
}
log.Info("Deleting Project now because its auto-delete timestamp is exceeded")
if err := gardenerutils.ConfirmDeletion(ctx, r.Client, project); err != nil {
if apierrors.IsNotFound(err) {
log.Info("Project already gone")
return nil
}
return err
}
return client.IgnoreNotFound(r.Client.Delete(ctx, project))
}
func (r *Reconciler) projectInUseDueToShoots(ctx context.Context, namespace string) (bool, error) {
return kubernetesutils.ResourcesExist(ctx, r.Client, &gardencorev1beta1.ShootList{}, r.Client.Scheme(), client.InNamespace(namespace))
}
func (r *Reconciler) projectInUseDueToBackupEntries(ctx context.Context, namespace string) (bool, error) {
return kubernetesutils.ResourcesExist(ctx, r.Client, &gardencorev1beta1.BackupEntryList{}, r.Client.Scheme(), client.InNamespace(namespace))
}
func (r *Reconciler) projectInUseDueToWorkloadIdentities(ctx context.Context, namespace string) (bool, error) {
workloadIdentityList := &metav1.PartialObjectMetadataList{}
workloadIdentityList.SetGroupVersionKind(securityv1alpha1.SchemeGroupVersion.WithKind("WorkloadIdentityList"))
if err := r.Client.List(
ctx,
workloadIdentityList,
client.InNamespace(namespace),
client.MatchingLabels{v1beta1constants.LabelCredentialsBindingReference: "true"},
); err != nil {
return false, err
}
if len(workloadIdentityList.Items) == 0 {
return false, nil
}
workloadIdentityNames := make(sets.Set[string], len(workloadIdentityList.Items))
for _, workloadIdentity := range workloadIdentityList.Items {
workloadIdentityNames.Insert(workloadIdentity.Name)
}
return r.relevantCredentialsBindingsInUse(ctx, func(credentialsBinding securityv1alpha1.CredentialsBinding) bool {
return credentialsBinding.CredentialsRef.APIVersion == securityv1alpha1.SchemeGroupVersion.String() &&
credentialsBinding.CredentialsRef.Kind == "WorkloadIdentity" &&
credentialsBinding.CredentialsRef.Namespace == namespace &&
workloadIdentityNames.Has(credentialsBinding.CredentialsRef.Name)
})
}
func (r *Reconciler) projectInUseDueToInternalSecrets(ctx context.Context, namespace string) (bool, error) {
internalSecretList := &metav1.PartialObjectMetadataList{}
internalSecretList.SetGroupVersionKind(gardencorev1beta1.SchemeGroupVersion.WithKind("InternalSecretList"))
if err := r.Client.List(
ctx,
internalSecretList,
client.InNamespace(namespace),
client.MatchingLabels{v1beta1constants.LabelCredentialsBindingReference: "true"},
); err != nil {
return false, err
}
if len(internalSecretList.Items) == 0 {
return false, nil
}
internalSecretNames := make(sets.Set[string], len(internalSecretList.Items))
for _, internalSecret := range internalSecretList.Items {
internalSecretNames.Insert(internalSecret.Name)
}
return r.relevantCredentialsBindingsInUse(ctx, func(credentialsBinding securityv1alpha1.CredentialsBinding) bool {
return credentialsBinding.CredentialsRef.APIVersion == gardencorev1beta1.SchemeGroupVersion.String() &&
credentialsBinding.CredentialsRef.Kind == "InternalSecret" &&
credentialsBinding.CredentialsRef.Namespace == namespace &&
internalSecretNames.Has(credentialsBinding.CredentialsRef.Name)
})
}
func (r *Reconciler) projectInUseDueToSecrets(ctx context.Context, namespace string) (bool, error) {
getSecrets := func(matchKeyLabel string) (*metav1.PartialObjectMetadataList, error) {
secretList := &metav1.PartialObjectMetadataList{}
secretList.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("SecretList"))
err := r.Client.List(
ctx,
secretList,
client.InNamespace(namespace),
gardenerutils.UncontrolledSecretSelector,
client.MatchingLabels{matchKeyLabel: "true"},
)
return secretList, err
}
secretBindingRefSecretList, err := getSecrets(v1beta1constants.LabelSecretBindingReference)
if err != nil {
return false, err
}
secretBindingSecretNames := sets.New[string]()
for _, secret := range secretBindingRefSecretList.Items {
secretBindingSecretNames.Insert(secret.Name)
}
if secretBindingSecretNames.Len() > 0 {
usedDueToSecretBindings, err := r.relevantSecretBindingsInUse(ctx, func(secretBinding gardencorev1beta1.SecretBinding) bool {
return secretBinding.SecretRef.Namespace == namespace && secretBindingSecretNames.Has(secretBinding.SecretRef.Name)
})
if err != nil {
return false, err
}
// exit early if the project is already used because of secrets referenced through secret bindings
if usedDueToSecretBindings {
return usedDueToSecretBindings, nil
}
}
credentialsBindingRefSecretList, err := getSecrets(v1beta1constants.LabelCredentialsBindingReference)
if err != nil {
return false, err
}
if len(credentialsBindingRefSecretList.Items) == 0 {
return false, nil
}
credentialsBindingsSecretNames := make(sets.Set[string], len(credentialsBindingRefSecretList.Items))
for _, secret := range credentialsBindingRefSecretList.Items {
credentialsBindingsSecretNames.Insert(secret.Name)
}
return r.relevantCredentialsBindingsInUse(ctx, func(credentialsBinding securityv1alpha1.CredentialsBinding) bool {
return credentialsBinding.CredentialsRef.APIVersion == corev1.SchemeGroupVersion.String() &&
credentialsBinding.CredentialsRef.Kind == "Secret" &&
credentialsBinding.CredentialsRef.Namespace == namespace &&
credentialsBindingsSecretNames.Has(credentialsBinding.CredentialsRef.Name)
})
}
func (r *Reconciler) projectInUseDueToQuotas(ctx context.Context, namespace string) (bool, error) {
quotaList := &metav1.PartialObjectMetadataList{}
quotaList.SetGroupVersionKind(gardencorev1beta1.SchemeGroupVersion.WithKind("QuotaList"))
if err := r.Client.List(ctx, quotaList, client.InNamespace(namespace)); err != nil {
return false, err
}
quotaNames := computeQuotaNames(quotaList.Items)
if quotaNames.Len() == 0 {
return false, nil
}
usedDueToSecretBindings, err := r.relevantSecretBindingsInUse(ctx, func(secretBinding gardencorev1beta1.SecretBinding) bool {
for _, quota := range secretBinding.Quotas {
if quota.Namespace == namespace && quotaNames.Has(quota.Name) {
return true
}
}
return false
})
if err != nil {
return false, err
}
// exit early if project is already marked as not stale
if usedDueToSecretBindings {
return usedDueToSecretBindings, nil
}
return r.relevantCredentialsBindingsInUse(ctx, func(credentialsBinding securityv1alpha1.CredentialsBinding) bool {
for _, quota := range credentialsBinding.Quotas {
if quota.Namespace == namespace && quotaNames.Has(quota.Name) {
return true
}
}
return false
})
}
func (r *Reconciler) relevantSecretBindingsInUse(ctx context.Context, isSecretBindingRelevantFunc func(secretBinding gardencorev1beta1.SecretBinding) bool) (bool, error) {
secretBindingList := &gardencorev1beta1.SecretBindingList{}
if err := r.Client.List(ctx, secretBindingList); err != nil {
return false, err
}
namespaceToSecretBindingNames := make(map[string]sets.Set[string])
for _, secretBinding := range secretBindingList.Items {
if !isSecretBindingRelevantFunc(secretBinding) {
continue
}
if _, ok := namespaceToSecretBindingNames[secretBinding.Namespace]; !ok {
namespaceToSecretBindingNames[secretBinding.Namespace] = sets.New(secretBinding.Name)
} else {
namespaceToSecretBindingNames[secretBinding.Namespace].Insert(secretBinding.Name)
}
}
return r.secretBindingInUse(ctx, namespaceToSecretBindingNames)
}
func (r *Reconciler) relevantCredentialsBindingsInUse(ctx context.Context, isCredentialsBindingRelevantFunc func(securityv1alpha1.CredentialsBinding) bool) (bool, error) {
credentialsBindingList := &securityv1alpha1.CredentialsBindingList{}
if err := r.Client.List(ctx, credentialsBindingList); err != nil {
return false, err
}
namespaceToCredentialsBindingNames := make(map[string]sets.Set[string])
for _, credentialsBinding := range credentialsBindingList.Items {
if !isCredentialsBindingRelevantFunc(credentialsBinding) {
continue
}
if _, ok := namespaceToCredentialsBindingNames[credentialsBinding.Namespace]; !ok {
namespaceToCredentialsBindingNames[credentialsBinding.Namespace] = sets.New(credentialsBinding.Name)
} else {
namespaceToCredentialsBindingNames[credentialsBinding.Namespace].Insert(credentialsBinding.Name)
}
}
return r.credentialsBindingInUse(ctx, namespaceToCredentialsBindingNames)
}
func (r *Reconciler) markProjectAsNotStale(ctx context.Context, project *gardencorev1beta1.Project) error {
patch := client.MergeFrom(project.DeepCopy())
project.Status.StaleSinceTimestamp = nil
project.Status.StaleAutoDeleteTimestamp = nil
return r.Client.Status().Patch(ctx, project, patch)
}
func (r *Reconciler) markProjectAsStale(ctx context.Context, project *gardencorev1beta1.Project) error {
patch := client.MergeFrom(project.DeepCopy())
if project.Status.StaleSinceTimestamp == nil {
project.Status.StaleSinceTimestamp = &metav1.Time{Time: r.Clock.Now()}
}
if project.Status.StaleSinceTimestamp.UTC().Add(time.Hour * 24 * time.Duration(*r.Config.StaleGracePeriodDays)).After(r.Clock.Now().UTC()) {
// We reset the potentially set auto-delete timestamp here to allow changing the StaleExpirationTimeDays
// configuration value and correctly applying the changes to all Projects that had already been assigned
// such a timestamp.
project.Status.StaleAutoDeleteTimestamp = nil
} else {
// If the project got stale we compute an auto delete timestamp only if the configured stale grace period is
// exceeded. Note that this might update the potentially already set auto-delete timestamp in case the
// StaleExpirationTimeDays configuration value was changed.
autoDeleteTimestamp := metav1.Time{Time: project.Status.StaleSinceTimestamp.Add(time.Hour * 24 * time.Duration(*r.Config.StaleExpirationTimeDays))}
// Don't allow to shorten the auto-delete timestamp as end-users might depend on the configured time. It may
// only be extended.
if project.Status.StaleAutoDeleteTimestamp == nil || autoDeleteTimestamp.After(project.Status.StaleAutoDeleteTimestamp.Time) {
project.Status.StaleAutoDeleteTimestamp = &autoDeleteTimestamp
}
}
return r.Client.Status().Patch(ctx, project, patch)
}
func (r *Reconciler) secretBindingInUse(ctx context.Context, namespaceToSecretBindingNames map[string]sets.Set[string]) (bool, error) {
if len(namespaceToSecretBindingNames) == 0 {
return false, nil
}
for namespace, secretBindingNames := range namespaceToSecretBindingNames {
shootList := &gardencorev1beta1.ShootList{}
if err := r.Client.List(ctx, shootList, client.InNamespace(namespace)); err != nil {
return false, err
}
for _, shoot := range shootList.Items {
if secretBindingNames.Has(ptr.Deref(shoot.Spec.SecretBindingName, "")) {
return true, nil
}
}
}
return false, nil
}
func (r *Reconciler) credentialsBindingInUse(ctx context.Context, namespaceToCredentialsBindingNames map[string]sets.Set[string]) (bool, error) {
if len(namespaceToCredentialsBindingNames) == 0 {
return false, nil
}
for namespace, credentialsBindingNames := range namespaceToCredentialsBindingNames {
shootList := &gardencorev1beta1.ShootList{}
if err := r.Client.List(ctx, shootList, client.InNamespace(namespace)); err != nil {
return false, err
}
for _, shoot := range shootList.Items {
if credentialsBindingNames.Has(ptr.Deref(shoot.Spec.CredentialsBindingName, "")) {
return true, nil
}
}
}
return false, nil
}
// computeQuotaNames determines the names of Quotas from the given slice.
func computeQuotaNames(quotaList []metav1.PartialObjectMetadata) sets.Set[string] {
names := sets.New[string]()
for _, quota := range quotaList {
names.Insert(quota.Name)
}
return names
}