Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support validating internal list items on list types #342

Closed
robbie-demuth opened this issue Oct 14, 2019 · 29 comments
Closed

Support validating internal list items on list types #342

robbie-demuth opened this issue Oct 14, 2019 · 29 comments
Labels
lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness.

Comments

@robbie-demuth
Copy link

robbie-demuth commented Oct 14, 2019

Per https://book.kubebuilder.io/reference/markers/crd-validation.html, validating tags like +kubebuilder:validation:MinLength and kubebuilder:validation:Pattern must be specified on string fields or string types. If one has a field of type []string, however, it is impossible to use such tags on the field directly to enforce validation on the individual list items. This forces users to alias the list item type and place the validation on the type itself like so:

// +kubebuilder:validation:MinLength=1
type NonEmptyString string

// +k8s:openapi-gen=true
type CRSpec struct {
  Field []NonEmptyString `json:"field"`
}

This leads to undesired consequences like having to export the type so that it can be referenced in controller packages and having to cast list items since there is no easy way to cast []NonEmptyString to []string.

It used to previously be possible to add the validation tag to the field itself like so:

// +k8s:openapi-gen=true
type CRSpec struct {
  // +kubebuilder:validation:MinLength=1
  Field []string `json:"field"`
}

Doing so, however, would generate the following CRD where the validation was present on both the list itself as well as its items:

field:
  items:
    minLength: 1
    type: string
  minLength: 1
  type: array

Now, the CRD is generated as follows:

field:
  items:
    type: string
  type: array

It'd be great if the validating tags could be specified on the field itself and result in the following CRD:

field:
  items:
    minLength: 1
    type: string
  type: array

Previously, I was using v0.9.0 of the Operator SDK which used controller-tools v0.1.11-0.20190411181648-9d55346c2bde. Now, I'm using v0.11.0 of the Operator SDK which uses controller-tools v0.2.0

@robbie-demuth
Copy link
Author

@DirectXMan12 @damemi @sttts - Do any of you have any insight into this? Could this be implemented by updating the ApplyToSchema methods to interact with the Items field for the desired validation markers?

@sttts
Copy link
Contributor

sttts commented Oct 15, 2019

The current solution is to alias the string type and add marker for the alias.

We had a discussion before to have a special syntax to apply something for items or additionalPropoerties. This was never implemented or fleshed out AFAIK. Something around the lines of

// +k8s:openapi-gen=true
type CRSpec struct {
  // +kubebuilder:validation:Items:MinLength=1
  Field []string `json:"field"`
}

@robbie-demuth
Copy link
Author

While aliasing the string type and adding the marker to the type itself works, doing so requires significant controller-side changes since there's no way to cast between slices of different types. The ability to do something like you've shown above would be a huge improvement.

@sttts
Copy link
Contributor

sttts commented Oct 15, 2019

It is painful, I understand. @DirectXMan12 can comment on that topic more than I can.

@DirectXMan12
Copy link
Contributor

we've thought about implementing :items: before. It's not entirely clear how the internals would work beyond literally having a copy of every marker, but that's a discussion for the internals. Someone would need to do a prototype.

We removed the way it used to work because it was very inconsistent and caused issues when you actually wanted a marker to apply to the field in general and not the item.

@DirectXMan12
Copy link
Contributor

I'd encourage new users to do the type aliasing, but I understand that that's clunky sometimes, and that it'd cause existing users some pain, so figuring out an alternate solution (like :items:) would be good.

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Feb 3, 2020
@robbie-demuth
Copy link
Author

I'd like for this issue to stay open since addressing it would be a huge quality of life improvement. Do only maintainers have the ability to use /remove-lifecycle-stale?

@robbie-demuth
Copy link
Author

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Feb 3, 2020
@FreedomBen
Copy link

For what it's worth, I'm running into this now. My CRD is pretty large and will require a great number of type aliases (and associated casts) which I'd really rather not do. Was it decided not to do this or is there just not anyone willing at the moment?

@robbie-demuth
Copy link
Author

I think the latter. I took a quick look through the code to try to evaluate the level of effort, but wasn't familiar enough with it to make an accurate guess

@DirectXMan12
Copy link
Contributor

Was it decided not to do this or is there just not anyone willing at the moment?

The core developers just don't really have bandwidth atm, and it's not super-high priority. If someone else wants to pick this up, that'd probably work.

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 19, 2020
@robbie-demuth
Copy link
Author

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 20, 2020
@DirectXMan12
Copy link
Contributor

/lifecycle frozen

@sttts
Copy link
Contributor

sttts commented Apr 8, 2022

This is still painful.

yjian118 added a commit to ttakenak/cloud-platform-deployment-manager that referenced this issue May 23, 2022
When using kubernetes-sigs/controller-tools v2 and above, there's a
know issue that the validation on intertal list items is no longer
supported.
See in: kubernetes-sigs/controller-tools#342.
This issue is opened 2 years ago and currently frozen(seems will not
fix in a short term). We can set a alia of the item type to introduce
the validation instead, otherwise, we'll need to remove these validations.

This commit gives an example about the effort to set the validation with
alia type on the subfunctions for review. Note: there's still many
validation of the items in a "StringList" need to implement.

