-
Notifications
You must be signed in to change notification settings - Fork 4
/
controller.go
73 lines (60 loc) · 2.08 KB
/
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
package main
import (
"context"
"fmt"
"time"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/tools/cache"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"github.com/uswitch/vault-webhook/pkg/apis/vaultwebhook.uswitch.com/v1alpha1"
webhookclient "github.com/uswitch/vault-webhook/pkg/client/clientset/versioned"
)
type bindingAggregator struct {
store cache.Store
controller cache.Controller
}
func NewListWatch(client *webhookclient.Clientset) *bindingAggregator {
binder := &bindingAggregator{}
watcher := cache.NewListWatchFromClient(client.VaultwebhookV1alpha1().RESTClient(), "databasecredentialbindings", "", fields.Everything())
binder.store, binder.controller = cache.NewIndexerInformer(watcher, &v1alpha1.DatabaseCredentialBinding{}, time.Minute, binder, cache.Indexers{})
cacheSize := prometheus.NewCounterFunc(
prometheus.CounterOpts{
Name: "database_credential_binding_cache_size",
Help: "Current size of the Database Credential Binding cache",
},
func() float64 { return float64(binder.cacheSize()) },
)
prometheus.MustRegister(cacheSize)
return binder
}
func (b *bindingAggregator) OnAdd(obj interface{}) {
log.Debugf("adding %+v", obj)
}
func (b *bindingAggregator) OnDelete(obj interface{}) {
log.Debugf("deleting %+v", obj)
}
func (b *bindingAggregator) OnUpdate(old, new interface{}) {
log.Debugf("updating %+v", new)
}
func (b *bindingAggregator) Run(ctx context.Context) error {
go b.controller.Run(ctx.Done())
cache.WaitForCacheSync(ctx.Done(), b.controller.HasSynced)
log.Debugf("cache controller synced")
return nil
}
func (b *bindingAggregator) List() ([]v1alpha1.DatabaseCredentialBinding, error) {
bindingList := make([]v1alpha1.DatabaseCredentialBinding, 0)
bindings := b.store.List()
for _, obj := range bindings {
binding, ok := obj.(*v1alpha1.DatabaseCredentialBinding)
if !ok {
return nil, fmt.Errorf("unexpected object in store: %+v", obj)
}
bindingList = append(bindingList, *binding)
}
return bindingList, nil
}
func (b *bindingAggregator) cacheSize() int {
return len(b.store.List())
}