-
Notifications
You must be signed in to change notification settings - Fork 62
/
main.go
336 lines (282 loc) · 9.94 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
Copyright 2021.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"time"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/utils/inotify"
"sigs.k8s.io/yaml"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/conversion"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"github.com/eraser-dev/eraser/api/unversioned"
"github.com/eraser-dev/eraser/api/unversioned/config"
eraserv1 "github.com/eraser-dev/eraser/api/v1"
eraserv1alpha1 "github.com/eraser-dev/eraser/api/v1alpha1"
v1alpha1Config "github.com/eraser-dev/eraser/api/v1alpha1/config"
eraserv1alpha2 "github.com/eraser-dev/eraser/api/v1alpha2"
v1alpha2Config "github.com/eraser-dev/eraser/api/v1alpha2/config"
eraserv1alpha3 "github.com/eraser-dev/eraser/api/v1alpha3"
v1alpha3Config "github.com/eraser-dev/eraser/api/v1alpha3/config"
"github.com/eraser-dev/eraser/controllers"
"github.com/eraser-dev/eraser/pkg/logger"
"github.com/eraser-dev/eraser/pkg/utils"
"github.com/eraser-dev/eraser/version"
//+kubebuilder:scaffold:imports
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
fromV1alpha1 = eraserv1alpha1.Convert_v1alpha1_EraserConfig_To_unversioned_EraserConfig
fromV1alpha2 = eraserv1alpha2.Convert_v1alpha2_EraserConfig_To_unversioned_EraserConfig
fromV1alpha3 = eraserv1alpha3.Convert_v1alpha3_EraserConfig_To_unversioned_EraserConfig
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(eraserv1alpha1.AddToScheme(scheme))
utilruntime.Must(eraserv1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
type apiVersion struct {
APIVersion string `json:"apiVersion"`
}
type convertFunc[T any] func(*T, *unversioned.EraserConfig, conversion.Scope) error
func main() {
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-ctx.Done()
os.Exit(1)
}()
var configFile string
flag.StringVar(&configFile, "config", "",
"The controller will load its initial configuration from this file. "+
"Omit this flag to use the default configuration values. "+
"Command-line flags override configuration from this file.")
flag.Parse()
if err := logger.Configure(); err != nil {
setupLog.Error(err, "unable to configure logger")
os.Exit(1)
}
// these can all be overwritten using EraserConfig.
options := ctrl.Options{
Scheme: scheme,
MetricsBindAddress: ":8889",
Port: 9443,
HealthProbeBindAddress: ":8081",
LeaderElection: false,
NewCache: cache.BuilderWithOptions(cache.Options{
SelectorsByObject: cache.SelectorsByObject{
// to watch eraser pods
&corev1.Pod{}: {
Field: fields.OneTermEqualSelector("metadata.namespace", utils.GetNamespace()),
},
// to watch eraser podTemplates
&corev1.PodTemplate{}: {
Field: fields.OneTermEqualSelector("metadata.namespace", utils.GetNamespace()),
},
// to watch eraser-manager-configs
&corev1.ConfigMap{}: {
Field: fields.OneTermEqualSelector("metadata.namespace", utils.GetNamespace()),
},
// to watch ImageJobs
&eraserv1.ImageJob{}: {},
// to watch ImageLists
&eraserv1.ImageList{}: {},
},
}),
}
if configFile == "" {
setupLog.Error(fmt.Errorf("config file was not supplied"), "aborting")
os.Exit(1)
}
cfg, err := getConfig(configFile)
if err != nil {
setupLog.Error(err, "error getting configuration")
os.Exit(1)
}
setupLog.V(1).Info("eraser config",
"manager", cfg.Manager,
"components", cfg.Components,
"options", fmt.Sprintf("%#v\n", options),
"typeMeta", fmt.Sprintf("%#v\n", cfg.TypeMeta),
)
eraserOpts := config.NewManager(cfg)
managerOpts := cfg.Manager
watcher, err := setupWatcher(configFile)
if err != nil {
setupLog.Error(err, "unable to set up configuration file watch")
os.Exit(1)
}
go startConfigWatch(cancel, watcher, eraserOpts, configFile)
if managerOpts.Profile.Enabled {
go func() {
server := &http.Server{
Addr: fmt.Sprintf("localhost:%d", managerOpts.Profile.Port),
ReadHeaderTimeout: 3 * time.Second,
}
err := server.ListenAndServe()
setupLog.Error(err, "pprof server failed")
}()
}
config := ctrl.GetConfigOrDie()
config.UserAgent = version.GetUserAgent("manager")
setupLog.Info("setting up manager", "userAgent", config.UserAgent)
mgr, err := ctrl.NewManager(config, options)
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
setupLog.Info("setup controllers")
if err = controllers.SetupWithManager(mgr, eraserOpts); err != nil {
setupLog.Error(err, "unable to setup controllers")
os.Exit(1)
}
//+kubebuilder:scaffold:builder
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up ready check")
os.Exit(1)
}
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
func getConfig(configFile string) (*unversioned.EraserConfig, error) {
fileBytes, err := os.ReadFile(configFile)
if err != nil {
setupLog.Error(err, "configuration is either missing or invalid")
os.Exit(1)
}
var av apiVersion
if err := yaml.Unmarshal(fileBytes, &av); err != nil {
setupLog.Error(err, "cannot unmarshal yaml", "bytes", string(fileBytes), "apiVersion", av)
os.Exit(1)
}
switch av.APIVersion {
case "eraser.sh/v1alpha1":
return getUnversioned(fileBytes, v1alpha1Config.Default(), fromV1alpha1)
case "eraser.sh/v1alpha2":
return getUnversioned(fileBytes, v1alpha2Config.Default(), fromV1alpha2)
case "eraser.sh/v1alpha3":
return getUnversioned(fileBytes, v1alpha3Config.Default(), fromV1alpha3)
default:
setupLog.Error(fmt.Errorf("unknown api version"), "error", "apiVersion", av.APIVersion)
return nil, err
}
}
func getUnversioned[T any](b []byte, defaults *T, convert convertFunc[T]) (*unversioned.EraserConfig, error) {
cfg := defaults
if err := yaml.Unmarshal(b, cfg); err != nil {
setupLog.Error(err, "configuration is either missing or invalid")
return nil, err
}
var unv unversioned.EraserConfig
if err := convert(cfg, &unv, nil); err != nil {
return nil, err
}
return &unv, nil
}
// Kubernetes manages configmap volume updates by creating a new file,
// changing the symlink, then deleting the old file. Hence, we want to
// watch for IN_DELETE_SELF events. In case the watch is dropped, we need
// to reestablish, so watch of IN_IGNORED too.
// https://ahmet.im/blog/kubernetes-inotify/ for more information.
func setupWatcher(configFile string) (*inotify.Watcher, error) {
watcher, err := inotify.NewWatcher()
if err != nil {
return nil, err
}
err = watcher.AddWatch(configFile, inotify.InDeleteSelf|inotify.InIgnored)
if err != nil {
return nil, err
}
return watcher, nil
}
func startConfigWatch(cancel context.CancelFunc, watcher *inotify.Watcher, eraserOpts *config.Manager, filename string) {
for {
select {
case ev := <-watcher.Event:
// by default inotify removes a watch on a file on an IN_DELETE_SELF
// event, so we have to remove and reinstate the watch
setupLog.V(1).Info("event", "event", ev)
if ev.Mask&inotify.InIgnored != 0 {
err := watcher.RemoveWatch(filename)
if err != nil {
setupLog.Error(err, "unable to remove watch on config")
}
err = watcher.AddWatch(filename, inotify.InDeleteSelf|inotify.InIgnored)
if err != nil {
setupLog.Error(err, "unable to set up new watch on configuration")
}
continue
}
var err error
oldConfig := new(unversioned.EraserConfig)
*oldConfig, err = eraserOpts.Read()
if err != nil {
setupLog.Error(err, "configuration could not be read", "event", ev, "filename", filename)
}
newConfig, err := getConfig(filename)
if err != nil {
setupLog.Error(err, "configuration is missing or invalid", "event", ev, "filename", filename)
continue
}
if err = eraserOpts.Update(newConfig); err != nil {
setupLog.Error(err, "configuration update failed")
continue
}
// read back the new configuration
*newConfig, err = eraserOpts.Read()
if err != nil {
setupLog.Error(err, "unable to read back new configuration")
continue
}
if needsRestart(oldConfig, newConfig) {
setupLog.Info("configurations differ in an irreconcileable way, restarting", "old", oldConfig.Components, "new", newConfig.Components)
// restarts the manager
cancel()
}
setupLog.V(1).Info("new configuration", "manager", newConfig.Manager, "components", newConfig.Components)
case err := <-watcher.Error:
setupLog.Error(err, "file watcher error")
}
}
}
func needsRestart(oldConfig, newConfig *unversioned.EraserConfig) bool {
type check struct {
collector bool
scanner bool
}
oldComponents := check{collector: oldConfig.Components.Collector.Enabled, scanner: oldConfig.Components.Scanner.Enabled}
newComponents := check{collector: newConfig.Components.Collector.Enabled, scanner: newConfig.Components.Scanner.Enabled}
return oldComponents != newComponents
}