diff --git a/apis/flowmetrics/v1alpha1/flowmetric_webhook.go b/apis/flowmetrics/v1alpha1/flowmetric_webhook.go index 70702e994..5883d41d4 100644 --- a/apis/flowmetrics/v1alpha1/flowmetric_webhook.go +++ b/apis/flowmetrics/v1alpha1/flowmetric_webhook.go @@ -70,18 +70,26 @@ func validateFlowMetric(_ context.Context, fMetric *FlowMetric) error { } if len(str) != 0 { - if !helper.FindFilter(str) { + 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) { + 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}, diff --git a/apis/flowmetrics/v1alpha1/flowmetric_webhook_test.go b/apis/flowmetrics/v1alpha1/flowmetric_webhook_test.go index d33ab9f94..f82201556 100644 --- a/apis/flowmetrics/v1alpha1/flowmetric_webhook_test.go +++ b/apis/flowmetrics/v1alpha1/flowmetric_webhook_test.go @@ -79,6 +79,32 @@ func TestFlowMetric(t *testing.T) { }, 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 { diff --git a/pkg/helper/helpers.go b/pkg/helper/helpers.go index 6a8d15fa1..4a989d279 100644 --- a/pkg/helper/helpers.go +++ b/pkg/helper/helpers.go @@ -92,21 +92,32 @@ func UnstructuredDuration(in *metav1.Duration) string { return in.ToUnstructured().(string) } -func FindFilter(labels []string) bool { +func FindFilter(labels []string, isNumber bool) bool { var cfg config.FrontendConfig + type filter struct { + exists bool + isNum bool + } err := yaml.Unmarshal(config.LoadStaticFrontendConfig(), &cfg) if err != nil { return false } - labelMap := make(map[string]bool) + labelMap := make(map[string]filter) for _, f := range cfg.Fields { - labelMap[f.Name] = true + labelMap[f.Name] = filter{true, false} + if f.Type == "number" { + labelMap[f.Name] = filter{true, true} + } } + for _, l := range labels { - if ok := labelMap[l]; !ok { + if ok := labelMap[l].exists; !ok { + return false + } + if isNumber && !labelMap[l].isNum { return false } }