-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
cell.go
101 lines (81 loc) · 2.82 KB
/
cell.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package controllerruntime
import (
"context"
"fmt"
"runtime/pprof"
"github.com/bombsimon/logrusr/v4"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrlRuntime "sigs.k8s.io/controller-runtime"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"github.com/cilium/cilium/pkg/hive/cell"
"github.com/cilium/cilium/pkg/hive/job"
ciliumv2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
"github.com/cilium/cilium/pkg/k8s/client"
)
// Cell integrates the components of the controller-runtime library into Hive.
// The Kubernetes controller-runtime Project is a set of go libraries for building Controllers.
// See https://github.com/kubernetes-sigs/controller-runtime for further information.
var Cell = cell.Module(
"controller-runtime",
"Manages the controller-runtime integration and its components",
cell.Provide(newScheme),
cell.Provide(newManager),
)
func newScheme() (*runtime.Scheme, error) {
scheme := clientgoscheme.Scheme
for gv, f := range map[fmt.Stringer]func(s *runtime.Scheme) error{
ciliumv2.SchemeGroupVersion: ciliumv2.AddToScheme,
} {
if err := f(scheme); err != nil {
return nil, fmt.Errorf("failed to add types from %s to scheme: %w", gv, err)
}
}
return scheme, nil
}
type managerParams struct {
cell.In
Logger logrus.FieldLogger
Lifecycle cell.Lifecycle
JobRegistry job.Registry
Scope cell.Scope
K8sClient client.Clientset
Scheme *runtime.Scheme
}
func newManager(params managerParams) (ctrlRuntime.Manager, error) {
if !params.K8sClient.IsEnabled() {
return nil, nil
}
// Register special comparison function for proto resource to support
// internal comparison of types depending on type (e.g. CiliumEnvoyConfig).
equality.Semantic.AddFunc(func(xdsResource1, xdsResource2 ciliumv2.XDSResource) bool {
return proto.Equal(xdsResource1.Any, xdsResource2.Any)
})
ctrlRuntime.SetLogger(logrusr.New(params.Logger))
mgr, err := ctrlRuntime.NewManager(params.K8sClient.RestConfig(), ctrlRuntime.Options{
Scheme: params.Scheme,
// Disable controller metrics server in favour of cilium's metrics server.
Metrics: metricsserver.Options{
BindAddress: "0",
},
Logger: logrusr.New(params.Logger),
})
if err != nil {
return nil, fmt.Errorf("failed to create new controller-runtime manager: %w", err)
}
jobGroup := params.JobRegistry.NewGroup(
params.Scope,
job.WithLogger(params.Logger),
job.WithPprofLabels(pprof.Labels("cell", "controller-runtime")),
)
jobGroup.Add(job.OneShot("manager", func(ctx context.Context, health cell.HealthReporter) error {
return mgr.Start(ctx)
}))
params.Lifecycle.Append(jobGroup)
return mgr, nil
}