Skip to content

Commit

Permalink
Merge pull request #106 from erikgb/generate-ac
Browse files Browse the repository at this point in the history
chore: generate applyconfigurations for APIs (SSA)
  • Loading branch information
zoetrope authored Nov 15, 2023
2 parents 5e3cfbd + 255eede commit b30b46b
Show file tree
Hide file tree
Showing 36 changed files with 419 additions and 39 deletions.
29 changes: 25 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,33 @@ help: ## Display this help.
HELM_CRDS_FILE := charts/accurate/templates/generated/crds.yaml
.PHONY: manifests
manifests: setup ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
controller-gen $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
controller-gen $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="{./api/..., ./controllers/...}" output:crd:artifacts:config=config/crd/bases
echo '{{- if .Values.installCRDs }}' > $(HELM_CRDS_FILE)
kustomize build config/kustomize-to-helm/overlays/crds | yq e "." -p yaml - >> $(HELM_CRDS_FILE)
echo '{{- end }}' >> $(HELM_CRDS_FILE)
kustomize build config/kustomize-to-helm/overlays/templates | yq e "." -p yaml - > charts/accurate/templates/generated/generated.yaml

.PHONY: generate
generate: setup ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
generate: setup generate-applyconfigurations ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
controller-gen object:headerFile="hack/boilerplate.go.txt" paths="{./api/...}"

GO_MODULE = $(shell go list -m)
API_DIRS = $(shell find api -mindepth 2 -type d | sed "s|^|$(shell go list -m)/|" | paste -sd ",")
AC_PKG = internal/applyconfigurations
.PHONY: generate-applyconfigurations
generate-applyconfigurations: applyconfiguration-gen ## Generate applyconfigurations to support typesafe SSA.
rm -rf $(AC_PKG)
@echo ">> generating $(AC_PKG)..."
$(APPLYCONFIGURATION_GEN) \
--go-header-file hack/boilerplate.go.txt \
--input-dirs "$(API_DIRS)" \
--output-package "$(GO_MODULE)/$(AC_PKG)" \
--trim-path-prefix "$(GO_MODULE)" \
--output-base "."

