Skip to content

Commit

Permalink
Use register-gen to generate schema registration boilerplate
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Wieczorek <[email protected]>
  • Loading branch information
twz123 committed Jun 4, 2024
1 parent f44033f commit 5c93ab8
Show file tree
Hide file tree
Showing 13 changed files with 303 additions and 156 deletions.
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,11 @@ go.sum: go.mod .k0sbuild.docker-image.k0s
# List of all the custom APIs that k0s defines.
api_group_versions := $(foreach path,$(wildcard pkg/apis/*/v*/doc.go),$(path:pkg/apis/%/doc.go=%))

# Declare the requisites for the generators operating on API group versions.
api_group_version_generator_files := .controller-gen.stamp zz_generated.register.go
$(foreach gv,$(api_group_versions),$(eval $(foreach f,$(api_group_version_generator_files),pkg/apis/$(gv)/$(f)): $$(shell find pkg/apis/$(gv)/ -maxdepth 1 -type f -name '*.go' -not -name '*_test.go' -not -name 'zz_generated*')))

# Run controller-gen for all API group versions.
$(foreach gv,$(api_group_versions),$(eval pkg/apis/$(gv)/.controller-gen.stamp: $$(shell find pkg/apis/$(gv)/ -maxdepth 1 -type f -name '*.go' -not -name '*_test.go' -not -name 'zz_generated*')))
$(foreach gv,$(api_group_versions),pkg/apis/$(gv)/.controller-gen.stamp): .k0sbuild.docker-image.k0s hack/tools/boilerplate.go.txt hack/tools/Makefile.variables
rm -rf 'static/manifests/$(dir $(@:pkg/apis/%/.controller-gen.stamp=%))CustomResourceDefinition'
mkdir -p 'static/manifests/$(dir $(@:pkg/apis/%/.controller-gen.stamp=%))'
Expand All @@ -130,6 +133,14 @@ $(foreach gv,$(api_group_versions),pkg/apis/$(gv)/.controller-gen.stamp): .k0sbu
touch -- '$@'
codegen_targets := $(foreach gv,$(api_group_versions),pkg/apis/$(gv)/.controller-gen.stamp)

# Run register-gen for all API group versions.
$(foreach gv,$(api_group_versions),pkg/apis/$(gv)/zz_generated.register.go): .k0sbuild.docker-image.k0s hack/tools/boilerplate.go.txt hack/tools/Makefile.variables
CGO_ENABLED=0 $(GO) run k8s.io/code-generator/cmd/register-gen@v$(kubernetes_version:1.%=0.%) \
--go-header-file=hack/tools/boilerplate.go.txt \
--output-file='$(notdir $@)' \
'github.com/k0sproject/k0s/$(dir $@)'
codegen_targets := $(foreach gv,$(api_group_versions),pkg/apis/$(gv)/zz_generated.register.go)

# Generate the k0s client-go clientset based on all custom API group versions.
clientset_input_dirs := $(foreach gv,$(api_group_versions),pkg/apis/$(gv))
codegen_targets += pkg/client/clientset/.client-gen.stamp
Expand Down
13 changes: 7 additions & 6 deletions inttest/addons/addons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/k0sproject/k0s/pkg/apis/helm/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
k8s "k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -124,8 +125,8 @@ func (as *AddonsSuite) deleteRelease(chart *v1beta1.Chart) {
return true, nil
}))

helmScheme, err := v1beta1.SchemeBuilder.Build()
as.Require().NoError(err)
helmScheme := runtime.NewScheme()
as.Require().NoError(v1beta1.Install(helmScheme))
chartClient, err := client.New(cfg, client.Options{Scheme: helmScheme})
as.Require().NoError(err)

Expand Down Expand Up @@ -179,8 +180,8 @@ func (as *AddonsSuite) deleteUninstalledChart(ctx context.Context) {
cfg, err := as.GetKubeConfig(as.ControllerNode(0))
as.Require().NoError(err)

scheme, err := v1beta1.SchemeBuilder.Build()
as.Require().NoError(err)
scheme := runtime.NewScheme()
as.Require().NoError(v1beta1.Install(scheme))
crClient, err := client.New(cfg, client.Options{Scheme: scheme})
as.Require().NoError(err)

Expand Down Expand Up @@ -243,8 +244,8 @@ func (as *AddonsSuite) waitForTestRelease(addonName, appVersion string, namespac
cfg, err := as.GetKubeConfig(as.ControllerNode(0))
as.Require().NoError(err)

helmScheme, err := v1beta1.SchemeBuilder.Build()
as.Require().NoError(err)
helmScheme := runtime.NewScheme()
as.Require().NoError(v1beta1.Install(helmScheme))
chartClient, err := client.New(cfg, client.Options{Scheme: helmScheme})
as.Require().NoError(err)
var chart v1beta1.Chart
Expand Down
33 changes: 0 additions & 33 deletions pkg/apis/autopilot/v1beta2/info.go

This file was deleted.

9 changes: 0 additions & 9 deletions pkg/apis/autopilot/v1beta2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func init() {
SchemeBuilder.Register(
&ControlNode{},
&ControlNodeList{},
&Plan{},
&PlanList{},
)
}

// ControlNode is a node which behaves as a controller, able to receive autopilot
// signaling updates.
//
Expand Down
7 changes: 0 additions & 7 deletions pkg/apis/autopilot/v1beta2/updateconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func init() {
SchemeBuilder.Register(
&UpdateConfig{},
&UpdateConfigList{},
)
}

const UpdateConfigFinalizer = "updateconfig.autopilot.k0sproject.io"

const (
Expand Down
74 changes: 74 additions & 0 deletions pkg/apis/autopilot/v1beta2/zz_generated.register.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 0 additions & 19 deletions pkg/apis/etcd/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,9 @@ package v1beta1
import (
"time"

"github.com/k0sproject/k0s/pkg/apis/etcd"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var (
// GroupVersion is group version used to register these objects
SchemeGroupVersion = schema.GroupVersion{Group: etcd.GroupName, Version: Version}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: SchemeGroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)

func init() {
SchemeBuilder.Register(&EtcdMember{}, &EtcdMemberList{})
}

// EtcdMember describes the nodes etcd membership status
//
// +kubebuilder:object:root=true
Expand Down
70 changes: 70 additions & 0 deletions pkg/apis/etcd/v1beta1/zz_generated.register.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 0 additions & 42 deletions pkg/apis/helm/v1beta1/groupversion_info.go

This file was deleted.

Loading

0 comments on commit 5c93ab8

Please sign in to comment.