Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NETOBSERV-1931: introduce flowcollector validation webhook #817

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ resources:
version: v1beta2
webhooks:
conversion: true
validation: true
webhookVersion: v1
- controller: true
group: core
Expand Down
188 changes: 188 additions & 0 deletions apis/flowcollector/v1beta2/flowcollector_validation_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package v1beta2

import (
"context"
"errors"
"fmt"
"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)
allW, allE = collect(allW, allE, w, errs)
w = r.warnLogLevels(fc)
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 *FlowCollector) admission.Warnings {
var w admission.Warnings
if fc.Spec.Agent.EBPF.LogLevel == "debug" || fc.Spec.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.Spec.Agent.EBPF.LogLevel))
}
if fc.Spec.Processor.LogLevel == "debug" || fc.Spec.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.Spec.Processor.LogLevel))
}
return w
}

// nolint:cyclop
func (r *FlowCollector) validateAgent(_ context.Context, fc *FlowCollector) (admission.Warnings, []error) {
var warnings admission.Warnings
if slices.Contains(fc.Spec.Agent.EBPF.Features, NetworkEvents) {
// 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.Spec.Agent.EBPF.Privileged {
warnings = append(warnings, "The NetworkEvents feature requires eBPF Agent to run in privileged mode")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be an error? We know it is not going to work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My rationale is that I'm setting warnings for things that won't work but won't break anything (e.g. a feature that will not be enabled), and errors for things that may break something or produce garbage / unexpected data (e.g. an invalid filter set on a metric will make the metric tell something completely different from the expectation)

}
}
if slices.Contains(fc.Spec.Agent.EBPF.Features, PacketDrop) && !fc.Spec.Agent.EBPF.Privileged {
warnings = append(warnings, "The PacketDrop feature requires eBPF Agent to run in privileged mode")
}
var errs []error
if fc.Spec.Agent.EBPF.FlowFilter != nil && fc.Spec.Agent.EBPF.FlowFilter.Enable != nil && *fc.Spec.Agent.EBPF.FlowFilter.Enable {
hasPorts := fc.Spec.Agent.EBPF.FlowFilter.Ports.IntVal > 0 || fc.Spec.Agent.EBPF.FlowFilter.Ports.StrVal != ""
if hasPorts {
if err := validateFilterPortConfig(fc.Spec.Agent.EBPF.FlowFilter.Ports); err != nil {
errs = append(errs, err)
}
}
hasSrcPorts := fc.Spec.Agent.EBPF.FlowFilter.SourcePorts.IntVal > 0 || fc.Spec.Agent.EBPF.FlowFilter.SourcePorts.StrVal != ""
if hasSrcPorts {
if err := validateFilterPortConfig(fc.Spec.Agent.EBPF.FlowFilter.SourcePorts); err != nil {
errs = append(errs, err)
}
}
hasDstPorts := fc.Spec.Agent.EBPF.FlowFilter.DestPorts.IntVal > 0 || fc.Spec.Agent.EBPF.FlowFilter.DestPorts.StrVal != ""
if hasDstPorts {
if err := validateFilterPortConfig(fc.Spec.Agent.EBPF.FlowFilter.DestPorts); 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 warnings, 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
}
Loading
Loading