-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
root.go
740 lines (635 loc) · 24.9 KB
/
root.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package cmd
import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
"github.com/cilium/hive/cell"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
xrate "golang.org/x/time/rate"
"google.golang.org/grpc"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
operatorApi "github.com/cilium/cilium/api/v1/operator/server"
ciliumdbg "github.com/cilium/cilium/cilium-dbg/cmd"
"github.com/cilium/cilium/operator/api"
"github.com/cilium/cilium/operator/auth"
"github.com/cilium/cilium/operator/endpointgc"
"github.com/cilium/cilium/operator/identitygc"
operatorK8s "github.com/cilium/cilium/operator/k8s"
operatorMetrics "github.com/cilium/cilium/operator/metrics"
operatorOption "github.com/cilium/cilium/operator/option"
"github.com/cilium/cilium/operator/pkg/bgpv2"
"github.com/cilium/cilium/operator/pkg/ciliumendpointslice"
"github.com/cilium/cilium/operator/pkg/ciliumenvoyconfig"
controllerruntime "github.com/cilium/cilium/operator/pkg/controller-runtime"
gatewayapi "github.com/cilium/cilium/operator/pkg/gateway-api"
"github.com/cilium/cilium/operator/pkg/ingress"
"github.com/cilium/cilium/operator/pkg/lbipam"
"github.com/cilium/cilium/operator/pkg/networkpolicy"
"github.com/cilium/cilium/operator/pkg/nodeipam"
"github.com/cilium/cilium/operator/pkg/secretsync"
operatorWatchers "github.com/cilium/cilium/operator/watchers"
"github.com/cilium/cilium/pkg/clustermesh/endpointslicesync"
"github.com/cilium/cilium/pkg/clustermesh/mcsapi"
cmtypes "github.com/cilium/cilium/pkg/clustermesh/types"
"github.com/cilium/cilium/pkg/controller"
"github.com/cilium/cilium/pkg/defaults"
"github.com/cilium/cilium/pkg/gops"
"github.com/cilium/cilium/pkg/hive"
"github.com/cilium/cilium/pkg/ipam/allocator"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
"github.com/cilium/cilium/pkg/k8s"
"github.com/cilium/cilium/pkg/k8s/apis"
k8sClient "github.com/cilium/cilium/pkg/k8s/client"
slim_corev1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/api/core/v1"
k8sversion "github.com/cilium/cilium/pkg/k8s/version"
"github.com/cilium/cilium/pkg/kvstore"
"github.com/cilium/cilium/pkg/kvstore/store"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/metrics"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/pprof"
"github.com/cilium/cilium/pkg/version"
)
var (
Operator = cell.Module(
"operator",
"Cilium Operator",
Infrastructure,
ControlPlane,
)
Infrastructure = cell.Module(
"operator-infra",
"Operator Infrastructure",
// Register the pprof HTTP handlers, to get runtime profiling data.
pprof.Cell,
cell.ProvidePrivate(func(cfg operatorPprofConfig) pprof.Config {
return pprof.Config{
Pprof: cfg.OperatorPprof,
PprofAddress: cfg.OperatorPprofAddress,
PprofPort: cfg.OperatorPprofPort,
}
}),
cell.Config(operatorPprofConfig{
OperatorPprofAddress: operatorOption.PprofAddressOperator,
OperatorPprofPort: operatorOption.PprofPortOperator,
}),
// Runs the gops agent, a tool to diagnose Go processes.
gops.Cell(defaults.GopsPortOperator),
// Provides Clientset, API for accessing Kubernetes objects.
k8sClient.Cell,
// Provides a ClientBuilderFunc that can be used by other cells to create a client.
k8sClient.ClientBuilderCell,
// Provides the modular metrics registry, metric HTTP server and legacy metrics cell.
operatorMetrics.Cell,
cell.Provide(func(
operatorCfg *operatorOption.OperatorConfig,
) operatorMetrics.SharedConfig {
return operatorMetrics.SharedConfig{
// Cloud provider specific allocators needs to read operatorCfg.EnableMetrics
// to add their metrics when it's set to true. Therefore, we leave the flag as global
// instead of declaring it as part of the metrics cell.
// This should be changed once the IPAM allocator is modularized.
EnableMetrics: operatorCfg.EnableMetrics,
EnableGatewayAPI: operatorCfg.EnableGatewayAPI,
}
}),
)
// ControlPlane implements the control functions.
ControlPlane = cell.Module(
"operator-controlplane",
"Operator Control Plane",
cell.Config(cmtypes.DefaultClusterInfo),
cell.Invoke(func(cinfo cmtypes.ClusterInfo) error { return cinfo.InitClusterIDMax() }),
cell.Invoke(func(cinfo cmtypes.ClusterInfo) error { return cinfo.Validate() }),
cell.Invoke(
registerOperatorHooks,
),
cell.Provide(func() *option.DaemonConfig {
return option.Config
}),
cell.Provide(func() *operatorOption.OperatorConfig {
return operatorOption.Config
}),
cell.Provide(func(
daemonCfg *option.DaemonConfig,
operatorCfg *operatorOption.OperatorConfig,
) identitygc.SharedConfig {
return identitygc.SharedConfig{
IdentityAllocationMode: daemonCfg.IdentityAllocationMode,
}
}),
cell.Provide(func(
daemonCfg *option.DaemonConfig,
) ciliumendpointslice.SharedConfig {
return ciliumendpointslice.SharedConfig{
EnableCiliumEndpointSlice: daemonCfg.EnableCiliumEndpointSlice,
}
}),
cell.Provide(func(
operatorCfg *operatorOption.OperatorConfig,
daemonCfg *option.DaemonConfig,
) endpointgc.SharedConfig {
return endpointgc.SharedConfig{
Interval: operatorCfg.EndpointGCInterval,
DisableCiliumEndpointCRD: daemonCfg.DisableCiliumEndpointCRD,
}
}),
api.HealthHandlerCell(
kvstoreEnabled,
isLeader.Load,
),
api.MetricsHandlerCell,
controller.Cell,
operatorApi.SpecCell,
api.ServerCell,
// These cells are started only after the operator is elected leader.
WithLeaderLifecycle(
// The CRDs registration should be the first operation to be invoked after the operator is elected leader.
apis.RegisterCRDsCell,
operatorK8s.ResourcesCell,
bgpv2.Cell,
lbipam.Cell,
nodeipam.Cell,
auth.Cell,
store.Cell,
endpointslicesync.Cell,
mcsapi.Cell,
legacyCell,
// When running in kvstore mode, the start hook of the identity GC
// cell blocks until the kvstore client has been initialized, which
// is performed by the legacyCell start hook. Hence, the identity GC
// cell is registered afterwards, to ensure the ordering of the
// setup operations. This is a hacky workaround until the kvstore is
// refactored into a proper cell.
identitygc.Cell,
// CiliumEndpointSlice controller depends on the CiliumEndpoint and
// CiliumEndpointSlice resources. It reconciles the state of CESs in the
// cluster based on the CEPs and CESs events.
// It is disabled if CiliumEndpointSlice is disabled in the cluster -
// when --enable-cilium-endpoint-slice is false.
ciliumendpointslice.Cell,
// Cilium Endpoint Garbage Collector. It removes all leaked Cilium
// Endpoints. Either once or periodically it validates all the present
// Cilium Endpoints and delete the ones that should be deleted.
endpointgc.Cell,
// Integrates the controller-runtime library and provides its components via Hive.
controllerruntime.Cell,
// Cilium Gateway API controller that manages the Gateway API related CRDs.
gatewayapi.Cell,
// Cilium Ingress controller that manages the Kubernetes Ingress related CRDs.
ingress.Cell,
// Cilium Secret synchronizes K8s TLS Secrets referenced by
// Ciliums "Ingress resources" from the application namespaces into a dedicated
// secrets namespace that is accessible by the Cilium Agents.
// Resources might be K8s `Ingress` or Gateway API `Gateway`.
secretsync.Cell,
// Cilium L7 LoadBalancing with Envoy.
ciliumenvoyconfig.Cell,
// Informational policy validation.
networkpolicy.Cell,
),
)
binaryName = filepath.Base(os.Args[0])
log = logging.DefaultLogger.WithField(logfields.LogSubsys, binaryName)
FlagsHooks []ProviderFlagsHooks
leaderElectionResourceLockName = "cilium-operator-resource-lock"
// Use a Go context so we can tell the leaderelection code when we
// want to step down
leaderElectionCtx context.Context
leaderElectionCtxCancel context.CancelFunc
// isLeader is an atomic boolean value that is true when the Operator is
// elected leader. Otherwise, it is false.
isLeader atomic.Bool
)
func NewOperatorCmd(h *hive.Hive) *cobra.Command {
cmd := &cobra.Command{
Use: binaryName,
Short: "Run " + binaryName,
Run: func(cobraCmd *cobra.Command, args []string) {
cmdRefDir := h.Viper().GetString(option.CMDRef)
if cmdRefDir != "" {
genMarkdown(cobraCmd, cmdRefDir)
os.Exit(0)
}
initEnv(h.Viper())
if err := h.Run(logging.DefaultSlogLogger); err != nil {
log.Fatal(err)
}
},
}
h.RegisterFlags(cmd.Flags())
// Enable fallback to direct API probing to check for support of Leases in
// case Discovery API fails.
h.Viper().Set(option.K8sEnableAPIDiscovery, true)
// Overwrite the metrics namespace with the one specific for the Operator
metrics.Namespace = metrics.CiliumOperatorNamespace
cmd.AddCommand(
MetricsCmd,
StatusCmd,
ciliumdbg.TroubleshootCmd,
h.Command(),
)
InitGlobalFlags(cmd, h.Viper())
for _, hook := range FlagsHooks {
hook.RegisterProviderFlag(cmd, h.Viper())
}
cobra.OnInitialize(option.InitConfig(cmd, "Cilium-Operator", "cilium-operators", h.Viper()))
return cmd
}
func Execute(cmd *cobra.Command) {
if err := cmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func registerOperatorHooks(log *slog.Logger, lc cell.Lifecycle, llc *LeaderLifecycle, clientset k8sClient.Clientset, shutdowner hive.Shutdowner) {
var wg sync.WaitGroup
lc.Append(cell.Hook{
OnStart: func(cell.HookContext) error {
wg.Add(1)
go func() {
runOperator(log, llc, clientset, shutdowner)
wg.Done()
}()
return nil
},
OnStop: func(ctx cell.HookContext) error {
if err := llc.Stop(log, ctx); err != nil {
return err
}
doCleanup()
wg.Wait()
return nil
},
})
}
func initEnv(vp *viper.Viper) {
// Prepopulate option.Config with options from CLI.
option.Config.Populate(vp)
operatorOption.Config.Populate(vp)
// add hooks after setting up metrics in the option.Config
logging.DefaultLogger.Hooks.Add(metrics.NewLoggingHook())
// Logging should always be bootstrapped first. Do not add any code above this!
if err := logging.SetupLogging(option.Config.LogDriver, logging.LogOptions(option.Config.LogOpt), binaryName, option.Config.Debug); err != nil {
log.Fatal(err)
}
option.LogRegisteredOptions(vp, log)
log.Infof("Cilium Operator %s", version.Version)
}
func doCleanup() {
isLeader.Store(false)
// Cancelling this context here makes sure that if the operator hold the
// leader lease, it will be released.
leaderElectionCtxCancel()
}
// runOperator implements the logic of leader election for cilium-operator using
// built-in leader election capability in kubernetes.
// See: https://github.com/kubernetes/client-go/blob/master/examples/leader-election/main.go
func runOperator(slog *slog.Logger, lc *LeaderLifecycle, clientset k8sClient.Clientset, shutdowner hive.Shutdowner) {
isLeader.Store(false)
leaderElectionCtx, leaderElectionCtxCancel = context.WithCancel(context.Background())
if clientset.IsEnabled() {
capabilities := k8sversion.Capabilities()
if !capabilities.MinimalVersionMet {
log.Fatalf("Minimal kubernetes version not met: %s < %s",
k8sversion.Version(), k8sversion.MinimalVersionConstraint)
}
}
// We only support Operator in HA mode for Kubernetes Versions having support for
// LeasesResourceLock.
// See docs on capabilities.LeasesResourceLock for more context.
if !k8sversion.Capabilities().LeasesResourceLock {
log.Info("Support for coordination.k8s.io/v1 not present, fallback to non HA mode")
if err := lc.Start(slog, leaderElectionCtx); err != nil {
log.WithError(err).Fatal("Failed to start leading")
}
return
}
// Get hostname for identity name of the lease lock holder.
// We identify the leader of the operator cluster using hostname.
operatorID, err := os.Hostname()
if err != nil {
log.WithError(err).Fatal("Failed to get hostname when generating lease lock identity")
}
operatorID = fmt.Sprintf("%s-%s", operatorID, rand.String(10))
ns := option.Config.K8sNamespace
// If due to any reason the CILIUM_K8S_NAMESPACE is not set we assume the operator
// to be in default namespace.
if ns == "" {
ns = metav1.NamespaceDefault
}
leResourceLock, err := resourcelock.NewFromKubeconfig(
resourcelock.LeasesResourceLock,
ns,
leaderElectionResourceLockName,
resourcelock.ResourceLockConfig{
// Identity name of the lock holder
Identity: operatorID,
},
clientset.RestConfig(),
operatorOption.Config.LeaderElectionRenewDeadline)
if err != nil {
log.WithError(err).Fatal("Failed to create resource lock for leader election")
}
// Start the leader election for running cilium-operators
log.Info("Waiting for leader election")
leaderelection.RunOrDie(leaderElectionCtx, leaderelection.LeaderElectionConfig{
Name: leaderElectionResourceLockName,
Lock: leResourceLock,
ReleaseOnCancel: true,
LeaseDuration: operatorOption.Config.LeaderElectionLeaseDuration,
RenewDeadline: operatorOption.Config.LeaderElectionRenewDeadline,
RetryPeriod: operatorOption.Config.LeaderElectionRetryPeriod,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: func(ctx context.Context) {
if err := lc.Start(slog, ctx); err != nil {
log.WithError(err).Error("Failed to start when elected leader, shutting down")
shutdowner.Shutdown(hive.ShutdownWithError(err))
}
},
OnStoppedLeading: func() {
log.WithField("operator-id", operatorID).Info("Leader election lost")
// Cleanup everything here, and exit.
shutdowner.Shutdown(hive.ShutdownWithError(errors.New("Leader election lost")))
},
OnNewLeader: func(identity string) {
if identity == operatorID {
log.Info("Leading the operator HA deployment")
} else {
log.WithFields(logrus.Fields{
"newLeader": identity,
"operatorID": operatorID,
}).Info("Leader re-election complete")
}
},
},
})
}
func kvstoreEnabled() bool {
if option.Config.KVStore == "" {
return false
}
return option.Config.IdentityAllocationMode == option.IdentityAllocationModeKVstore ||
operatorOption.Config.SyncK8sServices ||
operatorOption.Config.SyncK8sNodes
}
var legacyCell = cell.Invoke(registerLegacyOnLeader)
func registerLegacyOnLeader(lc cell.Lifecycle, clientset k8sClient.Clientset, resources operatorK8s.Resources, factory store.Factory) {
ctx, cancel := context.WithCancel(context.Background())
legacy := &legacyOnLeader{
ctx: ctx,
cancel: cancel,
clientset: clientset,
resources: resources,
storeFactory: factory,
}
lc.Append(cell.Hook{
OnStart: legacy.onStart,
OnStop: legacy.onStop,
})
}
type legacyOnLeader struct {
ctx context.Context
cancel context.CancelFunc
clientset k8sClient.Clientset
wg sync.WaitGroup
resources operatorK8s.Resources
storeFactory store.Factory
}
func (legacy *legacyOnLeader) onStop(_ cell.HookContext) error {
legacy.cancel()
// Wait for background goroutines to finish.
legacy.wg.Wait()
return nil
}
// OnOperatorStartLeading is the function called once the operator starts leading
// in HA mode.
func (legacy *legacyOnLeader) onStart(_ cell.HookContext) error {
isLeader.Store(true)
// Restart kube-dns as soon as possible since it helps etcd-operator to be
// properly setup. If kube-dns is not managed by Cilium it can prevent
// etcd from reaching out kube-dns in EKS.
// If this logic is modified, make sure the operator's clusterrole logic for
// pods/delete is also up-to-date.
if !legacy.clientset.IsEnabled() {
log.Infof("KubeDNS unmanaged pods controller disabled due to kubernetes support not enabled")
} else if option.Config.DisableCiliumEndpointCRD {
log.Infof("KubeDNS unmanaged pods controller disabled as %q option is set to 'disabled' in Cilium ConfigMap", option.DisableCiliumEndpointCRDName)
} else if operatorOption.Config.UnmanagedPodWatcherInterval != 0 {
legacy.wg.Add(1)
go func() {
defer legacy.wg.Done()
enableUnmanagedController(legacy.ctx, &legacy.wg, legacy.clientset)
}()
}
var (
nodeManager allocator.NodeEventHandler
err error
withKVStore bool
)
log.WithField(logfields.Mode, option.Config.IPAM).Info("Initializing IPAM")
switch ipamMode := option.Config.IPAM; ipamMode {
case ipamOption.IPAMAzure,
ipamOption.IPAMENI,
ipamOption.IPAMClusterPool,
ipamOption.IPAMMultiPool,
ipamOption.IPAMAlibabaCloud:
alloc, providerBuiltin := allocatorProviders[ipamMode]
if !providerBuiltin {
log.Fatalf("%s allocator is not supported by this version of %s", ipamMode, binaryName)
}
if err := alloc.Init(legacy.ctx); err != nil {
log.WithError(err).Fatalf("Unable to init %s allocator", ipamMode)
}
if pooledAlloc, ok := alloc.(operatorWatchers.PooledAllocatorProvider); ok {
// The following operation will block until all pools are restored, thus it
// is safe to continue starting node allocation right after return.
operatorWatchers.StartIPPoolAllocator(legacy.ctx, legacy.clientset, pooledAlloc, legacy.resources.CiliumPodIPPools)
}
nm, err := alloc.Start(legacy.ctx, &ciliumNodeUpdateImplementation{legacy.clientset})
if err != nil {
log.WithError(err).Fatalf("Unable to start %s allocator", ipamMode)
}
nodeManager = nm
}
if operatorOption.Config.BGPAnnounceLBIP {
log.Info("Starting LB IP allocator")
operatorWatchers.StartBGPBetaLBIPAllocator(legacy.ctx, legacy.clientset, legacy.resources.Services)
}
if kvstoreEnabled() {
var goopts *kvstore.ExtraOptions
scopedLog := log.WithFields(logrus.Fields{
"kvstore": option.Config.KVStore,
"address": option.Config.KVStoreOpt[fmt.Sprintf("%s.address", option.Config.KVStore)],
})
if legacy.clientset.IsEnabled() && operatorOption.Config.SyncK8sServices {
clusterInfo := cmtypes.ClusterInfo{
ID: option.Config.ClusterID,
Name: option.Config.ClusterName,
}
operatorWatchers.StartSynchronizingServices(legacy.ctx, &legacy.wg, operatorWatchers.ServiceSyncParameters{
ClusterInfo: clusterInfo,
Clientset: legacy.clientset,
Services: legacy.resources.Services,
Endpoints: legacy.resources.Endpoints,
SharedOnly: true,
StoreFactory: legacy.storeFactory,
SyncCallback: func(_ context.Context) {},
})
// If K8s is enabled we can do the service translation automagically by
// looking at services from k8s and retrieve the service IP from that.
// This makes cilium to not depend on kube dns to interact with etcd
if legacy.clientset.IsEnabled() {
svcURL, isETCDOperator := kvstore.IsEtcdOperator(option.Config.KVStore, option.Config.KVStoreOpt, option.Config.K8sNamespace)
if isETCDOperator {
scopedLog.Infof("%s running with service synchronization: automatic etcd service translation enabled", binaryName)
svcGetter := k8s.ServiceIPGetter(operatorWatchers.K8sSvcCache)
name, namespace, err := kvstore.SplitK8sServiceURL(svcURL)
if err != nil {
// If we couldn't derive the name/namespace for the given
// svcURL log the error so the user can see it.
// k8s.CreateCustomDialer won't be able to derive
// the name/namespace as well so it does not matter that
// we wait for all services to be synchronized with k8s.
scopedLog.WithError(err).WithFields(logrus.Fields{
"url": svcURL,
}).Error("Unable to derive service name from given url")
} else {
scopedLog.WithFields(logrus.Fields{
logfields.ServiceName: name,
logfields.ServiceNamespace: namespace,
}).Info("Retrieving service spec from k8s to perform automatic etcd service translation")
k8sSvc, err := legacy.clientset.CoreV1().Services(namespace).Get(legacy.ctx, name, metav1.GetOptions{})
switch {
case err == nil:
// Create another service cache that contains the
// k8s service for etcd. As soon the k8s caches are
// synced, this hijack will stop happening.
sc := k8s.NewServiceCache(nil, nil)
slimSvcObj, err := k8s.TransformToK8sService(k8sSvc)
if err != nil {
scopedLog.WithFields(logrus.Fields{
logfields.ServiceName: k8sSvc.Name,
logfields.ServiceNamespace: k8sSvc.Namespace,
}).Error("Failed to transform k8s service")
} else {
slimSvc := k8s.CastInformerEvent[slim_corev1.Service](slimSvcObj)
if slimSvc == nil {
// This will never happen but still log it
scopedLog.WithFields(logrus.Fields{
logfields.ServiceName: k8sSvc.Name,
logfields.ServiceNamespace: k8sSvc.Namespace,
}).Warn("BUG: invalid k8s service")
}
sc.UpdateService(slimSvc, nil)
svcGetter = operatorWatchers.NewServiceGetter(sc)
}
case k8sErrors.IsNotFound(err):
scopedLog.Error("Service not found in k8s")
default:
scopedLog.Warning("Unable to get service spec from k8s, this might cause network disruptions with etcd")
}
}
log := log.WithField(logfields.LogSubsys, "etcd")
goopts = &kvstore.ExtraOptions{
DialOption: []grpc.DialOption{
grpc.WithContextDialer(k8s.CreateCustomDialer(svcGetter, log, true)),
},
}
}
}
} else {
scopedLog.Infof("%s running without service synchronization: automatic etcd service translation disabled", binaryName)
}
scopedLog.Info("Connecting to kvstore")
if err := kvstore.Setup(legacy.ctx, option.Config.KVStore, option.Config.KVStoreOpt, goopts); err != nil {
scopedLog.WithError(err).Fatal("Unable to setup kvstore")
}
if legacy.clientset.IsEnabled() && operatorOption.Config.SyncK8sNodes {
withKVStore = true
}
startKvstoreWatchdog()
}
if legacy.clientset.IsEnabled() &&
(operatorOption.Config.RemoveCiliumNodeTaints || operatorOption.Config.SetCiliumIsUpCondition) {
log.WithFields(logrus.Fields{
logfields.K8sNamespace: operatorOption.Config.CiliumK8sNamespace,
"label-selector": operatorOption.Config.CiliumPodLabels,
"remove-cilium-node-taints": operatorOption.Config.RemoveCiliumNodeTaints,
"set-cilium-node-taints": operatorOption.Config.SetCiliumNodeTaints,
"set-cilium-is-up-condition": operatorOption.Config.SetCiliumIsUpCondition,
}).Info("Managing Cilium Node Taints or Setting Cilium Is Up Condition for Kubernetes Nodes")
operatorWatchers.HandleNodeTolerationAndTaints(&legacy.wg, legacy.clientset, legacy.ctx.Done())
}
ciliumNodeSynchronizer := newCiliumNodeSynchronizer(legacy.clientset, nodeManager, withKVStore)
if legacy.clientset.IsEnabled() {
if err := ciliumNodeSynchronizer.Start(legacy.ctx, &legacy.wg); err != nil {
log.WithError(err).Fatal("Unable to setup cilium node synchronizer")
}
// This is done to avoid accumulating stale updates and thus
// hindering scalability for large clusters.
RunCNPStatusNodesCleaner(
legacy.ctx,
legacy.clientset,
xrate.NewLimiter(
xrate.Limit(operatorOption.Config.CNPStatusCleanupQPS),
operatorOption.Config.CNPStatusCleanupBurst,
),
)
if operatorOption.Config.NodesGCInterval != 0 {
operatorWatchers.RunCiliumNodeGC(legacy.ctx, &legacy.wg, legacy.clientset, ciliumNodeSynchronizer.ciliumNodeStore, operatorOption.Config.NodesGCInterval)
}
}
if option.Config.IPAM == ipamOption.IPAMClusterPool || option.Config.IPAM == ipamOption.IPAMMultiPool {
// We will use CiliumNodes as the source of truth for the podCIDRs.
// Once the CiliumNodes are synchronized with the operator we will
// be able to watch for K8s Node events which they will be used
// to create the remaining CiliumNodes.
<-ciliumNodeSynchronizer.ciliumNodeManagerQueueSynced
// We don't want CiliumNodes that don't have podCIDRs to be
// allocated with a podCIDR already being used by another node.
// For this reason we will call Resync after all CiliumNodes are
// synced with the operator to signal the node manager, since it
// knows all podCIDRs that are currently set in the cluster, that
// it can allocate podCIDRs for the nodes that don't have a podCIDR
// set.
nodeManager.Resync(legacy.ctx, time.Time{})
}
if option.Config.IdentityAllocationMode == option.IdentityAllocationModeCRD {
if !legacy.clientset.IsEnabled() {
log.Fatal("CRD Identity allocation mode requires k8s to be configured.")
}
if operatorOption.Config.EndpointGCInterval == 0 {
log.Fatal("Cilium Identity garbage collector requires the CiliumEndpoint garbage collector to be enabled")
}
}
if legacy.clientset.IsEnabled() {
err = enableCNPWatcher(legacy.ctx, &legacy.wg, legacy.clientset)
if err != nil {
log.WithError(err).WithField(logfields.LogSubsys, "CNPWatcher").Fatal(
"Cannot connect to Kubernetes apiserver ")
}
err = enableCCNPWatcher(legacy.ctx, &legacy.wg, legacy.clientset)
if err != nil {
log.WithError(err).WithField(logfields.LogSubsys, "CCNPWatcher").Fatal(
"Cannot connect to Kubernetes apiserver ")
}
}
log.Info("Initialization complete")
return nil
}