Skip to content

Add OpenAPIModelNamer and opt-in generator support #537

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 2 commits into
base: master
Choose a base branch
from

Conversation

jpbetz
Copy link
Contributor

@jpbetz jpbetz commented May 14, 2025

This enables kube-openapi to use model names defined by the models instead of the <go-package>.<go-type-name> of the Go structs that defines the model.

This is entirely opt-in. Existing behavior is retained.

openapi-gen users may opt-in with the --use-openapi-model-names.

Models may opt-in by implementing the OpenAPIModelNamer interface introduced by this change:

type OpenAPIModelNamer interface {
	OpenAPIModelName() string
}

For example:

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

When used, the "canonical type names" and "REST friendly names" are the same model name.

This is simpler than the existing behavior where the name formats differ needlessly require conversion:

  • Canonical type names
    • e.g. k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1.CustomResourceDefinition
  • REST friendly names
    • e.g. io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1.CustomResourceDefinition

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: jpbetz

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 requested review from apelisse and sttts May 14, 2025 19:06
@k8s-ci-robot k8s-ci-robot added 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. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels May 14, 2025
@jpbetz
Copy link
Contributor Author

jpbetz commented May 14, 2025

/assign @Jefftree
cc @aojea @BenTheElder
/sig api-machinery
/triage accepted

@k8s-ci-robot k8s-ci-robot added sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. triage/accepted Indicates an issue or PR is ready to be actively worked on. labels May 14, 2025
@@ -339,8 +343,18 @@ func (g openAPITypeWriter) generateCall(t *types.Type) error {
// Only generate for struct type and ignore the rest
switch t.Kind {
case types.Struct:
if namer.IsPrivateGoName(t.Name.Name) { // skip private types
Copy link
Member

Choose a reason for hiding this comment

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

Did you encounter one of these types or is this just precautionary?

Copy link
Contributor Author

@jpbetz jpbetz May 15, 2025

Choose a reason for hiding this comment

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

int64Amount is leaking out from quantity into pkg/generated/openapi/zz_generated.openapi.go. A grep will show this. runtime.Object also shows up in that file.

@Jefftree
Copy link
Member

/lgtm
/hold

in case you want additional eyes

@k8s-ci-robot k8s-ci-robot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. lgtm "Looks good to me", indicates that a PR is ready to be merged. labels May 15, 2025
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. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. lgtm "Looks good to me", indicates that a PR is ready to be merged. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. 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
None yet
Development

Successfully merging this pull request may close these issues.

3 participants