-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #622 from msherif1234/validation_webhook
NETOBSERV-1614: Metrics API validation webhook
- Loading branch information
Showing
18 changed files
with
382 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/netobserv/network-observability-operator/pkg/helper" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
) | ||
|
||
// log is for logging in this package. | ||
var flowmetriclog = logf.Log.WithName("flowmetric-resource") | ||
|
||
type FlowMetricWebhook struct { | ||
FlowMetric | ||
} | ||
|
||
// +kubebuilder:webhook:verbs=create;update,path=/validate-flows-netobserv-io-v1alpha1-flowmetric,mutating=false,failurePolicy=fail,sideEffects=None,groups=flows.netobserv.io,resources=flowmetrics,versions=v1alpha1,name=flowmetricvalidationwebhook.netobserv.io,admissionReviewVersions=v1 | ||
var ( | ||
_ webhook.CustomValidator = &FlowMetricWebhook{FlowMetric{}} | ||
) | ||
|
||
func (r *FlowMetricWebhook) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(&FlowMetric{}). | ||
WithValidator(&FlowMetricWebhook{}). | ||
Complete() | ||
} | ||
|
||
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type | ||
func (r *FlowMetricWebhook) ValidateCreate(ctx context.Context, newObj runtime.Object) (warnings admission.Warnings, err error) { | ||
flowmetriclog.Info("validate create", "name", r.Name) | ||
newFlowMetric, ok := newObj.(*FlowMetric) | ||
if !ok { | ||
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an FlowMetric but got a %T", newObj)) | ||
} | ||
return nil, validateFlowMetric(ctx, newFlowMetric) | ||
} | ||
|
||
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type | ||
func (r *FlowMetricWebhook) ValidateUpdate(ctx context.Context, _, newObj runtime.Object) (warnings admission.Warnings, err error) { | ||
flowmetriclog.Info("validate update", "name", r.Name) | ||
newFlowMetric, ok := newObj.(*FlowMetric) | ||
if !ok { | ||
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an FlowMetric but got a %T", newObj)) | ||
} | ||
return nil, validateFlowMetric(ctx, newFlowMetric) | ||
} | ||
|
||
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type | ||
func (r *FlowMetricWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (warnings admission.Warnings, err error) { | ||
flowmetriclog.Info("validate delete", "name", r.Name) | ||
return nil, nil | ||
} | ||
|
||
func validateFlowMetric(_ context.Context, fMetric *FlowMetric) error { | ||
var str []string | ||
var allErrs field.ErrorList | ||
|
||
for _, f := range fMetric.Spec.Filters { | ||
str = append(str, f.Field) | ||
} | ||
|
||
if len(str) != 0 { | ||
if !helper.FindFilter(str, false) { | ||
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "filters"), str, | ||
fmt.Sprintf("invalid filter field: %s", str))) | ||
} | ||
} | ||
|
||
if len(fMetric.Spec.Labels) != 0 { | ||
if !helper.FindFilter(fMetric.Spec.Labels, false) { | ||
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "labels"), fMetric.Spec.Labels, | ||
fmt.Sprintf("invalid label name: %s", fMetric.Spec.Labels))) | ||
} | ||
} | ||
|
||
if fMetric.Spec.ValueField != "" { | ||
if !helper.FindFilter([]string{fMetric.Spec.ValueField}, true) { | ||
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "valueField"), fMetric.Spec.ValueField, | ||
fmt.Sprintf("invalid value field: %s", fMetric.Spec.ValueField))) | ||
} | ||
} | ||
|
||
if len(allErrs) != 0 { | ||
return apierrors.NewInvalid( | ||
schema.GroupKind{Group: GroupVersion.Group, Kind: FlowMetric{}.Kind}, | ||
fMetric.Name, allErrs) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestFlowMetric(t *testing.T) { | ||
tests := []struct { | ||
desc string | ||
m *FlowMetric | ||
expectedError string | ||
}{ | ||
{ | ||
desc: "Invalid FlowMetric Filter", | ||
m: &FlowMetric{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test1", | ||
Namespace: "test-namespace", | ||
}, | ||
Spec: FlowMetricSpec{ | ||
Filters: []MetricFilter{ | ||
{ | ||
Field: "test", | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedError: "invalid filter field", | ||
}, | ||
{ | ||
desc: "Valid FlowMetric Filter", | ||
m: &FlowMetric{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test1", | ||
Namespace: "test-namespace", | ||
}, | ||
Spec: FlowMetricSpec{ | ||
Filters: []MetricFilter{ | ||
{ | ||
Field: "DstK8S_Zone", | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedError: "", | ||
}, | ||
{ | ||
desc: "Invalid FlowMetric Label", | ||
m: &FlowMetric{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test1", | ||
Namespace: "test-namespace", | ||
}, | ||
Spec: FlowMetricSpec{ | ||
Labels: []string{ | ||
"test", | ||
}, | ||
}, | ||
}, | ||
expectedError: "invalid label name", | ||
}, | ||
{ | ||
desc: "Valid FlowMetric Label", | ||
m: &FlowMetric{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test1", | ||
Namespace: "test-namespace", | ||
}, | ||
Spec: FlowMetricSpec{ | ||
Labels: []string{ | ||
"DstK8S_Zone", | ||
}, | ||
}, | ||
}, | ||
expectedError: "", | ||
}, | ||
{ | ||
desc: "Valid valueField", | ||
m: &FlowMetric{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test1", | ||
Namespace: "test-namespace", | ||
}, | ||
Spec: FlowMetricSpec{ | ||
ValueField: "Bytes", | ||
}, | ||
}, | ||
expectedError: "", | ||
}, | ||
{ | ||
desc: "Invalid valueField", | ||
m: &FlowMetric{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test1", | ||
Namespace: "test-namespace", | ||
}, | ||
Spec: FlowMetricSpec{ | ||
ValueField: "DstAddr", | ||
}, | ||
}, | ||
expectedError: "invalid value field", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
err := validateFlowMetric(context.TODO(), test.m) | ||
if err == nil { | ||
if test.expectedError != "" { | ||
t.Errorf("%s: ValidateFlowMetric failed, no error found while expected: \"%s\"", test.desc, test.expectedError) | ||
} | ||
} else { | ||
if len(test.expectedError) == 0 { | ||
t.Errorf("%s: ValidateFlowMetric failed, unexpected error: \"%s\"", test.desc, err) | ||
} | ||
if !strings.Contains(fmt.Sprint(err), test.expectedError) { | ||
t.Errorf("%s: ValidateFlowMetric failed, expected error: \"%s\" to contain: \"%s\"", test.desc, err, test.expectedError) | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.