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

Why doesn't the API provide default values for ApiVersion and Kind? #3858

Open
cowwoc opened this issue Jan 8, 2025 · 3 comments
Open

Why doesn't the API provide default values for ApiVersion and Kind? #3858

cowwoc opened this issue Jan 8, 2025 · 3 comments

Comments

@cowwoc
Copy link

cowwoc commented Jan 8, 2025

Describe the bug
When I am constructing a V1ConfigMap or V1Job I believe there is only one reasonable value that can be assigned to the ApiVersion and Kind properties, so why does the API force users to fill these in by hand every time? Can't the API populate these default values for us when we construct a new instance?

@brendandburns
Copy link
Contributor

All of these API objects are code generated from the OpenAPI specification:

https://github.com/kubernetes/kubernetes/blob/master/api/openapi-spec/README.md

There are no default values specified in that specification and thus there's no default values that the code generator knows how to insert.

You could probably write a utility method which heuristically guesses at it using the class name and reflection

Instance.Create(Class<T> resourceClazz) T { ... }

@cowwoc
Copy link
Author

cowwoc commented Jan 9, 2025

There is nothing wrong with using code generators, but when their limitations hurt usability... it's a problem. I also noticed that many resources have identical methods but no common supertype. This makes it much harder for users to write their own helper methods because now they have to repeat the same code over and over again once per resource type.

Yes, users can generate utility methods but it would be much cheaper (many users, fewer developers) to fix it inside the library than make everyone write their own helpers outside it.

Is this something that can be improved?

@brendandburns
Copy link
Contributor

brendandburns commented Jan 10, 2025

The API surface area is so large and changes constantly which means that code generation is the only way to keep up with API surface area. So using a code generator is a hard requirement for this project (and all of the Kubernetes clients except Golang)

There are four ways that you can improve things:

  1. Improve the Kubernetes OpenAPI doc, since that's the source of the code generation
  2. Improve the code generator, you can find it here: https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator/src/main/resources/Java
  3. Add custom code that is more ergonomic (e.g. https://github.com/kubernetes-client/java/tree/master/extended/src/main/java/io/kubernetes/client/extended/kubectl)
  4. Add patches that get laid in after the code is generated: https://github.com/kubernetes-client/java/tree/master/scripts/patches

In the specific case of this issue, I think it could be fixed by 1) adding default values to that openapi/swagger doc. You might have to also do 2) if defaults aren't currently supported by the code generator.

There might be other things that you can think of as well, we're always open to PRs to fix things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants