-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
73 lines (58 loc) · 1.52 KB
/
filter.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
package kail
import (
"sort"
"github.com/boz/kcache/nsname"
"k8s.io/api/core/v1"
)
type ContainerFilter interface {
Accept(cs v1.ContainerStatus) bool
}
func NewContainerFilter(names []string) ContainerFilter {
return containerFilter(names)
}
type containerFilter []string
func (cf containerFilter) Accept(cs v1.ContainerStatus) bool {
if cs.State.Running == nil && cs.State.Terminated == nil {
return false
}
if len(cf) == 0 {
return true
}
for _, name := range cf {
if name == cs.Name {
return true
}
}
return false
}
func sourcesForPod(filter ContainerFilter, pod *v1.Pod) (nsname.NSName, map[eventSource]bool) {
id := nsname.ForObject(pod)
sources := make(map[eventSource]bool)
for _, cstatus := range pod.Status.ContainerStatuses {
if filter.Accept(cstatus) {
source := eventSource{id, cstatus.Name, pod.Spec.NodeName}
sources[source] = true
}
}
for _, cstatus := range pod.Status.InitContainerStatuses {
if filter.Accept(cstatus) {
source := eventSource{id, cstatus.Name, pod.Spec.NodeName}
sources[source] = true
}
}
return id, sources
}
func SourcesForPod(
filter ContainerFilter, pod *v1.Pod) (nsname.NSName, []EventSource) {
id, internal := sourcesForPod(filter, pod)
sources := make([]EventSource, 0, len(internal))
for source, _ := range internal {
sources = append(sources, source)
}
sort.Slice(sources, func(a, b int) bool {
na := sources[a].Namespace() + sources[a].Name()
nb := sources[b].Namespace() + sources[b].Name()
return na < nb
})
return id, sources
}