Skip to content

Commit

Permalink
Use register-gen to generate schema registration boilerplate
Browse files Browse the repository at this point in the history
Replace the handwritten stuff with that.

Signed-off-by: Tom Wieczorek <[email protected]>
  • Loading branch information
twz123 committed Jun 4, 2024
1 parent a62f105 commit 0d537ba
Show file tree
Hide file tree
Showing 17 changed files with 308 additions and 170 deletions.
24 changes: 20 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,23 @@ go.sum: go.mod .k0sbuild.docker-image.k0s
$(GO) mod tidy && touch -c -- '$@'

controllergen_targets += pkg/apis/helm/v1beta1/.controller-gen.stamp
pkg/apis/helm/v1beta1/.controller-gen.stamp: $(shell find pkg/apis/helm/v1beta1/ -maxdepth 1 -type f -name '*.go' -not -name '*_test.go' -not -name 'zz_generated*')
codegen_targets += pkg/apis/helm/v1beta1/zz_generated.register.go
pkg/apis/helm/v1beta1/.controller-gen.stamp pkg/apis/helm/v1beta1/zz_generated.register.go: $(shell find pkg/apis/helm/v1beta1/ -maxdepth 1 -type f -name '*.go' -not -name '*_test.go' -not -name 'zz_generated*')
pkg/apis/helm/v1beta1/.controller-gen.stamp: gen_output_dir = helm

controllergen_targets += pkg/apis/k0s/v1beta1/.controller-gen.stamp
pkg/apis/k0s/v1beta1/.controller-gen.stamp: $(shell find pkg/apis/k0s/v1beta1/ -maxdepth 1 -type f -name '*.go' -not -name '*_test.go' -not -name 'zz_generated*')
codegen_targets += pkg/apis/k0s/v1beta1/zz_generated.register.go
pkg/apis/k0s/v1beta1/.controller-gen.stamp pkg/apis/k0s/v1beta1/zz_generated.register.go: $(shell find pkg/apis/k0s/v1beta1/ -maxdepth 1 -type f -name '*.go' -not -name '*_test.go' -not -name 'zz_generated*')
pkg/apis/k0s/v1beta1/.controller-gen.stamp: gen_output_dir = v1beta1

controllergen_targets += pkg/apis/autopilot/v1beta2/.controller-gen.stamp
pkg/apis/autopilot/v1beta2/.controller-gen.stamp: $(shell find pkg/apis/autopilot/v1beta2/ -maxdepth 1 -type f -name '*.go' -not -name '*_test.go' -not -name 'zz_generated*')
codegen_targets += pkg/apis/autopilot/v1beta2/zz_generated.register.go
pkg/apis/autopilot/v1beta2/.controller-gen.stamp pkg/apis/autopilot/v1beta2/zz_generated.register.go: $(shell find pkg/apis/autopilot/v1beta2/ -maxdepth 1 -type f -name '*.go' -not -name '*_test.go' -not -name 'zz_generated*')
pkg/apis/autopilot/v1beta2/.controller-gen.stamp: gen_output_dir = autopilot

controllergen_targets += pkg/apis/etcd/v1beta1/.controller-gen.stamp
pkg/apis/etcd/v1beta1/.controller-gen.stamp: $(shell find pkg/apis/etcd/v1beta1/ -maxdepth 1 -type f -name '*.go' -not -name '*_test.go' -not -name 'zz_generated*')
codegen_targets += pkg/apis/etcd/v1beta1/zz_generated.register.go
pkg/apis/etcd/v1beta1/.controller-gen.stamp pkg/apis/etcd/v1beta1/zz_generated.register.go: $(shell find pkg/apis/etcd/v1beta1/ -maxdepth 1 -type f -name '*.go' -not -name '*_test.go' -not -name 'zz_generated*')
pkg/apis/etcd/v1beta1/.controller-gen.stamp: gen_output_dir = etcd

codegen_targets += $(controllergen_targets)
Expand All @@ -142,6 +146,18 @@ pkg/apis/%/.controller-gen.stamp: .k0sbuild.docker-image.k0s hack/tools/boilerpl
&& mv -f -- "$$gendir"/zz_generated.deepcopy.go '$(dir $@).'
touch -- '$@'

pkg/apis/%/zz_generated.register.go: .k0sbuild.docker-image.k0s hack/tools/boilerplate.go.txt embedded-bins/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 $@).tmp' \
'github.com/k0sproject/k0s/$(dir $@)' || \
{ \
ret=$$?; \
rm -f '$(dir $@)_$(notdir $@).tmp'; \
exit $$ret; \
}
mv '$(dir $@)_$(notdir $@).tmp' '$@'

clientset_input_dirs := pkg/apis/autopilot/v1beta2 pkg/apis/k0s/v1beta1 pkg/apis/helm/v1beta1 pkg/apis/etcd/v1beta1
codegen_targets += pkg/client/clientset/.client-gen.stamp
pkg/client/clientset/.client-gen.stamp: $(shell find $(clientset_input_dirs) -type f -name '*.go' -not -name '*_test.go' -not -name 'zz_generated*')
Expand Down
13 changes: 4 additions & 9 deletions inttest/addons/addons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/k0sproject/k0s/internal/pkg/templatewriter"
"github.com/k0sproject/k0s/inttest/common"
"github.com/k0sproject/k0s/pkg/apis/helm/v1beta1"
k0sscheme "github.com/k0sproject/k0s/pkg/client/clientset/scheme"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -124,9 +125,7 @@ func (as *AddonsSuite) deleteRelease(chart *v1beta1.Chart) {
return true, nil
}))

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

as.T().Logf("Expecting chart %s/%s to be deleted", chart.Namespace, chart.Name)
Expand Down Expand Up @@ -179,9 +178,7 @@ 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)
crClient, err := client.New(cfg, client.Options{Scheme: scheme})
crClient, err := client.New(cfg, client.Options{Scheme: k0sscheme.Scheme})
as.Require().NoError(err)

as.Require().NoError(wait.PollUntilContextCancel(ctx, 1*time.Second, true, func(ctx context.Context) (bool, error) {
Expand Down Expand Up @@ -243,9 +240,7 @@ 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)
chartClient, err := client.New(cfg, client.Options{Scheme: helmScheme})
chartClient, err := client.New(cfg, client.Options{Scheme: k0sscheme.Scheme})
as.Require().NoError(err)
var chart v1beta1.Chart
var lastResourceVersion string
Expand Down
2 changes: 0 additions & 2 deletions pkg/apis/autopilot/v1beta2/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,3 @@

// Package v1beta2 is the v1beta2 version of the API.
package v1beta2

const Version = "v1beta2"
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.

2 changes: 0 additions & 2 deletions pkg/apis/etcd/v1beta1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,3 @@

// Package k0s contains API Schema definitions for the etcd.k0sproject.io API group.
package v1beta1

const Version = "v1beta1"
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.

2 changes: 0 additions & 2 deletions pkg/apis/helm/v1beta1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,3 @@ limitations under the License.

// Package v1beta1 is the v1beta1 version of the API.
package v1beta1

const Version = "v1beta1"
42 changes: 0 additions & 42 deletions pkg/apis/helm/v1beta1/groupversion_info.go

This file was deleted.

Loading

0 comments on commit 0d537ba

Please sign in to comment.