forked from oracle/cluster-api-provider-oci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
407 lines (369 loc) · 14.3 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
Copyright (c) 2021, 2022 Oracle and/or its affiliates.
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 (
"flag"
"os"
"time"
infrastructurev1beta1 "github.com/oracle/cluster-api-provider-oci/api/v1beta1"
infrastructurev1beta2 "github.com/oracle/cluster-api-provider-oci/api/v1beta2"
"github.com/oracle/cluster-api-provider-oci/cloud/config"
"github.com/oracle/cluster-api-provider-oci/cloud/scope"
"github.com/oracle/cluster-api-provider-oci/controllers"
expV1Beta1 "github.com/oracle/cluster-api-provider-oci/exp/api/v1beta1"
expV1Beta2 "github.com/oracle/cluster-api-provider-oci/exp/api/v1beta2"
expcontrollers "github.com/oracle/cluster-api-provider-oci/exp/controllers"
"github.com/oracle/cluster-api-provider-oci/feature"
"github.com/oracle/cluster-api-provider-oci/version"
"github.com/oracle/oci-go-sdk/v65/common"
"github.com/spf13/pflag"
"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/tools/leaderelection/resourcelock"
"k8s.io/component-base/logs"
logsV1 "k8s.io/component-base/logs/api/v1"
_ "k8s.io/component-base/logs/json/register"
"k8s.io/klog/v2"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
expclusterv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
logOptions = logs.NewOptions()
)
const (
AuthConfigDirectory = "AUTH_CONFIG_DIR"
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(infrastructurev1beta1.AddToScheme(scheme))
utilruntime.Must(infrastructurev1beta2.AddToScheme(scheme))
utilruntime.Must(clusterv1.AddToScheme(scheme))
utilruntime.Must(expV1Beta1.AddToScheme(scheme))
utilruntime.Must(expV1Beta2.AddToScheme(scheme))
utilruntime.Must(expclusterv1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
func main() {
var metricsAddr string
var enableLeaderElection bool
var leaderElectionNamespace string
var leaderElectionLeaseDuration time.Duration
var leaderElectionRenewDeadline time.Duration
var leaderElectionRetryPeriod time.Duration
var watchNamespace string
var probeAddr string
var webhookPort int
var webhookCertDir string
// Flags for reconciler concurrency
var ociClusterConcurrency int
var ociMachineConcurrency int
var ociMachinePoolConcurrency int
var initOciClientsOnStartup bool
var enableInstanceMetadataServiceLookup bool
fs := pflag.CommandLine
logs.AddFlags(fs, logs.SkipLoggingConfigurationFlags())
logsV1.AddFlags(logOptions, fs)
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.")
flag.StringVar(
&leaderElectionNamespace,
"leader-election-namespace",
"",
"Namespace that the controller performs leader election in. If unspecified, the controller will discover which namespace it is running in.",
)
flag.DurationVar(
&leaderElectionLeaseDuration,
"leader-elect-lease-duration",
15*time.Second,
"Interval at which non-leader candidates will wait to force acquire leadership (duration string)",
)
flag.DurationVar(
&leaderElectionRenewDeadline,
"leader-elect-renew-deadline",
10*time.Second,
"Duration that the leading controller manager will retry refreshing leadership before giving up (duration string)",
)
flag.DurationVar(
&leaderElectionRetryPeriod,
"leader-elect-retry-period",
2*time.Second,
"Duration the LeaderElector clients should wait between tries of actions (duration string)",
)
flag.IntVar(&webhookPort,
"webhook-port",
9443,
"Webhook Server port.",
)
flag.StringVar(&webhookCertDir, "webhook-cert-dir", "/tmp/k8s-webhook-server/serving-certs/",
"Webhook cert dir, only used when webhook-port is specified.")
flag.IntVar(
&ociClusterConcurrency,
"ocicluster-concurrency",
5,
"Number of OciClusters to process simultaneously",
)
flag.IntVar(
&ociMachineConcurrency,
"ocimachine-concurrency",
10,
"Number of OciMachines to process simultaneously",
)
flag.IntVar(
&ociMachinePoolConcurrency,
"ocimachinepool-concurrency",
5,
"Number of OciMachinePools to process simultaneously",
)
flag.BoolVar(
&initOciClientsOnStartup,
"init-oci-clients-on-startup",
true,
"Initialize OCI clients on startup",
)
flag.StringVar(
&watchNamespace,
"namespace",
"",
"Namespace that the controller watches to reconcile cluster-api objects. If unspecified, the controller watches for cluster-api objects across all namespaces.",
)
flag.BoolVar(
&enableInstanceMetadataServiceLookup,
"enable-instance-metadata-service-lookup",
false,
"Initialize OCI clients on startup",
)
opts := zap.Options{
Development: true,
}
opts.BindFlags(flag.CommandLine)
// Need to use pflags for kubernetes feature flags
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
feature.MutableGates.AddFlag(pflag.CommandLine)
pflag.Parse()
if err := logsV1.ValidateAndApply(logOptions, nil); err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
// klog.Background will automatically use the right logger.
ctrl.SetLogger(klog.Background())
var watchNamespaces map[string]cache.Config
if watchNamespace != "" {
watchNamespaces = map[string]cache.Config{
watchNamespace: {},
}
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{
BindAddress: metricsAddr,
},
WebhookServer: webhook.NewServer(webhook.Options{
Port: webhookPort,
CertDir: webhookCertDir,
}),
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "controller-leader-elect-capoci",
LeaderElectionResourceLock: resourcelock.LeasesResourceLock,
LeaderElectionNamespace: leaderElectionNamespace,
LeaseDuration: &leaderElectionLeaseDuration,
RenewDeadline: &leaderElectionRenewDeadline,
RetryPeriod: &leaderElectionRetryPeriod,
Cache: cache.Options{
DefaultNamespaces: watchNamespaces,
},
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
// Setup the context that's going to be used in controllers and for the manager.
ctx := ctrl.SetupSignalHandler()
var clientProvider *scope.ClientProvider
var region string
if initOciClientsOnStartup {
authConfigDir := os.Getenv(AuthConfigDirectory)
if authConfigDir == "" {
setupLog.Error(err, "auth config directory environment variable is not set")
os.Exit(1)
}
authConfig, err := config.FromDir(authConfigDir)
if err != nil {
setupLog.Error(err, "invalid auth config file")
os.Exit(1)
}
setupLog.Info("CAPOCI Version", "version", version.GitVersion)
ociAuthConfigProvider, err := config.NewConfigurationProvider(authConfig)
if err != nil {
setupLog.Error(err, "authentication provider could not be initialised")
os.Exit(1)
}
region, err = ociAuthConfigProvider.Region()
if err != nil {
setupLog.Error(err, "unable to get OCI region from AuthConfigProvider")
os.Exit(1)
}
clientProvider, err = scope.NewClientProvider(scope.ClientProviderParams{
OciAuthConfigProvider: ociAuthConfigProvider})
if err != nil {
setupLog.Error(err, "unable to create OCI ClientProvider")
os.Exit(1)
}
_, err = clientProvider.GetOrBuildClient(region)
if err != nil {
setupLog.Error(err, "authentication provider could not be initialised")
os.Exit(1)
}
}
if enableInstanceMetadataServiceLookup {
common.EnableInstanceMetadataServiceLookup()
}
if err = (&controllers.OCIClusterReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Region: region,
ClientProvider: clientProvider,
Recorder: mgr.GetEventRecorderFor("ocicluster-controller"),
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: ociClusterConcurrency}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", scope.OCIClusterKind)
os.Exit(1)
}
if err = (&controllers.OCIMachineReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ClientProvider: clientProvider,
Region: region,
Recorder: mgr.GetEventRecorderFor("ocimachine-controller"),
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: ociMachineConcurrency}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", scope.OCIMachineKind)
os.Exit(1)
}
if err = (&controllers.OCIManagedClusterReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Region: region,
ClientProvider: clientProvider,
Recorder: mgr.GetEventRecorderFor("ocimanagedcluster-controller"),
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: ociClusterConcurrency}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", scope.OCIManagedClusterKind)
os.Exit(1)
}
if err = (&controllers.OCIManagedClusterControlPlaneReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Region: region,
ClientProvider: clientProvider,
Recorder: mgr.GetEventRecorderFor("ocimanagedclustercontrolplane-controller"),
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: ociClusterConcurrency}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", scope.OCIManagedClusterControlPlaneKind)
os.Exit(1)
}
if feature.Gates.Enabled(feature.MachinePool) {
setupLog.Info("MACHINE POOL experimental feature enabled")
setupLog.V(1).Info("enabling machine pool controller")
if err := (&expcontrollers.OCIMachinePoolReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ClientProvider: clientProvider,
Recorder: mgr.GetEventRecorderFor("ocimachinepool-controller"),
Region: region,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: ociMachinePoolConcurrency}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", scope.OCIMachinePoolKind)
os.Exit(1)
}
setupLog.Info("OKE experimental feature enabled")
setupLog.V(1).Info("enabling managed machine pool controller")
if err = (&expcontrollers.OCIManagedMachinePoolReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Region: region,
ClientProvider: clientProvider,
Recorder: mgr.GetEventRecorderFor("ocimanagedmachinepool-controller"),
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: ociMachinePoolConcurrency}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", scope.OCIManagedMachinePoolKind)
os.Exit(1)
}
if err = (&expcontrollers.OCIVirtualMachinePoolReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Region: region,
ClientProvider: clientProvider,
Recorder: mgr.GetEventRecorderFor("ocivirtualmachinepool-controller"),
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: ociClusterConcurrency}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", scope.OCIVirtualMachinePoolKind)
os.Exit(1)
}
if err = (&expcontrollers.OCIMachinePoolMachineReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Region: region,
ClientProvider: clientProvider,
Recorder: mgr.GetEventRecorderFor("ocimachinepoolmachine-controller"),
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: ociClusterConcurrency}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "OCIMachinePoolMachine")
os.Exit(1)
}
}
if err = (&infrastructurev1beta2.OCICluster{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "OCICluster")
os.Exit(1)
}
if err = (&infrastructurev1beta2.OCIMachineTemplate{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "OCIMachineTemplate")
os.Exit(1)
}
if err = (&infrastructurev1beta2.OCIManagedCluster{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "OCIManagedCluster")
os.Exit(1)
}
if err = (&infrastructurev1beta2.OCIManagedControlPlane{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "OCIManagedControlPlane")
os.Exit(1)
}
if err = (&expV1Beta2.OCIManagedMachinePool{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "OCIManagedMachinePool")
os.Exit(1)
}
if err = (&expV1Beta2.OCIVirtualMachinePool{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "OCIVirtualMachinePool")
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(ctx); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}