gateway-api icon indicating copy to clipboard operation
gateway-api copied to clipboard

Investigate if CEL can validate that a list is sorted alphabetically

Open robscott opened this issue 2 years ago • 5 comments

What happened: In #2461 we added a SupportedFeatures list to GatewayClass status. The spec requires that to be sorted alphabetically, but ideally we could also validate that with CEL. At first glance this appears to be impossible, but it would be good to get a more authoritative answer.

What you expected to happen: Validation to enforce that this field is in alphabetical order.

cc @alexzielenski @cici37 @TristonianJones

robscott avatar Oct 09 '23 04:10 robscott

@robscott you would need to introduce custom functions to check sortedness, as CEL doesn't currently support this feature. It can definitely be done, especially if we supported index, value style comprehensions in CEL.

values.range(idx, v, idx == 0 || values[idx-1] <= v)

TristonianJones avatar Oct 09 '23 04:10 TristonianJones

Thanks @TristonianJones! Are there any plans to support index, value style comprehensions in CEL? Do you happen to know if CRD authors have the flexibility to define custom functions or if those can only be added via upstream Kubernetes changes?

robscott avatar Oct 09 '23 04:10 robscott

This issue has not been updated in over 1 year, and should be re-triaged.

You can:

  • Confirm that this issue is still relevant with /triage accepted (org members only)
  • Close this issue with /close

For more details on the triage process, see https://www.kubernetes.dev/docs/guide/issue-triage/

/remove-triage accepted

k8s-triage-robot avatar Oct 08 '24 22:10 k8s-triage-robot

@robscott CEL just added support for two variable comprehensions. I'll cut a release once in back from vacation, but they're in HEAD now

TristonianJones avatar Oct 09 '24 06:10 TristonianJones

Very cool, thanks for the update @TristonianJones! Looking forward to seeing this in k8s.

/triage accepted

robscott avatar Oct 09 '24 17:10 robscott

This issue has not been updated in over 1 year, and should be re-triaged.

You can:

  • Confirm that this issue is still relevant with /triage accepted (org members only)
  • Close this issue with /close

For more details on the triage process, see https://www.kubernetes.dev/docs/guide/issue-triage/

/remove-triage accepted

k8s-triage-robot avatar Oct 09 '25 18:10 k8s-triage-robot

Kubernetes library has the isSorted for lists: https://kubernetes.io/docs/reference/using-api/cel/#kubernetes-list-library

rikatz avatar Nov 27 '25 18:11 rikatz

I have tested here against an array field and worked like :chefkiss:

// +kubebuilder:validation:XValidation:message="should be sorted alphabetically",rule="self.isSorted()"
Features []Feature `json:"features,omitempty"`

Once applying, VAP blocks unordered features.

@robscott I am worried this may be some breaking change on the API if we decide to apply it to status, but otherwise we can move with it :)

rikatz avatar Nov 27 '25 18:11 rikatz