-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
150 lines (135 loc) · 4.36 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
package main
import (
"flag"
"os"
"strings"
routev1 "github.com/openshift/api/route/v1"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
configv1alpha1 "github.com/six-group/haproxy-operator/apis/config/v1alpha1"
proxyv1alpha1 "github.com/six-group/haproxy-operator/apis/proxy/v1alpha1"
"github.com/six-group/haproxy-operator/controllers/config"
"github.com/six-group/haproxy-operator/controllers/instance"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
crzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
)
const envLeaderElect = "LEADER_ELECT"
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(configv1alpha1.AddToScheme(scheme))
utilruntime.Must(proxyv1alpha1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
func main() {
var metricsAddr string
var probeAddr string
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.Parse()
setupLogging()
setupLog.Info("starting operator")
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{
BindAddress: metricsAddr,
},
HealthProbeBindAddress: probeAddr,
LeaderElection: strings.EqualFold(os.Getenv(envLeaderElect), "true"),
LeaderElectionID: "acc50d8e.haproxy.com",
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
// Inspect cluster to verify availability of extra features and setup schema
if err := instance.InspectCluster(); err != nil {
setupLog.Info("unable to inspect cluster")
}
if instance.IsPrometheusAPIAvailable() {
if err := monitoringv1.AddToScheme(mgr.GetScheme()); err != nil {
setupLog.Error(err, "")
os.Exit(1)
}
}
if instance.IsRouteAPIAvailable() {
if err := routev1.Install(mgr.GetScheme()); err != nil {
setupLog.Error(err, "")
os.Exit(1)
}
}
if err = (&instance.Reconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Instance")
os.Exit(1)
}
if err = (&config.Reconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Object: &configv1alpha1.Listen{},
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Listen")
os.Exit(1)
}
if err = (&config.Reconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Object: &configv1alpha1.Frontend{},
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Frontend")
os.Exit(1)
}
if err = (&config.Reconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Object: &configv1alpha1.Backend{},
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Backend")
os.Exit(1)
}
if err = (&config.Reconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Object: &configv1alpha1.Resolver{},
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Resolver")
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(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
func setupLogging() {
encCfg := zap.NewProductionEncoderConfig()
encCfg.EncodeTime = zapcore.ISO8601TimeEncoder
opts := crzap.Options{
Encoder: zapcore.NewJSONEncoder(encCfg),
}
logger := crzap.New(crzap.UseFlagOptions(&opts))
ctrl.SetLogger(logger)
// replace klog logger
klog.SetLogger(ctrl.Log)
}