Skip to content

Commit

Permalink
Document new schemebuilder patters in provider migration doc
Browse files Browse the repository at this point in the history
Signed-off-by: Furkat Gofurov <furkat.gofurov@suse.com>
  • Loading branch information
furkatgofurov7 committed Aug 8, 2023
1 parent bda002f commit a763a5e
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions docs/book/src/developer/providers/migrations/v1.5-to-v1.6.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,45 @@ maintainers of providers and consumers of our Go API.

### Suggested changes for providers

- To enable clients to encode and decode your API, your types must be able to be registered within a [scheme](https://pkg.go.dev/k8s.io/apimachinery/pkg/runtime#Scheme). The default kubebuilder scheme builder introduces a dependency on controller-runtime to your API types.

To mitigate it, Cluster API removed the reliance on controller-runtime scheme builder (see [#9045](https://github.com/kubernetes-sigs/cluster-api/pull/9045)) in order to reduce the number of dependancies being introcuded to its API types and make API types easily consumable by other projects.

E.g. now CAPI uses the following schemebuilder pattern:

```diff
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+ import "k8s.io/apimachinery/pkg/runtime"
- import "sigs.k8s.io/controller-runtime/pkg/scheme"

var (
// GroupVersion is group version used to register these objects.
....

+ // schemeBuilder is used to add go types to the GroupVersionKind scheme.
+ schemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
- // 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
- AddToScheme = SchemeBuilder.AddToScheme

+ objectTypes = []runtime.Object{}
)

+ func addKnownTypes(scheme *runtime.Scheme) error {
+ scheme.AddKnownTypes(GroupVersion, objectTypes...)
+ metav1.AddToGroupVersion(scheme, GroupVersion)
+ return nil
+ }
```

and type registration:

```diff
func init() {
+ objectTypes = append(objectTypes, &Captain{}, &CaptainList{})
- SchemeBuilder.Register(&Captain{}, &CaptainList{})
}
```

0 comments on commit a763a5e

Please sign in to comment.