Skip to content

Commit 6efcfdf

Browse files
authored
Merge pull request #1972 from robscott/v0.7.0-cleanup
Validation cleanup from v0.7.0 API Review
2 parents 16c54cd + ae904c2 commit 6efcfdf

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

apis/v1alpha2/validation/grpcroute.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ var (
3333
repeatableGRPCRouteFilters = []gatewayv1a2.GRPCRouteFilterType{
3434
gatewayv1a2.GRPCRouteFilterExtensionRef,
3535
}
36-
validServiceName = `^(?i)\.?[a-z_][a-z_0-9]*(\.[a-z_][a-z_0-9]*)*$`
37-
validMethodName = `^[A-Za-z_][A-Za-z_0-9]*$`
36+
validServiceName = `^(?i)\.?[a-z_][a-z_0-9]*(\.[a-z_][a-z_0-9]*)*$`
37+
validServiceNameRegex = regexp.MustCompile(validServiceName)
38+
validMethodName = `^[A-Za-z_][A-Za-z_0-9]*$`
39+
validMethodNameRegex = regexp.MustCompile(validMethodName)
3840
)
3941

4042
// ValidateGRPCRoute validates GRPCRoute according to the Gateway API specification.
@@ -76,27 +78,15 @@ func validateRuleMatches(matches []gatewayv1a2.GRPCRouteMatch, path *field.Path)
7678
errs = append(errs, field.Required(path.Index(i).Child("method"), "one or both of `service` or `method` must be specified"))
7779
}
7880
// GRPCRoute method matcher admits two types: Exact and RegularExpression.
79-
// If not specified, the match will be treated as type Exact
81+
// If not specified, the match will be treated as type Exact (also the default value for this field).
8082
if m.Method.Type == nil || *m.Method.Type == gatewayv1a2.GRPCMethodMatchExact {
81-
if m.Method.Service != nil {
82-
r, err := regexp.Compile(validServiceName)
83-
if err != nil {
84-
errs = append(errs, field.InternalError(path.Index(i).Child("method"),
85-
fmt.Errorf("could not compile service name matching regex: %w", err)))
86-
} else if !r.MatchString(*m.Method.Service) {
87-
errs = append(errs, field.Invalid(path.Index(i).Child("method"), *m.Method.Service,
88-
fmt.Sprintf("must only contain valid characters (matching %s)", validServiceName)))
89-
}
83+
if m.Method.Service != nil && !validServiceNameRegex.MatchString(*m.Method.Service) {
84+
errs = append(errs, field.Invalid(path.Index(i).Child("method"), *m.Method.Service,
85+
fmt.Sprintf("must only contain valid characters (matching %s)", validServiceName)))
9086
}
91-
if m.Method.Method != nil {
92-
r, err := regexp.Compile(validMethodName)
93-
if err != nil {
94-
errs = append(errs, field.InternalError(path.Index(i).Child("method"),
95-
fmt.Errorf("could not compile method name matching regex: %w", err)))
96-
} else if !r.MatchString(*m.Method.Method) {
97-
errs = append(errs, field.Invalid(path.Index(i).Child("method"), *m.Method.Method,
98-
fmt.Sprintf("must only contain valid characters (matching %s)", validMethodName)))
99-
}
87+
if m.Method.Method != nil && !validMethodNameRegex.MatchString(*m.Method.Method) {
88+
errs = append(errs, field.Invalid(path.Index(i).Child("method"), *m.Method.Method,
89+
fmt.Sprintf("must only contain valid characters (matching %s)", validMethodName)))
10090
}
10191
}
10292
}

apis/v1beta1/validation/gateway.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func ValidateTLSCertificateRefs(listeners []gatewayv1b1.Listener, path *field.Pa
126126
// must be unique within the Gateway
127127
func ValidateListenerNames(listeners []gatewayv1b1.Listener, path *field.Path) field.ErrorList {
128128
var errs field.ErrorList
129-
nameMap := map[gatewayv1b1.SectionName]struct{}{}
129+
nameMap := make(map[gatewayv1b1.SectionName]struct{}, len(listeners))
130130
for i, c := range listeners {
131131
if _, found := nameMap[c.Name]; found {
132132
errs = append(errs, field.Duplicate(path.Index(i).Child("name"), fmt.Sprintln("must be unique within the Gateway")))

0 commit comments

Comments
 (0)