-
Notifications
You must be signed in to change notification settings - Fork 335
/
health_check.go
84 lines (73 loc) · 2.44 KB
/
health_check.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package filters
import (
"context"
"path"
"github.com/cilium/tetragon/api/v1/tetragon"
v1 "github.com/cilium/tetragon/pkg/oldhubble/api/v1"
hubbleFilters "github.com/cilium/tetragon/pkg/oldhubble/filters"
shell "github.com/kballard/go-shellquote"
)
func MaybeExecProbe(binary string, args string, execProbe []string) bool {
// If the exec probe is empty for whatever reason, return false.
if len(execProbe) == 0 {
return false
}
argList, err := shell.Split(args)
if err != nil {
return false
}
// Exec will append a script name to argument list if a sh/bash script is executed with a shebang,
// so we need to remove the first argument so that we can compare it to execProbe.
// e.g.
// "binary": "/health/ping_liveness_local.sh",
// "arguments": "/health/ping_liveness_local.sh 5"
// concatenated together will be ["/health/ping_liveness_local.sh", "/health/ping_liveness_local.sh", "5"],
// but execProbe will have only ["/health/ping_liveness_local.sh", "5"].
if execProbe[0] == binary && len(argList) > 0 && argList[0] == binary {
argList = argList[1:]
}
processCommand := append([]string{binary}, argList...)
if len(execProbe) != len(processCommand) {
return false
}
if path.IsAbs(execProbe[0]) {
// exec probe path is absolute. Compare the full paths.
if processCommand[0] != execProbe[0] {
return false
}
} else {
// exec probe path is relative. Only compare the basenames.
if path.Base(processCommand[0]) != path.Base(execProbe[0]) {
return false
}
}
for i := 1; i < len(execProbe); i++ {
if execProbe[i] != processCommand[i] {
return false
}
}
return true
}
func canBeHealthCheck(process *tetragon.Process) bool {
return process != nil && process.Pod != nil && process.Pod.Container != nil && process.Pod.Container.MaybeExecProbe
}
func filterByHealthCheck(healthCheck bool) hubbleFilters.FilterFunc {
return func(ev *v1.Event) bool {
process := GetProcess(ev)
parent := GetParent(ev)
if healthCheck {
return canBeHealthCheck(process) || canBeHealthCheck(parent)
}
return !canBeHealthCheck(process) && !canBeHealthCheck(parent)
}
}
type HealthCheckFilter struct{}
func (f *HealthCheckFilter) OnBuildFilter(_ context.Context, ff *tetragon.Filter) ([]hubbleFilters.FilterFunc, error) {
var fs []hubbleFilters.FilterFunc
if ff.HealthCheck != nil {
fs = append(fs, filterByHealthCheck(ff.HealthCheck.Value))
}
return fs, nil
}