2020-12-11 01:34:05 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package supervisorstorage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2020-12-11 23:21:34 +00:00
|
|
|
v1 "k8s.io/api/core/v1"
|
2020-12-11 01:34:05 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2020-12-11 23:21:34 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/clock"
|
2020-12-11 01:34:05 +00:00
|
|
|
corev1informers "k8s.io/client-go/informers/core/v1"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
|
|
|
|
pinnipedcontroller "go.pinniped.dev/internal/controller"
|
|
|
|
"go.pinniped.dev/internal/controllerlib"
|
|
|
|
"go.pinniped.dev/internal/crud"
|
|
|
|
"go.pinniped.dev/internal/plog"
|
|
|
|
)
|
|
|
|
|
2020-12-11 23:21:34 +00:00
|
|
|
const minimumRepeatInterval = 30 * time.Second
|
|
|
|
|
2020-12-11 01:34:05 +00:00
|
|
|
type garbageCollectorController struct {
|
2020-12-11 23:21:34 +00:00
|
|
|
secretInformer corev1informers.SecretInformer
|
|
|
|
kubeClient kubernetes.Interface
|
|
|
|
clock clock.Clock
|
|
|
|
timeOfMostRecentSweep time.Time
|
2020-12-11 01:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GarbageCollectorController(
|
2020-12-11 23:21:34 +00:00
|
|
|
clock clock.Clock,
|
2020-12-11 01:34:05 +00:00
|
|
|
kubeClient kubernetes.Interface,
|
|
|
|
secretInformer corev1informers.SecretInformer,
|
|
|
|
withInformer pinnipedcontroller.WithInformerOptionFunc,
|
|
|
|
) controllerlib.Controller {
|
2020-12-18 17:36:28 +00:00
|
|
|
isSecretWithGCAnnotation := func(obj metav1.Object) bool {
|
|
|
|
secret, ok := obj.(*v1.Secret)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, ok = secret.Annotations[crud.SecretLifetimeAnnotationKey]
|
|
|
|
return ok
|
|
|
|
}
|
2020-12-11 01:34:05 +00:00
|
|
|
return controllerlib.New(
|
|
|
|
controllerlib.Config{
|
|
|
|
Name: "garbage-collector-controller",
|
|
|
|
Syncer: &garbageCollectorController{
|
|
|
|
secretInformer: secretInformer,
|
|
|
|
kubeClient: kubeClient,
|
2020-12-11 23:21:34 +00:00
|
|
|
clock: clock,
|
2020-12-11 01:34:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
withInformer(
|
|
|
|
secretInformer,
|
2020-12-18 17:36:28 +00:00
|
|
|
controllerlib.FilterFuncs{
|
|
|
|
AddFunc: isSecretWithGCAnnotation,
|
|
|
|
UpdateFunc: func(oldObj, newObj metav1.Object) bool {
|
|
|
|
return isSecretWithGCAnnotation(oldObj) || isSecretWithGCAnnotation(newObj)
|
|
|
|
},
|
|
|
|
DeleteFunc: func(obj metav1.Object) bool { return false }, // ignore all deletes
|
2021-05-04 16:38:47 +00:00
|
|
|
ParentFunc: pinnipedcontroller.SingletonQueue(),
|
2020-12-18 17:36:28 +00:00
|
|
|
},
|
2020-12-11 01:34:05 +00:00
|
|
|
controllerlib.InformerOption{},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *garbageCollectorController) Sync(ctx controllerlib.Context) error {
|
2021-05-04 16:38:47 +00:00
|
|
|
// make sure we have a consistent, static meaning for the current time during the sync loop
|
|
|
|
frozenClock := clock.NewFakeClock(c.clock.Now())
|
|
|
|
|
2020-12-11 23:21:34 +00:00
|
|
|
// The Sync method is triggered upon any change to any Secret, which would make this
|
|
|
|
// controller too chatty, so it rate limits itself to a more reasonable interval.
|
|
|
|
// Note that even during a period when no secrets are changing, it will still run
|
|
|
|
// at the informer's full-resync interval (as long as there are some secrets).
|
2021-05-04 16:38:47 +00:00
|
|
|
if since := frozenClock.Since(c.timeOfMostRecentSweep); since < minimumRepeatInterval {
|
|
|
|
ctx.Queue.AddAfter(ctx.Key, minimumRepeatInterval-since)
|
2020-12-11 23:21:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
plog.Info("starting storage garbage collection sweep")
|
2021-05-04 16:38:47 +00:00
|
|
|
c.timeOfMostRecentSweep = frozenClock.Now()
|
2020-12-11 23:21:34 +00:00
|
|
|
|
2020-12-11 01:34:05 +00:00
|
|
|
listOfSecrets, err := c.secretInformer.Lister().List(labels.Everything())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-11 23:21:34 +00:00
|
|
|
|
2020-12-11 01:34:05 +00:00
|
|
|
for i := range listOfSecrets {
|
|
|
|
secret := listOfSecrets[i]
|
2020-12-11 23:21:34 +00:00
|
|
|
|
|
|
|
timeString, ok := secret.Annotations[crud.SecretLifetimeAnnotationKey]
|
2020-12-11 01:34:05 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-11 23:21:34 +00:00
|
|
|
|
|
|
|
garbageCollectAfterTime, err := time.Parse(crud.SecretLifetimeAnnotationDateFormat, timeString)
|
2020-12-11 01:34:05 +00:00
|
|
|
if err != nil {
|
2020-12-11 23:21:34 +00:00
|
|
|
plog.WarningErr("could not parse resource timestamp for garbage collection", err, logKV(secret))
|
2020-12-11 01:34:05 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-12-11 23:21:34 +00:00
|
|
|
|
2021-05-04 16:38:47 +00:00
|
|
|
if garbageCollectAfterTime.Before(frozenClock.Now()) {
|
2020-12-11 01:34:05 +00:00
|
|
|
err = c.kubeClient.CoreV1().Secrets(secret.Namespace).Delete(ctx.Context, secret.Name, metav1.DeleteOptions{})
|
|
|
|
if err != nil {
|
2020-12-11 23:21:34 +00:00
|
|
|
plog.WarningErr("failed to garbage collect resource", err, logKV(secret))
|
|
|
|
continue
|
2020-12-11 01:34:05 +00:00
|
|
|
}
|
2020-12-11 23:21:34 +00:00
|
|
|
plog.Info("storage garbage collector deleted resource", logKV(secret))
|
2020-12-11 01:34:05 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-11 23:21:34 +00:00
|
|
|
|
2020-12-11 01:34:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-12-11 23:21:34 +00:00
|
|
|
|
|
|
|
func logKV(secret *v1.Secret) []interface{} {
|
|
|
|
return []interface{}{
|
|
|
|
"secretName", secret.Name,
|
|
|
|
"secretNamespace", secret.Namespace,
|
|
|
|
"secretType", string(secret.Type),
|
|
|
|
"garbageCollectAfter", secret.Annotations[crud.SecretLifetimeAnnotationKey],
|
|
|
|
}
|
|
|
|
}
|