Skip to content

Commit 01d2a80

Browse files
authored
refactor: Register types with scheme using pattern from Cluster API (#1124)
**What problem does this PR solve?**: This pattern removes the dependency on controller-runtime from the API module. It is recommended by Cluster API. https://cluster-api.sigs.k8s.io/developer/providers/getting-started/implement-api-types#registering-apis-in-the-scheme **Which issue(s) this PR fixes**: Fixes # **How Has This Been Tested?**: <!-- Please describe the tests that you ran to verify your changes. Provide output from the tests and any manual steps needed to replicate the tests. --> **Special notes for your reviewer**: <!-- Use this to provide any additional information to the reviewers. This may include: - Best way to review the PR. - Where the author wants the most review attention on. - etc. --> Because the api module contains external types that do have a dependency on controller-runtime, we have to make a similar change to these types upstream before we can remove our dependency on controller-runtime.
1 parent 9226297 commit 01d2a80

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

api/v1alpha1/clusterconfig_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ type CoreDNS struct {
330330

331331
//nolint:gochecknoinits // Idiomatic to use init functions to register APIs with scheme.
332332
func init() {
333-
SchemeBuilder.Register(
333+
objectTypes = append(objectTypes,
334334
&AWSClusterConfig{},
335335
&DockerClusterConfig{},
336336
&NutanixClusterConfig{},

api/v1alpha1/groupversion_info.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
package v1alpha1
55

66
import (
7+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
runtime "k8s.io/apimachinery/pkg/runtime"
79
"k8s.io/apimachinery/pkg/runtime/schema"
8-
"sigs.k8s.io/controller-runtime/pkg/scheme"
910
)
1011

1112
const APIGroup = "caren.nutanix.com"
@@ -14,9 +15,17 @@ var (
1415
// GroupVersion is group version used to register these objects.
1516
GroupVersion = schema.GroupVersion{Group: APIGroup, Version: "v1alpha1"}
1617

17-
// SchemeBuilder is used to add go types to the GroupVersionKind scheme.
18-
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
18+
// schemeBuilder is used to add go types to the GroupVersionKind scheme.
19+
schemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
1920

2021
// AddToScheme adds the types in this group-version to the given scheme.
21-
AddToScheme = SchemeBuilder.AddToScheme
22+
AddToScheme = schemeBuilder.AddToScheme
23+
24+
objectTypes = []runtime.Object{}
2225
)
26+
27+
func addKnownTypes(scheme *runtime.Scheme) error {
28+
scheme.AddKnownTypes(GroupVersion, objectTypes...)
29+
metav1.AddToGroupVersion(scheme, GroupVersion)
30+
return nil
31+
}

api/v1alpha1/nodeconfig_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ type NodeRegistrationOptions struct {
166166

167167
//nolint:gochecknoinits // Idiomatic to use init functions to register APIs with scheme.
168168
func init() {
169-
SchemeBuilder.Register(
169+
objectTypes = append(objectTypes,
170170
&AWSWorkerNodeConfig{},
171171
&DockerWorkerNodeConfig{},
172172
&NutanixWorkerNodeConfig{},

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)