-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (75 loc) · 2.4 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
package main
import (
"flag"
"os"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/healthz"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/source"
)
var log = logf.Log.WithName("longhorn-external-share-manager")
func main() {
// Setup logger
zapOpts := zap.Options{}
zapOpts.BindFlags(flag.CommandLine)
// Setup controller options
o := newOptions()
o.addFlags(flag.CommandLine)
flag.Parse()
logf.SetLogger(zap.New(zap.UseFlagOptions(&zapOpts)))
mainLog := log.WithName("main")
mainLog.Info("Starting longhorn-external-share-manager")
mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{
HealthProbeBindAddress: o.healthAddr,
Metrics: metricsserver.Options{
BindAddress: o.metricsAddr,
},
Cache: cache.Options{
SyncPeriod: &o.syncPeriod,
},
LeaderElection: true,
LeaderElectionResourceLock: "leases",
LeaderElectionID: "longhorn-external-share-manager",
})
if err != nil {
mainLog.Error(err, "Unable to set up longhorn-share-manager")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("ping", healthz.Ping); err != nil {
mainLog.Error(err, "Unable to create ready check")
os.Exit(1)
}
if err := mgr.AddHealthzCheck("ping", healthz.Ping); err != nil {
mainLog.Error(err, "Unable to create health check")
os.Exit(1)
}
c, err := controller.New("longhorn-external-share-manager", mgr, controller.Options{
Reconciler: &reconcileSVC{
client: mgr.GetClient(),
log: log.WithName("reconciler"),
},
// Do not reconcile concurrently
MaxConcurrentReconciles: 1,
})
if err != nil {
mainLog.Error(err, "Unable to set up controller")
os.Exit(1)
}
if err := c.Watch(source.Kind(mgr.GetCache(), &corev1.Service{}), &handler.EnqueueRequestForObject{}); err != nil {
mainLog.Error(err, "Unable to watch Services")
os.Exit(1)
}
mainLog.Info("Starting manager")
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
mainLog.Error(err, "Unable to run manager")
os.Exit(1)
}
}