Skip to content

Commit 14afcd9

Browse files
authored
Consistent validation for reference types (#430)
* Consistent validation for reference types * Code updates after API type changes * Moving Label types to shared_types.go
1 parent b1fed6c commit 14afcd9

File tree

12 files changed

+166
-91
lines changed

12 files changed

+166
-91
lines changed

api/v1alpha2/inferencemodel_types.go

+3-10
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,18 @@ type PoolObjectReference struct {
107107
//
108108
// +optional
109109
// +kubebuilder:default="inference.networking.x-k8s.io"
110-
// +kubebuilder:validation:MaxLength=253
111-
// +kubebuilder:validation:Pattern=`^$|^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`
112-
Group string `json:"group,omitempty"`
110+
Group Group `json:"group,omitempty"`
113111

114112
// Kind is kind of the referent. For example "InferencePool".
115113
//
116114
// +optional
117115
// +kubebuilder:default="InferencePool"
118-
// +kubebuilder:validation:MinLength=1
119-
// +kubebuilder:validation:MaxLength=63
120-
// +kubebuilder:validation:Pattern=`^[a-zA-Z]([-a-zA-Z0-9]*[a-zA-Z0-9])?$`
121-
Kind string `json:"kind,omitempty"`
116+
Kind Kind `json:"kind,omitempty"`
122117

123118
// Name is the name of the referent.
124119
//
125-
// +kubebuilder:validation:MinLength=1
126-
// +kubebuilder:validation:MaxLength=253
127120
// +kubebuilder:validation:Required
128-
Name string `json:"name"`
121+
Name ObjectName `json:"name"`
129122
}
130123

131124
// Criticality defines how important it is to serve the model compared to other models.

api/v1alpha2/inferencepool_types.go

+8-50
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ type Extension struct {
9090
// ExtensionReference is a reference to the extension deployment.
9191
type ExtensionReference struct {
9292
// Group is the group of the referent.
93-
// When unspecified or empty string, core API group is inferred.
93+
// The default value is "", representing the Core API group.
9494
//
9595
// +optional
9696
// +kubebuilder:default=""
97-
Group *string `json:"group,omitempty"`
97+
Group *Group `json:"group,omitempty"`
9898

9999
// Kind is the Kubernetes resource kind of the referent. For example
100100
// "Service".
@@ -109,20 +109,19 @@ type ExtensionReference struct {
109109
//
110110
// +optional
111111
// +kubebuilder:default=Service
112-
Kind *string `json:"kind,omitempty"`
112+
Kind *Kind `json:"kind,omitempty"`
113113

114114
// Name is the name of the referent.
115115
//
116116
// +kubebuilder:validation:Required
117-
Name string `json:"name"`
117+
Name ObjectName `json:"name"`
118118

119-
// The port number on the service running the extension. When unspecified, implementations SHOULD infer a
120-
// default value of 9002 when the Kind is Service.
119+
// The port number on the service running the extension. When unspecified,
120+
// implementations SHOULD infer a default value of 9002 when the Kind is
121+
// Service.
121122
//
122-
// +kubebuilder:validation:Minimum=1
123-
// +kubebuilder:validation:Maximum=65535
124123
// +optional
125-
PortNumber *int32 `json:"targetPortNumber,omitempty"`
124+
PortNumber *PortNumber `json:"portNumber,omitempty"`
126125
}
127126

128127
// ExtensionConnection encapsulates options that configures the connection to the extension.
@@ -147,47 +146,6 @@ const (
147146
FailClose ExtensionFailureMode = "FailClose"
148147
)
149148

150-
// LabelKey was originally copied from: https://github.com/kubernetes-sigs/gateway-api/blob/99a3934c6bc1ce0874f3a4c5f20cafd8977ffcb4/apis/v1/shared_types.go#L694-L731
151-
// Duplicated as to not take an unexpected dependency on gw's API.
152-
//
153-
// LabelKey is the key of a label. This is used for validation
154-
// of maps. This matches the Kubernetes "qualified name" validation that is used for labels.
155-
// Labels are case sensitive, so: my-label and My-Label are considered distinct.
156-
//
157-
// Valid values include:
158-
//
159-
// * example
160-
// * example.com
161-
// * example.com/path
162-
// * example.com/path.html
163-
//
164-
// Invalid values include:
165-
//
166-
// * example~ - "~" is an invalid character
167-
// * example.com. - can not start or end with "."
168-
//
169-
// +kubebuilder:validation:MinLength=1
170-
// +kubebuilder:validation:MaxLength=253
171-
// +kubebuilder:validation:Pattern=`^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?([A-Za-z0-9][-A-Za-z0-9_.]{0,61})?[A-Za-z0-9]$`
172-
type LabelKey string
173-
174-
// LabelValue is the value of a label. This is used for validation
175-
// of maps. This matches the Kubernetes label validation rules:
176-
// * must be 63 characters or less (can be empty),
177-
// * unless empty, must begin and end with an alphanumeric character ([a-z0-9A-Z]),
178-
// * could contain dashes (-), underscores (_), dots (.), and alphanumerics between.
179-
//
180-
// Valid values include:
181-
//
182-
// * MyValue
183-
// * my.name
184-
// * 123-my-value
185-
//
186-
// +kubebuilder:validation:MinLength=0
187-
// +kubebuilder:validation:MaxLength=63
188-
// +kubebuilder:validation:Pattern=`^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$`
189-
type LabelValue string
190-
191149
// InferencePoolStatus defines the observed state of InferencePool
192150
type InferencePoolStatus struct {
193151
// Parents is a list of parent resources (usually Gateways) that are

api/v1alpha2/shared_types.go

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha2
18+
19+
// Group refers to a Kubernetes Group. It must either be an empty string or a
20+
// RFC 1123 subdomain.
21+
//
22+
// This validation is based off of the corresponding Kubernetes validation:
23+
// https://github.com/kubernetes/apimachinery/blob/02cfb53916346d085a6c6c7c66f882e3c6b0eca6/pkg/util/validation/validation.go#L208
24+
//
25+
// Valid values include:
26+
//
27+
// * "" - empty string implies core Kubernetes API group
28+
// * "gateway.networking.k8s.io"
29+
// * "foo.example.com"
30+
//
31+
// Invalid values include:
32+
//
33+
// * "example.com/bar" - "/" is an invalid character
34+
//
35+
// +kubebuilder:validation:MaxLength=253
36+
// +kubebuilder:validation:Pattern=`^$|^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`
37+
type Group string
38+
39+
// Kind refers to a Kubernetes Kind.
40+
//
41+
// Valid values include:
42+
//
43+
// * "Service"
44+
// * "HTTPRoute"
45+
//
46+
// Invalid values include:
47+
//
48+
// * "invalid/kind" - "/" is an invalid character
49+
//
50+
// +kubebuilder:validation:MinLength=1
51+
// +kubebuilder:validation:MaxLength=63
52+
// +kubebuilder:validation:Pattern=`^[a-zA-Z]([-a-zA-Z0-9]*[a-zA-Z0-9])?$`
53+
type Kind string
54+
55+
// ObjectName refers to the name of a Kubernetes object.
56+
// Object names can have a variety of forms, including RFC 1123 subdomains,
57+
// RFC 1123 labels, or RFC 1035 labels.
58+
//
59+
// +kubebuilder:validation:MinLength=1
60+
// +kubebuilder:validation:MaxLength=253
61+
type ObjectName string
62+
63+
// PortNumber defines a network port.
64+
//
65+
// +kubebuilder:validation:Minimum=1
66+
// +kubebuilder:validation:Maximum=65535
67+
type PortNumber int32
68+
69+
// LabelKey was originally copied from: https://github.com/kubernetes-sigs/gateway-api/blob/99a3934c6bc1ce0874f3a4c5f20cafd8977ffcb4/apis/v1/shared_types.go#L694-L731
70+
// Duplicated as to not take an unexpected dependency on gw's API.
71+
//
72+
// LabelKey is the key of a label. This is used for validation
73+
// of maps. This matches the Kubernetes "qualified name" validation that is used for labels.
74+
// Labels are case sensitive, so: my-label and My-Label are considered distinct.
75+
//
76+
// Valid values include:
77+
//
78+
// * example
79+
// * example.com
80+
// * example.com/path
81+
// * example.com/path.html
82+
//
83+
// Invalid values include:
84+
//
85+
// * example~ - "~" is an invalid character
86+
// * example.com. - can not start or end with "."
87+
//
88+
// +kubebuilder:validation:MinLength=1
89+
// +kubebuilder:validation:MaxLength=253
90+
// +kubebuilder:validation:Pattern=`^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?([A-Za-z0-9][-A-Za-z0-9_.]{0,61})?[A-Za-z0-9]$`
91+
type LabelKey string
92+
93+
// LabelValue is the value of a label. This is used for validation
94+
// of maps. This matches the Kubernetes label validation rules:
95+
// * must be 63 characters or less (can be empty),
96+
// * unless empty, must begin and end with an alphanumeric character ([a-z0-9A-Z]),
97+
// * could contain dashes (-), underscores (_), dots (.), and alphanumerics between.
98+
//
99+
// Valid values include:
100+
//
101+
// * MyValue
102+
// * my.name
103+
// * 123-my-value
104+
//
105+
// +kubebuilder:validation:MinLength=0
106+
// +kubebuilder:validation:MaxLength=63
107+
// +kubebuilder:validation:Pattern=`^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$`
108+
type LabelValue string

api/v1alpha2/zz_generated.deepcopy.go

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client-go/applyconfiguration/api/v1alpha2/extension.go

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client-go/applyconfiguration/api/v1alpha2/extensionreference.go

+12-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client-go/applyconfiguration/api/v1alpha2/poolobjectreference.go

+10-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)