.PHONY: apidoc
apidoc: setup $(wildcard api/*/*_types.go)
crd-to-markdown --links docs/links.csv -f api/v1/subnamespace_types.go -n SubNamespace > docs/crd_subnamespace.md
crd-to-markdown --links docs/links.csv -f api/accurate/v1/subnamespace_types.go -n SubNamespace > docs/crd_subnamespace.md

.PHONY: book
book: setup
Expand Down Expand Up @@ -109,6 +123,13 @@ $(SETUP_ENVTEST):
# see https://github.com/kubernetes-sigs/controller-runtime/tree/master/tools/setup-envtest
GOBIN=$(shell pwd)/bin go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

APPLYCONFIGURATION_GEN := $(shell pwd)/bin/applyconfiguration-gen
.PHONY: applyconfiguration-gen
applyconfiguration-gen: $(APPLYCONFIGURATION_GEN) ## Download applyconfiguration-gen locally if necessary
$(APPLYCONFIGURATION_GEN):
# see https://github.com/kubernetes/code-generator/tree/master/cmd/applyconfiguration-gen
GOBIN=$(shell pwd)/bin go install k8s.io/code-generator/cmd/[email protected]

# go-get-tool will 'go get' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-get-tool
Expand Down
2 changes: 1 addition & 1 deletion PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ resources:
domain: cybozu.com
group: accurate
kind: SubNamespace
path: github.com/cybozu-go/accurate/api/v1
path: github.com/cybozu-go/accurate/api/accurate/v1
version: v1
webhooks:
defaulting: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package v1
import (
"testing"

accuratev2alpha1 "github.com/cybozu-go/accurate/api/v2alpha1"
accuratev2alpha1 "github.com/cybozu-go/accurate/api/accurate/v2alpha1"
utilconversion "github.com/cybozu-go/accurate/internal/util/conversion"
fuzz "github.com/google/gofuzz"
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
Expand Down
3 changes: 3 additions & 0 deletions api/accurate/v1/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// +kubebuilder:object:generate=true
// +groupName=accurate.cybozu.com
package v1
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// Package v1 contains API Schema definitions for the accurate v1 API group
// +kubebuilder:object:generate=true
// +groupName=accurate.cybozu.com
package v1

import (
Expand All @@ -9,11 +7,11 @@ import (
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "accurate.cybozu.com", Version: "v1"}
// SchemeGroupVersion is group version used to register these objects
SchemeGroupVersion = schema.GroupVersion{Group: "accurate.cybozu.com", Version: "v1"}

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

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"strconv"

accuratev2alpha1 "github.com/cybozu-go/accurate/api/v2alpha1"
accuratev2alpha1 "github.com/cybozu-go/accurate/api/accurate/v2alpha1"
"github.com/cybozu-go/accurate/pkg/constants"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/meta"
Expand All @@ -20,8 +20,8 @@ func (src *SubNamespace) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*accuratev2alpha1.SubNamespace)

logger := getConversionLogger(src).WithValues(
"source", GroupVersion.Version,
"destination", GroupVersion.Version,
"source", SchemeGroupVersion.Version,
"destination", SchemeGroupVersion.Version,
)
logger.V(5).Info("converting")

Expand Down Expand Up @@ -57,8 +57,8 @@ func (dst *SubNamespace) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*accuratev2alpha1.SubNamespace)

logger := getConversionLogger(src).WithValues(
"source", GroupVersion.Version,
"destination", GroupVersion.Version,
"source", SchemeGroupVersion.Version,
"destination", SchemeGroupVersion.Version,
)
logger.V(5).Info("converting")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package v1
import (
"testing"

accuratev2alpha1 "github.com/cybozu-go/accurate/api/v2alpha1"
accuratev2alpha1 "github.com/cybozu-go/accurate/api/accurate/v2alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kstatus "sigs.k8s.io/cli-utils/pkg/kstatus/status"
)
Expand Down
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions api/accurate/v2alpha1/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// +kubebuilder:object:generate=true
// +groupName=accurate.cybozu.com
package v2alpha1
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// Package v2alpha1 contains API Schema definitions for the accurate v2alpha1 API group
// +kubebuilder:object:generate=true
// +groupName=accurate.cybozu.com
package v2alpha1

import (
Expand All @@ -9,11 +7,11 @@ import (
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "accurate.cybozu.com", Version: "v2alpha1"}
// SchemeGroupVersion is group version used to register these objects
SchemeGroupVersion = schema.GroupVersion{Group: "accurate.cybozu.com", Version: "v2alpha1"}

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

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type SubNamespaceSpec struct {
//+kubebuilder:unservedversion
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+genclient

// SubNamespace is the Schema for the subnamespaces API
type SubNamespace struct {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion cmd/accurate-controller/sub/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"

accuratev1 "github.com/cybozu-go/accurate/api/v1"
accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
"github.com/cybozu-go/accurate/controllers"
"github.com/cybozu-go/accurate/hooks"
"github.com/cybozu-go/accurate/pkg/config"
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectl-accurate/sub/sub_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

accuratev1 "github.com/cybozu-go/accurate/api/v1"
accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectl-accurate/sub/sub_cut.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

accuratev1 "github.com/cybozu-go/accurate/api/v1"
accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
"github.com/cybozu-go/accurate/pkg/constants"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectl-accurate/sub/sub_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

accuratev1 "github.com/cybozu-go/accurate/api/v1"
accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
"github.com/cybozu-go/accurate/pkg/constants"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectl-accurate/sub/sub_graft.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

accuratev1 "github.com/cybozu-go/accurate/api/v1"
accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
"github.com/cybozu-go/accurate/pkg/constants"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectl-accurate/sub/sub_move.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

accuratev1 "github.com/cybozu-go/accurate/api/v1"
accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
"github.com/cybozu-go/accurate/pkg/constants"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectl-accurate/sub/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sub

import (
accuratev1 "github.com/cybozu-go/accurate/api/v1"
accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand Down
2 changes: 1 addition & 1 deletion controllers/namespace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"path"
"reflect"

accuratev1 "github.com/cybozu-go/accurate/api/v1"
accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
"github.com/cybozu-go/accurate/pkg/constants"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
Expand Down
2 changes: 1 addition & 1 deletion controllers/namespace_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"time"

accuratev1 "github.com/cybozu-go/accurate/api/v1"
accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
"github.com/cybozu-go/accurate/pkg/constants"
"github.com/cybozu-go/accurate/pkg/indexing"
. "github.com/onsi/ginkgo/v2"
Expand Down
2 changes: 1 addition & 1 deletion controllers/subnamespace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

accuratev1 "github.com/cybozu-go/accurate/api/v1"
accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
"github.com/cybozu-go/accurate/pkg/constants"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down
2 changes: 1 addition & 1 deletion controllers/subnamespace_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"time"

accuratev1 "github.com/cybozu-go/accurate/api/v1"
accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
"github.com/cybozu-go/accurate/pkg/constants"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down
2 changes: 1 addition & 1 deletion controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

accuratev1 "github.com/cybozu-go/accurate/api/v1"
accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
"github.com/cybozu-go/accurate/pkg/config"
"github.com/cybozu-go/accurate/pkg/constants"
"github.com/cybozu-go/accurate/pkg/feature"
Expand Down
2 changes: 1 addition & 1 deletion e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"errors"
"os"

accuratev1 "github.com/cybozu-go/accurate/api/v1"
accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
"github.com/cybozu-go/accurate/pkg/constants"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
k8s.io/klog/v2 v2.100.1
sigs.k8s.io/cli-utils v0.35.0
sigs.k8s.io/controller-runtime v0.16.2
sigs.k8s.io/structured-merge-diff/v4 v4.3.0
sigs.k8s.io/yaml v1.3.0
)

Expand Down Expand Up @@ -87,5 +88,4 @@ require (
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.3.0 // indirect
)
2 changes: 1 addition & 1 deletion hooks/subnamespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"net/http"
"regexp"

accuratev1 "github.com/cybozu-go/accurate/api/v1"
accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
"github.com/cybozu-go/accurate/pkg/config"
"github.com/cybozu-go/accurate/pkg/constants"
admissionv1 "k8s.io/api/admission/v1"
Expand Down
2 changes: 1 addition & 1 deletion hooks/subnamespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package hooks
import (
"context"

accuratev1 "github.com/cybozu-go/accurate/api/v1"
accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
"github.com/cybozu-go/accurate/pkg/constants"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down
2 changes: 1 addition & 1 deletion hooks/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

admissionv1beta1 "k8s.io/api/admission/v1beta1"
//+kubebuilder:scaffold:imports
accuratev1 "github.com/cybozu-go/accurate/api/v1"
accuratev1 "github.com/cybozu-go/accurate/api/accurate/v1"
"github.com/cybozu-go/accurate/pkg/config"
"github.com/cybozu-go/accurate/pkg/indexing"
"k8s.io/apimachinery/pkg/runtime"
Expand Down
Loading

0 comments on commit b30b46b

Please sign in to comment.