/
indexers.go
118 lines (107 loc) · 2.9 KB
/
indexers.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
package storage
import (
"errors"
"strings"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
discoveryv1 "k8s.io/api/discovery/v1"
"k8s.io/client-go/tools/cache"
"github.com/liqotech/liqo/pkg/virtualKubelet"
apimgmt "github.com/liqotech/liqo/pkg/virtualKubelet/apiReflection"
)
var InformerIndexers = map[apimgmt.ApiType]func() cache.Indexers{
apimgmt.Configmaps: configmapsIndexers,
apimgmt.EndpointSlices: endpointSlicesIndexers,
apimgmt.Pods: podsIndexers,
apimgmt.ReplicaSets: replicasetsIndexers,
apimgmt.Secrets: secretsIndexers,
apimgmt.Services: servicesIndexers,
}
func configmapsIndexers() cache.Indexers {
i := cache.Indexers{}
i["configmaps"] = func(obj interface{}) ([]string, error) {
cm, ok := obj.(*corev1.ConfigMap)
if !ok {
return []string{}, errors.New("cannot convert obj to configmap")
}
return []string{
strings.Join([]string{cm.Namespace, cm.Name}, "/"),
}, nil
}
return i
}
func endpointSlicesIndexers() cache.Indexers {
i := cache.Indexers{}
i["endpointslices"] = func(obj interface{}) ([]string, error) {
endpointSlice, ok := obj.(*discoveryv1.EndpointSlice)
if !ok {
return []string{}, errors.New("cannot convert obj to endpointslice")
}
return []string{
strings.Join([]string{endpointSlice.Namespace, endpointSlice.Name}, "/"),
}, nil
}
return i
}
func podsIndexers() cache.Indexers {
i := cache.Indexers{}
i["pods"] = func(obj interface{}) ([]string, error) {
po, ok := obj.(*corev1.Pod)
if !ok {
return []string{}, errors.New("cannot convert obj to pod")
}
var label string
if po.Labels != nil {
label = po.Labels[virtualKubelet.ReflectedpodKey]
}
indices := []string{
strings.Join([]string{po.Namespace, po.Name}, "/"),
po.Name,
}
if label != "" {
indices = append(indices, label)
}
return indices, nil
}
return i
}
func replicasetsIndexers() cache.Indexers {
i := cache.Indexers{}
i["replicasets"] = func(obj interface{}) ([]string, error) {
replicaset, ok := obj.(*appsv1.ReplicaSet)
if !ok {
return []string{}, errors.New("cannot convert obj to replicaset")
}
return []string{
strings.Join([]string{replicaset.Namespace, replicaset.Name}, "/"),
replicaset.Name,
}, nil
}
return i
}
func secretsIndexers() cache.Indexers {
i := cache.Indexers{}
i["secrets"] = func(obj interface{}) ([]string, error) {
secret, ok := obj.(*corev1.Secret)
if !ok {
return []string{}, errors.New("cannot convert obj to secret")
}
return []string{
strings.Join([]string{secret.Namespace, secret.Name}, "/"),
}, nil
}
return i
}
func servicesIndexers() cache.Indexers {
i := cache.Indexers{}
i["services"] = func(obj interface{}) ([]string, error) {
svc, ok := obj.(*corev1.Service)
if !ok {
return []string{}, errors.New("cannot convert obj to service")
}
return []string{
strings.Join([]string{svc.Namespace, svc.Name}, "/"),
}, nil
}
return i
}