Skip to content

Commit 2ab9ba5

Browse files
committed
Add support for Kubebuilder v3
1 parent e949bb1 commit 2ab9ba5

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

makelib/kubebuilder-v3.mk

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Copyright 2019 Pressinfra Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
ifndef __KUBEBUILDERV3_MAKEFILE__
16+
__KUBEBUILDERV3_MAKEFILE__ := included
17+
18+
include $(COMMON_SELF_DIR)/golang.mk
19+
20+
# ====================================================================================
21+
# Options
22+
23+
CRD_DIR ?= config/crd/bases
24+
RBAC_DIR ?= config/rbac
25+
26+
BOILERPLATE_FILE ?= hack/boilerplate.go.txt
27+
28+
CONTROLLER_GEN_CRD_OPTIONS ?= crd output:crd:artifacts:config=$(CRD_DIR)
29+
CONTROLLER_GEN_RBAC_OPTIONS ?= rbac:roleName=manager-role output:rbac:artifacts:config=$(RBAC_DIR)
30+
CONTROLLER_GEN_WEBHOOK_OPTIONS ?= webhook
31+
CONTROLLER_GEN_OBJECT_OPTIONS ?= object:headerFile=$(BOILERPLATE_FILE)
32+
CONTROLLER_GEN_PATHS ?= $(foreach t,$(GO_SUBDIRS),paths=./$(t)/...)
33+
34+
KUBEBUILDER_ASSETS_VERSION ?= 1.19.2
35+
KUBEBUILDER_ASSETS = $(CACHE_DIR)/kubebuilder/k8s/$(KUBEBUILDER_ASSETS_VERSION)-$(HOSTOS)-$(HOSTARCH)
36+
export KUBEBUILDER_ASSETS
37+
38+
# ====================================================================================
39+
# tools
40+
41+
# setup-envtest download and install
42+
SETUP_ENVTEST_VERSION ?= 0.0.0-20211206022232-3ffc700bc2a3
43+
SETUP_ENVTEST_DOWNLOAD_URL ?= sigs.k8s.io/controller-runtime/tools/setup-envtest
44+
$(eval $(call tool.go.install,setup-envtest,v$(SETUP_ENVTEST_VERSION),$(SETUP_ENVTEST_DOWNLOAD_URL)))
45+
46+
# kubebuilder download and install
47+
KUBEBUILDER_VERSION ?= 3.2.0
48+
KUBEBUILDER_DOWNLOAD_URL ?= https://github.com/kubernetes-sigs/kubebuilder/releases/download/v$(KUBEBUILDER_VERSION)/kubebuilder_$(HOST_PLATFORM)
49+
$(eval $(call tool.download,kubebuilder,$(KUBEBUILDER_VERSION),$(KUBEBUILDER_DOWNLOAD_URL)))
50+
51+
# controller-gen download and install
52+
CONTROLLER_GEN_VERSION ?= 0.7.0
53+
CONTROLLER_GEN_DOWNLOAD_URL ?= sigs.k8s.io/controller-tools/cmd/controller-gen
54+
$(eval $(call tool.go.install,controller-gen,v$(CONTROLLER_GEN_VERSION),$(CONTROLLER_GEN_DOWNLOAD_URL)))
55+
56+
build.tools: |$(KUBEBUILDER_ASSETS)
57+
$(KUBEBUILDER_ASSETS): $(SETUP_ENVTEST)
58+
@echo ${TIME} ${BLUE}[TOOL]${CNone} installing kubebuilder assets for Kubernetes $(KUBEBUILDER_ASSETS_VERSION)
59+
@$(SETUP_ENVTEST) --bin-dir=$(CACHE_DIR)/kubebuilder --os=$(HOSTOS) --arch=$(HOSTARCH) use $(KUBEBUILDER_ASSETS_VERSION)
60+
@$(OK) installing kubebuilder assets for Kubernetes $(KUBEBUILDER_ASSETS_VERSION)
61+
62+
# ====================================================================================
63+
# Kubebuilder Targets
64+
65+
$(eval $(call common.target,kubebuilder.manifests))
66+
# Generate manifests e.g. CRD, RBAC etc.
67+
.do.kubebuilder.manifests: $(CONTROLLER_GEN)
68+
@$(INFO) Generating Kubernetes \(CRDs, RBAC, WebhookConfig, etc.\) manifests
69+
@$(CONTROLLER_GEN) \
70+
$(CONTROLLER_GEN_CRD_OPTIONS) \
71+
$(CONTROLLER_GEN_RBAC_OPTIONS) \
72+
$(CONTROLLER_CONTROLLER_GEN_WEBHOOK_OPTIONS) \
73+
$(CONTROLLER_GEN_PATHS)
74+
@$(OK) Generating Kubernetes \(CRDs, RBAC, WebhookConfig, etc.\) manifests
75+
.PHONY: .do.kubebuilder.manifests
76+
.kubebuilder.manifests.run: .do.kubebuilder.manifests
77+
78+
$(eval $(call common.target,kubebuilder.code))
79+
# Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
80+
.do.kubebuilder.code: $(CONTROLLER_GEN)
81+
@$(INFO) Generating DeepCopy, DeepCopyInto, and DeepCopyObject code
82+
@$(CONTROLLER_GEN) \
83+
$(CONTROLLER_GEN_OBJECT_OPTIONS) \
84+
$(CONTROLLER_GEN_PATHS)
85+
@$(OK) Generating DeepCopy, DeepCopyInto, and DeepCopyObject code
86+
.PHONY: .do.kubebuilder.code
87+
.kubebuilder.code.run: .do.kubebuilder.code
88+
89+
# ====================================================================================
90+
# Common Targets
91+
92+
.test.init: |$(KUBEBUILDER_ASSETS)
93+
go.test.unit: |$(KUBEBUILDER_ASSETS)
94+
go.generate: kubebuilder.code
95+
.generate.init: .kubebuilder.manifests.init
96+
.generate.run: .kubebuilder.manifests.run
97+
.generate.done: .kubebuilder.manifests.done
98+
99+
# ====================================================================================
100+
# Special Targets
101+
102+
define KUBEBULDERV3_HELPTEXT
103+
Kubebuilder Targets:
104+
kubebuilder.manifests Generates Kubernetes custom resources manifests (e.g. CRDs RBACs, ...)
105+
kubebuilder.code Generates DeepCopy, DeepCopyInto, and DeepCopyObject code
106+
107+
endef
108+
export KUBEBULDERV3_HELPTEXT
109+
110+
.kubebuilder.help:
111+
@echo "$$KUBEBULDERV3_HELPTEXT"
112+
113+
.help: .kubebuilder.help
114+
.PHONY: .kubebuilder.help
115+
116+
endif # __KUBEBUILDERV3_MAKEFILE__

0 commit comments

Comments
 (0)