Signed-off-by: Yuxing Jiang <Yuxing.Jiang@windriver.com>
yjian118 added a commit to ttakenak/cloud-platform-deployment-manager that referenced this issue May 23, 2022
Due to the known issue in the upstream package:
kubernetes-sigs/controller-tools#342, this commit removes the
validation on the service types.

Note: this is a temperary work around, need to add these validations
back once we validate the fix for the subfunctions.

Signed-off-by: Yuxing Jiang <Yuxing.Jiang@windriver.com>
AlexanderYastrebov added a commit to szuecs/routegroup-client that referenced this issue Mar 26, 2024
controller-gen does not support validating internal list items on list types,
see kubernetes-sigs/controller-tools#342

To add host pattern we used perl hack that is hard to extend to multiple
validations.

Also #16 added optional tls spec that contains hosts field for which
perl hack worked by accident, see #16 (comment)

This change replaces perl hack for a go hack and adds max length constraint.

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
@AlexanderYastrebov
Copy link
Contributor

We've added a post-processing step that adds validation fields to the generated CRD yaml, see szuecs/routegroup-client#38

@alvaroaleman
Copy link
Member

We've added a post-processing step that adds validation fields to the generated CRD yaml, see szuecs/routegroup-client#38

just for everyones awareness, we'd highly appreciate a change to fix this and its going to benefit the ecosystem a lot more than downstream workarounds.

AlexanderYastrebov added a commit to AlexanderYastrebov/controller-tools that referenced this issue Mar 26, 2024
Fixes kubernetes-sigs#342

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
AlexanderYastrebov added a commit to AlexanderYastrebov/controller-tools that referenced this issue Mar 26, 2024
For kubernetes-sigs#342

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
@AlexanderYastrebov
Copy link
Contributor

just for everyones awareness, we'd highly appreciate a change to fix this and its going to benefit the ecosystem a lot more than downstream workarounds.

Ok, I've created #897

@JoelSpeed
Copy link
Contributor

Why not just use a type alias and apply the validation to the alias? That works well and you have a clear MaxLength on the alias, rather than having both MaxLength and MaxItems on a single field which is likely to cause confusion.

I think already people expect MaxLength to act as MaxItems on an array, at least presently they get an error in this case, with the change proposed, they won't get an error, but likely also won't get the schema they intended

@robbie-demuth
Copy link
Author

Why not just use a type alias and apply the validation to the alias? That works well and you have a clear MaxLength on the alias, rather than having both MaxLength and MaxItems on a single field which is likely to cause confusion.

#342 (comment)

@alvaroaleman
Copy link
Member

yeah I agree with the type alias, its impractical. We need to find a way to specify that a given validation is for the items of a list and not the list as a whole.

AlexanderYastrebov added a commit to AlexanderYastrebov/controller-tools that referenced this issue Mar 27, 2024
For kubernetes-sigs#342

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
AlexanderYastrebov added a commit to AlexanderYastrebov/controller-tools that referenced this issue Mar 27, 2024
For kubernetes-sigs#342

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
@AlexanderYastrebov
Copy link
Contributor

AlexanderYastrebov commented Mar 27, 2024

We need to find a way to specify that a given validation is for the items of a list and not the list as a whole.

Ok, I've created #898

AlexanderYastrebov added a commit to AlexanderYastrebov/controller-tools that referenced this issue Mar 27, 2024
For kubernetes-sigs#342

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
AlexanderYastrebov added a commit to AlexanderYastrebov/controller-tools that referenced this issue Mar 27, 2024
For kubernetes-sigs#342

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
AlexanderYastrebov added a commit to AlexanderYastrebov/controller-tools that referenced this issue Mar 28, 2024
For kubernetes-sigs#342

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
AlexanderYastrebov added a commit to AlexanderYastrebov/controller-tools that referenced this issue Mar 28, 2024
For kubernetes-sigs#342

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
AlexanderYastrebov added a commit to AlexanderYastrebov/controller-tools that referenced this issue Mar 28, 2024
For kubernetes-sigs#342

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
AlexanderYastrebov added a commit to AlexanderYastrebov/controller-tools that referenced this issue Mar 29, 2024
For kubernetes-sigs#342

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
AlexanderYastrebov added a commit to szuecs/routegroup-client that referenced this issue Apr 3, 2024
The issue kubernetes-sigs/controller-tools#342
was fixed and CRD generator now supports validation markers for array items.

This change uses items markers and removes hack used to add
max length and pattern constraints to hosts.

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
@sbueringer
Copy link
Member

/close

PR is merged. Thx @AlexanderYastrebov

@k8s-ci-robot
Copy link
Contributor

@sbueringer: Closing this issue.

In response to this:

/close

PR is merged. Thx @AlexanderYastrebov

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

AlexanderYastrebov added a commit to szuecs/routegroup-client that referenced this issue Apr 22, 2024
The issue kubernetes-sigs/controller-tools#342
was fixed and CRD generator now supports validation markers for array items.

This change uses items markers and removes hack used to add
max length and pattern constraints to hosts.

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness.
Projects
None yet
Development

No branches or pull requests