Skip to content

Commit d67d1f4

Browse files
committed
feat: enhance ApisixUpstream with health checks, TLS, and CRD validations
1 parent b55be27 commit d67d1f4

File tree

4 files changed

+238
-33
lines changed

4 files changed

+238
-33
lines changed

api/v2/apisixupstream_types.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
)
2020

2121
// ApisixUpstreamSpec describes the specification of ApisixUpstream.
22+
// +kubebuilder:validation:XValidation:rule="has(self.externalNodes)!=has(discovery)"
2223
type ApisixUpstreamSpec struct {
2324
// IngressClassName is the name of an IngressClass cluster resource.
2425
// controller implementations use this field to know whether they should be
@@ -29,6 +30,7 @@ type ApisixUpstreamSpec struct {
2930
// ExternalNodes contains external nodes the Upstream should use
3031
// If this field is set, the upstream will use these nodes directly without any further resolves
3132
// +kubebuilder:validation:Optional
33+
// +kubebuilder:validation:MinItems=1
3234
ExternalNodes []ApisixUpstreamExternalNode `json:"externalNodes,omitempty" yaml:"externalNodes,omitempty"`
3335

3436
ApisixUpstreamConfig `json:",inline" yaml:",inline"`
@@ -76,6 +78,7 @@ type ApisixUpstreamConfig struct {
7678
// The scheme used to talk with the upstream.
7779
// Now value can be http, grpc.
7880
// +kubebuilder:validation:Optional
81+
// +kubebuilder:validation:Enum=http;https;grpc;grpcs;
7982
Scheme string `json:"scheme,omitempty" yaml:"scheme,omitempty"`
8083

8184
// How many times that the proxy (Apache APISIX) should do when
@@ -103,6 +106,7 @@ type ApisixUpstreamConfig struct {
103106
// Configures the host when the request is forwarded to the upstream.
104107
// Can be one of pass, node or rewrite.
105108
// +kubebuilder:validation:Optional
109+
// +kubebuilder:validation:Enum=pass;node;rewrite;
106110
PassHost string `json:"passHost,omitempty" yaml:"passHost,omitempty"`
107111

108112
// Specifies the host of the Upstream request. This is only valid if
@@ -140,7 +144,9 @@ type LoadBalancer struct {
140144

141145
// HealthCheck describes the upstream health check parameters.
142146
type HealthCheck struct {
143-
Active *ActiveHealthCheck `json:"active" yaml:"active"`
147+
// +kubebuilder:validation:Required
148+
Active *ActiveHealthCheck `json:"active" yaml:"active"`
149+
// +kubebuilder:validation:Optional
144150
Passive *PassiveHealthCheck `json:"passive,omitempty" yaml:"passive,omitempty"`
145151
}
146152

@@ -161,10 +167,15 @@ type Discovery struct {
161167

162168
// ActiveHealthCheck defines the active kind of upstream health check.
163169
type ActiveHealthCheck struct {
164-
Type string `json:"type,omitempty" yaml:"type,omitempty"`
165-
Timeout time.Duration `json:"timeout,omitempty" yaml:"timeout,omitempty"`
166-
Concurrency int `json:"concurrency,omitempty" yaml:"concurrency,omitempty"`
167-
Host string `json:"host,omitempty" yaml:"host,omitempty"`
170+
// +kubebuilder:validation:Optional
171+
// +kubebuilder:validation:Enum=http;https;tcp;
172+
Type string `json:"type,omitempty" yaml:"type,omitempty"`
173+
Timeout time.Duration `json:"timeout,omitempty" yaml:"timeout,omitempty"`
174+
// +kubebuilder:validation:Minimum=0
175+
Concurrency int `json:"concurrency,omitempty" yaml:"concurrency,omitempty"`
176+
Host string `json:"host,omitempty" yaml:"host,omitempty"`
177+
// +kubebuilder:validation:Minimum=0
178+
// +kubebuilder:validation:Maximum=65535
168179
Port int32 `json:"port,omitempty" yaml:"port,omitempty"`
169180
HTTPPath string `json:"httpPath,omitempty" yaml:"httpPath,omitempty"`
170181
StrictTLS *bool `json:"strictTLS,omitempty" yaml:"strictTLS,omitempty"`
@@ -200,17 +211,27 @@ type ActiveHealthCheckUnhealthy struct {
200211
// PassiveHealthCheckHealthy defines the conditions to judge whether
201212
// an upstream node is healthy with the passive manner.
202213
type PassiveHealthCheckHealthy struct {
214+
// +kubebuilder:validation:Optional
215+
// +kubebuilder:validation:MinItems=1
203216
HTTPCodes []int `json:"httpCodes,omitempty" yaml:"httpCodes,omitempty"`
204-
Successes int `json:"successes,omitempty" yaml:"successes,omitempty"`
217+
// +kubebuilder:validation:Minimum=0
218+
// +kubebuilder:validation:Maximum=254
219+
Successes int `json:"successes,omitempty" yaml:"successes,omitempty"`
205220
}
206221

207222
// PassiveHealthCheckUnhealthy defines the conditions to judge whether
208223
// an upstream node is unhealthy with the passive manager.
209224
type PassiveHealthCheckUnhealthy struct {
210-
HTTPCodes []int `json:"httpCodes,omitempty" yaml:"httpCodes,omitempty"`
211-
HTTPFailures int `json:"httpFailures,omitempty" yaml:"http_failures,omitempty"`
212-
TCPFailures int `json:"tcpFailures,omitempty" yaml:"tcpFailures,omitempty"`
213-
Timeouts int `json:"timeout,omitempty" yaml:"timeout,omitempty"`
225+
// +kubebuilder:validation:Optional
226+
// +kubebuilder:validation:MinItems=1
227+
HTTPCodes []int `json:"httpCodes,omitempty" yaml:"httpCodes,omitempty"`
228+
// +kubebuilder:validation:Minimum=0
229+
// +kubebuilder:validation:Maximum=254
230+
HTTPFailures int `json:"httpFailures,omitempty" yaml:"http_failures,omitempty"`
231+
// +kubebuilder:validation:Minimum=0
232+
// +kubebuilder:validation:Maximum=254
233+
TCPFailures int `json:"tcpFailures,omitempty" yaml:"tcpFailures,omitempty"`
234+
Timeouts int `json:"timeout,omitempty" yaml:"timeout,omitempty"`
214235
}
215236

216237
func init() {

api/v2/shared_types.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,22 @@ const (
136136
ExternalTypeService ApisixUpstreamExternalType = "Service"
137137
)
138138

139+
const (
140+
// HealthCheckHTTP represents the HTTP kind health check.
141+
HealthCheckHTTP = "http"
142+
// HealthCheckHTTPS represents the HTTPS kind health check.
143+
HealthCheckHTTPS = "https"
144+
// HealthCheckTCP represents the TCP kind health check.
145+
HealthCheckTCP = "tcp"
146+
147+
// HealthCheckMaxConsecutiveNumber is the max number for
148+
// the consecutive success/failure in upstream health check.
149+
HealthCheckMaxConsecutiveNumber = 254
150+
// ActiveHealthCheckMinInterval is the minimum interval for
151+
// the active health check.
152+
ActiveHealthCheckMinInterval = time.Second
153+
)
154+
139155
var schemeToPortMaps = map[string]int{
140156
SchemeHTTP: 80,
141157
SchemeHTTPS: 443,

config/crd/bases/apisix.apache.org_apisixupstreams.yaml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ spec:
7474
weight:
7575
type: integer
7676
type: object
77+
minItems: 1
7778
type: array
7879
healthCheck:
7980
description: The health check configurations for the upstream.
@@ -83,6 +84,7 @@ spec:
8384
health check.
8485
properties:
8586
concurrency:
87+
minimum: 0
8688
type: integer
8789
healthy:
8890
description: |-
@@ -92,10 +94,13 @@ spec:
9294
httpCodes:
9395
items:
9496
type: integer
97+
minItems: 1
9598
type: array
9699
interval:
97100
type: string
98101
successes:
102+
maximum: 254
103+
minimum: 0
99104
type: integer
100105
type: object
101106
host:
@@ -104,6 +109,8 @@ spec:
104109
type: string
105110
port:
106111
format: int32
112+
maximum: 65535
113+
minimum: 0
107114
type: integer
108115
requestHeaders:
109116
items:
@@ -119,6 +126,10 @@ spec:
119126
format: int64
120127
type: integer
121128
type:
129+
enum:
130+
- http
131+
- https
132+
- tcp
122133
type: string
123134
unhealthy:
124135
description: |-
@@ -128,12 +139,17 @@ spec:
128139
httpCodes:
129140
items:
130141
type: integer
142+
minItems: 1
131143
type: array
132144
httpFailures:
145+
maximum: 254
146+
minimum: 0
133147
type: integer
134148
interval:
135149
type: string
136150
tcpFailures:
151+
maximum: 254
152+
minimum: 0
137153
type: integer
138154
timeout:
139155
type: integer
@@ -152,8 +168,11 @@ spec:
152168
httpCodes:
153169
items:
154170
type: integer
171+
minItems: 1
155172
type: array
156173
successes:
174+
maximum: 254
175+
minimum: 0
157176
type: integer
158177
type: object
159178
type:
@@ -166,10 +185,15 @@ spec:
166185
httpCodes:
167186
items:
168187
type: integer
188+
minItems: 1
169189
type: array
170190
httpFailures:
191+
maximum: 254
192+
minimum: 0
171193
type: integer
172194
tcpFailures:
195+
maximum: 254
196+
minimum: 0
173197
type: integer
174198
timeout:
175199
type: integer
@@ -207,6 +231,10 @@ spec:
207231
description: |-
208232
Configures the host when the request is forwarded to the upstream.
209233
Can be one of pass, node or rewrite.
234+
enum:
235+
- pass
236+
- node
237+
- rewrite
210238
type: string
211239
portLevelSettings:
212240
items:
@@ -239,6 +267,7 @@ spec:
239267
upstream health check.
240268
properties:
241269
concurrency:
270+
minimum: 0
242271
type: integer
243272
healthy:
244273
description: |-
@@ -248,10 +277,13 @@ spec:
248277
httpCodes:
249278
items:
250279
type: integer
280+
minItems: 1
251281
type: array
252282
interval:
253283
type: string
254284
successes:
285+
maximum: 254
286+
minimum: 0
255287
type: integer
256288
type: object
257289
host:
@@ -260,6 +292,8 @@ spec:
260292
type: string
261293
port:
262294
format: int32
295+
maximum: 65535
296+
minimum: 0
263297
type: integer
264298
requestHeaders:
265299
items:
@@ -275,6 +309,10 @@ spec:
275309
format: int64
276310
type: integer
277311
type:
312+
enum:
313+
- http
314+
- https
315+
- tcp
278316
type: string
279317
unhealthy:
280318
description: |-
@@ -284,12 +322,17 @@ spec:
284322
httpCodes:
285323
items:
286324
type: integer
325+
minItems: 1
287326
type: array
288327
httpFailures:
328+
maximum: 254
329+
minimum: 0
289330
type: integer
290331
interval:
291332
type: string
292333
tcpFailures:
334+
maximum: 254
335+
minimum: 0
293336
type: integer
294337
timeout:
295338
type: integer
@@ -308,8 +351,11 @@ spec:
308351
httpCodes:
309352
items:
310353
type: integer
354+
minItems: 1
311355
type: array
312356
successes:
357+
maximum: 254
358+
minimum: 0
313359
type: integer
314360
type: object
315361
type:
@@ -322,10 +368,15 @@ spec:
322368
httpCodes:
323369
items:
324370
type: integer
371+
minItems: 1
325372
type: array
326373
httpFailures:
374+
maximum: 254
375+
minimum: 0
327376
type: integer
328377
tcpFailures:
378+
maximum: 254
379+
minimum: 0
329380
type: integer
330381
timeout:
331382
type: integer
@@ -356,6 +407,10 @@ spec:
356407
description: |-
357408
Configures the host when the request is forwarded to the upstream.
358409
Can be one of pass, node or rewrite.
410+
enum:
411+
- pass
412+
- node
413+
- rewrite
359414
type: string
360415
port:
361416
description: Port is a Kubernetes Service port, it should be
@@ -372,6 +427,11 @@ spec:
372427
description: |-
373428
The scheme used to talk with the upstream.
374429
Now value can be http, grpc.
430+
enum:
431+
- http
432+
- https
433+
- grpc
434+
- grpcs
375435
type: string
376436
subsets:
377437
description: |-
@@ -438,6 +498,11 @@ spec:
438498
description: |-
439499
The scheme used to talk with the upstream.
440500
Now value can be http, grpc.
501+
enum:
502+
- http
503+
- https
504+
- grpc
505+
- grpcs
441506
type: string
442507
subsets:
443508
description: |-
@@ -490,6 +555,8 @@ spec:
490555
the pass_host is set to rewrite
491556
type: string
492557
type: object
558+
x-kubernetes-validations:
559+
- rule: has(self.externalNodes)!=has(discovery)
493560
type: object
494561
served: true
495562
storage: true

0 commit comments

Comments
 (0)