forked from codeready-toolchain/member-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
344 lines (307 loc) · 12.6 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
337
338
339
340
341
342
343
344
package main
import (
"flag"
"fmt"
"os"
goruntime "runtime"
"github.com/codeready-toolchain/member-operator/controllers/idler"
membercfg "github.com/codeready-toolchain/member-operator/controllers/memberoperatorconfig"
"github.com/codeready-toolchain/member-operator/controllers/memberstatus"
"github.com/codeready-toolchain/member-operator/controllers/nstemplateset"
"github.com/codeready-toolchain/member-operator/controllers/useraccount"
"github.com/codeready-toolchain/member-operator/controllers/useraccountstatus"
"github.com/codeready-toolchain/member-operator/pkg/apis"
"github.com/codeready-toolchain/member-operator/pkg/che"
"github.com/codeready-toolchain/member-operator/version"
"github.com/codeready-toolchain/toolchain-common/controllers/toolchaincluster"
"github.com/codeready-toolchain/toolchain-common/pkg/cluster"
commonconfig "github.com/codeready-toolchain/toolchain-common/pkg/configuration"
"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"
"k8s.io/client-go/rest"
klogv1 "k8s.io/klog"
klogv2 "k8s.io/klog/v2"
metrics "k8s.io/metrics/pkg/apis/metrics/v1beta1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
runtimecluster "sigs.k8s.io/controller-runtime/pkg/cluster"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
//+kubebuilder:scaffold:imports
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(apis.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
func printVersion() {
setupLog.Info(fmt.Sprintf("Operator Version: %s", version.Version))
setupLog.Info(fmt.Sprintf("Go Version: %s", goruntime.Version()))
setupLog.Info(fmt.Sprintf("Go OS/Arch: %s/%s", goruntime.GOOS, goruntime.GOARCH))
setupLog.Info(fmt.Sprintf("Commit: %s", version.Commit))
setupLog.Info(fmt.Sprintf("BuildTime: %s", version.BuildTime))
}
//+kubebuilder:rbac:groups=toolchain.dev.openshift.com,resources=toolchainclusters,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=toolchain.dev.openshift.com,resources=toolchainclusters/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=toolchain.dev.openshift.com,resources=toolchainclusters/finalizers,verbs=update
//+kubebuilder:rbac:groups=admissionregistration.k8s.io,resources=mutatingwebhookconfigurations,verbs=get;list;watch;update;patch;create;delete
//+kubebuilder:rbac:groups=scheduling.k8s.io,resources=priorityclasses,verbs=get;list;watch;update;patch;create;delete
//+kubebuilder:rbac:groups="",resources=secrets;configmaps;services;services/finalizers;serviceaccounts,verbs=get;list;watch;update;patch;create;delete
//+kubebuilder:rbac:groups=apps,resources=deployments;deployments/finalizers;replicasets,verbs=get;list;watch;update;patch;create;delete
//+kubebuilder:rbac:groups=monitoring.coreos.com,resources=servicemonitors,verbs=get;list;watch;update;patch;create;delete
func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
opts := zap.Options{
Development: true,
Encoder: zapcore.NewJSONEncoder(zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
FunctionKey: zapcore.OmitKey,
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}),
}
opts.BindFlags(flag.CommandLine)
flag.Parse()
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
// also set the client-go logger so we get the same JSON output
klogv2.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
// see https://github.com/kubernetes/klog#coexisting-with-klogv2
// BEGIN : hack to redirect klogv1 calls to klog v2
// Tell klog NOT to log into STDERR. Otherwise, we risk
// certain kinds of API errors getting logged into a directory not
// available in a `FROM scratch` Docker container, causing us to abort
var klogv1Flags flag.FlagSet
klogv1.InitFlags(&klogv1Flags)
if err := klogv1Flags.Set("logtostderr", "false"); err != nil { // By default klog v1 logs to stderr, switch that off
setupLog.Error(err, "")
os.Exit(1)
}
if err := klogv1Flags.Set("stderrthreshold", "FATAL"); err != nil { // stderrthreshold defaults to ERROR, so we don't get anything in stderr
setupLog.Error(err, "")
os.Exit(1)
}
klogv1.SetOutputBySeverity("INFO", klogWriter{}) // tell klog v1 to use the custom writer
// END : hack to redirect klogv1 calls to klog v2
printVersion()
// Get a config to talk to the apiserver
cfg, err := config.GetConfig()
if err != nil {
setupLog.Error(err, "")
os.Exit(1)
}
namespace, err := commonconfig.GetWatchNamespace()
if err != nil {
setupLog.Error(err, "failed to get watch namespace")
os.Exit(1)
}
crtConfig, err := getCRTConfiguration(cfg)
if err != nil {
setupLog.Error(err, "failed to get toolchain configuration")
os.Exit(1)
}
crtConfig.Print()
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "2fc71baf.toolchain.member.operator",
Namespace: namespace,
ClientDisableCacheFor: []client.Object{&metrics.NodeMetrics{}},
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
allNamespacesCluster, err := runtimecluster.New(ctrl.GetConfigOrDie(), func(options *runtimecluster.Options) {
options.Scheme = scheme
})
if err != nil {
setupLog.Error(err, "unable to start allNamespaceCluster")
os.Exit(1)
}
err = mgr.Add(allNamespacesCluster)
if err != nil {
setupLog.Error(err, "unable to add allNamespaceCluster to manager")
os.Exit(1)
}
allNamespacesClient, allNamespacesCache, err := newAllNamespacesClient(cfg)
if err != nil {
setupLog.Error(err, "")
os.Exit(1)
}
// initialize che client
che.InitDefaultCheClient(allNamespacesClient)
// Setup all Controllers
if err = toolchaincluster.NewReconciler(
mgr,
namespace,
crtConfig.ToolchainCluster().HealthCheckTimeout(),
).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ToolchainCluster")
os.Exit(1)
}
if err := (&idler.Reconciler{
Scheme: mgr.GetScheme(),
AllNamespacesClient: allNamespacesClient,
Client: mgr.GetClient(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Idler")
os.Exit(1)
}
if err = (&memberstatus.Reconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
GetHostCluster: cluster.GetHostCluster,
AllNamespacesClient: allNamespacesClient,
CheClient: che.DefaultClient,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "MemberStatus")
os.Exit(1)
}
if err = (nstemplateset.NewReconciler(&nstemplateset.APIClient{
Client: mgr.GetClient(),
AllNamespacesClient: allNamespacesClient,
Scheme: mgr.GetScheme(),
GetHostCluster: cluster.GetHostCluster,
})).SetupWithManager(mgr, allNamespacesCluster); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "NSTemplateSet")
os.Exit(1)
}
if err = (&useraccount.Reconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
CheClient: che.DefaultClient,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "UserAccount")
os.Exit(1)
}
if err = (&useraccountstatus.Reconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
GetHostCluster: cluster.GetHostCluster,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "UserAccountStatus")
os.Exit(1)
}
if err = (&membercfg.Reconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("MemberOperatorConfig"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "MemberOperatorConfig")
os.Exit(1)
}
//+kubebuilder:scaffold:builder
stopChannel := ctrl.SetupSignalHandler()
go func() {
setupLog.Info("Waiting for cache to sync")
if !mgr.GetCache().WaitForCacheSync(stopChannel) {
setupLog.Error(fmt.Errorf("timed out waiting for main cache to sync"), "")
os.Exit(1)
}
setupLog.Info("Starting ToolchainCluster health checks.")
toolchaincluster.StartHealthChecks(stopChannel, mgr, namespace, crtConfig.ToolchainCluster().HealthCheckPeriod())
// create or update Member status during the operator deployment
setupLog.Info("Creating/updating the MemberStatus resource")
memberStatusName := membercfg.MemberStatusName
if err := memberstatus.CreateOrUpdateResources(mgr.GetClient(), mgr.GetScheme(), namespace, memberStatusName); err != nil {
setupLog.Error(err, "cannot create/update MemberStatus resource")
os.Exit(1)
}
setupLog.Info("Created/updated the MemberStatus resource")
}()
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)
}
go func() {
if err := allNamespacesCache.Start(stopChannel); err != nil {
setupLog.Error(err, "failed to start all-namespaces cache")
os.Exit(1)
}
}()
setupLog.Info("starting manager")
if err := mgr.Start(stopChannel); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
// newAllNamespacesClient creates a new client that watches (as opposed to the standard client) resources in all namespaces.
// This client should be used only for resources and kinds that are retrieved from other namespaces than the watched one.
// This will help keeping a reasonable memory usage for this operator since the cache won't store all other namespace scoped
// resources (secrets, etc.).
func newAllNamespacesClient(config *rest.Config) (client.Client, cache.Cache, error) {
clusterAllNamespaces, err := runtimecluster.New(config, func(clusterOptions *runtimecluster.Options) {
clusterOptions.Scheme = scheme
})
if err != nil {
return nil, nil, err
}
return clusterAllNamespaces.GetClient(), clusterAllNamespaces.GetCache(), nil
}
// getCRTConfiguration creates the client used for configuration and
// returns the loaded crt configuration
func getCRTConfiguration(config *rest.Config) (membercfg.Configuration, error) {
// create client that will be used for retrieving the member operator config maps
cl, err := client.New(config, client.Options{
Scheme: scheme,
})
if err != nil {
return membercfg.Configuration{}, err
}
return membercfg.GetConfiguration(cl)
}
// OutputCallDepth is the stack depth where we can find the origin of this call
const OutputCallDepth = 6
// DefaultPrefixLength is the length of the log prefix that we have to strip out
const DefaultPrefixLength = 53
// klogWriter is used in SetOutputBySeverity call below to redirect
// any calls to klogv1 to end up in klogv2
type klogWriter struct{}
func (kw klogWriter) Write(p []byte) (n int, err error) {
if len(p) < DefaultPrefixLength {
klogv2.InfoDepth(OutputCallDepth, string(p))
return len(p), nil
}
if p[0] == 'I' {
klogv2.InfoDepth(OutputCallDepth, string(p[DefaultPrefixLength:]))
} else if p[0] == 'W' {
klogv2.WarningDepth(OutputCallDepth, string(p[DefaultPrefixLength:]))
} else if p[0] == 'E' {
klogv2.ErrorDepth(OutputCallDepth, string(p[DefaultPrefixLength:]))
} else if p[0] == 'F' {
klogv2.FatalDepth(OutputCallDepth, string(p[DefaultPrefixLength:]))
} else {
klogv2.InfoDepth(OutputCallDepth, string(p[DefaultPrefixLength:]))
}
return len(p), nil
}