-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
watcher.go
79 lines (68 loc) · 2.05 KB
/
watcher.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
74
75
76
77
78
79
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package auth
import (
"context"
"github.com/cilium/workerpool"
"github.com/sirupsen/logrus"
"github.com/cilium/cilium/operator/auth/identity"
"github.com/cilium/cilium/pkg/hive/cell"
ciliumv2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
"github.com/cilium/cilium/pkg/k8s/resource"
)
// params contains all the dependencies for the identity-gc.
// They will be provided through dependency injection.
type params struct {
cell.In
Logger logrus.FieldLogger
Lifecycle cell.Lifecycle
IdentityClient identity.Provider
Identity resource.Resource[*ciliumv2.CiliumIdentity]
Cfg Config
}
// IdentityWatcher represents the Cilium identities watcher.
// It watches for Cilium identities and upserts or deletes them in Spire.
type IdentityWatcher struct {
logger logrus.FieldLogger
identityClient identity.Provider
identity resource.Resource[*ciliumv2.CiliumIdentity]
wg *workerpool.WorkerPool
cfg Config
}
func registerIdentityWatcher(p params) {
if !p.Cfg.Enabled {
return
}
iw := &IdentityWatcher{
logger: p.Logger,
identityClient: p.IdentityClient,
identity: p.Identity,
wg: workerpool.New(1),
cfg: p.Cfg,
}
p.Lifecycle.Append(cell.Hook{
OnStart: func(ctx cell.HookContext) error {
return iw.wg.Submit("identity-watcher", func(ctx context.Context) error {
return iw.run(ctx)
})
},
OnStop: func(_ cell.HookContext) error {
return iw.wg.Close()
},
})
}
func (iw *IdentityWatcher) run(ctx context.Context) error {
for e := range iw.identity.Events(ctx) {
var err error
switch e.Kind {
case resource.Upsert:
err = iw.identityClient.Upsert(ctx, e.Object.GetName())
iw.logger.WithError(err).WithField("identity", e.Object.GetName()).Info("Upsert identity")
case resource.Delete:
err = iw.identityClient.Delete(ctx, e.Object.GetName())
iw.logger.WithError(err).WithField("identity", e.Object.GetName()).Info("Delete identity")
}
e.Done(err)
}
return nil
}