|
33 | 33 | repeatableGRPCRouteFilters = []gatewayv1a2.GRPCRouteFilterType{ |
34 | 34 | gatewayv1a2.GRPCRouteFilterExtensionRef, |
35 | 35 | } |
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) |
38 | 40 | ) |
39 | 41 |
|
40 | 42 | // ValidateGRPCRoute validates GRPCRoute according to the Gateway API specification. |
@@ -76,27 +78,15 @@ func validateRuleMatches(matches []gatewayv1a2.GRPCRouteMatch, path *field.Path) |
76 | 78 | errs = append(errs, field.Required(path.Index(i).Child("method"), "one or both of `service` or `method` must be specified")) |
77 | 79 | } |
78 | 80 | // 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). |
80 | 82 | 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))) |
90 | 86 | } |
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))) |
100 | 90 | } |
101 | 91 | } |
102 | 92 | } |
|
0 commit comments