Skip to content

Commit

Permalink
introduce runtime-openapi-gen to generate openapi spec on release
Browse files Browse the repository at this point in the history
* openapi-gen: modified upstream openapi-gen to generate zz_generated.openapi.go files
* runtime-openapi-gen consumes runtime SDK catalog and generated openapi spec to generate openapi yaml spec

Co-authored-by: Stefan Bueringer <buringerst@vmware.com>
Co-authored-by: Christian Schlotter <schlotterc@vmware.com>
  • Loading branch information
sbueringer and chrischdi committed May 12, 2022
1 parent dd89b09 commit 2344b28
Show file tree
Hide file tree
Showing 16 changed files with 3,964 additions and 19 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ run:
- e2e
skip-files:
- "zz_generated.*\\.go$"
- "vendored_openapi\\.go$"
skip-dirs:
- third_party
allow-parallel-runners: true
50 changes: 47 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ KPROMO_PKG := sigs.k8s.io/promo-tools/v3/cmd/kpromo
CONVERSION_VERIFIER_BIN := conversion-verifier
CONVERSION_VERIFIER := $(abspath $(TOOLS_BIN_DIR)/$(CONVERSION_VERIFIER_BIN))

OPENAPI_GEN_VER := 5e7f5fd
OPENAPI_GEN_BIN := openapi-gen
# We are intentionally using the binary without version suffix, to avoid the version
# in generated files.
OPENAPI_GEN := $(abspath $(TOOLS_BIN_DIR)/$(OPENAPI_GEN_BIN))
OPENAPI_GEN_PKG := k8s.io/kube-openapi/cmd/openapi-gen

RUNTIME_OPENAPI_GEN_BIN := runtime-openapi-gen
RUNTIME_OPENAPI_GEN := $(abspath $(TOOLS_BIN_DIR)/$(RUNTIME_OPENAPI_GEN_BIN))

TILT_PREPARE_BIN := tilt-prepare
TILT_PREPARE := $(abspath $(TOOLS_BIN_DIR)/$(TILT_PREPARE_BIN))

Expand Down Expand Up @@ -179,8 +189,8 @@ help: # Display this help
ALL_GENERATE_MODULES = core kubeadm-bootstrap kubeadm-control-plane

.PHONY: generate
generate: ## Run all generate-manifests-*, generate-go-deepcopy-*, generate-go-conversions-* targets
$(MAKE) generate-modules generate-manifests generate-go-deepcopy generate-go-conversions
generate: ## Run all generate-manifests-*, generate-go-deepcopy-*, generate-go-conversions-* and generate-go-openapi targets
$(MAKE) generate-modules generate-manifests generate-go-deepcopy generate-go-conversions generate-go-openapi
$(MAKE) -C $(CAPD_DIR) generate

.PHONY: generate-manifests
Expand Down Expand Up @@ -328,6 +338,18 @@ generate-go-conversions-kubeadm-control-plane: $(CONVERSION_GEN) ## Generate con
--output-file-base=zz_generated.conversion $(CONVERSION_GEN_OUTPUT_BASE) \
--go-header-file=./hack/boilerplate/boilerplate.generatego.txt

.PHONY: generate-go-openapi
generate-go-openapi: $(OPENAPI_GEN) $(CONTROLLER_GEN) ## Generate openapi go code for runtime SDK
@for pkg in "api/v1beta1" "$(EXP_DIR)/runtime/hooks/api/v1alpha1"; do \
$(MAKE) clean-generated-openapi-definitions SRC_DIRS="./$${pkg}"; \
echo "** Generating openapi schema for types in ./$${pkg} **"; \
$(OPENAPI_GEN) \
--input-dirs=./$${pkg} \
--output-file-base=zz_generated.openapi \
--output-package=sigs.k8s.io/cluster-api/$${pkg} \
--go-header-file=./hack/boilerplate/boilerplate.generatego.txt; \
done

.PHONY: generate-modules
generate-modules: ## Run go mod tidy to ensure modules are up to date
go mod tidy
Expand Down Expand Up @@ -590,7 +612,7 @@ manifest-modification: # Set the manifest images to the staging/production bucke
$(MAKE) set-manifest-pull-policy PULL_POLICY=IfNotPresent TARGET_RESOURCE="./controlplane/kubeadm/config/default/manager_pull_policy.yaml"

