-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
243 lines (204 loc) · 8.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
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
/*
Copyright 2022 The l7mp/stunner team.
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"
"flag"
"fmt"
"os"
"strings"
"time"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"
"go.uber.org/zap/zapcore"
"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/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gwapiv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
stnrv1 "github.com/l7mp/stunner/pkg/apis/v1"
"github.com/l7mp/stunner/pkg/buildinfo"
"github.com/l7mp/stunner-gateway-operator/internal/config"
"github.com/l7mp/stunner-gateway-operator/internal/operator"
"github.com/l7mp/stunner-gateway-operator/internal/renderer"
"github.com/l7mp/stunner-gateway-operator/internal/updater"
opdefault "github.com/l7mp/stunner-gateway-operator/pkg/config"
stnrgwv1 "github.com/l7mp/stunner-gateway-operator/api/v1"
stnrgwv1a1 "github.com/l7mp/stunner-gateway-operator/api/v1alpha1"
)
const (
envVarMode = "STUNNER_GATEWAY_OPERATOR_DATAPLANE_MODE"
envVarAddress = "STUNNER_GATEWAY_OPERATOR_ADDRESS"
)
var (
scheme = runtime.NewScheme()
version = "dev"
commitHash = "n/a"
buildDate = "<unknown>"
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(gwapiv1a2.AddToScheme(scheme))
utilruntime.Must(gwapiv1.AddToScheme(scheme))
utilruntime.Must(stnrgwv1a1.AddToScheme(scheme))
utilruntime.Must(stnrgwv1.AddToScheme(scheme))
}
func main() {
var controllerName, dataplaneMode, metricsAddr, cdsAddr, throttleTimeout, probeAddr string
var enableLeaderElection, enableEDS, disableEndpontSliceController, enableFinalizer bool
flag.StringVar(&controllerName, "controller-name", opdefault.DefaultControllerName,
"The conroller name to be used in the GatewayClass resource to bind it to this operator.")
flag.StringVar(&throttleTimeout, "throttle-timeout", opdefault.DefaultThrottleTimeout.String(),
"Time interval to wait between subsequent config renders.")
flag.BoolVar(&enableEDS, "endpoint-discovery", opdefault.DefaultEnableEndpointDiscovery,
fmt.Sprintf("Enable endpoint discovery, default: %t.", opdefault.DefaultEnableEndpointDiscovery))
flag.StringVar(&dataplaneMode, "dataplane-mode", opdefault.DefaultDataplaneMode,
`Managed dataplane mode: either "managed" (automatic dataplane provisioning using the config discovery service) or "legacy" (dataplane(s) provided by the user).`)
flag.StringVar(&cdsAddr, "config-discovery-address", stnrv1.DefaultConfigDiscoveryAddress, `Config discovery server endpoint.`)
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.BoolVar(&disableEndpontSliceController, "disable-endpontslice-controller", false,
"Disable the EndpointSlice controller and fall back to the legacy Endpoints controller.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.BoolVar(&enableFinalizer, "enable-finalizer", opdefault.DefaultEnableFinalizer,
"Clean up allocated resources and invalidate resource statuses on operator exit.")
opts := zap.Options{
Development: true,
DestWriter: os.Stderr,
StacktraceLevel: zapcore.Level(3),
TimeEncoder: zapcore.RFC3339NanoTimeEncoder,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()
logger := zap.New(zap.UseFlagOptions(&opts))
ctrl.SetLogger(logger.WithName("ctrl-runtime"))
setupLog := logger.WithName("setup")
buildInfo := buildinfo.BuildInfo{Version: version, CommitHash: commitHash, BuildDate: buildDate}
setupLog.Info(fmt.Sprintf("starting STUNner gateway operator %s", buildInfo.String()))
config.EnableEndpointDiscovery = enableEDS
config.EndpointSliceAvailable = !disableEndpontSliceController // controller may override this
config.EnableFinalizer = enableFinalizer
setupLog.Info("operator flags",
"endpoint discovery", config.EnableEndpointDiscovery,
"endpointslice-controller", config.EndpointSliceAvailable,
"finalizer", config.EnableFinalizer)
if dataplaneMode == opdefault.DefaultDataplaneMode {
// dataplane mode not overrridden on the command line: use env var
envMode, ok := os.LookupEnv(envVarMode)
if ok {
dataplaneMode = envMode
}
}
config.DataplaneMode = config.NewDataplaneMode(dataplaneMode)
setupLog.Info("dataplane mode", "mode", config.DataplaneMode.String())
// CDS address not overrridden on the command line: use env var
config.ConfigDiscoveryAddress = cdsAddr
if podAddr, ok := os.LookupEnv(envVarAddress); ok {
// default port
as := strings.Split(cdsAddr, ":")
if len(as) != 2 || as[1] == "" {
setupLog.Info("invalid CDS server address", "address", cdsAddr)
os.Exit(1)
}
config.ConfigDiscoveryAddress = fmt.Sprintf("%s:%s", podAddr, as[1])
}
setupLog.Info("config discovery server", "local-addr", cdsAddr,
"remote-addr", config.ConfigDiscoveryAddress)
if d, err := time.ParseDuration(throttleTimeout); err != nil {
setupLog.Info("setting rate-limiting (throttle timeout)", "timeout", throttleTimeout)
config.ThrottleTimeout = d
}
setupLog.Info("setting up Kubernetes controller manager")
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{
BindAddress: metricsAddr,
},
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "92062b70.l7mp.io",
})
if err != nil {
setupLog.Error(err, "unable to set up Kubernetes controller manager")
os.Exit(1)
}
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("setting up STUNner config renderer")
r := renderer.NewRenderer(renderer.RendererConfig{
Scheme: scheme,
Logger: logger,
})
setupLog.Info("setting up updater client")
u := updater.NewUpdater(updater.UpdaterConfig{
Manager: mgr,
Logger: logger,
})
setupLog.Info("setting up CDS server", "address", cdsAddr)
c := config.NewCDSServer(cdsAddr, logger)
setupLog.Info("setting up operator")
op := operator.NewOperator(operator.OperatorConfig{
ControllerName: controllerName,
Manager: mgr,
RenderCh: r.GetRenderChannel(),
ConfigCh: c.GetConfigUpdateChannel(),
UpdaterCh: u.GetUpdaterChannel(),
Logger: logger,
})
r.SetOperatorChannel(op.GetOperatorChannel())
u.SetAckChannel(op.GetOperatorChannel())
op.SetProgressReporters(r, u, c)
// create a general context, which will be canceled by the operator
mgrCtx, mgrCancel := context.WithCancel(context.Background())
defer mgrCancel()
setupLog.Info("starting renderer thread")
if err := r.Start(mgrCtx); err != nil {
setupLog.Error(err, "problem running renderer")
os.Exit(1)
}
setupLog.Info("starting updater thread")
if err := u.Start(mgrCtx); err != nil {
setupLog.Error(err, "could not run updater")
os.Exit(1)
}
setupLog.Info("starting config discovery server")
if err := c.Start(mgrCtx); err != nil {
setupLog.Error(err, "could not run config discovery server")
os.Exit(1)
}
opCtx := ctrl.SetupSignalHandler()
setupLog.Info("starting operator thread")
if err := op.Start(opCtx, mgrCancel); err != nil {
setupLog.Error(err, "problem running operator")
os.Exit(1)
}
setupLog.Info("starting Kubernetes controller manager")
if err := mgr.Start(mgrCtx); err != nil {
setupLog.Error(err, "problem running manager")
// no way to gracefully terminate: give up and exit with an error
os.Exit(1)
}
}