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 6, 2024
1 parent 6e20dd5 commit fcb0564
Show file tree
Hide file tree
Showing 24 changed files with 314 additions and 185 deletions.
28 changes: 23 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,14 @@ 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=%))

# Run controller-gen for all API group versions.
codegen_targets := $(foreach gv,$(api_group_versions),pkg/apis/$(gv)/.controller-gen.stamp)
$(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
# Declare the requisites for the generators operating on API group versions.
api_group_version_targets := .controller-gen.stamp zz_generated.register.go
$(foreach gv,$(api_group_versions),$(eval $(foreach t,$(api_group_version_targets),pkg/apis/$(gv)/$(t)): $$(shell find pkg/apis/$(gv)/ -maxdepth 1 -type f -name '*.go' -not -name '*_test.go' -not -name 'zz_generated*')))

# Run controller-gen for each API group version.
controller_gen_targets := $(foreach gv,$(api_group_versions),pkg/apis/$(gv)/.controller-gen.stamp)
codegen_targets := $(controller_gen_targets)
$(controller_gen_targets): .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=%))'
gendir="$$(mktemp -d .controller-gen.XXXXXX.tmp)" \
Expand All @@ -130,6 +134,20 @@ $(foreach gv,$(api_group_versions),pkg/apis/$(gv)/.controller-gen.stamp): .k0sbu
&& mv -f -- "$$gendir"/zz_generated.deepcopy.go '$(dir $@).'
touch -- '$@'

# Run register-gen for each API group version.
register_gen_targets := $(foreach gv,$(api_group_versions),pkg/apis/$(gv)/zz_generated.register.go)
codegen_targets += $(register_gen_targets)
$(register_gen_targets): .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' '$@'

# 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 All @@ -149,7 +167,7 @@ pkg/client/clientset/.client-gen.stamp: .k0sbuild.docker-image.k0s hack/tools/bo
touch -- '$@'

codegen_targets += static/zz_generated_assets.go
static/zz_generated_assets.go: $(foreach gv,$(api_group_versions),pkg/apis/$(gv)/.controller-gen.stamp) # to generate the CRDs into static/manifests/*/CustomResourceDefinition
static/zz_generated_assets.go: $(controller_gen_targets) # to generate the CRDs into static/manifests/*/CustomResourceDefinition
static/zz_generated_assets.go: $(shell find static/manifests/calico static/manifests/windows static/misc -type f)
static/zz_generated_assets.go: .k0sbuild.docker-image.k0s hack/tools/Makefile.variables
CGO_ENABLED=0 $(GO) run github.com/kevinburke/go-bindata/go-bindata@v$(go-bindata_version) \
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
3 changes: 1 addition & 2 deletions inttest/common/autopilot/waitfor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"fmt"

autopilot "github.com/k0sproject/k0s/pkg/apis/autopilot"
apv1beta2 "github.com/k0sproject/k0s/pkg/apis/autopilot/v1beta2"
appc "github.com/k0sproject/k0s/pkg/autopilot/controller/plans/core"
apclient "github.com/k0sproject/k0s/pkg/client/clientset"
Expand Down Expand Up @@ -64,7 +63,7 @@ func WaitForCRDByName(ctx context.Context, client extensionsclient.Apiextensions
)

return watch.FromClient[*crdList, crd](client.CustomResourceDefinitions()).
WithObjectName(fmt.Sprintf("%s.%s", name, autopilot.GroupName)).
WithObjectName(fmt.Sprintf("%s.%s", name, apv1beta2.GroupName)).
WithErrorCallback(common.RetryWatchErrors(logrus.Infof)).
Until(ctx, func(item *crd) (bool, error) {
for _, cond := range item.Status.Conditions {
Expand Down
2 changes: 0 additions & 2 deletions pkg/apis/autopilot/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,3 @@
// Package autopilot contains API Schema definitions for the
// autopilot.k0sproject.io API group.
package autopilot

const GroupName = "autopilot.k0sproject.io"
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/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@

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

const GroupName = "etcd.k0sproject.io"
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/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@ limitations under the License.

// Package helm contains API Schema definitions for the helm.k0sproject.io API group.
package helm

const GroupName = "helm.k0sproject.io"
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"
Loading

0 comments on commit fcb0564

Please sign in to comment.