/
kubernetes.go
82 lines (78 loc) · 2.85 KB
/
kubernetes.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
package manager
import (
clabernetesapisv1alpha1 "github.com/srl-labs/clabernetes/apis/v1alpha1"
"k8s.io/apimachinery/pkg/labels"
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
ctrlruntime "sigs.k8s.io/controller-runtime"
ctrlruntimecache "sigs.k8s.io/controller-runtime/pkg/cache"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
ctrlruntimemetricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
)
func newManager(scheme *apimachineryruntime.Scheme, appName string) (ctrlruntime.Manager, error) {
return ctrlruntime.NewManager(
ctrlruntime.GetConfigOrDie(),
ctrlruntime.Options{
Logger: klog.NewKlogr(),
Scheme: scheme,
Metrics: ctrlruntimemetricsserver.Options{
BindAddress: "0",
},
LeaderElection: false,
NewCache: func(
config *rest.Config,
opts ctrlruntimecache.Options,
) (ctrlruntimecache.Cache, error) {
opts.DefaultLabelSelector = labels.SelectorFromSet(
labels.Set{
// only cache objects with the "clabernetes/app" label, why would we care
// about anything else (for now -- and we can override it with opts.ByObject
// anyway?! and... who the hell calls their app "clabernetes" so this should
// really limit the cache nicely :)
// currently this matters for launcher service accounts and role bindings
"clabernetes/app": appName,
},
)
opts.ByObject = map[ctrlruntimeclient.Object]ctrlruntimecache.ByObject{
// obviously we need to cache all "our" topology objects, so do that
&clabernetesapisv1alpha1.Topology{}: {
Namespaces: map[string]ctrlruntimecache.Config{
ctrlruntimecache.AllNamespaces: {
LabelSelector: labels.Everything(),
},
},
},
// we need to cache all our image request crs too of course
&clabernetesapisv1alpha1.ImageRequest{}: {
Namespaces: map[string]ctrlruntimecache.Config{
ctrlruntimecache.AllNamespaces: {
LabelSelector: labels.Everything(),
},
},
},
// watch our config "singleton" too; while this is sorta/basically a "cluster"
// CR -- we dont want to have to force users to have cluster wide perms, *and*
// we want to be able to set an owner ref to the manager deployment, so the
// config *is* namespaced, so... watch all the namespaces for the config...
&clabernetesapisv1alpha1.Config{}: {
Namespaces: map[string]ctrlruntimecache.Config{
ctrlruntimecache.AllNamespaces: {
LabelSelector: labels.Everything(),
},
},
},
// our tunnel "connectivity" cr
&clabernetesapisv1alpha1.Connectivity{}: {
Namespaces: map[string]ctrlruntimecache.Config{
ctrlruntimecache.AllNamespaces: {
LabelSelector: labels.Everything(),
},
},
},
}
return ctrlruntimecache.New(config, opts)
},
},
)
}