-
Notifications
You must be signed in to change notification settings - Fork 369
/
pod_regex.go
54 lines (48 loc) · 1.19 KB
/
pod_regex.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon
package filters
import (
"context"
"fmt"
"regexp"
"github.com/cilium/tetragon/api/v1/tetragon"
v1 "github.com/cilium/tetragon/pkg/oldhubble/api/v1"
hubbleFilters "github.com/cilium/tetragon/pkg/oldhubble/filters"
)
func filterByPodRegex(podPatterns []string) (hubbleFilters.FilterFunc, error) {
var pods []*regexp.Regexp
for _, pattern := range podPatterns {
query, err := regexp.Compile(pattern)
if err != nil {
return nil, fmt.Errorf("failed to compile regexp: %v", err)
}
pods = append(pods, query)
}
return func(ev *v1.Event) bool {
process := GetProcess(ev)
if process == nil {
return false
}
if process.Pod == nil {
return false
}
for _, pod := range pods {
if pod.MatchString(process.Pod.Name) {
return true
}
}
return false
}, nil
}
type PodRegexFilter struct{}
func (f *PodRegexFilter) OnBuildFilter(_ context.Context, ff *tetragon.Filter) ([]hubbleFilters.FilterFunc, error) {
var fs []hubbleFilters.FilterFunc
if ff.PodRegex != nil {
dnsFilters, err := filterByPodRegex(ff.PodRegex)
if err != nil {
return nil, err
}
fs = append(fs, dnsFilters)
}
return fs, nil
}