-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.go
282 lines (250 loc) · 9.57 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
/*
Copyright 2022 The Kubernetes Authors.
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"
"fmt"
"os"
"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"
flag "github.com/spf13/pflag"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
goflag "flag"
_ "k8s.io/client-go/plugin/pkg/client/auth"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/component-base/logs"
logsv1 "k8s.io/component-base/logs/api/v1"
"sigs.k8s.io/cluster-api/util/flags"
"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/controller"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/manager"
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
infrav1b1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta1"
infrav1b2 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta2"
infrav1b3 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3"
"sigs.k8s.io/cluster-api-provider-cloudstack/controllers"
"sigs.k8s.io/cluster-api-provider-cloudstack/controllers/utils"
//+kubebuilder:scaffold:imports
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
tlsOptions = flags.TLSOptions{}
logOptions = logs.NewOptions()
)
func init() {
klog.InitFlags(nil)
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(clusterv1.AddToScheme(scheme))
utilruntime.Must(infrav1b1.AddToScheme(scheme))
utilruntime.Must(infrav1b2.AddToScheme(scheme))
utilruntime.Must(infrav1b3.AddToScheme(scheme))
utilruntime.Must(controlplanev1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
type managerOpts struct {
CloudConfigFile string
MetricsAddr string
EnableLeaderElection bool
ProbeAddr string
WatchingNamespace string
WatchFilterValue string
CertDir string
CloudStackClusterConcurrency int
CloudStackMachineConcurrency int
CloudStackAffinityGroupConcurrency int
CloudStackFailureDomainConcurrency int
EnableCloudStackCksSync bool
}
func setFlags() *managerOpts {
opts := &managerOpts{}
flag.StringVar(
&opts.CloudConfigFile,
"cloud-config-file",
"/config/cloud-config",
"Overrides the default path to the cloud-config file that contains the CloudStack credentials.")
flag.StringVar(
&opts.MetricsAddr,
"metrics-bind-addr",
"localhost:8080",
"The address the metric endpoint binds to.")
flag.StringVar(
&opts.ProbeAddr,
"health-probe-bind-address",
":8081",
"The address the probe endpoint binds to.")
flag.BoolVar(
&opts.EnableLeaderElection,
"leader-elect",
false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.StringVar(
&opts.WatchingNamespace,
"namespace",
"",
"Namespace that the controller watches to reconcile cluster-api objects. If unspecified, "+
"the controller watches for cluster-api objects across all namespaces.")
flag.StringVar( // TODO: use filter per CAPI book instructions in upgrade to v1alpha4.
&opts.WatchFilterValue,
"watch-filter",
"",
fmt.Sprintf(
"Label value that the controller watches to reconcile cluster-api objects. "+
"Label key is always %s. If unspecified, the controller watches for all cluster-api objects.",
clusterv1.WatchLabel))
flag.StringVar(
&opts.CertDir,
"webhook-cert-dir",
"/tmp/k8s-webhook-server/serving-certs/",
"Specify the directory where webhooks will get tls certificates.")
flag.IntVar(
&opts.CloudStackClusterConcurrency,
"cloudstackcluster-concurrency",
10,
"Maximum concurrent reconciles for CloudStackCluster resources",
)
flag.IntVar(
&opts.CloudStackMachineConcurrency,
"cloudstackmachine-concurrency",
10,
"Maximum concurrent reconciles for CloudStackMachine resources",
)
flag.IntVar(
&opts.CloudStackAffinityGroupConcurrency,
"cloudstackaffinitygroup-concurrency",
5,
"Maximum concurrent reconciles for CloudStackAffinityGroup resources",
)
flag.IntVar(
&opts.CloudStackFailureDomainConcurrency,
"cloudstackfailuredomain-concurrency",
5,
"Maximum concurrent reconciles for CloudStackFailureDomain resources",
)
flag.BoolVar(
&opts.EnableCloudStackCksSync,
"enable-cloudstack-cks-sync",
false,
"Enable syncing of CloudStack clusters and machines with CKS clusters and machines",
)
return opts
}
func main() {
opts := setFlags() // Add our options to flag set.
logsv1.AddFlags(logOptions, flag.CommandLine)
flags.AddTLSOptions(flag.CommandLine, &tlsOptions)
flag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
flag.CommandLine.AddGoFlagSet(goflag.CommandLine) // Merge klog's goflag flags into the pflags.
flag.Parse()
if err := logsv1.ValidateAndApply(logOptions, nil); err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
ctrl.SetLogger(klogr.New())
tlsOptionOverrides, err := flags.GetTLSOptionOverrideFuncs(tlsOptions)
if err != nil {
setupLog.Error(err, "unable to add TLS settings to the webhook server")
os.Exit(1)
}
// Create the controller manager.
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: opts.MetricsAddr,
Port: 9443,
HealthProbeBindAddress: opts.ProbeAddr,
LeaderElection: opts.EnableLeaderElection,
LeaderElectionID: "capc-leader-election-controller",
Namespace: opts.WatchingNamespace,
CertDir: opts.CertDir,
TLSOpts: tlsOptionOverrides,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
// Register reconcilers with the controller manager.
base := utils.ReconcilerBase{
K8sClient: mgr.GetClient(),
BaseLogger: ctrl.Log.WithName("controllers"),
Recorder: mgr.GetEventRecorderFor("capc-controller-manager"),
Scheme: mgr.GetScheme(),
}
ctx := ctrl.SetupSignalHandler()
setupReconcilers(ctx, base, *opts, mgr)
infrav1b3.K8sClient = base.K8sClient
// +kubebuilder:scaffold:builder
// Add health and ready checks.
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)
}
// Start the controller manager.
if err = (&infrav1b3.CloudStackCluster{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "CloudStackCluster")
os.Exit(1)
}
if err = (&infrav1b3.CloudStackMachine{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "CloudStackMachine")
os.Exit(1)
}
if err = (&infrav1b3.CloudStackMachineTemplate{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "CloudStackMachineTemplate")
os.Exit(1)
}
setupLog.Info("starting manager")
if err := mgr.Start(ctx); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
func setupReconcilers(ctx context.Context, base utils.ReconcilerBase, opts managerOpts, mgr manager.Manager) {
if err := (&controllers.CloudStackClusterReconciler{ReconcilerBase: base}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: opts.CloudStackClusterConcurrency}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CloudStackCluster")
os.Exit(1)
}
if err := (&controllers.CloudStackMachineReconciler{ReconcilerBase: base}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: opts.CloudStackMachineConcurrency}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CloudStackMachine")
os.Exit(1)
}
if err := (&controllers.CloudStackIsoNetReconciler{ReconcilerBase: base}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CloudStackIsoNetReconciler")
os.Exit(1)
}
if err := (&controllers.CloudStackAffinityGroupReconciler{ReconcilerBase: base}).SetupWithManager(mgr, controller.Options{MaxConcurrentReconciles: opts.CloudStackAffinityGroupConcurrency}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CloudStackAffinityGroup")
os.Exit(1)
}
if err := (&controllers.CloudStackFailureDomainReconciler{ReconcilerBase: base}).SetupWithManager(mgr, controller.Options{MaxConcurrentReconciles: opts.CloudStackFailureDomainConcurrency}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CloudStackFailureDomain")
os.Exit(1)
}
if opts.EnableCloudStackCksSync {
if err := (&controllers.CksClusterReconciler{ReconcilerBase: base}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CKSClusterController")
os.Exit(1)
}
if err := (&controllers.CksMachineReconciler{ReconcilerBase: base}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CKSMachineController")
os.Exit(1)
}
}
}