This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
secret_rotation_controller.go
63 lines (54 loc) · 2.13 KB
/
secret_rotation_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
package quarkssecret
import (
"context"
"fmt"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"
qsv1a1 "code.cloudfoundry.org/quarks-secret/pkg/kube/apis/quarkssecret/v1alpha1"
"code.cloudfoundry.org/quarks-utils/pkg/config"
"code.cloudfoundry.org/quarks-utils/pkg/ctxlog"
)
// AddSecretRotation resets all QuarksSecret to status' to generated=false
func AddSecretRotation(ctx context.Context, config *config.Config, mgr manager.Manager) error {
ctx = ctxlog.NewContextWithRecorder(ctx, "secret-rotation-reconciler", mgr.GetEventRecorderFor("quarks-secret-recorder"))
r := NewSecretRotationReconciler(ctx, config, mgr)
// Create a new controller
c, err := controller.New("secret-rotation-controller", mgr, controller.Options{
Reconciler: r,
MaxConcurrentReconciles: config.MaxQuarksSecretWorkers,
})
if err != nil {
return errors.Wrap(err, "Adding quarks secret controller to manager failed.")
}
nsPred := newNSPredicate(ctx, mgr.GetClient(), config.MonitoredID)
// Watch for changes to QuarksSecrets
p := predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
o := e.Object.(*corev1.ConfigMap)
labels := o.GetLabels()
_, found := labels[qsv1a1.LabelSecretRotationTrigger]
if found {
ctxlog.NewPredicateEvent(e.Object).Debug(
ctx, e.Meta, "corev1.ConfigMap",
fmt.Sprintf("Create predicate passed for '%s/%s'", e.Meta.GetNamespace(), e.Meta.GetName()),
)
return true
}
return false
},
DeleteFunc: func(e event.DeleteEvent) bool { return false },
GenericFunc: func(e event.GenericEvent) bool { return false },
UpdateFunc: func(e event.UpdateEvent) bool { return false },
}
err = c.Watch(&source.Kind{Type: &corev1.ConfigMap{}}, &handler.EnqueueRequestForObject{}, nsPred, p)
if err != nil {
return errors.Wrapf(err, "Watching quarks secrets failed in quarksSecret controller.")
}
return nil
}