-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
157 lines (136 loc) · 4.54 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
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"flag"
"net/http"
"net/http/pprof"
"os"
"github.com/spf13/pflag"
runv1alpha3 "github.com/vmware-tanzu/tanzu-framework/apis/run/v1alpha3"
akov1beta1 "github.com/vmware/load-balancer-and-ingress-services-for-kubernetes/pkg/apis/ako/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/component-base/logs"
logsv1 "k8s.io/component-base/logs/api/v1"
"k8s.io/klog/v2"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
akoov1alpha1 "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/api/v1alpha1"
"github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/controllers"
ako_operator "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/ako-operator"
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
logOptions = logs.NewOptions()
metricsAddr string
enableLeaderElection bool
profilerAddress string
)
func initLog() {
ctrl.SetLogger(klog.Background())
}
func init() {
initLog()
// ignoring errors
_ = clientgoscheme.AddToScheme(scheme)
_ = clusterv1.AddToScheme(scheme)
_ = akoov1alpha1.AddToScheme(scheme)
_ = akov1beta1.AddToScheme(scheme)
_ = runv1alpha3.AddToScheme(scheme)
}
// InitFlags initializes the flags.
func InitFlags(fs *pflag.FlagSet) {
logsv1.AddFlags(logOptions, fs)
fs.StringVar(&metricsAddr, "metrics-addr", "localhost:8080", "The address the metric endpoint binds to.")
fs.BoolVar(&enableLeaderElection, "enable-leader-election", false, "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
fs.StringVar(&profilerAddress, "profiler-addr", "", "Bind address to expose the pprof profiler")
}
func main() {
InitFlags(pflag.CommandLine)
pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
// Set log level 2 as default.
if err := pflag.CommandLine.Set("v", "2"); err != nil {
setupLog.Error(err, "Failed to set default log level")
os.Exit(1)
}
pflag.Parse()
if err := logsv1.ValidateAndApply(logOptions, nil); err != nil {
setupLog.Error(err, "Unable to start manager")
os.Exit(1)
}
if profilerAddress != "" {
setupLog.Info(
"Profiler listening for requests",
"profiler-addr", profilerAddress)
go runProfiler(profilerAddress)
}
// Default webserver port is 9443.
mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{
Scheme: scheme,
LeaderElection: enableLeaderElection,
Metrics: metricsserver.Options{
BindAddress: metricsAddr,
},
Client: client.Options{
Cache: &client.CacheOptions{
DisableFor: []client.Object{
&corev1.ConfigMap{},
&corev1.Secret{},
},
},
},
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
err = controllers.SetupReconcilers(mgr)
if err != nil {
setupLog.Error(err, "Unable to setup reconcilers")
os.Exit(1)
}
printRunningEnv()
//setup webhook here
if err = (&akoov1alpha1.AKODeploymentConfig{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "AKODeploymentConfig")
os.Exit(1)
}
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
func runProfiler(addr string) {
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
err := http.ListenAndServe(addr, mux)
if err != nil {
setupLog.Error(err, "unable to start listening")
}
}
func printRunningEnv() {
if ako_operator.IsBootStrapCluster() {
setupLog.Info("AKO Operator Running in Bootstrap Kind Cluster")
} else {
setupLog.Info("AKO Operator Running in Management Cluster")
}
if ako_operator.IsClusterClassEnabled() {
setupLog.Info("AKO Operator Running in Cluster Class Based Cluster")
} else {
setupLog.Info("AKO Operator Running in Legacy Cluster")
}
}