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

Generify lister-gen #121574

Merged
merged 4 commits into from Apr 22, 2024
Merged

Generify lister-gen #121574

merged 4 commits into from Apr 22, 2024

Conversation

skitt
Copy link
Member

@skitt skitt commented Oct 27, 2023

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

This adds a generic implementation of a lister, and uses it to replace the template code in generated listers. The corresponding templates are no longer used and are removed.

Listers are reduced to their interfaces (non-namespaced and namespaced if appropriate), their specific structs, and their constructors. All method implementations are provided by the generic implementation. The dedicated interface is preserved so that each lister can have its own set of methods (e.g. the method returning the namespaced lister if appropriate), and the dedicated struct is preserved to allow expansions to be defined where necessary.

The external interface is unchanged and doesn't expose generics.

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

The core of this change is https://github.com/kubernetes/kubernetes/pull/121574/files#diff-8b2fc7ac28a79479e5ffb0b072f0b5d1b308ff4cb29daa3ae0657e23d200ac46 and https://github.com/kubernetes/kubernetes/pull/121574/files#diff-08b8fe190a6fe29b3cb7708a75aa9e7256459c4f7d941854d76eae79862ff56f — the rest of the change is generated code.

Does this PR introduce a user-facing change?

NONE

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 27, 2023
@k8s-ci-robot
Copy link
Contributor

Hi @skitt. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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.

@k8s-ci-robot k8s-ci-robot added needs-priority Indicates a PR lacks a `priority/foo` label and requires one. area/code-generation sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/auth Categorizes an issue or PR as relevant to SIG Auth. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Oct 27, 2023
@skitt
Copy link
Member Author

skitt commented Oct 27, 2023

@thockin if you have the time I’m interested in your thoughts on this and #121439 — I understand you like generics but are also wary of their overuse.

Copy link
Member

@thockin thockin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't find this abusive AT ALL. I think this is exactly what generics are good for, and anything that reduces the LOC count of generated code is a win.

I'd love to see us explore a "v2" client which really leans into generics (even at the user-facing API) and uses less code generation. These codegen tools are crusty and hard to maintain and even harder to change (hence why my go-workspaces work has stalled).

resource schema.GroupResource
}

// Namespace wraps a lister and namespace for a given type.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment and code don't match (type name)

@thockin
Copy link
Member

thockin commented Oct 27, 2023

@liggitt @jpbetz @deads2k

@skitt
Copy link
Member Author

skitt commented Oct 30, 2023

I'd love to see us explore a "v2" client which really leans into generics (even at the user-facing API) and uses less code generation. These codegen tools are crusty and hard to maintain and even harder to change (hence why my go-workspaces work has stalled).

Yes, I’ve been looking at two approaches, generifying the static client (without changing the external interface), and building a new client more along the lines of the dynamic client but using generics to produce concrete types instead of unstructured.Unstructured. The latter quickly runs into limitations inherent to (current) Go generics (e.g. not being able create an instance of the type parameter), so some codegen would still be required, but the resulting API would be useful in many different scenarios. One feature I would like, which I haven’t dug into much yet, would be to make lists generic (instead of StatefulSetList, have List[StatefulSet]), which would reduce the need for functions such as meta.ExtractList.

Go’s duck-typing means that clients can layer their own generic abstractions on top of K8s, e.g. submariner-io/admiral#756, but it would be nice to have a common layer everyone can use — and in a place where current limitations can be addressed (see the List handling in that PR; we still need to hard-code singleton-to-list pairs).

@skitt
Copy link
Member Author

skitt commented Oct 31, 2023

/ok-to-test

@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. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 31, 2023
@seans3
Copy link
Contributor

seans3 commented Oct 31, 2023

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Oct 31, 2023
@skitt skitt force-pushed the generic-lister-gen branch 2 times, most recently from 013aa14 to 1acab76 Compare November 3, 2023 10:02
@skitt
Copy link
Member Author

skitt commented Nov 3, 2023

I’ve updated the PR to address the linting issues, and also to leave client-go/generic free for a possible future generic client (alongside dynamic etc.). The helpers are moved into listers, and renamed to reflect their nature (for structs) and reduce stuttering (for constructors).

@thockin
Copy link
Member

thockin commented Nov 3, 2023

If this were broken into commits with "real work" and "regenerated code" it would help review.

Who is the right approver for this? I love the idea, but I am not deeply familiar with this area

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 7, 2024
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: dbbc5d6c9985e5c91887118dd54e6499fcff5267

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 7, 2024
@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Apr 18, 2024
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 18, 2024
@skitt
Copy link
Member Author

skitt commented Apr 18, 2024

Thanks @pacoxu, this does indeed need a last-minute codegen re-run before merging to make sure all the generated code is up-to-date.

@thockin this means the PR needs a new lgtm 😉

@pacoxu
Copy link
Member

pacoxu commented Apr 18, 2024

/unhold

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Apr 18, 2024
@skitt
Copy link
Member Author

skitt commented Apr 18, 2024

/retest-required

Copy link
Member

@thockin thockin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm picking nits now.


// NamespacedResourceIndexer wraps an indexer, resource, and namespace for a given type.
// This is intended for use by listers (generated by lister-gen) only.
type NamespacedResourceIndexer[T runtime.Object] struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idea:

Why not just have 1 type:

ResourceIndexer[T runtime.Object] struct {
	indexer  cache.Indexer
	resource schema.GroupResource
	namespace string // empty for non-namespaced types
}

and then just test l.namespace for empty. One code path, one type. Two
constructors functions (New and NewNamespaced).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I had tried that and run into issues, but apparently not; I’ve updated the PR to use a single type.

@skitt
Copy link
Member Author

skitt commented Apr 19, 2024

/retest-required

Signed-off-by: Stephen Kitt <skitt@redhat.com>
This adds a generic implementation of a lister, and uses it to replace
the template code in generated listers. The corresponding templates
are no longer used and are removed.

Listers are reduced to their interfaces (non-namespaced and namespaced
if appropriate), their specific structs, and their constructors. All
method implementations are provided by the generic implementation. The
dedicated interface is preserved so that each lister can have its own
set of methods (e.g. the method returning the namespaced lister if
appropriate), and the dedicated struct is preserved to allow
expansions to be defined where necessary.

The external interface is unchanged and doesn't expose generics.

Signed-off-by: Stephen Kitt <skitt@redhat.com>
Signed-off-by: Stephen Kitt <skitt@redhat.com>
In particular, document that ListAllByNamespace delegates to ListAll
if no namespace is specified.

Signed-off-by: Stephen Kitt <skitt@redhat.com>
Copy link
Member

@thockin thockin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 22, 2024
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: b6224f7179f65c79182ad89dcb2b461e7c371df6

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: skitt, thockin

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

The pull request process is described 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

@skitt: 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-e2e-gce 54e8993 link unknown /test pull-kubernetes-e2e-gce

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

@k8s-ci-robot k8s-ci-robot merged commit 6b26038 into kubernetes:master Apr 22, 2024
13 of 14 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.31 milestone Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/code-generation cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note-none Denotes a PR that doesn't merit a release note. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/auth Categorizes an issue or PR as relevant to SIG Auth. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

5 participants