Skip to content

Conversation

jpbetz
Copy link
Contributor

@jpbetz jpbetz commented Sep 8, 2025

This moves from a reflection based approach to OpenAPI model naming to a declarative approach.

OpenAPIModelName() receiver functions to access OpenAPI model names of API types are generated into zz_generated.model_name.go files. This allows API authors to declare the desired OpenAPI model packages when the model name derived from the go package path is not desirable.

This PR also migrates the kubernetes API to use this generator (all OpenAPI model names remain the same).

For example:

// doc.go

// +k8s:openapi-gen=true
// +k8s:deepcopy-gen=package
// +k8s:protobuf-gen=package
// +k8s:prerelease-lifecycle-gen=true
// +k8s:openapi-model-package=io.k8s.api.core.v1

// +groupName=
package v1

Generates:

// zz_generated.model_name.go
func (in Pod) OpenAPIModelName() string {
	return "io.k8s.api.core.v1.Pod"
}

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Sep 8, 2025
@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 8, 2025
@jpbetz jpbetz force-pushed the named-model-gen branch 2 times, most recently from 052f613 to 9ea3e5e Compare September 8, 2025 22:06
@jpbetz jpbetz force-pushed the named-model-gen branch 3 times, most recently from 6572856 to d411358 Compare September 9, 2025 15:11
@jpbetz
Copy link
Contributor Author

jpbetz commented Sep 9, 2025

@BenTheElder @Jefftree @thockin

I've moved the codegen part of this into openapi-gen. This is a backward compatible change that enables us to switch away from go package name reflection in k8s.

Signed-off-by: Joe Betz <jpbetz@google.com>
Signed-off-by: Joe Betz <jpbetz@google.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.

/lgtm
/approve

I am not intimately familair with this generator but this PR all seems reasonable to me :)

"the base Go import-path under which to generate results")
fs.StringVar(&args.OutputFile, "output-file", "generated.openapi.go",
"the name of the file to be generated")
fs.StringVar(&args.OutputModelNameFile, "output-model-name-file", "",
Copy link
Member

Choose a reason for hiding this comment

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

Why not output to the same file?

EDIT: Oh I see. 1 Generator -> 1 file. Probably not worth changing, but IMO outputting to a single file would be cleaner, if we run across similar things in other generators. Unless there's a reason this is better that I am missing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is one file for the generated openapi definition, and there are N files for all the zz_geneated.model_name.go files, one for each package containing types that need this function added. Those files are very similar to deep copy generated files.

Copy link
Member

Choose a reason for hiding this comment

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

OH RIGHT. Nevermind me :)

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

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: jpbetz, 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 merged commit d67c058 into kubernetes:master Sep 9, 2025
6 checks passed
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. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants