-
Notifications
You must be signed in to change notification settings - Fork 93
/
filter.go
99 lines (80 loc) · 2.12 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
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
package kail
import (
"regexp"
"sort"
"github.com/boz/kcache/filter"
"github.com/boz/kcache/nsname"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/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
}
func NewNameRegexFilter(regex string) (filter.Filter, error) {
compile, err := regexp.Compile(regex)
if err != nil {
return nil, err
}
return nameRegexFilter{compile}, nil
}
type nameRegexFilter struct {
regex *regexp.Regexp
}
func (f nameRegexFilter) Accept(obj metav1.Object) bool {
return f.regex.MatchString(obj.GetName())
}
func (f nameRegexFilter) Equals(other filter.Filter) bool {
if other, ok := other.(nameRegexFilter); ok {
return f.regex.String() == other.regex.String()
}
return false
}