.PHONY: release-manifests
release-manifests: $(RELEASE_DIR) $(KUSTOMIZE) ## Build the manifests to publish with a release
release-manifests: $(RELEASE_DIR) $(KUSTOMIZE) $(RUNTIME_OPENAPI_GEN) ## Build the manifests to publish with a release
# Build core-components.
$(KUSTOMIZE) build config/default > $(RELEASE_DIR)/core-components.yaml
# Build bootstrap-components.
Expand All @@ -607,6 +629,9 @@ release-manifests: $(RELEASE_DIR) $(KUSTOMIZE) ## Build the manifests to publish
# Add metadata to the release artifacts
cp metadata.yaml $(RELEASE_DIR)/metadata.yaml

# Generate OpenAPI specification.
$(RUNTIME_OPENAPI_GEN) --version $(RELEASE_TAG) --output-file $(RELEASE_DIR)/runtime-sdk-openapi.yaml

.PHONY: release-manifests-dev
release-manifests-dev: ## Build the development manifests and copies them in the release folder
# Release CAPD components and add them to the release dir
Expand Down Expand Up @@ -761,6 +786,10 @@ clean-release-git: ## Restores the git files usually modified during a release
clean-generated-conversions: ## Remove files generated by conversion-gen from the mentioned dirs. Example SRC_DIRS="./api/v1alpha4"
(IFS=','; for i in $(SRC_DIRS); do find $$i -type f -name 'zz_generated.conversion*' -exec rm -f {} \;; done)

.PHONY: clean-generated-openapi-definitions
clean-generated-openapi-definitions: ## Remove files generated by openapi-gen from the mentioned dirs. Example SRC_DIRS="./api/v1alpha4"
(IFS=','; for i in $(SRC_DIRS); do find $$i -type f -name 'zz_generated.openapi*' -exec rm -f {} \;; done)

## --------------------------------------
## Hack / Tools
## --------------------------------------
Expand All @@ -773,6 +802,12 @@ $(CONTROLLER_GEN_BIN): $(CONTROLLER_GEN) ## Build a local copy of controller-gen
.PHONY: $(CONVERSION_GEN_BIN)
$(CONVERSION_GEN_BIN): $(CONVERSION_GEN) ## Build a local copy of conversion-gen.

.PHONY: $(OPENAPI_GEN_BIN)
$(OPENAPI_GEN_BIN): $(OPENAPI_GEN) ## Build a local copy of openapi-gen.

.PHONY: $(RUNTIME_OPENAPI_GEN_BIN)
$(RUNTIME_OPENAPI_GEN_BIN): $(RUNTIME_OPENAPI_GEN) ## Build a local copy of runtime-openapi-gen.

.PHONY: $(CONVERSION_VERIFIER_BIN)
$(CONVERSION_VERIFIER_BIN): $(CONVERSION_VERIFIER) ## Build a local copy of conversion-verifier.

Expand Down Expand Up @@ -812,6 +847,15 @@ $(CONVERSION_GEN): # Build conversion-gen from tools folder.
$(CONVERSION_VERIFIER): $(TOOLS_DIR)/go.mod # Build conversion-verifier from tools folder.
cd $(TOOLS_DIR); go build -tags=tools -o $(BIN_DIR)/$(CONVERSION_VERIFIER_BIN) sigs.k8s.io/cluster-api/hack/tools/conversion-verifier

.PHONY: $(OPENAPI_GEN)
$(OPENAPI_GEN): # Build openapi-gen from tools folder.
GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) $(OPENAPI_GEN_PKG) $(OPENAPI_GEN_BIN) $(OPENAPI_GEN_VER)

## We are forcing a rebuilt of runtime-openapi-gen via PHONY so that we're always using an up-to-date version.
.PHONY: $(RUNTIME_OPENAPI_GEN)
$(RUNTIME_OPENAPI_GEN): $(TOOLS_DIR)/go.mod # Build openapi-gen from tools folder.
cd $(TOOLS_DIR); go build -tags=tools -o $(BIN_DIR)/$(RUNTIME_OPENAPI_GEN_BIN) sigs.k8s.io/cluster-api/hack/tools/runtime-openapi-gen

$(GOTESTSUM): # Build gotestsum from tools folder.
GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) $(GOTESTSUM_PKG) $(GOTESTSUM_BIN) $(GOTESTSUM_VER)

Expand Down
1 change: 1 addition & 0 deletions api/v1beta1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ limitations under the License.
*/

// Package v1beta1 contains the v1beta1 API implementation.
// +k8s:openapi-gen=true
package v1beta1
Loading

0 comments on commit 2344b28

Please sign in to comment.