forked from oracle/cluster-api-provider-oci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
299 lines (263 loc) · 10.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
/*
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"
infrastructurev1beta1 "github.com/oracle/cluster-api-provider-oci/api/v1beta1"
"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"
infrav1exp "github.com/oracle/cluster-api-provider-oci/exp/api/v1beta1"
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/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/component-base/logs"
_ "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/controller"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
logOptions = logs.NewOptions()
webhookPort int
webhookCertDir string
// Flags for reconciler concurrency
ociClusterConcurrency int
ociMachineConcurrency int
ociMachinePoolConcurrency int
)
const (
AuthConfigDirectory = "AUTH_CONFIG_DIR"
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(infrastructurev1beta1.AddToScheme(scheme))
utilruntime.Must(clusterv1.AddToScheme(scheme))
utilruntime.Must(expV1Beta1.AddToScheme(scheme))
utilruntime.Must(expclusterv1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
var webhookPort int
fs := pflag.CommandLine
logs.AddFlags(fs, logs.SkipLoggingConfigurationFlags())
logOptions.AddFlags(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.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",
)
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 := logOptions.ValidateAndApply(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())
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: webhookPort,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "controller-leader-elect-capoci",
CertDir: webhookCertDir,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
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)
}
// Setup the context that's going to be used in controllers and for the manager.
ctx := ctrl.SetupSignalHandler()
region, err := ociAuthConfigProvider.Region()
if err != nil {
setupLog.Error(err, "unable to get OCI region from AuthConfigProvider")
os.Exit(1)
}
clientProvider, err := scope.NewClientProvider(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 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 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)
}
}
if feature.Gates.Enabled(feature.OKE) {
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.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 = (&expcontrollers.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 err = (&infrastructurev1beta1.OCICluster{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "OCICluster")
os.Exit(1)
}
if err = (&infrastructurev1beta1.OCIMachineTemplate{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "OCIMachineTemplate")
os.Exit(1)
}
if err = (&infrav1exp.OCIManagedCluster{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "OCIManagedCluster")
os.Exit(1)
}
if err = (&infrav1exp.OCIManagedControlPlane{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "OCIManagedControlPlane")
os.Exit(1)
}
if err = (&infrav1exp.OCIManagedMachinePool{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "OCIManagedMachinePool")
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)
}
}