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 {
|
|
|
|
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-11 23:21:34 +00:00
|
|
|
pinnipedcontroller.MatchAnythingFilter(nil),
|
2020-12-11 01:34:05 +00:00
|
|
|
controllerlib.InformerOption{},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *garbageCollectorController) Sync(ctx controllerlib.Context) error {
|
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).
|
|
|
|
if c.clock.Now().Sub(c.timeOfMostRecentSweep) < minimumRepeatInterval {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
plog.Info("starting storage garbage collection sweep")
|
|
|
|
c.timeOfMostRecentSweep = c.clock.Now()
|
|
|
|
|
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
|
|
|
|
|
|
|
if garbageCollectAfterTime.Before(c.clock.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],
|
|
|
|
}
|
|
|
|
}
|