Skip to content

Commit

Permalink
Prepare for multi-module repo, refactor code-gen (#547)
Browse files Browse the repository at this point in the history
* Move `apis` into `api`
* Move internal types from `apis` to `onmetal-apiserver/internal/apis`
* Adapt code generation to produce correct, future stand-alone
  directories (e.g. `client-go`, defaults / conversions to internal
  directory)
* Update how toolchaing programs are fetched: Previously, we used
  `hack/tools.go`, however that polluted our `go.mod`. With the new
  approach, we download the tools using `go install`
* Update tool fetching as per the latest kubebuilder makefile template
  https://github.com/kubernetes-sigs/kubebuilder/blob/89f58d3f43ff0f9e8c22c9ffff2f6fc3a744248b/pkg/plugins/golang/v3/scaffolds/internal/templates/makefile.go
* Move `testutils/validation` to `onmetal-apiserver` as it's only
  required there
  • Loading branch information
adracus authored Nov 17, 2022
1 parent 43e1ba7 commit bddbf8c
Show file tree
Hide file tree
Showing 521 changed files with 3,317 additions and 13,614 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check-codegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
run: make generate
- name: Compare the expected and actual generated/* directories
run: |
if [ "$(git diff --ignore-space-at-eol generated/ | wc -l)" -gt "0" ]; then
if [ "$(git diff --ignore-space-at-eol client-go/ | wc -l)" -gt "0" ]; then
echo "Detected uncommitted changes after build. Consider running 'make generate'."
echo "See status below:"
git diff
Expand Down
13 changes: 11 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
run:
concurrency: 4
timeout: 10m

linters:
Expand All @@ -7,6 +8,7 @@ linters:
- ineffassign
- misspell
- goimports
- importas

severity:
default-severity: error
Expand All @@ -21,8 +23,15 @@ linters-settings:
- name: exported
- name: if-return
disabled: true
importas:
alias:
- pkg: github.com/onmetal/onmetal-api/api/(\w+)/(v[\w\d]+)
alias: $1$2
- pkg: github.com/onmetal/onmetal-api/onmetal-apiserver/internal/apis/(\w+)/(v[\w\d]+)
alias: $1$2
- pkg: github.com/onmetal/onmetal-api/onmetal-apiserver/internal/apis/(\w+)
alias: $1

issues:
exclude:
# Exclude stutter issues (for now)
exclude: # Exclude stutter issues (for now)
- "exported: type name will be used as (.+) by other packages, and that stutters; consider calling this (.+)"
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ COPY go.sum go.sum
RUN go mod download

# Copy the go source
COPY apis/ apis/
COPY api/ api/
COPY apiutils/ apiutils/
COPY generated/ generated/
COPY client-go/ client-go/
COPY machinepoollet/ machinepoollet/
COPY onmetal-apiserver/ onmetal-apiserver/
COPY onmetal-controller-manager/ onmetal-controller-manager/
Expand Down
124 changes: 103 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,19 @@ help: ## Display this help.

.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role webhook paths="./onmetal-controller-manager/controllers/...;./apis/..." output:rbac:artifacts:config=config/controller/rbac
$(CONTROLLER_GEN) rbac:roleName=manager-role webhook paths="./onmetal-controller-manager/controllers/...;./api/..." output:rbac:artifacts:config=config/controller/rbac

.PHONY: generate
generate:
generate: vgopath deepcopy-gen client-gen lister-gen informer-gen defaulter-gen conversion-gen openapi-gen
go mod download
VGOPATH=$(VGOPATH) \
DEEPCOPY_GEN=$(DEEPCOPY_GEN) \
CLIENT_GEN=$(CLIENT_GEN) \
LISTER_GEN=$(LISTER_GEN) \
INFORMER_GEN=$(INFORMER_GEN) \
DEFAULTER_GEN=$(DEFAULTER_GEN) \
CONVERSION_GEN=$(CONVERSION_GEN) \
OPENAPI_GEN=$(OPENAPI_GEN) \
./hack/update-codegen.sh

.PHONY: fmt
Expand All @@ -66,26 +75,29 @@ lint: ## Run golangci-lint on the code.

.PHONY: clean
clean: ## Clean any artifacts that can be regenerated.
rm -rf generated/*
rm -rf client-go/informers
rm -rf client-go/listers
rm -rf client-go/onmetalapi
rm -rf client-go/openapi

.PHONY: addlicense
addlicense: ## Add license headers to all go files.
find . -name '*.go' -exec go run github.com/google/addlicense -c 'OnMetal authors' {} +
.PHONY: add-license
add-license: addlicense ## Add license headers to all go files.
find . -name '*.go' -exec $(ADDLICENSE) -c 'OnMetal authors' {} +

.PHONY: checklicense
checklicense: ## Check that every file has a license header present.
find . -name '*.go' -exec go run github.com/google/addlicense -check -c 'OnMetal authors' {} +
.PHONY: check-license
check-license: ## Check that every file has a license header present.
find . -name '*.go' -exec $(ADDLICENSE) -check -c 'OnMetal authors' {} +

.PHONY: check
check: manifests generate addlicense lint test # Generate manifests, code, lint, add licenses, test
check: manifests generate add-license lint test # Generate manifests, code, lint, add licenses, test

.PHONY: docs
docs: ## Run go generate to generate API reference documentation.
go run github.com/ahmetb/gen-crd-api-reference-docs -api-dir ./apis/common/v1alpha1 -config ./hack/api-reference/common-config.json -template-dir ./hack/api-reference/template -out-file ./docs/api-reference/common.md
go run github.com/ahmetb/gen-crd-api-reference-docs -api-dir ./apis/compute/v1alpha1 -config ./hack/api-reference/compute-config.json -template-dir ./hack/api-reference/template -out-file ./docs/api-reference/compute.md
go run github.com/ahmetb/gen-crd-api-reference-docs -api-dir ./apis/storage/v1alpha1 -config ./hack/api-reference/storage-config.json -template-dir ./hack/api-reference/template -out-file ./docs/api-reference/storage.md
go run github.com/ahmetb/gen-crd-api-reference-docs -api-dir ./apis/networking/v1alpha1 -config ./hack/api-reference/networking-config.json -template-dir ./hack/api-reference/template -out-file ./docs/api-reference/networking.md
go run github.com/ahmetb/gen-crd-api-reference-docs -api-dir ./apis/ipam/v1alpha1 -config ./hack/api-reference/ipam-config.json -template-dir ./hack/api-reference/template -out-file ./docs/api-reference/ipam.md
docs: gen-crd-api-reference-docs ## Run go generate to generate API reference documentation.
$(GEN_CRD_API_REFERENCE_DOCS) -api-dir ./api/common/v1alpha1 -config ./hack/api-reference/common-config.json -template-dir ./hack/api-reference/template -out-file ./docs/api-reference/common.md
$(GEN_CRD_API_REFERENCE_DOCS) -api-dir ./api/compute/v1alpha1 -config ./hack/api-reference/compute-config.json -template-dir ./hack/api-reference/template -out-file ./docs/api-reference/compute.md
$(GEN_CRD_API_REFERENCE_DOCS) -api-dir ./api/storage/v1alpha1 -config ./hack/api-reference/storage-config.json -template-dir ./hack/api-reference/template -out-file ./docs/api-reference/storage.md
$(GEN_CRD_API_REFERENCE_DOCS) -api-dir ./api/networking/v1alpha1 -config ./hack/api-reference/networking-config.json -template-dir ./hack/api-reference/template -out-file ./docs/api-reference/networking.md
$(GEN_CRD_API_REFERENCE_DOCS) -api-dir ./api/ipam/v1alpha1 -config ./hack/api-reference/ipam-config.json -template-dir ./hack/api-reference/template -out-file ./docs/api-reference/ipam.md

.PHONY: start-docs
start-docs: ## Start the local mkdocs based development environment.
Expand Down Expand Up @@ -120,8 +132,8 @@ run: manifests generate fmt vet ## Run a controller from your host.

.PHONY: docker-build
docker-build: test ## Build docker image with the manager.
docker build --target apiserver -t ${CONTROLLER_IMG} .
docker build --target manager -t ${APISERVER_IMG} .
docker build --target apiserver -t ${APISERVER_IMG} .
docker build --target manager -t ${CONTROLLER_IMG} .

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
Expand Down Expand Up @@ -226,28 +238,98 @@ KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
OPENAPI_EXTRACTOR ?= $(LOCALBIN)/openapi-extractor
DEEPCOPY_GEN ?= $(LOCALBIN)/deepcopy-gen
CLIENT_GEN ?= $(LOCALBIN)/client-gen
LISTER_GEN ?= $(LOCALBIN)/lister-gen
INFORMER_GEN ?= $(LOCALBIN)/informer-gen
DEFAULTER_GEN ?= $(LOCALBIN)/defaulter-gen
CONVERSION_GEN ?= $(LOCALBIN)/conversion-gen
OPENAPI_GEN ?= $(LOCALBIN)/openapi-gen
VGOPATH ?= $(LOCALBIN)/vgopath
GEN_CRD_API_REFERENCE_DOCS ?= $(LOCALBIN)/gen-crd-api-reference-docs
ADDLICENSE ?= $(LOCALBIN)/addlicense

## Tool Versions
KUSTOMIZE_VERSION ?= v3.8.7
CODE_GENERATOR_VERSION ?= v0.24.3
CONTROLLER_TOOLS_VERSION ?= v0.9.0
VGOPATH_VERSION ?= v0.0.2
GEN_CRD_API_REFERENCE_DOCS_VERSION ?= v0.3.0
ADDLICENSE_VERSION ?= v1.1.0

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
.PHONY: kustomize
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
$(KUSTOMIZE): $(LOCALBIN)
curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN)
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
rm -rf $(LOCALBIN)/kustomize; \
fi
test -s $(LOCALBIN)/kustomize || { curl -Ss $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }

.PHONY: controller-gen
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
$(CONTROLLER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)

.PHONY: deepcopy-gen
deepcopy-gen: $(DEEPCOPY_GEN) ## Download deepcopy-gen locally if necessary.
$(DEEPCOPY_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/deepcopy-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/deepcopy-gen@$(CODE_GENERATOR_VERSION)

.PHONY: client-gen
client-gen: $(CLIENT_GEN) ## Download client-gen locally if necessary.
$(CLIENT_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/client-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/client-gen@$(CODE_GENERATOR_VERSION)

.PHONY: lister-gen
lister-gen: $(LISTER_GEN) ## Download lister-gen locally if necessary.
$(LISTER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/lister-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/lister-gen@$(CODE_GENERATOR_VERSION)

.PHONY: informer-gen
informer-gen: $(INFORMER_GEN) ## Download informer-gen locally if necessary.
$(INFORMER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/informer-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/informer-gen@$(CODE_GENERATOR_VERSION)

.PHONY: defaulter-gen
defaulter-gen: $(DEFAULTER_GEN) ## Download defaulter-gen locally if necessary.
$(DEFAULTER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/defaulter-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/defaulter-gen@$(CODE_GENERATOR_VERSION)

.PHONY: conversion-gen
conversion-gen: $(CONVERSION_GEN) ## Download conversion-gen locally if necessary.
$(CONVERSION_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/conversion-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/conversion-gen@$(CODE_GENERATOR_VERSION)

.PHONY: openapi-gen
openapi-gen: $(OPENAPI_GEN) ## Download openapi-gen locally if necessary.
$(OPENAPI_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/openapi-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/openapi-gen@$(CODE_GENERATOR_VERSION)

.PHONY: vgopath
vgopath: $(VGOPATH) ## Download vgopath locally if necessary.
$(VGOPATH): $(LOCALBIN)
test -s $(LOCALBIN)/vgopath || GOBIN=$(LOCALBIN) go install github.com/onmetal/vgopath@$(VGOPATH_VERSION)

.PHONY: envtest
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

.PHONY: openapi-extractor
openapi-extractor: $(OPENAPI_EXTRACTOR) ## Download openapi-extractor locally if necessary.
$(OPENAPI_EXTRACTOR): $(LOCALBIN)
GOBIN=$(LOCALBIN) go install github.com/onmetal/openapi-extractor/cmd/openapi-extractor@latest
test -s $(LOCALBIN)/openapi-extractor || GOBIN=$(LOCALBIN) go install github.com/onmetal/openapi-extractor/cmd/openapi-extractor@latest

.PHONY: gen-crd-api-reference-docs
gen-crd-api-reference-docs: $(GEN_CRD_API_REFERENCE_DOCS) ## Download gen-crd-api-reference-docs locally if necessary.
$(GEN_CRD_API_REFERENCE_DOCS): $(LOCALBIN)
test -s $(LOCALBIN)/gen-crd-api-reference-docs || GOBIN=$(LOCALBIN) go install github.com/ahmetb/gen-crd-api-reference-docs@$(GEN_CRD_API_REFERENCE_DOCS_VERSION)

.PHONY: addlicense
addlicense: $(ADDLICENSE) ## Download addlicense locally if necessary.
$(ADDLICENSE): $(LOCALBIN)
test-s $(LOCALBIN)/addlicense || GOBIN=$(LOCALBIN) go install github.com/google/addlicense@$(ADDLICENSE_VERSION)

16 changes: 8 additions & 8 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ resources:
domain: onmetal.de
group: compute
kind: MachineClass
path: github.com/onmetal/onmetal-api/apis/compute/v1alpha1
path: github.com/onmetal/onmetal-api/api/compute/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
Expand All @@ -21,7 +21,7 @@ resources:
domain: onmetal.de
group: compute
kind: MachinePool
path: github.com/onmetal/onmetal-api/apis/compute/v1alpha1
path: github.com/onmetal/onmetal-api/api/compute/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
Expand All @@ -30,7 +30,7 @@ resources:
domain: onmetal.de
group: storage
kind: VolumePool
path: github.com/onmetal/onmetal-api/apis/storage/v1alpha1
path: github.com/onmetal/onmetal-api/api/storage/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
Expand All @@ -39,7 +39,7 @@ resources:
domain: onmetal.de
group: storage
kind: VolumeClass
path: github.com/onmetal/onmetal-api/apis/storage/v1alpha1
path: github.com/onmetal/onmetal-api/api/storage/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
Expand All @@ -48,7 +48,7 @@ resources:
domain: onmetal.de
group: storage
kind: Volume
path: github.com/onmetal/onmetal-api/apis/storage/v1alpha1
path: github.com/onmetal/onmetal-api/api/storage/v1alpha1
version: v1alpha1
webhooks:
validation: true
Expand All @@ -60,7 +60,7 @@ resources:
domain: onmetal.de
group: compute
kind: Machine
path: github.com/onmetal/onmetal-api/apis/compute/v1alpha1
path: github.com/onmetal/onmetal-api/api/compute/v1alpha1
version: v1alpha1
webhooks:
validation: true
Expand All @@ -72,14 +72,14 @@ resources:
domain: api.onmetal.de
group: ipam
kind: Prefix
path: github.com/onmetal/onmetal-api/apis/ipam/v1alpha1
path: github.com/onmetal/onmetal-api/api/ipam/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
namespaced: true
domain: api.onmetal.de
group: ipam
kind: PrefixAllocation
path: github.com/onmetal/onmetal-api/apis/ipam/v1alpha1
path: github.com/onmetal/onmetal-api/api/ipam/v1alpha1
version: v1alpha1
version: "3"
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ type LocalUIDReference struct {
}

// IP is an IP address.
// +kubebuilder:validation:Type=string
type IP struct {
netip.Addr `json:"-"`
}
Expand Down Expand Up @@ -270,7 +269,6 @@ func EqualIPRanges(a, b IPRange) bool {
}

// IPPrefix represents a network prefix.
// +kubebuilder:validation:Type=string
// +nullable
type IPPrefix struct {
netip.Prefix `json:"-"`
Expand Down Expand Up @@ -391,7 +389,6 @@ type Taint struct {
Effect TaintEffect `json:"effect"`
}

// +kubebuilder:validation:Enum=NoSchedule
type TaintEffect string

const (
Expand Down Expand Up @@ -452,7 +449,6 @@ func (t *Toleration) ToleratesTaint(taint *Taint) bool {
}

// A toleration operator is the set of operators that can be used in a toleration.
// +kubebuilder:validation:Enum=Equal;Exists
type TolerationOperator string

const (
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion apis/common/v1alpha1/doc.go → api/common/v1alpha1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
// +groupName=common.api.onmetal.de

// Package v1alpha1 is the v1alpha1 version of the API.
package v1alpha1 // import "github.com/onmetal/onmetal-api/apis/common/v1alpha1"
package v1alpha1 // import "github.com/onmetal/onmetal-api/api/common/v1alpha1"
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package v1alpha1

import (
networkingv1alpha1 "github.com/onmetal/onmetal-api/apis/networking/v1alpha1"
storagev1alpha1 "github.com/onmetal/onmetal-api/apis/storage/v1alpha1"
networkingv1alpha1 "github.com/onmetal/onmetal-api/api/networking/v1alpha1"
storagev1alpha1 "github.com/onmetal/onmetal-api/api/storage/v1alpha1"
)

// EphemeralNetworkInterfaceSource is a definition for an ephemeral (i.e. coupled to the lifetime of the surrounding
Expand Down
6 changes: 2 additions & 4 deletions apis/compute/v1alpha1/doc.go → api/compute/v1alpha1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
* limitations under the License.
*/

// +k8s:deepcopy-gen=package
// +k8s:conversion-gen=github.com/onmetal/onmetal-api/apis/compute
// +k8s:openapi-gen=true
// +k8s:defaulter-gen=TypeMeta
// +k8s:deepcopy-gen=package
// +groupName=compute.api.onmetal.de

// Package v1alpha1 is the v1alpha1 version of the API.
package v1alpha1 // import "github.com/onmetal/onmetal-api/apis/compute/v1alpha1"
package v1alpha1 // import "github.com/onmetal/onmetal-api/api/compute/v1alpha1"
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

commonv1alpha1 "github.com/onmetal/onmetal-api/apis/common/v1alpha1"
commonv1alpha1 "github.com/onmetal/onmetal-api/api/common/v1alpha1"
)

// MachineSpec defines the desired state of Machine
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

commonv1alpha1 "github.com/onmetal/onmetal-api/apis/common/v1alpha1"
commonv1alpha1 "github.com/onmetal/onmetal-api/api/common/v1alpha1"
)

// MachinePoolSpec defines the desired state of MachinePool
Expand Down
Loading

0 comments on commit bddbf8c

Please sign in to comment.