forked from netobserv/netobserv-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflowcollector_validation_webhook.go
More file actions
276 lines (250 loc) · 8.94 KB
/
flowcollector_validation_webhook.go
File metadata and controls
276 lines (250 loc) · 8.94 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package v1beta2
import (
"context"
"errors"
"fmt"
"net"
"slices"
"strconv"
"strings"
kerr "k8s.io/apimachinery/pkg/api/errors"
runtime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"github.com/netobserv/network-observability-operator/pkg/cluster"
)
var (
log = logf.Log.WithName("flowcollector-resource")
CurrentClusterInfo *cluster.Info
)
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *FlowCollector) ValidateCreate(ctx context.Context, newObj runtime.Object) (admission.Warnings, error) {
log.Info("validate create", "name", r.Name)
fc, ok := newObj.(*FlowCollector)
if !ok {
return nil, kerr.NewBadRequest(fmt.Sprintf("expected a FlowCollector but got a %T", newObj))
}
return r.validate(ctx, fc)
}
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *FlowCollector) ValidateUpdate(ctx context.Context, _, newObj runtime.Object) (admission.Warnings, error) {
log.Info("validate update", "name", r.Name)
fc, ok := newObj.(*FlowCollector)
if !ok {
return nil, kerr.NewBadRequest(fmt.Sprintf("expected a FlowCollector but got a %T", newObj))
}
return r.validate(ctx, fc)
}
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *FlowCollector) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
log.Info("validate delete", "name", r.Name)
return nil, nil
}
func (r *FlowCollector) validate(ctx context.Context, fc *FlowCollector) (admission.Warnings, error) {
var allW admission.Warnings
var allE []error
w, errs := r.validateAgent(ctx, &fc.Spec)
allW, allE = collect(allW, allE, w, errs)
w, errs = r.validateConversationTracking(ctx, &fc.Spec)
allW, allE = collect(allW, allE, w, errs)
w = r.warnLogLevels(&fc.Spec)
allW, allE = collect(allW, allE, w, nil)
return allW, errors.Join(allE...)
}
func collect(wPool admission.Warnings, errsPool []error, w admission.Warnings, errs []error) (admission.Warnings, []error) {
if len(w) > 0 {
wPool = append(wPool, w...)
}
if len(errs) > 0 {
errsPool = append(errsPool, errs...)
}
return wPool, errsPool
}
func (r *FlowCollector) warnLogLevels(fc *FlowCollectorSpec) admission.Warnings {
var w admission.Warnings
if fc.Agent.EBPF.LogLevel == "debug" || fc.Agent.EBPF.LogLevel == "trace" {
w = append(w, fmt.Sprintf("The log level for the eBPF agent is %s, which impacts performance and resource footprint.", fc.Agent.EBPF.LogLevel))
}
if fc.Processor.LogLevel == "debug" || fc.Processor.LogLevel == "trace" {
w = append(w, fmt.Sprintf("The log level for the processor (flowlogs-pipeline) is %s, which impacts performance and resource footprint.", fc.Processor.LogLevel))
}
return w
}
// nolint:cyclop
func (r *FlowCollector) validateAgent(_ context.Context, fc *FlowCollectorSpec) (admission.Warnings, []error) {
var warnings admission.Warnings
if slices.Contains(fc.Agent.EBPF.Features, NetworkEvents) ||
slices.Contains(fc.Agent.EBPF.Features, UDNMapping) {
// Make sure required version of ocp is installed
if CurrentClusterInfo != nil && CurrentClusterInfo.IsOpenShift() {
b, err := CurrentClusterInfo.OpenShiftVersionIsAtLeast("4.18.0")
if err != nil {
warnings = append(warnings, fmt.Sprintf("Could not detect OpenShift cluster version: %s", err.Error()))
} else if !b {
warnings = append(warnings, fmt.Sprintf("The NetworkEvents feature requires OpenShift 4.18 or above (version detected: %s)", CurrentClusterInfo.GetOpenShiftVersion()))
}
} else {
warnings = append(warnings, "The NetworkEvents feature is only supported with OpenShift")
}
if !fc.Agent.EBPF.Privileged {
warnings = append(warnings, "The NetworkEvents feature requires eBPF Agent to run in privileged mode")
}
}
if slices.Contains(fc.Agent.EBPF.Features, PacketDrop) && !fc.Agent.EBPF.Privileged {
warnings = append(warnings, "The PacketDrop feature requires eBPF Agent to run in privileged mode")
}
if slices.Contains(fc.Agent.EBPF.Features, EbpfManager) && !fc.Agent.EBPF.Privileged {
warnings = append(warnings, "The BPF Manager feature requires eBPF Agent to run in privileged mode")
}
if slices.Contains(fc.Agent.EBPF.Features, UDNMapping) && !fc.Agent.EBPF.Privileged {
warnings = append(warnings, "The UDNMapping feature requires eBPF Agent to run in privileged mode")
}
var errs []error
if fc.Agent.EBPF.FlowFilter != nil && fc.Agent.EBPF.FlowFilter.Enable != nil && *fc.Agent.EBPF.FlowFilter.Enable {
m := make(map[string]bool)
for i := range fc.Agent.EBPF.FlowFilter.FlowFilterRules {
rule := fc.Agent.EBPF.FlowFilter.FlowFilterRules[i]
if found := m[rule.CIDR]; found {
errs = append(errs, fmt.Errorf("flow filter rule CIDR %s already exists", rule.CIDR))
break
}
m[rule.CIDR] = true
errs = append(errs, validateFilter(&rule)...)
}
errs = append(errs, validateFilter(fc.Agent.EBPF.FlowFilter)...)
}
return warnings, errs
}
type filter interface {
getCIDR() string
getPorts() intstr.IntOrString
getSrcPorts() intstr.IntOrString
getDstPorts() intstr.IntOrString
}
func (f *EBPFFlowFilter) getCIDR() string {
return f.CIDR
}
func (f *EBPFFlowFilter) getPorts() intstr.IntOrString {
return f.Ports
}
func (f *EBPFFlowFilter) getSrcPorts() intstr.IntOrString {
return f.SourcePorts
}
func (f *EBPFFlowFilter) getDstPorts() intstr.IntOrString {
return f.DestPorts
}
func (f *EBPFFlowFilterRule) getCIDR() string {
return f.CIDR
}
func (f *EBPFFlowFilterRule) getPorts() intstr.IntOrString {
return f.Ports
}
func (f *EBPFFlowFilterRule) getSrcPorts() intstr.IntOrString {
return f.SourcePorts
}
func (f *EBPFFlowFilterRule) getDstPorts() intstr.IntOrString {
return f.DestPorts
}
func validateFilter[T filter](f T) []error {
var errs []error
cidr := f.getCIDR()
if cidr != "" {
if _, _, err := net.ParseCIDR(cidr); err != nil {
errs = append(errs, err)
}
}
ports := f.getPorts()
hasPorts := ports.IntVal > 0 || ports.StrVal != ""
if hasPorts {
if err := validateFilterPortConfig(ports); err != nil {
errs = append(errs, err)
}
}
srcPorts := f.getSrcPorts()
hasSrcPorts := srcPorts.IntVal > 0 || srcPorts.StrVal != ""
if hasSrcPorts {
if err := validateFilterPortConfig(srcPorts); err != nil {
errs = append(errs, err)
}
}
dstPorts := f.getDstPorts()
hasDstPorts := dstPorts.IntVal > 0 || dstPorts.StrVal != ""
if hasDstPorts {
if err := validateFilterPortConfig(dstPorts); err != nil {
errs = append(errs, err)
}
}
if hasPorts && hasSrcPorts {
errs = append(errs, errors.New("cannot configure agent filter with ports and sourcePorts, they are mutually exclusive"))
}
if hasPorts && hasDstPorts {
errs = append(errs, errors.New("cannot configure agent filter with ports and destPorts, they are mutually exclusive"))
}
return errs
}
func validateFilterPortConfig(value intstr.IntOrString) error {
if value.Type == intstr.Int {
return nil
}
sVal := value.String()
if strings.Contains(sVal, "-") {
ps := strings.SplitN(sVal, "-", 2)
if len(ps) != 2 {
return fmt.Errorf("invalid ports range: expected two integers separated by '-' but found %s", sVal)
}
start, err := validatePortString(ps[0])
if err != nil {
return fmt.Errorf("start port in range: %w", err)
}
end, err := validatePortString(ps[1])
if err != nil {
return fmt.Errorf("end port in range: %w", err)
}
if start >= end {
return fmt.Errorf("invalid port range: start is greater or equal to end")
}
return nil
} else if strings.Contains(sVal, ",") {
ps := strings.Split(sVal, ",")
if len(ps) != 2 {
return fmt.Errorf("invalid ports couple: expected two integers separated by ',' but found %s", sVal)
}
_, err := validatePortString(ps[0])
if err != nil {
return fmt.Errorf("first port: %w", err)
}
_, err = validatePortString(ps[1])
if err != nil {
return fmt.Errorf("second port: %w", err)
}
return nil
}
// Should be a single port then
_, err := validatePortString(sVal)
if err != nil {
return err
}
return nil
}
func validatePortString(s string) (uint16, error) {
p, err := strconv.ParseUint(s, 10, 16)
if err != nil {
return 0, fmt.Errorf("invalid port number %w", err)
}
if p == 0 {
return 0, fmt.Errorf("invalid port 0")
}
return uint16(p), nil
}
func (r *FlowCollector) validateConversationTracking(_ context.Context, fc *FlowCollectorSpec) (admission.Warnings, []error) {
var warnings admission.Warnings
if fc.Processor.LogTypes != nil && *fc.Processor.LogTypes == LogTypeAll {
warnings = append(warnings, "Enabling all log types (in spec.processor.logTypes) has a high impact on resources footprint")
}
var errs []error
if fc.Processor.LogTypes != nil && *fc.Processor.LogTypes != LogTypeFlows && fc.Loki.Enable != nil && !*fc.Loki.Enable {
errs = append(errs, errors.New("enabling conversation tracking without Loki is not allowed, as it generates extra processing for no benefit"))
}
return warnings, errs
}