Skip to content

Commit d03c8dd

Browse files
committed
Tightening validation (kubernetes-sigs#772)
1 parent 1081e97 commit d03c8dd

9 files changed

+189
-133
lines changed

apis/v1alpha2/gateway_types.go

+5-12
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,7 @@ type Listener struct {
148148
// within a Gateway.
149149
//
150150
// Support: Core
151-
//
152-
// +kubebuilder:validation:MaxLength=253
153-
Name string `json:"name"`
151+
Name SectionName `json:"name"`
154152

155153
// Hostname specifies the virtual hostname to match for protocol types that
156154
// define this concept. When unspecified, "", or `*`, all hostnames are
@@ -322,6 +320,7 @@ type GatewayTLSConfig struct {
322320
// Support: Implementation-specific
323321
//
324322
// +optional
323+
// +kubebuilder:validation:MaxProperties=16
325324
Options map[string]string `json:"options,omitempty"`
326325
}
327326

@@ -414,14 +413,10 @@ type RouteGroupKind struct {
414413
//
415414
// +optional
416415
// +kubebuilder:default=gateway.networking.k8s.io
417-
// +kubebuilder:validation:MaxLength=253
418-
Group *string `json:"group,omitempty"`
416+
Group *Group `json:"group,omitempty"`
419417

420418
// Kind is the kind of the Route.
421-
//
422-
// +kubebuilder:validation:MinLength=1
423-
// +kubebuilder:validation:MaxLength=253
424-
Kind string `json:"kind"`
419+
Kind Kind `json:"kind"`
425420
}
426421

427422
// GatewayAddress describes an address that can be bound to a Gateway.
@@ -622,9 +617,7 @@ type ListenerStatus struct {
622617
// Name is the name of the Listener. If the Gateway has more than one
623618
// Listener present, each ListenerStatus MUST specify a name. The names of
624619
// ListenerStatus objects MUST be unique within a Gateway.
625-
//
626-
// +kubebuilder:validation:MaxLength=253
627-
Name string `json:"name"`
620+
Name SectionName `json:"name"`
628621

629622
// SupportedKinds is the list indicating the Kinds supported by this
630623
// listener. When this is not specified on the Listener, this MUST represent

apis/v1alpha2/gatewayclass_types.go

+5-14
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ type GatewayClassSpec struct {
6161
// and PATH is a valid HTTP path as defined by RFC 3986.
6262
//
6363
// Support: Core
64-
//
65-
// +kubebuilder:validation:MaxLength=253
66-
Controller string `json:"controller"`
64+
Controller GatewayController `json:"controller"`
6765

6866
// ParametersRef is a reference to a resource that contains the configuration
6967
// parameters corresponding to the GatewayClass. This is optional if the
@@ -83,7 +81,6 @@ type GatewayClassSpec struct {
8381

8482
// Description helps describe a GatewayClass with more details.
8583
//
86-
//
8784
// +kubebuilder:validation:MaxLength=64
8885
// +optional
8986
Description *string `json:"description,omitempty"`
@@ -93,15 +90,10 @@ type GatewayClassSpec struct {
9390
// configuration resource within the cluster.
9491
type ParametersReference struct {
9592
// Group is the group of the referent.
96-
//
97-
// +kubebuilder:validation:MaxLength=253
98-
Group string `json:"group"`
93+
Group Group `json:"group"`
9994

10095
// Kind is kind of the referent.
101-
//
102-
// +kubebuilder:validation:MinLength=1
103-
// +kubebuilder:validation:MaxLength=253
104-
Kind string `json:"kind"`
96+
Kind Kind `json:"kind"`
10597

10698
// Name is the name of the referent.
10799
//
@@ -111,6 +103,7 @@ type ParametersReference struct {
111103

112104
// Scope represents if the referent is a Cluster or Namespace scoped resource.
113105
// This may be set to "Cluster" or "Namespace".
106+
//
114107
// +kubebuilder:validation:Enum=Cluster;Namespace
115108
// +kubebuilder:default=Cluster
116109
// +optional
@@ -120,10 +113,8 @@ type ParametersReference struct {
120113
// This field is required when scope is set to "Namespace" and ignored when
121114
// scope is set to "Cluster".
122115
//
123-
// +kubebuilder:validation:MinLength=1
124-
// +kubebuilder:validation:MaxLength=253
125116
// +optional
126-
Namespace *string `json:"namespace,omitempty"`
117+
Namespace *Namespace `json:"namespace,omitempty"`
127118
}
128119

129120
// GatewayClassConditionType is the type for status conditions on

apis/v1alpha2/httproute_types.go

+39-11
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ type HTTPPathMatch struct {
262262
//
263263
// +optional
264264
// +kubebuilder:default="/"
265+
// +kubebuilder:validation:MaxLength=1024
265266
Value *string `json:"value,omitempty"`
266267
}
267268

@@ -282,6 +283,24 @@ const (
282283
HeaderMatchImplementationSpecific HeaderMatchType = "ImplementationSpecific"
283284
)
284285

286+
// HTTPHeaderName is the name of an HTTP header.
287+
//
288+
// Valid values include:
289+
//
290+
// * "Authorization"
291+
// * "Set-Cookie"
292+
//
293+
// Invalid values include:
294+
//
295+
// * ":method" - ":" is an invalid character. This means that pseudo headers are
296+
// not currently supported by this type.
297+
// * "/invalid" - "/" is an invalid character
298+
//
299+
// +kubebuilder:validation:MinLength=1
300+
// +kubebuilder:validation:MaxLength=256
301+
// +kubebuilder:validation:Pattern=`^[A-Za-z0-9!#$%&'*+\-.^_\x60|~]+$`
302+
type HTTPHeaderName string
303+
285304
// HTTPHeaderMatch describes how to select a HTTP route by matching HTTP request
286305
// headers.
287306
type HTTPHeaderMatch struct {
@@ -314,10 +333,7 @@ type HTTPHeaderMatch struct {
314333
// Generally, proxies should follow the guidance from the RFC:
315334
// https://www.rfc-editor.org/rfc/rfc7230.html#section-3.2.2 regarding
316335
// processing a repeated header, with special handling for "Set-Cookie".
317-
//
318-
// +kubebuilder:validation:MinLength=1
319-
// +kubebuilder:validation:MaxLength=256
320-
Name string `json:"name"`
336+
Name HTTPHeaderName `json:"name"`
321337

322338
// Value is the value of HTTP Header to be matched.
323339
//
@@ -423,14 +439,20 @@ type HTTPRouteMatch struct {
423439
// ANDed together, meaning, a request must match all the specified headers
424440
// to select the route.
425441
//
442+
// +listType=map
443+
// +listMapKey=name
426444
// +optional
445+
// +kubebuilder:validation:MaxItems=16
427446
Headers []HTTPHeaderMatch `json:"headers,omitempty"`
428447

429448
// QueryParams specifies HTTP query parameter matchers. Multiple match
430449
// values are ANDed together, meaning, a request must match all the
431450
// specified query parameters to select the route.
432451
//
452+
// +listType=map
453+
// +listMapKey=name
433454
// +optional
455+
// +kubebuilder:validation:MaxItems=16
434456
QueryParams []HTTPQueryParamMatch `json:"queryParams,omitempty"`
435457

436458
// Method specifies HTTP method matcher.
@@ -574,10 +596,7 @@ type HTTPHeader struct {
574596
// entries with an equivalent header name MUST be ignored. Due to the
575597
// case-insensitivity of header names, "foo" and "Foo" are considered
576598
// equivalent.
577-
//
578-
// +kubebuilder:validation:MinLength=1
579-
// +kubebuilder:validation:MaxLength=256
580-
Name string `json:"name"`
599+
Name HTTPHeaderName `json:"name"`
581600

582601
// Value is the value of HTTP Header to be matched.
583602
//
@@ -604,6 +623,9 @@ type HTTPRequestHeaderFilter struct {
604623
// my-header: bar
605624
//
606625
// +optional
626+
// +listType=map
627+
// +listMapKey=name
628+
// +kubebuilder:validation:MaxItems=16
607629
Set []HTTPHeader `json:"set,omitempty"`
608630

609631
// Add adds the given header(s) (name, value) to the request
@@ -623,6 +645,9 @@ type HTTPRequestHeaderFilter struct {
623645
// my-header: bar
624646
//
625647
// +optional
648+
// +listType=map
649+
// +listMapKey=name
650+
// +kubebuilder:validation:MaxItems=16
626651
Add []HTTPHeader `json:"add,omitempty"`
627652

628653
// Remove the given header(s) from the HTTP request before the
@@ -659,29 +684,32 @@ type HTTPRequestRedirect struct {
659684
// +optional
660685
// +kubebuilder:validation:Enum=HTTP;HTTPS
661686
Protocol *string `json:"protocol,omitempty"`
687+
662688
// Hostname is the hostname to be used in the value of the `Location`
663689
// header in the response.
664690
// When empty, the hostname of the request is used.
665691
//
666692
// Support: Core
667693
//
668694
// +optional
669-
Hostname *string `json:"hostname,omitempty"`
695+
Hostname *Hostname `json:"hostname,omitempty"`
696+
670697
// Port is the port to be used in the value of the `Location`
671698
// header in the response.
672699
// When empty, port (if specified) of the request is used.
673700
//
674701
// Support: Extended
675702
//
676703
// +optional
677-
Port *int `json:"port,omitempty"`
704+
Port *PortNumber `json:"port,omitempty"`
705+
678706
// StatusCode is the HTTP status code to be used in response.
679707
//
680708
// Support: Core
681709
//
682710
// +optional
683711
// +kubebuilder:default=302
684-
// +kubebuilder:validation=301;302
712+
// +kubebuilder:validation:Enum=301;302
685713
StatusCode *int `json:"statusCode,omitempty"`
686714
}
687715

apis/v1alpha2/object_reference_types.go

+15-29
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,12 @@ package v1alpha2
1919
// LocalObjectReference identifies an API object within the namespace of the
2020
// referrer.
2121
type LocalObjectReference struct {
22-
// Group is the group of the referent.
23-
//
24-
// +kubebuilder:validation:MaxLength=253
25-
Group string `json:"group"`
22+
// Group is the group of the referent. For example, "networking.k8s.io".
23+
// When unspecified (empty string), core API group is inferred.
24+
Group Group `json:"group"`
2625

27-
// Kind is kind of the referent.
28-
//
29-
// +kubebuilder:validation:MinLength=1
30-
// +kubebuilder:validation:MaxLength=253
31-
Kind string `json:"kind"`
26+
// Kind is kind of the referent. For example "HTTPRoute" or "Service".
27+
Kind Kind `json:"kind"`
3228

3329
// Name is the name of the referent.
3430
//
@@ -39,21 +35,18 @@ type LocalObjectReference struct {
3935

4036
// ObjectReference identifies an API object including its namespace.
4137
type ObjectReference struct {
42-
// Group is the group of the referent.
38+
// Group is the group of the referent. For example, "networking.k8s.io".
4339
// When unspecified (empty string), core API group is inferred.
4440
//
4541
// +optional
4642
// +kubebuilder:default=""
47-
// +kubebuilder:validation:MaxLength=253
48-
Group *string `json:"group"`
43+
Group *Group `json:"group"`
4944

50-
// Kind is kind of the referent.
45+
// Kind is kind of the referent. For example "HTTPRoute" or "Service".
5146
//
5247
// +optional
5348
// +kubebuilder:default=Service
54-
// +kubebuilder:validation:MinLength=1
55-
// +kubebuilder:validation:MaxLength=253
56-
Kind *string `json:"kind"`
49+
Kind *Kind `json:"kind"`
5750

5851
// Name is the name of the referent.
5952
//
@@ -71,10 +64,8 @@ type ObjectReference struct {
7164
//
7265
// Support: Core
7366
//
74-
// +kubebuilder:validation:MinLength=1
75-
// +kubebuilder:validation:MaxLength=253
7667
// +optional
77-
Namespace *string `json:"namespace,omitempty"`
68+
Namespace *Namespace `json:"namespace,omitempty"`
7869
}
7970

8071
// BackendObjectReference defines how an ObjectReference that is
@@ -86,21 +77,18 @@ type ObjectReference struct {
8677
// owner to accept the reference. See the ReferencePolicy documentation
8778
// for details.
8879
type BackendObjectReference struct {
89-
// Group is the group of the referent.
80+
// Group is the group of the referent. For example, "networking.k8s.io".
9081
// When unspecified (empty string), core API group is inferred.
9182
//
9283
// +optional
9384
// +kubebuilder:default=""
94-
// +kubebuilder:validation:MaxLength=253
95-
Group *string `json:"group,omitempty"`
85+
Group *Group `json:"group,omitempty"`
9686

97-
// Kind is kind of the referent.
87+
// Kind is kind of the referent. For example "HTTPRoute" or "Service".
9888
//
9989
// +optional
10090
// +kubebuilder:default=Service
101-
// +kubebuilder:validation:MinLength=1
102-
// +kubebuilder:validation:MaxLength=253
103-
Kind *string `json:"kind,omitempty"`
91+
Kind *Kind `json:"kind,omitempty"`
10492

10593
// Name is the name of the referent.
10694
//
@@ -118,10 +106,8 @@ type BackendObjectReference struct {
118106
//
119107
// Support: Core
120108
//
121-
// +kubebuilder:validation:MinLength=1
122-
// +kubebuilder:validation:MaxLength=253
123109
// +optional
124-
Namespace *string `json:"namespace,omitempty"`
110+
Namespace *Namespace `json:"namespace,omitempty"`
125111

126112
// Port specifies the destination port number to use for this resource.
127113
// Port is required when the referent is a Kubernetes Service.

apis/v1alpha2/policy_types.go

+3-11
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,10 @@ package v1alpha2
2323
// for Gateway API.
2424
type PolicyTargetReference struct {
2525
// Group is the group of the target resource.
26-
//
27-
// +kubebuilder:validation:MinLength=1
28-
// +kubebuilder:validation:MaxLength=253
29-
Group string `json:"group"`
26+
Group Group `json:"group"`
3027

3128
// Kind is kind of the target resource.
32-
//
33-
// +kubebuilder:validation:MinLength=1
34-
// +kubebuilder:validation:MaxLength=253
35-
Kind string `json:"kind"`
29+
Kind Kind `json:"kind"`
3630

3731
// Name is the name of the target resource.
3832
//
@@ -45,10 +39,8 @@ type PolicyTargetReference struct {
4539
// namespace, it MUST only apply to traffic originating from the same
4640
// namespace as the policy.
4741
//
48-
// +kubebuilder:validation:MinLength=1
49-
// +kubebuilder:validation:MaxLength=253
5042
// +optional
51-
Namespace *string `json:"namespace,omitempty"`
43+
Namespace *Namespace `json:"namespace,omitempty"`
5244

5345
// ClassName is the name of the class this policy should apply to. When
5446
// unspecified, the policy will apply to all classes that support it.

0 commit comments

Comments
 (0)