forked from kubernetes-retired/cluster-api-provider-nested
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
156 lines (136 loc) · 5.39 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
/*
Copyright 2019 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 (
"flag"
"fmt"
"os"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/apis"
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/controller"
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/webhook"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/healthz"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/controller/constants"
logrutil "sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/controller/util/logr"
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/version"
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/version/verflag"
)
func main() {
var (
logFile string
metricsAddr string
healthAddr string
masterProvisioner string
leaderElection bool
leaderElectionCmName string
maxConcurrentReconciles int
versionOpt bool
disableStacktrace bool
enableWebhook bool
)
flag.StringVar(&metricsAddr, "metrics-addr", ":0", "The address the metric endpoint binds to.")
flag.StringVar(&healthAddr, "health-addr", ":8080", "The address of the healthz/readyz endpoint binds to.")
flag.StringVar(&masterProvisioner, "master-prov", "native",
"The underlying platform that will provision master for virtualcluster.")
flag.BoolVar(&leaderElection, "leader-election", true, "If enable leaderelection for vc-manager")
flag.StringVar(&leaderElectionCmName, "le-cm-name", "vc-manager-leaderelection-lock",
"The name of the configmap that will be used as the resourcelook for leaderelection")
flag.IntVar(&maxConcurrentReconciles, "num-reconciles", 10,
"The max number reconcilers of virtualcluster controller")
flag.StringVar(&logFile, "log-file", "", "The path of the logfile, if not set, only log to the stderr")
flag.BoolVar(&versionOpt, "version", false, "Print the version information")
flag.BoolVar(&disableStacktrace, "disable-stacktrace", false, "If set, the automatic stacktrace is disabled")
flag.BoolVar(&enableWebhook, "enable-webhook", false, "If set, the virtualcluster webhook is enabled")
flag.Parse()
// print version information
if versionOpt {
fmt.Printf("VirtualCluster %s\n", verflag.GetVersion(version.Get()))
os.Exit(0)
}
loggr, err := logrutil.NewLogger(logFile, disableStacktrace)
if err != nil {
panic(fmt.Sprintf("fail to initialize logr: %s", err))
}
logf.SetLogger(loggr)
log := logf.Log.WithName("entrypoint")
// Get a config to talk to the apiserver
log.Info("setting up client for manager")
cfg, err := config.GetConfig()
if err != nil {
log.Error(err, "unable to set up client config")
os.Exit(1)
}
// Create a new Cmd to provide shared dependencies and start components
log.Info("setting up manager")
mgrOpt := manager.Options{
MetricsBindAddress: metricsAddr,
LeaderElection: leaderElection,
LeaderElectionID: leaderElectionCmName,
CertDir: constants.VirtualClusterWebhookCertDir,
Port: constants.VirtualClusterWebhookPort,
HealthProbeBindAddress: healthAddr,
}
mgr, err := ctrl.NewManager(cfg, mgrOpt)
if err != nil {
log.Error(err, "unable to set up overall controller manager")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("ping", healthz.Ping); err != nil {
log.Error(err, "unable to create ready check")
os.Exit(1)
}
if err := mgr.AddHealthzCheck("ping", healthz.Ping); err != nil {
log.Error(err, "unable to create health check")
os.Exit(1)
}
log.Info("Registering Components.")
// Setup Scheme for all resources
log.Info("setting up scheme")
if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
log.Error(err, "unable add APIs to scheme")
os.Exit(1)
}
if err := clusterv1.AddToScheme(mgr.GetScheme()); err != nil {
log.Error(err, "unable add APIs to scheme")
os.Exit(1)
}
// Setup all Controllers
log.Info("Setting up controller")
if err := (&controller.Controllers{
Log: log.WithName("Controllers"),
Client: mgr.GetClient(),
ProvisionerName: masterProvisioner,
MaxConcurrentReconciles: maxConcurrentReconciles,
}).SetupWithManager(mgr); err != nil {
log.Error(err, "unable to register controllers to the manager")
os.Exit(1)
}
if enableWebhook == true {
log.Info("setting up webhooks")
if err := webhook.AddToManager(mgr, mgrOpt.CertDir); err != nil {
log.Error(err, "unable to register webhooks to the manager")
os.Exit(1)
}
}
// Start the Cmd
log.Info("Starting the Cmd.")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
log.Error(err, "unable to run the manager")
os.Exit(1)
}
}