Skip to content

Commit

Permalink
Add validation for valueField to make sure either empty or number
Browse files Browse the repository at this point in the history
Signed-off-by: Mohamed Mahmoud <[email protected]>
  • Loading branch information
msherif1234 committed Apr 24, 2024
1 parent 98bdf1f commit e9c8717
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
12 changes: 10 additions & 2 deletions apis/flowmetrics/v1alpha1/flowmetric_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
26 changes: 26 additions & 0 deletions apis/flowmetrics/v1alpha1/flowmetric_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 15 additions & 4 deletions pkg/helper/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit e9c8717

Please sign in to comment.