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

code-generator/client-gen: decouple core group from package name 'api' #125162

Merged
merged 5 commits into from
Jun 27, 2024

Conversation

sttts
Copy link
Contributor

@sttts sttts commented May 28, 2024

What type of PR is this?

/kind bug

What this PR does / why we need it:

client-gen didn't work with api/v1-like directory structures. This PR removes special casing of the api package name, and move that logic into the // +groupName= marker.

This PR also adds an example for a single API group to ensure the generated code is correct. Compare how client-gen is launched to support this single group:

kube::codegen::gen_client \
    --with-watch \
    --with-applyconfig \
    --output-dir "${SCRIPT_ROOT}/single" \
    --output-pkg "${THIS_PKG}/single" \
    --boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" \
    --one-input-api "api" \
    "${SCRIPT_ROOT}/single"

Which issue(s) this PR fixes:

Fixes kubernetes/code-generator#167

Special notes for your reviewer:

Note that there are no changes to generated client-go code for the legacy core group.

Also note that the generated client for the "single" example uses config.APIPath = "/apis", not /api as the legacy core group client (this would have been the case before this PR).

Does this PR introduce a user-facing change?

Fix code-generator client-gen to work with `api/v1`-like package structure.

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


@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. kind/bug Categorizes issue or PR as related to a bug. 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-priority Indicates a PR lacks a `priority/foo` label and requires one. labels May 28, 2024
@k8s-ci-robot k8s-ci-robot added area/code-generation kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API 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. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels May 28, 2024
@sttts sttts added the priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. label May 28, 2024
@k8s-ci-robot k8s-ci-robot removed the needs-priority Indicates a PR lacks a `priority/foo` label and requires one. label May 28, 2024
@k8s-triage-robot
Copy link

This PR may require API review.

If so, when the changes are ready, complete the pre-review checklist and request an API review.

Status of requested reviews is tracked in the API Review project.

@cici37
Copy link
Contributor

cici37 commented May 28, 2024

/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 May 28, 2024
@sttts sttts force-pushed the sttts-code-generator-core-group branch from c27b22b to 4186e8c Compare May 29, 2024 20:41
pkg/apis/core/doc.go Show resolved Hide resolved
@@ -7,6 +7,7 @@ API rule violation: names_match,k8s.io/apimachinery/pkg/apis/meta/v1,StatusCause
API rule violation: names_match,k8s.io/apimachinery/pkg/apis/meta/v1,Time,Time
API rule violation: names_match,k8s.io/apimachinery/pkg/runtime,Unknown,ContentEncoding
API rule violation: names_match,k8s.io/apimachinery/pkg/runtime,Unknown,ContentType
API rule violation: names_match,k8s.io/code-generator/examples/apiserver/apis/core/v1,TestTypeStatus,Blah
Copy link
Member

Choose a reason for hiding this comment

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

I can't tell if this is intentional (ie. the lack of a JSON name matters to the test) or just because we are too lazy to fix these?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

probably lazy and good cargo culting of the broken example. Let me check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

staging/src/k8s.io/api/core/v1/doc.go Show resolved Hide resolved
@sttts sttts force-pushed the sttts-code-generator-core-group branch from e3f6314 to 2edb86b Compare June 4, 2024 18:22
@mdbooth
Copy link
Contributor

mdbooth commented Jun 25, 2024

I can confirm that this PR allows me to generate applyconfiguration and clientset in https://github.com/kubernetes-sigs/cluster-api-provider-openstack.

The gen_client invocation I tested is:

kube::codegen::gen_client \
    --with-watch \
    --with-applyconfig \
    --output-dir "${PROJECT_ROOT}/client" \
    --output-pkg sigs.k8s.io/cluster-api-provider-openstack/client \
    --versioned-name clientset \
    --boilerplate "${SCRIPT_ROOT}/boilerplate.go.txt" \
    --one-input-api "api" \
    "${PROJECT_ROOT}"

That's slightly different to the example in the description as the input argument is the project root rather than the output directory.

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

@@ -71,31 +71,25 @@ func (g *genGroup) Imports(c *generator.Context) (imports []string) {
func (g *genGroup) GenerateType(c *generator.Context, t *types.Type, w io.Writer) error {
sw := generator.NewSnippetWriter(w, c, "$", "$")

apiPath := func(group string) string {
Copy link
Member

Choose a reason for hiding this comment

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

I had to backtrack to find where group is being set to "core" - Group.NonEmpty() says:

func (g Group) NonEmpty() string {                                                                                                                                              
    if g == "api" {
        return "core"
    }   
    return string(g)
}

So where does "api" come from? I guess that comes from the filesystem layout of the legacy group.

Do you think it would make sense to move legacy code or somehow "normalize" it so these weird special cases go away?

Copy link
Contributor Author

@sttts sttts Jun 27, 2024

Choose a reason for hiding this comment

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

That "api" is now gone, replaced with "". Core is still hardcoded. But that's just a symbol for the go types.

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

LGTM label has been added.

Git tree hash: d4fb258aa250e1447a6fe1ffcbbec486894bfb94

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: sttts, 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 k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 25, 2024
@pacoxu
Copy link
Member

pacoxu commented Jun 26, 2024

/retest

@pacoxu
Copy link
Member

pacoxu commented Jun 26, 2024

/hold
for ci failure
Feel free to unhold once the CI is green.

@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 Jun 26, 2024
@sttts sttts force-pushed the sttts-code-generator-core-group branch from 2edb86b to ac3b764 Compare June 27, 2024 13:33
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 27, 2024
@k8s-ci-robot k8s-ci-robot requested a review from thockin June 27, 2024 13:33
@mdbooth
Copy link
Contributor

mdbooth commented Jun 27, 2024

I've retested this locally. I am still able to use this to generate a clientset in cluster-api-provider-openstack.

/lgtm

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

LGTM label has been added.

Git tree hash: b25e6e60cf126c99c8677963d1e740dc1543ad65

@shaneutt
Copy link
Member

Just wanted to throw a thank you to everyone who worked on this, it is appreciated! 🫡

@thockin
Copy link
Member

thockin commented Jun 27, 2024

@sttts was this last push just a rebase? I can't discern what relevant changes are in the diff?

/lgtm
/hold JIC I missed something?

@sttts
Copy link
Contributor Author

sttts commented Jun 27, 2024

Yes, rebase and codegen, some go docs changed.

@sttts
Copy link
Contributor Author

sttts commented Jun 27, 2024

/hold cancel

@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 Jun 27, 2024
@k8s-ci-robot k8s-ci-robot merged commit 2c6daa4 into kubernetes:master Jun 27, 2024
14 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.31 milestone Jun 27, 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/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. release-note Denotes a PR that will be considered when it comes time to generate release notes. 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. 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.

client-gen generates empty "core" group clientsets when "api" package directory is in use for input APIs
8 participants