Skip to content

Commit 3ae9fa7

Browse files
authored
Merge pull request #2226 from gauravkghildiyal/cel-validation
Introduce validations through CEL for Gateway and GatewayClass.
2 parents 7aabadb + 2210ded commit 3ae9fa7

10 files changed

+831
-0
lines changed

apis/v1beta1/gateway_types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ type GatewaySpec struct {
117117
// +listMapKey=name
118118
// +kubebuilder:validation:MinItems=1
119119
// +kubebuilder:validation:MaxItems=64
120+
// +kubebuilder:validation:XValidation:message="tls must be set for protocols ['HTTPS', 'TLS']",rule="self.all(l, l.protocol in ['HTTPS', 'TLS'] ? has(l.tls) : true)"
121+
// +kubebuilder:validation:XValidation:message="tls must be empty for protocols ['HTTP', 'TCP', 'UDP']",rule="self.all(l, l.protocol in ['HTTP', 'TCP', 'UDP'] ? !has(l.tls) : true)"
122+
// +kubebuilder:validation:XValidation:message="hostname must be empty for protocols ['TCP', 'UDP']",rule="self.all(l, l.protocol in ['TCP', 'UDP'] ? (!has(l.hostname) || l.hostname == '') : true)"
123+
// +kubebuilder:validation:XValidation:message="Listener name must be unique within the Gateway",rule="self.all(l1, self.exists_one(l2, l1.name == l2.name))"
124+
// +kubebuilder:validation:XValidation:message="Combination of port, protocol and hostname must be unique for each listener",rule="self.all(l1, self.exists_one(l2, l1.port == l2.port && l1.protocol == l2.protocol && (has(l1.hostname) && has(l2.hostname) ? l1.hostname == l2.hostname : true)))"
120125
Listeners []Listener `json:"listeners"`
121126

122127
// Infrastructure defines infrastructure level attributes about this Gateway instance.
@@ -151,6 +156,8 @@ type GatewaySpec struct {
151156
//
152157
// +optional
153158
// +kubebuilder:validation:MaxItems=16
159+
// +kubebuilder:validation:XValidation:message="IPAddress values must be unique",rule="self.all(a1, a1.type == 'IPAddress' ? self.exists_one(a2, a2.type == a1.type && a2.value == a1.value) : true )"
160+
// +kubebuilder:validation:XValidation:message="Hostname values must be unique",rule="self.all(a1, a1.type == 'Hostname' ? self.exists_one(a2, a2.type == a1.type && a2.value == a1.value) : true )"
154161
Addresses []GatewayAddress `json:"addresses,omitempty"`
155162
}
156163

@@ -361,6 +368,8 @@ const (
361368
)
362369

363370
// GatewayTLSConfig describes a TLS configuration.
371+
//
372+
// +kubebuilder:validation:XValidation:message="certificateRefs must be set and not empty when TLSModeType is Terminate",rule="self.mode == 'Terminate' ? size(self.certificateRefs) > 0 : true"
364373
type GatewayTLSConfig struct {
365374
// Mode defines the TLS behavior for the TLS session initiated by the client.
366375
// There are two possible modes:
@@ -526,6 +535,8 @@ type RouteGroupKind struct {
526535
}
527536

528537
// GatewayAddress describes an address that can be bound to a Gateway.
538+
//
539+
// +kubebuilder:validation:XValidation:message="Hostname value must only contain valid characters (matching ^(\\*\\.)?[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$)",rule="self.type == 'Hostname' ? self.value.matches('^(\\\\*\\\\.)?[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$'): true"
529540
type GatewayAddress struct {
530541
// Type of the address.
531542
//

apis/v1beta1/gatewayclass_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ type GatewayClassSpec struct {
8181
// This field is not mutable and cannot be empty.
8282
//
8383
// Support: Core
84+
//
85+
// +kubebuilder:validation:XValidation:message="Value is immutable",rule="self == oldSelf"
8486
ControllerName GatewayController `json:"controllerName"`
8587

8688
// ParametersRef is a reference to a resource that contains the configuration

config/crd/experimental/gateway.networking.k8s.io_gatewayclasses.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/experimental/gateway.networking.k8s.io_gateways.yaml

Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/standard/gateway.networking.k8s.io_gatewayclasses.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/standard/gateway.networking.k8s.io_gateways.yaml

Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)