-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
167 lines (147 loc) · 4.93 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
package main
import (
"fmt"
"os"
"time"
"go.uber.org/zap/zapcore"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
"github.com/knadh/koanf"
"github.com/knadh/koanf/providers/posflag"
flag "github.com/spf13/pflag"
syncv1alpha1 "github.com/vshn/espejo/api/v1alpha1"
"github.com/vshn/espejo/controllers"
// +kubebuilder:scaffold:imports
)
var (
// These will be populated by Goreleaser
version string
commit string
date string
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
koanfInstance = koanf.New(".")
config = Configuration{
LeaderElection: false,
MetricsAddr: ":8080",
ReconcileInterval: "10s",
}
)
type (
// Configuration holds all the operator-wide configurable settings.
Configuration struct {
LeaderElection bool `koanf:"enable-leader-election"`
MetricsAddr string `koanf:"metrics-addr"`
ReconcileInterval string `koanf:"reconcile-interval"`
Debug bool `koanf:"verbose"`
}
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(syncv1alpha1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}
func main() {
loadConfig()
setupLogger()
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Metrics: server.Options{
BindAddress: config.MetricsAddr,
},
LeaderElection: config.LeaderElection,
LeaderElectionID: "bd39f6a0.appuio.ch",
// Limit the manager to only watch the given namespace
NewCache: func(config *rest.Config, opts cache.Options) (cache.Cache, error) {
opts.DefaultNamespaces = map[string]cache.Config{
getWatchNamespace(): {},
}
return cache.New(config, opts)
},
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
interval, err := time.ParseDuration(config.ReconcileInterval)
if err != nil {
setupLog.Error(err, "could not parse interval")
os.Exit(1)
}
setupLog.V(1).Info("Configuration from flags", "config", config)
supplier := func() *controllers.SyncConfigReconciler {
return &controllers.SyncConfigReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Namespace"),
Scheme: mgr.GetScheme(),
}
}
mainScr := supplier()
mainScr.ReconcileInterval = interval
mainScr.WatchNamespace = getWatchNamespace()
mainScr.Log = ctrl.Log.WithName("controllers").WithName("SyncConfig")
if err = mainScr.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "SyncConfig")
os.Exit(1)
}
if err = (&controllers.NamespaceReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Namespace"),
Scheme: mgr.GetScheme(),
NewSyncConfigReconciler: supplier,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Namespace")
os.Exit(1)
}
// +kubebuilder:scaffold:builder
setupLog.WithValues("version", version, "date", date, "commit", commit).Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
// getWatchNamespace returns the Namespace the operator should be watching for changes
// An empty value means the operator is running with cluster scope.
func getWatchNamespace() string {
return os.Getenv("WATCH_NAMESPACE")
}
// loadConfig will populate the configuration
func loadConfig() {
f := flag.NewFlagSet("config", flag.ContinueOnError)
f.String("metrics-addr", config.MetricsAddr, "The address the metric endpoint binds to.")
f.Bool("enable-leader-election", config.LeaderElection, "Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
f.String("reconcile-interval", config.ReconcileInterval, "The interval of which SyncConfigs get reconciled.")
f.BoolP("verbose", "v", config.Debug, "Enable debug mode")
f.Usage = func() {
fmt.Println("Usage of Espejo:")
fmt.Print(f.FlagUsages())
os.Exit(0)
}
if err := f.Parse(os.Args[1:]); err != nil {
setupLog.Error(err, "Could not parse flags.")
os.Exit(1)
}
if err := koanfInstance.Load(posflag.Provider(f, ".", koanfInstance), nil); err != nil {
setupLog.Error(err, "Could not configure settings from flags.")
os.Exit(1)
}
if err := koanfInstance.Unmarshal("", &config); err != nil {
setupLog.Error(err, "Could not unmarshal config.")
os.Exit(1)
}
}
func setupLogger() {
logLevel := zapcore.InfoLevel
if config.Debug {
logLevel = zapcore.DebugLevel
}
ctrl.SetLogger(zap.New(zap.UseDevMode(true), zap.Level(logLevel)))
}