forked from kubernetes-sigs/aws-load-balancer-controller
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
202 lines (180 loc) · 7.83 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
/*
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 (
"github.com/go-logr/logr"
"github.com/spf13/pflag"
zapraw "go.uber.org/zap"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"os"
elbv2api "sigs.k8s.io/aws-load-balancer-controller/apis/elbv2/v1beta1"
elbv2controller "sigs.k8s.io/aws-load-balancer-controller/controllers/elbv2"
"sigs.k8s.io/aws-load-balancer-controller/controllers/ingress"
"sigs.k8s.io/aws-load-balancer-controller/controllers/service"
"sigs.k8s.io/aws-load-balancer-controller/pkg/aws"
"sigs.k8s.io/aws-load-balancer-controller/pkg/aws/throttle"
"sigs.k8s.io/aws-load-balancer-controller/pkg/config"
"sigs.k8s.io/aws-load-balancer-controller/pkg/inject"
"sigs.k8s.io/aws-load-balancer-controller/pkg/k8s"
"sigs.k8s.io/aws-load-balancer-controller/pkg/networking"
"sigs.k8s.io/aws-load-balancer-controller/pkg/runtime"
"sigs.k8s.io/aws-load-balancer-controller/pkg/targetgroupbinding"
"sigs.k8s.io/aws-load-balancer-controller/pkg/version"
corewebhook "sigs.k8s.io/aws-load-balancer-controller/webhooks/core"
elbv2webhook "sigs.k8s.io/aws-load-balancer-controller/webhooks/elbv2"
networkingwebhook "sigs.k8s.io/aws-load-balancer-controller/webhooks/networking"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/metrics"
// +kubebuilder:scaffold:imports
)
var (
scheme = k8sruntime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
_ = clientgoscheme.AddToScheme(scheme)
_ = elbv2api.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
}
func main() {
infoLogger := getLoggerWithLogLevel("info")
infoLogger.Info("version",
"GitVersion", version.GitVersion,
"GitCommit", version.GitCommit,
"BuildDate", version.BuildDate,
)
controllerCFG, err := loadControllerConfig()
if err != nil {
infoLogger.Error(err, "unable to load controller config")
os.Exit(1)
}
ctrl.SetLogger(getLoggerWithLogLevel(controllerCFG.LogLevel))
cloud, err := aws.NewCloud(controllerCFG.AWSConfig, metrics.Registry)
if err != nil {
setupLog.Error(err, "unable to initialize AWS cloud")
os.Exit(1)
}
restCFG, err := config.BuildRestConfig(controllerCFG.RuntimeConfig)
if err != nil {
setupLog.Error(err, "unable to build REST config")
os.Exit(1)
}
rtOpts := config.BuildRuntimeOptions(controllerCFG.RuntimeConfig, scheme)
mgr, err := ctrl.NewManager(restCFG, rtOpts)
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
config.ConfigureWebhookServerCert(controllerCFG.RuntimeConfig, mgr)
clientSet, err := kubernetes.NewForConfig(mgr.GetConfig())
if err != nil {
setupLog.Error(err, "unable to obtain clientSet")
os.Exit(1)
}
podInfoRepo := k8s.NewDefaultPodInfoRepo(clientSet.CoreV1().RESTClient(), rtOpts.Namespace, ctrl.Log)
finalizerManager := k8s.NewDefaultFinalizerManager(mgr.GetClient(), ctrl.Log)
sgManager := networking.NewDefaultSecurityGroupManager(cloud.EC2(), ctrl.Log)
sgReconciler := networking.NewDefaultSecurityGroupReconciler(sgManager, ctrl.Log)
azInfoProvider := networking.NewDefaultAZInfoProvider(cloud.EC2(), ctrl.Log.WithName("az-info-provider"))
subnetResolver := networking.NewDefaultSubnetsResolver(azInfoProvider, cloud.EC2(), cloud.VpcID(), controllerCFG.ClusterName, ctrl.Log.WithName("subnets-resolver"))
vpcResolver := networking.NewDefaultVPCResolver(cloud.EC2(), cloud.VpcID(), ctrl.Log.WithName("vpc-resolver"))
tgbResManager := targetgroupbinding.NewDefaultResourceManager(mgr.GetClient(), cloud.ELBV2(), cloud.EC2(),
podInfoRepo, sgManager, sgReconciler, cloud.VpcID(), controllerCFG.ClusterName, mgr.GetEventRecorderFor("targetGroupBinding"), ctrl.Log)
ingGroupReconciler := ingress.NewGroupReconciler(cloud, mgr.GetClient(), mgr.GetEventRecorderFor("ingress"),
finalizerManager, sgManager, sgReconciler, subnetResolver,
controllerCFG, ctrl.Log.WithName("controllers").WithName("ingress"))
svcReconciler := service.NewServiceReconciler(cloud, mgr.GetClient(), mgr.GetEventRecorderFor("service"),
finalizerManager, sgManager, sgReconciler, subnetResolver, vpcResolver,
controllerCFG, ctrl.Log.WithName("controllers").WithName("service"))
tgbReconciler := elbv2controller.NewTargetGroupBindingReconciler(mgr.GetClient(), mgr.GetEventRecorderFor("targetGroupBinding"),
finalizerManager, tgbResManager,
controllerCFG, ctrl.Log.WithName("controllers").WithName("targetGroupBinding"))
ctx := ctrl.SetupSignalHandler()
if err = ingGroupReconciler.SetupWithManager(ctx, mgr, clientSet); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Ingress")
os.Exit(1)
}
if err = svcReconciler.SetupWithManager(ctx, mgr); err != nil {
setupLog.Error(err, "Unable to create controller", "controller", "Service")
os.Exit(1)
}
if err := tgbReconciler.SetupWithManager(ctx, mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "TargetGroupBinding")
os.Exit(1)
}
// Add liveness probe
err = mgr.AddHealthzCheck("health-ping", healthz.Ping)
setupLog.Info("adding health check for controller")
if err != nil {
setupLog.Error(err, "unable add a health check")
os.Exit(1)
}
podReadinessGateInjector := inject.NewPodReadinessGate(controllerCFG.PodWebhookConfig,
mgr.GetClient(), ctrl.Log.WithName("pod-readiness-gate-injector"))
corewebhook.NewPodMutator(podReadinessGateInjector).SetupWithManager(mgr)
elbv2webhook.NewTargetGroupBindingMutator(cloud.ELBV2(), ctrl.Log).SetupWithManager(mgr)
elbv2webhook.NewTargetGroupBindingValidator(mgr.GetClient(), ctrl.Log).SetupWithManager(mgr)
networkingwebhook.NewIngressValidator(mgr.GetClient(), controllerCFG.IngressConfig, ctrl.Log).SetupWithManager(mgr)
//+kubebuilder:scaffold:builder
go func() {
setupLog.Info("starting podInfo repo")
if err := podInfoRepo.Start(ctx); err != nil {
setupLog.Error(err, "problem running podInfo repo")
os.Exit(1)
}
}()
if err := podInfoRepo.WaitForCacheSync(ctx); err != nil {
setupLog.Error(err, "problem wait for podInfo repo sync")
os.Exit(1)
}
if err := mgr.Start(ctx); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
// loadControllerConfig loads the controller configuration.
func loadControllerConfig() (config.ControllerConfig, error) {
defaultAWSThrottleCFG := throttle.NewDefaultServiceOperationsThrottleConfig()
controllerCFG := config.ControllerConfig{
AWSConfig: aws.CloudConfig{ThrottleConfig: defaultAWSThrottleCFG},
}
fs := pflag.NewFlagSet("", pflag.ExitOnError)
controllerCFG.BindFlags(fs)
if err := fs.Parse(os.Args); err != nil {
return config.ControllerConfig{}, err
}
if err := controllerCFG.Validate(); err != nil {
return config.ControllerConfig{}, err
}
return controllerCFG, nil
}
// getLoggerWithLogLevel returns logger with specific log level.
func getLoggerWithLogLevel(logLevel string) logr.Logger {
var zapLevel zapraw.AtomicLevel
switch logLevel {
case "info":
zapLevel = zapraw.NewAtomicLevelAt(zapraw.InfoLevel)
case "debug":
zapLevel = zapraw.NewAtomicLevelAt(zapraw.DebugLevel)
default:
zapLevel = zapraw.NewAtomicLevelAt(zapraw.InfoLevel)
}
logger := zap.New(zap.UseDevMode(false),
zap.Level(zapLevel),
zap.StacktraceLevel(zapraw.NewAtomicLevelAt(zapraw.FatalLevel)))
return runtime.NewConciseLogger(logger)
}