-
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.
NETOBSERV-1614: Metrics API validation webhook
Signed-off-by: Mohamed Mahmoud <[email protected]>
- Loading branch information
1 parent
b26356d
commit 161b813
Showing
13 changed files
with
221 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/netobserv/network-observability-operator/pkg/helper" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
// log is for logging in this package. | ||
var flowmetriclog = logf.Log.WithName("flowmetric-resource") | ||
|
||
func (r *FlowMetric) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(r). | ||
Complete() | ||
} | ||
|
||
//+kubebuilder:webhook:path=/validate-netobserv-io-v1alpha1-flowmetric,mutating=false,failurePolicy=fail,sideEffects=None,groups=netobserv.io,resources=flowmetrics,verbs=create;update,versions=v1alpha1,name=flowmetricvalidationwebhook.netobserv.io,admissionReviewVersions=v1 | ||
|
||
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type | ||
func (r *FlowMetric) ValidateCreate() error { | ||
flowmetriclog.Info("validate create", "name", r.Name) | ||
return r.validateFlowMetric() | ||
} | ||
|
||
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type | ||
func (r *FlowMetric) ValidateUpdate(_ runtime.Object) error { | ||
flowmetriclog.Info("validate update", "name", r.Name) | ||
return r.validateFlowMetric() | ||
} | ||
|
||
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type | ||
func (r *FlowMetric) ValidateDelete() error { | ||
flowmetriclog.Info("validate delete", "name", r.Name) | ||
return nil | ||
} | ||
|
||
func (r *FlowMetric) validateFlowMetric() error { | ||
|
||
for _, f := range r.Spec.Filters { | ||
if !helper.FindFilter(f.Field) { | ||
return fmt.Errorf("invalid filter name %s", f.Field) | ||
} | ||
} | ||
|
||
for _, l := range r.Spec.Labels { | ||
if !helper.FindFilter(l) { | ||
return fmt.Errorf("invalid label name %s", l) | ||
} | ||
} | ||
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,95 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"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 name", | ||
}, | ||
{ | ||
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: "", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
err := test.m.validateFlowMetric() | ||
if err == nil { | ||
if test.expectedError != "" { | ||
t.Errorf("%s: ValidateFlowMetric failed, no error found while expected: \"%s\"", test.desc, test.expectedError) | ||
} | ||
} else { | ||
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
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
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.