Skip to content

Commit 8fb93bb

Browse files
authored
Change validation in Duration type in CRDs and NGF (#2525)
Problem: Duration type in crds was limited to being in seconds or milliseconds. This is inconvenient with the keepalive_time nginx directive which defaults to 1 hour. Solution: Relax the validation to allow for minutes and hours. Testing: Tested that crds can use minutes and hours for their duration fields.
1 parent 605bef5 commit 8fb93bb

File tree

8 files changed

+37
-25
lines changed

8 files changed

+37
-25
lines changed

apis/v1alpha1/shared_types.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package v1alpha1
22

33
// Duration is a string value representing a duration in time.
4-
// Duration can be specified in milliseconds (ms) or seconds (s) A value without a suffix is seconds.
5-
// Examples: 120s, 50ms.
4+
// Duration can be specified in milliseconds (ms), seconds (s), minutes (m), hours (h).
5+
// A value without a suffix is seconds.
6+
// Examples: 120s, 50ms, 5m, 1h.
67
//
7-
// +kubebuilder:validation:Pattern=`^\d{1,4}(ms|s)?$`
8+
// +kubebuilder:validation:Pattern=`^[0-9]{1,4}(ms|s|m|h)?$`
89
type Duration string
910

1011
// SpanAttribute is a key value pair to be added to a tracing span.

config/crd/bases/gateway.nginx.org_clientsettingspolicies.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ spec:
7070
If a client does not transmit anything within this time, the request is terminated with the
7171
408 (Request Time-out) error.
7272
Default: https://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_timeout.
73-
pattern: ^\d{1,4}(ms|s)?$
73+
pattern: ^[0-9]{1,4}(ms|s|m|h)?$
7474
type: string
7575
type: object
7676
keepAlive:
@@ -91,21 +91,21 @@ spec:
9191
Time defines the maximum time during which requests can be processed through one keep-alive connection.
9292
After this time is reached, the connection is closed following the subsequent request processing.
9393
Default: https://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_time.
94-
pattern: ^\d{1,4}(ms|s)?$
94+
pattern: ^[0-9]{1,4}(ms|s|m|h)?$
9595
type: string
9696
timeout:
9797
description: Timeout defines the keep-alive timeouts for clients.
9898
properties:
9999
header:
100100
description: 'Header sets the timeout in the "Keep-Alive:
101101
timeout=time" response header field.'
102-
pattern: ^\d{1,4}(ms|s)?$
102+
pattern: ^[0-9]{1,4}(ms|s|m|h)?$
103103
type: string
104104
server:
105105
description: |-
106106
Server sets the timeout during which a keep-alive client connection will stay open on the server side.
107107
Setting this value to 0 disables keep-alive client connections.
108-
pattern: ^\d{1,4}(ms|s)?$
108+
pattern: ^[0-9]{1,4}(ms|s|m|h)?$
109109
type: string
110110
type: object
111111
x-kubernetes-validations:

config/crd/bases/gateway.nginx.org_nginxproxies.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ spec:
156156
description: |-
157157
Interval is the maximum interval between two exports.
158158
Default: https://nginx.org/en/docs/ngx_otel_module.html#otel_exporter
159-
pattern: ^\d{1,4}(ms|s)?$
159+
pattern: ^[0-9]{1,4}(ms|s|m|h)?$
160160
type: string
161161
required:
162162
- endpoint

deploy/crds.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ spec:
6969
If a client does not transmit anything within this time, the request is terminated with the
7070
408 (Request Time-out) error.
7171
Default: https://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_timeout.
72-
pattern: ^\d{1,4}(ms|s)?$
72+
pattern: ^[0-9]{1,4}(ms|s|m|h)?$
7373
type: string
7474
type: object
7575
keepAlive:
@@ -90,21 +90,21 @@ spec:
9090
Time defines the maximum time during which requests can be processed through one keep-alive connection.
9191
After this time is reached, the connection is closed following the subsequent request processing.
9292
Default: https://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_time.
93-
pattern: ^\d{1,4}(ms|s)?$
93+
pattern: ^[0-9]{1,4}(ms|s|m|h)?$
9494
type: string
9595
timeout:
9696
description: Timeout defines the keep-alive timeouts for clients.
9797
properties:
9898
header:
9999
description: 'Header sets the timeout in the "Keep-Alive:
100100
timeout=time" response header field.'
101-
pattern: ^\d{1,4}(ms|s)?$
101+
pattern: ^[0-9]{1,4}(ms|s|m|h)?$
102102
type: string
103103
server:
104104
description: |-
105105
Server sets the timeout during which a keep-alive client connection will stay open on the server side.
106106
Setting this value to 0 disables keep-alive client connections.
107-
pattern: ^\d{1,4}(ms|s)?$
107+
pattern: ^[0-9]{1,4}(ms|s|m|h)?$
108108
type: string
109109
type: object
110110
x-kubernetes-validations:
@@ -741,7 +741,7 @@ spec:
741741
description: |-
742742
Interval is the maximum interval between two exports.
743743
Default: https://nginx.org/en/docs/ngx_otel_module.html#otel_exporter
744-
pattern: ^\d{1,4}(ms|s)?$
744+
pattern: ^[0-9]{1,4}(ms|s|m|h)?$
745745
type: string
746746
required:
747747
- endpoint

internal/mode/static/nginx/config/policies/clientsettings/validator_test.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,19 @@ func TestValidator_Validate(t *testing.T) {
103103
return p
104104
}),
105105
expConditions: []conditions.Condition{
106-
staticConds.NewPolicyInvalid("[spec.body.timeout: Invalid value: \"invalid\": \\d{1,4}(ms|s)? " +
107-
"(e.g. '5ms', or '10s', regex used for validation is 'must contain a number followed by 'ms' or 's''), " +
108-
"spec.keepAlive.time: Invalid value: \"invalid\": \\d{1,4}(ms|s)? (e.g. '5ms', or '10s', regex used for " +
109-
"validation is 'must contain a number followed by 'ms' or 's''), spec.keepAlive.timeout.server: Invalid value: " +
110-
"\"invalid\": \\d{1,4}(ms|s)? (e.g. '5ms', or '10s', regex used for validation is 'must contain a number " +
111-
"followed by 'ms' or 's''), spec.keepAlive.timeout.header: Invalid value: \"invalid\": \\d{1,4}(ms|s)? " +
112-
"(e.g. '5ms', or '10s', regex used for validation is 'must contain a number followed by 'ms' or 's'')]"),
106+
staticConds.NewPolicyInvalid(
107+
"[spec.body.timeout: Invalid value: \"invalid\": ^[0-9]{1,4}(ms|s|m|h)? " +
108+
"(e.g. '5ms', or '10s', or '500m', or '1000h', regex used for validation is " +
109+
"'must contain an, at most, four digit number followed by 'ms', 's', 'm', or 'h''), " +
110+
"spec.keepAlive.time: Invalid value: \"invalid\": ^[0-9]{1,4}(ms|s|m|h)? " +
111+
"(e.g. '5ms', or '10s', or '500m', or '1000h', regex used for validation is " +
112+
"'must contain an, at most, four digit number followed by 'ms', 's', 'm', or 'h''), " +
113+
"spec.keepAlive.timeout.server: Invalid value: \"invalid\": ^[0-9]{1,4}(ms|s|m|h)? " +
114+
"(e.g. '5ms', or '10s', or '500m', or '1000h', regex used for validation is " +
115+
"'must contain an, at most, four digit number followed by 'ms', 's', 'm', or 'h''), " +
116+
"spec.keepAlive.timeout.header: Invalid value: \"invalid\": ^[0-9]{1,4}(ms|s|m|h)? " +
117+
"(e.g. '5ms', or '10s', or '500m', or '1000h', regex used for validation is " +
118+
"'must contain an, at most, four digit number followed by 'ms', 's', 'm', or 'h'')]"),
113119
},
114120
},
115121
{

internal/mode/static/nginx/config/validation/generic.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func (GenericValidator) ValidateServiceName(name string) error {
3939
}
4040

4141
const (
42-
durationStringFmt = `\d{1,4}(ms|s)?`
43-
durationStringErrMsg = "must contain a number followed by 'ms' or 's'"
42+
durationStringFmt = `^[0-9]{1,4}(ms|s|m|h)?`
43+
durationStringErrMsg = "must contain an, at most, four digit number followed by 'ms', 's', 'm', or 'h'"
4444
)
4545

4646
var durationStringFmtRegexp = regexp.MustCompile("^" + durationStringFmt + "$")
@@ -51,6 +51,8 @@ func (GenericValidator) ValidateNginxDuration(duration string) error {
5151
examples := []string{
5252
"5ms",
5353
"10s",
54+
"500m",
55+
"1000h",
5456
}
5557

5658
return errors.New(k8svalidation.RegexError(durationStringFmt, durationStringErrMsg, examples...))

internal/mode/static/nginx/config/validation/generic_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ func TestValidateNginxDuration(t *testing.T) {
5656
`5ms`,
5757
`10s`,
5858
`123ms`,
59+
`5m`,
60+
`2h`,
5961
)
6062

6163
testInvalidValuesForSimpleValidator(
6264
t,
6365
validator.ValidateNginxDuration,
6466
`test`,
6567
`12345`,
66-
`5m`,
68+
`5k`,
6769
)
6870
}
6971

site/content/reference/api.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,8 +812,9 @@ Support: Gateway, HTTPRoute, GRPCRoute.</p>
812812
</p>
813813
<p>
814814
<p>Duration is a string value representing a duration in time.
815-
Duration can be specified in milliseconds (ms) or seconds (s) A value without a suffix is seconds.
816-
Examples: 120s, 50ms.</p>
815+
Duration can be specified in milliseconds (ms), seconds (s), minutes (m), hours (h).
816+
A value without a suffix is seconds.
817+
Examples: 120s, 50ms, 5m, 1h.</p>
817818
</p>
818819
<h3 id="gateway.nginx.org/v1alpha1.IPFamilyType">IPFamilyType
819820
(<code>string</code> alias)</p><a class="headerlink" href="#gateway.nginx.org%2fv1alpha1.IPFamilyType" title="Permanent link">¶</a>

0 commit comments

Comments
 (0)