-
Notifications
You must be signed in to change notification settings - Fork 151
/
server.go
125 lines (103 loc) · 3.33 KB
/
server.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package watcher
import (
"io/ioutil"
"github.com/fluxcd/pkg/runtime/events"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"github.com/weaveworks/weave-gitops/pkg/helm"
//+kubebuilder:scaffold:imports
"github.com/weaveworks/weave-gitops/pkg/helm/watcher/cache"
"github.com/weaveworks/weave-gitops/pkg/helm/watcher/controller"
)
const controllerName = "helm-watcher"
var (
scheme = runtime.NewScheme()
)
type Options struct {
KubeClient client.Client
Cache cache.Cache
MetricsBindAddress string
HealthzBindAddress string
NotificationControllerAddress string
WatcherPort int
}
type Watcher struct {
cache cache.Cache
repoManager helm.HelmRepoManager
metricsBindAddress string
healthzBindAddress string
watcherPort int
notificationAddress string
}
func NewWatcher(opts Options) (*Watcher, error) {
tempDir, err := ioutil.TempDir("", "profile_cache")
if err != nil {
return nil, err
}
if err := clientgoscheme.AddToScheme(scheme); err != nil {
return nil, err
}
if err := sourcev1.AddToScheme(scheme); err != nil {
return nil, err
}
return &Watcher{
cache: opts.Cache,
repoManager: helm.NewRepoManager(opts.KubeClient, tempDir),
healthzBindAddress: opts.HealthzBindAddress,
metricsBindAddress: opts.MetricsBindAddress,
notificationAddress: opts.NotificationControllerAddress,
watcherPort: opts.WatcherPort,
}, nil
}
func (w *Watcher) StartWatcher(log logr.Logger) error {
ctrl.SetLogger(log.WithName("helm-watcher"))
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: w.metricsBindAddress,
HealthProbeBindAddress: w.healthzBindAddress,
Port: w.watcherPort,
Logger: ctrl.Log,
})
if err != nil {
ctrl.Log.Error(err, "unable to create manager")
return err
}
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
ctrl.Log.Error(err, "unable to set up health check")
return err
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
ctrl.Log.Error(err, "unable to set up ready check")
return err
}
var eventRecorder *events.Recorder
if w.notificationAddress != "" {
var err error
if eventRecorder, err = events.NewRecorder(mgr, ctrl.Log, w.notificationAddress, controllerName); err != nil {
ctrl.Log.Error(err, "unable to create event recorder")
return err
}
}
if err = (&controller.HelmWatcherReconciler{
Client: mgr.GetClient(),
Cache: w.cache,
RepoManager: w.repoManager,
Scheme: scheme,
ExternalEventRecorder: eventRecorder,
}).SetupWithManager(mgr); err != nil {
ctrl.Log.Error(err, "unable to create controller", "controller", "HelmWatcherReconciler")
return err
}
ctrl.Log.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
ctrl.Log.Error(err, "problem running manager")
return err
}
return nil
}