Skip to content

[WIP] [Don't review] Validation gen #132629

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

Open
wants to merge 121 commits into
base: master
Choose a base branch
from

Conversation

lalitc375
Copy link
Contributor

[WIP] [Don't review] Validation gen - Test PR.

yongruilin and others added 30 commits June 4, 2025 16:45
Also fix boilerplate errors
This crashes during codegen with a pretty useless panic(), which doesn't
make sense any more.

```
$ pushd; ./hack/update-codegen.sh valid; pushd
~/src/kubernetes ~/src/kubernetes/staging/src/k8s.io/code-generator/cmd/validation-gen
+++ [0418 23:56:03] validation: 69 targets
panic: alias type should already have been unwrapped

goroutine 1 [running]:
k8s.io/code-generator/cmd/validation-gen/validators.requirednessTagValidator.GetValidations({{0x86cb10?, 0xc0000c0b60?}}, {{0x86ca5a, 0xd}, 0xc01ff9efc0, 0xc01ff9f0a0, 0xc02a3f0cd0, 0xc02a4092c0}, {0x0, 0x0, ...}, ...)
	/home/thockin/src/kubernetes/staging/src/k8s.io/code-generator/cmd/validation-gen/validators/required.go:69 +0x40f
k8s.io/code-generator/cmd/validation-gen/validators.(*registry).ExtractValidations(0xc0a4e0, {{0x86ca5a, 0xd}, 0xc01ff9efc0, 0xc01ff9f0a0, 0xc02a3f0cd0, 0xc02a4092c0}, {0xc0200640e0, 0x2, 0x2})
```

Subsequent commits will clean this area up more.
Before this, pointer fields would fail at codegen.
Prior to this it would look at the .Kind, see "Alias", and generate
calls to (e.g.) `RequiredValue()`.  Now calls `RequiredSlice()`.
I did an audit of how we are handling pointerness and aliases in
validators.  In a word - inconsistent.

I went through each validator and added test cases where they were
missing (previous commits). This commit changes how Context.Type is
defined for typedefs. Previously Context.Type was the potentially
aliased and potentially pointerful type for struct fields, but the
"real" concrete type for typedefs.  Now it is consistent, and validators
need to unalias and unpointer types (which they had to do anyway because
it was inconsistent.
Previous commits focused on checking for pointers and aliases more appropriately.

What emerged was two functions `realType` which gave us the concrete
native type (non-pointer, non-alias) an `unaliasType` which stripped
aliasing but only until it hit pointerness.  They were sort of
inconsistently applied and in a few places I found the composition of
them to be weird.

I want to reframe those as:

1) `func nativeType(t) t` which removes all aliasing but retains pointerness.  So for example:
```
type T1 string
type T2 *T1
type T3 *T2

type Foo struct {
    Name *T3
}
```

Calling `nativeType()` on the `Name` member would yield `***string`.
This is what we want in most places.  We need to retain pointerness
because (e.g.) we DO NOT support pointer to slice, and we want that to
fail, but we DO support pointer to string and we want that to succeed,
but `optional` needs to handle pointers and non-pointers differently.

We filter totally disallowed things early on, but if a pointer to slice
should sneak in somehow, this will refuse to generate bad code (fail in
an obvious way).

2) `func nonPointer(t) t` which removes all pointerness (if present),
but does not look past aliases. We needed to compose these both and call
`nonPointer(nativeType(t))` in a few places. E.g. when applying
`+k8s:format`, we need to know that the field is a string OR pointer to
string.

These two are more composable, I think.  If you agree I can finish that
off this weekend and ping the PR.  If not, would like to hear your
thoughts.  Mostly the goal is put as many defensive checks in place as
possible.  New validators will copy these ones and I want it to be as
clear as possible.
@k8s-ci-robot k8s-ci-robot added the sig/contributor-experience Categorizes an issue or PR as relevant to SIG Contributor Experience. label Jun 30, 2025
@k8s-ci-robot k8s-ci-robot requested review from andrewsykim, aojea and a team June 30, 2025 17:07
@k8s-ci-robot k8s-ci-robot added sig/instrumentation Categorizes an issue or PR as relevant to SIG Instrumentation. sig/network Categorizes an issue or PR as relevant to SIG Network. sig/node Categorizes an issue or PR as relevant to SIG Node. sig/scheduling Categorizes an issue or PR as relevant to SIG Scheduling. wg/device-management Categorizes an issue or PR as relevant to WG Device Management. labels Jun 30, 2025
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jun 30, 2025
@yongruilin
Copy link
Contributor

/ok-to-test
/hold

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jun 30, 2025
@k8s-ci-robot k8s-ci-robot added area/kube-proxy sig/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. sig/storage Categorizes an issue or PR as relevant to SIG Storage. labels Jun 30, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lalitc375
Once this PR has been reviewed and has the lgtm label, please assign liggitt, priyankasaggu11929 for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot
Copy link
Contributor

k8s-ci-robot commented Jul 1, 2025

@lalitc375: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-kubernetes-linter-hints 5439435 link false /test pull-kubernetes-linter-hints

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

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-sigs/prow repository. I understand the commands that are listed here.

@k8s-ci-robot
Copy link
Contributor

PR needs rebase.

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-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/apiserver area/cloudprovider area/code-generation area/dependency Issues or PRs related to dependency changes area/kube-proxy area/kubectl area/kubelet cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/contains-merge-commits Indicates a PR which contains merge commits. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/apps Categorizes an issue or PR as relevant to SIG Apps. sig/architecture Categorizes an issue or PR as relevant to SIG Architecture. sig/auth Categorizes an issue or PR as relevant to SIG Auth. sig/cli Categorizes an issue or PR as relevant to SIG CLI. sig/cloud-provider Categorizes an issue or PR as relevant to SIG Cloud Provider. sig/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. sig/contributor-experience Categorizes an issue or PR as relevant to SIG Contributor Experience. sig/instrumentation Categorizes an issue or PR as relevant to SIG Instrumentation. sig/network Categorizes an issue or PR as relevant to SIG Network. sig/node Categorizes an issue or PR as relevant to SIG Node. sig/scheduling Categorizes an issue or PR as relevant to SIG Scheduling. sig/storage Categorizes an issue or PR as relevant to SIG Storage. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. wg/device-management Categorizes an issue or PR as relevant to WG Device Management.
Projects
Status: 🆕 New
Status: Needs Triage
Status: No status
Status: Needs Triage
Development

Successfully merging this pull request may close these issues.

6 participants