-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
🌱 Remove reliance on controller-runtime scheme builder #9045
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,3 +62,68 @@ As the deleted comments request, run `make manager manifests` to regenerate some | |
git add . | ||
git commit -m "Added cluster types" | ||
``` | ||
|
||
# Registering APIs in the scheme | ||
|
||
To enable clients to encode and decode your API, your types must be able to be registered within a [scheme]. | ||
|
||
[scheme]: https://pkg.go.dev/k8s.io/apimachinery/pkg/runtime#Scheme | ||
|
||
By default, Kubebuilder will provide you with a scheme builder like: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have an issue in Kubebuilder to try steer away from the current pattern? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not yet, I'll make one |
||
|
||
```go | ||
import "sigs.k8s.io/controller-runtime/pkg/scheme" | ||
|
||
var ( | ||
// SchemeBuilder is used to add go types to the GroupVersionKind scheme | ||
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion} | ||
|
||
// AddToScheme adds the types in this group-version to the given scheme. | ||
AddToScheme = SchemeBuilder.AddToScheme | ||
) | ||
``` | ||
|
||
and scheme registration that looks like: | ||
|
||
```go | ||
func init() { | ||
SchemeBuilder.Register(&Captain{}, &CaptainList{}) | ||
} | ||
``` | ||
|
||
This pattern introduces a dependency on controller-runtime to your API types, which is discouraged for | ||
API packages as it makes it more difficult for consumers of your API to import your API types. | ||
In general, you should minimise the imports within the API folder of your package to allow your API types | ||
to be imported cleanly into other projects. | ||
|
||
To mitigate this, use the following schemebuilder pattern: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc @furkatgofurov7 In case we might want to include this as an optional action item in the v1.6 release There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vincepri For the provider migration doc, right? Sounds like a good idea! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct yeah There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vincepri @sbueringer +1 to add it to the provider migration doc. Opened #9146 to cover that, PTAL and let me know what you think, thanks! |
||
|
||
```go | ||
import "k8s.io/apimachinery/pkg/runtime" | ||
|
||
var ( | ||
// schemeBuilder is used to add go types to the GroupVersionKind scheme. | ||
schemeBuilder = runtime.NewSchemeBuilder(addKnownTypes) | ||
|
||
// AddToScheme adds the types in this group-version to the given scheme. | ||
AddToScheme = schemeBuilder.AddToScheme | ||
|
||
objectTypes = []runtime.Object{} | ||
) | ||
|
||
func addKnownTypes(scheme *runtime.Scheme) error { | ||
scheme.AddKnownTypes(GroupVersion, objectTypes...) | ||
metav1.AddToGroupVersion(scheme, GroupVersion) | ||
return nil | ||
} | ||
``` | ||
|
||
and register types as below: | ||
|
||
```go | ||
func init() { | ||
objectTypes = append(objectTypes, &Captain{}, &CaptainList{}) | ||
} | ||
``` | ||
|
||
This pattern reduces the number of dependencies being introduced into the API package within your project. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: Isn't this all the same as just:
Would it be simpler to just define the func?
(seems a lot easier to understand to me, but maybe I'm missing something)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pattern seems to be common if you look at say core/v1 or any other API package, so I was just following the usual pattern.
In those other APIs the schemebuilder is exported though, perhaps we should be leaving ours exported for consistency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No strong opinion from my side (@vincepri?)
I was just wondering if we have to go through the schemeBuilder if we only export the AddToScheme func as we could do that in a simpler way.