Skip to content

Commit

Permalink
v1alpha2 validation fix/update (#883)
Browse files Browse the repository at this point in the history
* move v1alpha2/validation utility to pkg utility directory

* v1alpha2 validation fix/update

* change pkg/utils to pkg/util

* remove undefined function tests

* remove pkg/utils
  • Loading branch information
ccfish2 authored Sep 28, 2021
1 parent 0df0f34 commit e237162
Show file tree
Hide file tree
Showing 7 changed files with 409 additions and 213 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ issues:
- gocyclo
- errcheck
- dupl
exclude:
- Using the variable on range scope `tc` in function literal
22 changes: 9 additions & 13 deletions apis/v1alpha1/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ func TestValidateGateway(t *testing.T) {
}

for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
gw := baseGateway.DeepCopy()
tc.mutate(gw)
Expand Down Expand Up @@ -405,13 +404,11 @@ func TestValidateHTTPRoute(t *testing.T) {
errCount: 0,
},
}
for _, tt := range tests {
// copy variable to avoid scope problems with ranges
tt := tt
t.Run(tt.name, func(t *testing.T) {
errs := ValidateHTTPRoute(&tt.hRoute)
if len(errs) != tt.errCount {
t.Errorf("ValidateHTTPRoute() got %v errors, want %v errors", len(errs), tt.errCount)
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
errs := ValidateHTTPRoute(&tc.hRoute)
if len(errs) != tc.errCount {
t.Errorf("ValidateHTTPRoute() got %v errors, want %v errors", len(errs), tc.errCount)
}
})
}
Expand Down Expand Up @@ -490,11 +487,10 @@ func TestValidateGatewayClassUpdate(t *testing.T) {
want: nil,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if got := ValidateGatewayClassUpdate(tt.args.oldClass, tt.args.newClass); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ValidateGatewayClassUpdate() = %v, want %v", got, tt.want)
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if got := ValidateGatewayClassUpdate(tc.args.oldClass, tc.args.newClass); !reflect.DeepEqual(got, tc.want) {
t.Errorf("ValidateGatewayClassUpdate() = %v, want %v", got, tc.want)
}
})
}
Expand Down
9 changes: 4 additions & 5 deletions apis/v1alpha2/validation/gatewayclass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ func TestValidateGatewayClassUpdate(t *testing.T) {
want: nil,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if got := ValidateGatewayClassUpdate(tt.args.oldClass, tt.args.newClass); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ValidateGatewayClassUpdate() = %v, want %v", got, tt.want)
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if got := ValidateGatewayClassUpdate(tc.args.oldClass, tc.args.newClass); !reflect.DeepEqual(got, tc.want) {
t.Errorf("ValidateGatewayClassUpdate() = %v, want %v", got, tc.want)
}
})
}
Expand Down
27 changes: 26 additions & 1 deletion apis/v1alpha2/validation/httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func validateHTTPRouteUniqueFilters(rules []gatewayv1a2.HTTPRouteRule, path *fie
}
// custom filters don't have any validation
for _, key := range repeatableHTTPRouteFilters {
counts[key] = 0
delete(counts, key)
}

for filterType, count := range counts {
Expand All @@ -64,7 +64,32 @@ func validateHTTPRouteUniqueFilters(rules []gatewayv1a2.HTTPRouteRule, path *fie
}
}

if errList := validateHTTPBackendUniqueFilters(rule.BackendRefs, path, i); len(errList) > 0 {
errs = append(errs, errList...)
}
}

return errs
}

func validateHTTPBackendUniqueFilters(ref []gatewayv1a2.HTTPBackendRef, path *field.Path, i int) field.ErrorList {
var errs field.ErrorList

for _, bkr := range ref {
counts := map[gatewayv1a2.HTTPRouteFilterType]int{}
for _, filter := range bkr.Filters {
counts[filter.Type]++
}

for _, key := range repeatableHTTPRouteFilters {
delete(counts, key)
}

for filterType, count := range counts {
if count > 1 {
errs = append(errs, field.Invalid(path.Index(i).Child("BackendRefs"), filterType, "cannot be used multiple times in the same backend"))
}
}
}
return errs
}
Loading

0 comments on commit e237162

Please sign in to comment.