/
gateway.go
419 lines (330 loc) · 11.8 KB
/
gateway.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
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 gateway
import (
"context"
"os"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/pkg/errors"
"github.com/submariner-io/admiral/pkg/log"
"github.com/submariner-io/admiral/pkg/syncer/broker"
"github.com/submariner-io/admiral/pkg/watcher"
subv1 "github.com/submariner-io/submariner/pkg/apis/submariner.io/v1"
"github.com/submariner-io/submariner/pkg/cable"
"github.com/submariner-io/submariner/pkg/cableengine"
"github.com/submariner-io/submariner/pkg/cableengine/healthchecker"
"github.com/submariner-io/submariner/pkg/cableengine/syncer"
"github.com/submariner-io/submariner/pkg/cidr"
submclientset "github.com/submariner-io/submariner/pkg/client/clientset/versioned"
"github.com/submariner-io/submariner/pkg/controllers/datastoresyncer"
"github.com/submariner-io/submariner/pkg/controllers/tunnel"
"github.com/submariner-io/submariner/pkg/endpoint"
"github.com/submariner-io/submariner/pkg/natdiscovery"
"github.com/submariner-io/submariner/pkg/pod"
"github.com/submariner-io/submariner/pkg/types"
"github.com/submariner-io/submariner/pkg/versions"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/client-go/tools/record"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)
const (
defaultLeaseDuration = 15 * time.Second
defaultRenewDeadline = 10 * time.Second
defaultRetryPeriod = 2 * time.Second
LeaderElectionLockName = "submariner-gateway-lock"
)
type Interface interface {
Run(ctx context.Context) error
}
type LeaderElectionConfig struct {
LeaseDuration time.Duration
RenewDeadline time.Duration
RetryPeriod time.Duration
}
type Config struct {
LeaderElectionConfig
Spec types.SubmarinerSpecification
SyncerConfig broker.SyncerConfig
WatcherConfig watcher.Config
SubmarinerClient submclientset.Interface
KubeClient kubernetes.Interface
LeaderElectionClient kubernetes.Interface
NewCableEngine func(localCluster *types.SubmarinerCluster, localEndpoint *endpoint.Local) cableengine.Engine
NewNATDiscovery func(localEndpoint *endpoint.Local) (natdiscovery.Interface, error)
}
type gatewayType struct {
Config
airGapped bool
cableHealthChecker healthchecker.Interface
cableEngineSyncer *syncer.GatewaySyncer
cableEngine cableengine.Engine
publicIPWatcher *endpoint.PublicIPWatcher
datastoreSyncer *datastoresyncer.DatastoreSyncer
natDiscovery natdiscovery.Interface
gatewayPod *pod.GatewayPod
hostName string
localEndpoint *endpoint.Local
fatalError chan error
leaderComponentsStarted *sync.WaitGroup
recorder record.EventRecorder
}
var logger = log.Logger{Logger: logf.Log.WithName("Gateway")}
func New(config *Config) (Interface, error) {
logger.Info("Initializing the gateway engine")
g := &gatewayType{
Config: *config,
fatalError: make(chan error),
}
if g.LeaseDuration == 0 {
g.LeaseDuration = defaultLeaseDuration
}
if g.RenewDeadline == 0 {
g.RenewDeadline = defaultRenewDeadline
}
if g.RetryPeriod == 0 {
g.RetryPeriod = defaultRetryPeriod
}
var err error
g.hostName, err = os.Hostname()
if err != nil {
return nil, errors.Wrap(err, "error getting hostname")
}
logger.Info("Creating the cable engine")
localCluster := submarinerClusterFrom(&g.Spec)
if g.Spec.CableDriver == "" {
g.Spec.CableDriver = cable.GetDefaultCableDriver()
}
g.Spec.CableDriver = strings.ToLower(g.Spec.CableDriver)
g.airGapped = os.Getenv("AIR_GAPPED_DEPLOYMENT") == "true"
logger.Infof("AIR_GAPPED_DEPLOYMENT is set to %t", g.airGapped)
localEndpointSpec, err := endpoint.GetLocalSpec(&g.Spec, g.KubeClient, g.airGapped)
if err != nil {
return nil, errors.Wrap(err, "error creating local endpoint object")
}
g.localEndpoint = endpoint.NewLocal(localEndpointSpec, g.SyncerConfig.LocalClient, g.Spec.Namespace)
g.cableEngine = g.NewCableEngine(localCluster, g.localEndpoint)
g.natDiscovery, err = g.NewNATDiscovery(g.localEndpoint)
if err != nil {
return nil, errors.Wrap(err, "error creating the NAT discovery handler")
}
logger.Info("Creating the datastore syncer")
g.SyncerConfig.LocalNamespace = g.Spec.Namespace
g.datastoreSyncer = datastoresyncer.New(&g.SyncerConfig, localCluster, g.localEndpoint)
g.initCableHealthChecker()
g.cableEngineSyncer = syncer.NewGatewaySyncer(
g.cableEngine,
g.SubmarinerClient.SubmarinerV1().Gateways(g.Spec.Namespace),
versions.Submariner(), g.cableHealthChecker)
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(logger.V(log.DEBUG).Infof)
g.recorder = eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: "submariner-controller"})
return g, nil
}
func (g *gatewayType) Run(ctx context.Context) error {
if g.Spec.Uninstall {
logger.Info("Uninstalling the submariner gateway engine")
return g.uninstall(ctx)
}
logger.Info("Starting the gateway engine")
g.cableEngine.SetupNATDiscovery(g.natDiscovery)
err := g.natDiscovery.Run(ctx.Done())
if err != nil {
return errors.Wrap(err, "error starting NAT discovery server")
}
g.gatewayPod, err = pod.NewGatewayPod(ctx, g.KubeClient)
if err != nil {
return errors.Wrap(err, "error creating a handler to update the gateway pod")
}
var waitGroup sync.WaitGroup
g.runAsync(&waitGroup, func() {
//nolint:contextcheck // Intentionally not passing the context b/c it can't be used after cancellation.
g.cableEngineSyncer.Run(ctx.Done())
})
if !g.airGapped {
g.initPublicIPWatcher()
}
err = g.startLeaderElection(ctx)
if err != nil {
return errors.Wrap(err, "error starting leader election")
}
select {
case <-ctx.Done():
case fatalErr := <-g.fatalError:
g.cableEngineSyncer.SetGatewayStatusError(ctx, fatalErr)
if err := g.gatewayPod.SetHALabels(ctx, subv1.HAStatusPassive); err != nil {
logger.Warningf("Error updating pod label: %s", err)
}
return fatalErr
}
waitGroup.Wait()
logger.Info("Gateway engine stopped")
return nil
}
func (g *gatewayType) runAsync(waitGroup *sync.WaitGroup, run func()) {
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
run()
}()
}
func (g *gatewayType) startLeaderElection(ctx context.Context) error {
logger.Info("Starting leader election")
g.leaderComponentsStarted = &sync.WaitGroup{}
rl, err := resourcelock.New(resourcelock.LeasesResourceLock, g.Spec.Namespace, LeaderElectionLockName,
g.LeaderElectionClient.CoreV1(), g.LeaderElectionClient.CoordinationV1(), resourcelock.ResourceLockConfig{
Identity: g.hostName + "-submariner-gateway",
EventRecorder: g.recorder,
})
if err != nil {
return errors.Wrap(err, "error creating leader election resource lock")
}
go leaderelection.RunOrDie(ctx, leaderelection.LeaderElectionConfig{
Lock: rl,
LeaseDuration: g.LeaseDuration,
RenewDeadline: g.RenewDeadline,
RetryPeriod: g.RetryPeriod,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: g.onStartedLeading,
OnStoppedLeading: g.onStoppedLeading,
},
})
return nil
}
func (g *gatewayType) onStartedLeading(ctx context.Context) {
logger.Info("Leadership acquired - starting controllers")
if err := g.cableEngine.StartEngine(); err != nil {
g.fatalError <- errors.Wrap(err, "error starting the cable engine")
return
}
g.runAsync(g.leaderComponentsStarted, func() {
msgLogged := atomic.Bool{}
_ = wait.PollUntilContextCancel(ctx, 100*time.Millisecond, true, func(ctx context.Context) (bool, error) {
if err := g.gatewayPod.SetHALabels(ctx, subv1.HAStatusActive); err != nil {
if msgLogged.CompareAndSwap(false, true) {
logger.Warningf("Error updating pod label to active: %s", err)
}
return false, nil
}
return true, nil
})
})
g.runAsync(g.leaderComponentsStarted, func() {
err := g.datastoreSyncer.Start(ctx)
if errors.Is(err, context.Canceled) {
return
}
if err != nil {
g.fatalError <- errors.Wrap(err, "error running the datastore syncer")
}
})
g.runAsync(g.leaderComponentsStarted, func() {
watcherConfig := g.WatcherConfig
if err := tunnel.StartController(g.cableEngine, g.Spec.Namespace, &watcherConfig, ctx.Done()); err != nil {
g.fatalError <- errors.Wrap(err, "error running the tunnel controller")
}
})
if g.cableHealthChecker != nil {
g.runAsync(g.leaderComponentsStarted, func() {
if err := g.cableHealthChecker.Start(ctx.Done()); err != nil {
logger.Errorf(err, "Error starting healthChecker")
}
})
}
if g.publicIPWatcher != nil {
go g.publicIPWatcher.Run(ctx.Done())
}
}
func (g *gatewayType) onStoppedLeading() {
logger.Info("Leadership lost")
// Make sure all the components were at least started before we try to restart.
g.leaderComponentsStarted.Wait()
if g.cableHealthChecker != nil {
g.cableHealthChecker.Stop()
}
g.cableEngine.Stop()
logger.Info("Controllers stopped")
if err := g.gatewayPod.SetHALabels(context.Background(), subv1.HAStatusPassive); err != nil {
logger.Warningf("Error updating pod label to passive: %s", err)
}
err := g.startLeaderElection(context.Background())
if err != nil {
g.fatalError <- errors.Wrap(err, "error restarting leader election")
}
}
func (g *gatewayType) initPublicIPWatcher() {
publicIPConfig := &endpoint.PublicIPWatcherConfig{
SubmSpec: &g.Spec,
K8sClient: g.KubeClient,
LocalEndpoint: g.localEndpoint,
}
g.publicIPWatcher = endpoint.NewPublicIPWatcher(publicIPConfig)
}
func (g *gatewayType) initCableHealthChecker() {
var err error
if !g.Spec.HealthCheckEnabled {
logger.Info("The CableEngine HealthChecker is disabled")
} else {
watcherConfig := g.WatcherConfig
g.cableHealthChecker, err = healthchecker.New(&healthchecker.Config{
WatcherConfig: &watcherConfig,
EndpointNamespace: g.Spec.Namespace,
ClusterID: g.Spec.ClusterID,
PingInterval: g.Spec.HealthCheckInterval,
MaxPacketLossCount: g.Spec.HealthCheckMaxPacketLossCount,
})
if err != nil {
logger.Errorf(err, "Error creating healthChecker")
}
}
}
func (g *gatewayType) uninstall(ctx context.Context) error {
err := g.cableEngine.StartEngine()
if err != nil {
// As we are in the process of cleaning up, ignore any initialization errors.
logger.Errorf(err, "Error starting the cable driver")
}
// The Gateway object has to be deleted before invoking the cableEngine.Cleanup
g.cableEngineSyncer.CleanupGatewayEntry(ctx)
err = g.cableEngine.Cleanup()
if err != nil {
logger.Errorf(err, "Error while cleaning up of cable drivers")
}
dsErr := g.datastoreSyncer.Cleanup(ctx)
return errors.Wrap(dsErr, "Error cleaning up the datastore")
}
func submarinerClusterFrom(submSpec *types.SubmarinerSpecification) *types.SubmarinerCluster {
// The Cluster resource requires a value for the GlobalCIDR.
globalCIDR := submSpec.GlobalCidr
if globalCIDR == nil {
globalCIDR = []string{}
}
return &types.SubmarinerCluster{
ID: submSpec.ClusterID,
Spec: subv1.ClusterSpec{
ClusterID: submSpec.ClusterID,
ColorCodes: []string{"blue"}, // This is a fake value, used only for upgrade purposes
ServiceCIDR: cidr.ExtractIPv4Subnets(submSpec.ServiceCidr),
ClusterCIDR: cidr.ExtractIPv4Subnets(submSpec.ClusterCidr),
GlobalCIDR: globalCIDR,
},
}
}