Skip to content

Commit

Permalink
wip: crd: require unique hosts
Browse files Browse the repository at this point in the history
CRD does not support `uniqueItems: true` validation:
```
The CustomResourceDefinition "routegroups.zalando.org" is invalid:
spec.validation.openAPIV3Schema.properties[spec].properties[hosts].uniqueItems:
Forbidden: uniqueItems cannot be set to true since the runtime complexity becomes quadratic
```

This change adds [validation rule](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation-rules)
to enforce unique hosts.

It also adds maxItems limit to overcome rule cost error:
```
The CustomResourceDefinition "routegroups.zalando.org" is invalid:
* spec.validation.openAPIV3Schema.properties[spec].properties[hosts].x-kubernetes-validations[0].rule: Forbidden: estimated rule cost exceeds budget by factor of more than 100x (try simplifying the rule, or adding maxItems, maxProperties, and maxLength where arrays, maps, and strings are declared)
* spec.validation.openAPIV3Schema.properties[spec].properties[hosts].x-kubernetes-validations[0].rule: Forbidden: contributed to estimated rule cost total exceeding cost limit for entire OpenAPIv3 schema
* spec.validation.openAPIV3Schema: Forbidden: x-kubernetes-validations estimated rule cost total for entire OpenAPIv3 schema exceeds budget by factor of more than 100x (try simplifying the rule, or adding maxItems, maxProperties, and maxLength where arrays, maps, and strings are declared)
```

Note that validation rule is not added to `tls` spec because then rule cost error re-appears apparently due to exceeding total allowed schema cost.

Example validation error:
```
Error from server (Invalid): error when creating "rg.yaml": RouteGroup.zalando.org "duplicate-hosts" is invalid: spec.hosts: Invalid value: "array": hosts must be unique
```

Signed-off-by: Alexander Yastrebov <[email protected]>
  • Loading branch information
AlexanderYastrebov committed Mar 27, 2024
1 parent 5c0ecfd commit 5cceca1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions apis/zalando.org/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type RouteGroupList struct {
type RouteGroupSpec struct {
// List of hostnames for the RouteGroup
// +kubebuilder:validation:MinItems=1
// +kubebuilder:validation:MaxItems=255
// +kubebuilder:validation:XValidation:rule="self.all(i, size(self.filter(j, j==i)) == 1)", message="hosts must be unique"
Hosts []string `json:"hosts,omitempty"`
// List of backends that can be referenced in the routes
Backends []RouteGroupBackend `json:"backends"`
Expand Down
4 changes: 4 additions & 0 deletions zalando.org_routegroups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,12 @@ spec:
maxLength: 255
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?([.][a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
type: string
maxItems: 255
minItems: 1
type: array
x-kubernetes-validations:
- message: hosts must be unique
rule: self.all(i, size(self.filter(j, j==i)) == 1)
routes:
description: Routes describe how a matching HTTP request is handled
and where it is forwarded to
Expand Down

0 comments on commit 5cceca1

Please sign in to comment.