@@ -14,133 +14,79 @@ DOCKER_IMAGE_NAME ?= cert-manager/sample-external-issuer/controller
1414# Image URL to use all building/pushing image targets
1515IMG ?= ${DOCKER_REGISTRY}/${DOCKER_IMAGE_NAME}:${VERSION}
1616
17- # BIN is the directory where tools will be installed
18- export BIN ?= ${CURDIR}/bin
19-
20- OS := $(shell go env GOOS)
21- ARCH := $(shell go env GOARCH)
22-
23- # Kind
24- KIND_VERSION := 0.18.0
25- KIND := ${BIN}/kind-${KIND_VERSION}
26- K8S_CLUSTER_NAME := sample-external-issuer-e2e
2717
2818# cert-manager
2919CERT_MANAGER_VERSION ?= 1.11.1
3020
31- # Controller tools
32- CONTROLLER_GEN_VERSION := 0.11.3
33- CONTROLLER_GEN := ${BIN}/controller-gen
3421
3522INSTALL_YAML ?= build/install.yaml
3623
3724.PHONY : all
38- all : manager
25+ all : build
3926
40- # Run tests
41- .PHONY : test
42- test : generate fmt vet manifests
43- go test ./... -coverprofile cover.out
27+ # #@ General
4428
45- # Build manager binary
46- .PHONY : manager
47- manager : generate fmt vet
48- go build -o bin/manager main.go
29+ # The help target prints out all targets with their descriptions organized
30+ # beneath their categories. The categories are represented by '##@' and the
31+ # target descriptions by '##'. The awk commands is responsible for reading the
32+ # entire set of makefiles included in this invocation, looking for lines of the
33+ # file as xyz: ## something, and then pretty-format the target and help. Then,
34+ # if there's a line with ##@ something, that gets pretty-printed as a category.
35+ # More info on the usage of ANSI control characters for terminal formatting:
36+ # https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
37+ # More info on the awk command:
38+ # http://linuxcommand.org/lc3_adv_awk.php
4939
50- # Run against the configured Kubernetes cluster in ~/.kube/config
51- .PHONY : run
52- run : generate fmt vet manifests
53- go run ./main.go
54-
55- # Install CRDs into a cluster
56- .PHONY : install
57- install : manifests
58- kustomize build config/crd | kubectl apply -f -
59-
60- # Uninstall CRDs from a cluster
61- .PHONY : uninstall
62- uninstall : manifests
63- kustomize build config/crd | kubectl delete -f -
64-
65- # TODO(wallrj): .PHONY ensures that the install file is always regenerated,
66- # because I this really depends on the checksum of the Docker image and all the
67- # base Kustomize files.
68- .PHONY : ${INSTALL_YAML}
69- ${INSTALL_YAML} :
70- mkdir -p $(dir $@ )
71- rm -rf build/kustomize
72- mkdir -p build/kustomize
73- cd build/kustomize
74- kustomize create --resources ../../config/default
75- kustomize edit set image controller=${IMG}
76- cd ${CURDIR}
77- kustomize build build/kustomize > $@
40+ .PHONY : help
41+ help : # # Display this help.
42+ @awk ' BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST )
7843
79- # Deploy controller in the configured Kubernetes cluster in ~/.kube/config
80- .PHONY : deploy
81- deploy : ${INSTALL_YAML}
82- kubectl apply -f ${INSTALL_YAML}
44+ # #@ Development
8345
84- # Generate manifests e.g. CRD, RBAC etc.
8546.PHONY : manifests
86- manifests : ${CONTROLLER_GEN}
87- $(CONTROLLER_GEN ) rbac:roleName=manager-role webhook crd paths=" ./..." output:crd:artifacts:config=config/crd/bases
47+ manifests : controller-gen # # Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
48+ $(CONTROLLER_GEN ) rbac:roleName=manager-role crd webhook paths=" ./..." output:crd:artifacts:config=config/crd/bases
49+
50+ .PHONY : generate
51+ generate : controller-gen # # Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
52+ $(CONTROLLER_GEN ) object:headerFile=" hack/boilerplate.go.txt" paths=" ./..."
8853
89- # Run go fmt against code
9054.PHONY : fmt
91- fmt :
55+ fmt : # # Run go fmt against code.
9256 go fmt ./...
9357
94- # Run go vet against code
9558.PHONY : vet
96- vet :
59+ vet : # # Run go vet against code.
9760 go vet ./...
9861
99- # Generate code
100- generate : ${CONTROLLER_GEN}
101- $(CONTROLLER_GEN ) object:headerFile=" hack/boilerplate.go.txt" paths=" ./..."
102-
103- # Build the docker image
104- .PHONY : docker-build
105- docker-build :
106- docker build \
107- --build-arg VERSION=$(VERSION ) \
108- --tag ${IMG} \
109- --file Dockerfile \
110- ${CURDIR}
62+ .PHONY : test
63+ test : manifests generate fmt vet # # Run tests
64+ go test ./... -coverprofile cover.out
11165
112- # Push the docker image
113- .PHONY : docker-push
114- docker-push :
115- docker push ${IMG}
66+ # #@ E2E testing
11667
117- ${CONTROLLER_GEN} : | ${BIN}
118- GOBIN=${BIN} go install sigs.k8s.io/controller-tools/cmd/controller-gen@v${CONTROLLER_GEN_VERSION}
68+ K8S_CLUSTER_NAME := sample-external-issuer-e2e
11969
120- # ==================================
121- # E2E testing
122- # ==================================
12370.PHONY : kind-cluster
12471kind-cluster : # # Use Kind to create a Kubernetes cluster for E2E tests
125- kind-cluster : ${KIND}
72+ kind-cluster : kind
12673 ${KIND} get clusters | grep ${K8S_CLUSTER_NAME} || ${KIND} create cluster --name ${K8S_CLUSTER_NAME}
12774
12875.PHONY : kind-load
129- kind-load : # # Load all the Docker images into Kind
76+ kind-load : kind # # Load all the Docker images into Kind
13077 ${KIND} load docker-image --name ${K8S_CLUSTER_NAME} ${IMG}
13178
13279.PHONY : kind-export-logs
133- kind-export-logs :
80+ kind-export-logs : kind # # Export Kind logs
13481 ${KIND} export logs --name ${K8S_CLUSTER_NAME} ${E2E_ARTIFACTS_DIRECTORY}
13582
136-
13783.PHONY : deploy-cert-manager
13884deploy-cert-manager : # # Deploy cert-manager in the configured Kubernetes cluster in ~/.kube/config
13985 kubectl apply --filename=https://github.com/cert-manager/cert-manager/releases/download/v${CERT_MANAGER_VERSION} /cert-manager.yaml
14086 kubectl wait --for=condition=Available --timeout=300s apiservice v1.cert-manager.io
14187
14288.PHONY : e2e
143- e2e :
89+ e2e : # # Run E2E tests
14490 kubectl apply --filename config/samples
14591
14692 kubectl wait --for=condition=Ready --timeout=5s issuers.sample-issuer.example.com issuer-sample
@@ -153,12 +99,128 @@ e2e:
15399
154100 kubectl delete --filename config/samples
155101
156- # ==================================
157- # Download: tools in ${BIN}
158- # ==================================
159- ${BIN} :
160- mkdir -p ${BIN}
102+ # #@ Build
103+
104+ .PHONY : build
105+ build : manifests generate fmt vet # # Build manager binary
106+ go build -o bin/manager main.go
107+
108+ .PHONY : run
109+ run : manifests generate fmt vet # # Run a controller from your host.
110+ go run ./main.go
111+
112+ # If you wish built the manager image targeting other platforms you can use the --platform flag.
113+ # (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
114+ # More info: https://docs.docker.com/develop/develop-images/build_enhancements/
115+ .PHONY : docker-build
116+ docker-build : # # Build docker image with the manager.
117+ docker build \
118+ --build-arg VERSION=$(VERSION ) \
119+ --tag ${IMG} \
120+ --file Dockerfile \
121+ ${CURDIR}
122+
123+ # Push the docker image
124+ .PHONY : docker-push
125+ docker-push : # # Push docker image with the manager.
126+ docker push ${IMG}
127+
128+ # PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
129+ # architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
130+ # - able to use docker buildx . More info: https://docs.docker.com/build/buildx/
131+ # - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/
132+ # - be able to push the image for your registry (i.e. if you do not inform a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
133+ # To properly provided solutions that supports more than one platform you should use this option.
134+ PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
135+ .PHONY : docker-buildx
136+ docker-buildx : test # # Build and push docker image for the manager for cross-platform support
137+ # copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
138+ sed -e ' 1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
139+ - docker buildx create --name project-v3-builder
140+ docker buildx use project-v3-builder
141+ - docker buildx build --push --platform=$(PLATFORMS ) --tag ${IMG} -f Dockerfile.cross .
142+ - docker buildx rm project-v3-builder
143+ rm Dockerfile.cross
144+
145+ # #@ Deployment
146+
147+ ifndef ignore-not-found
148+ ignore-not-found = false
149+ endif
150+
151+ .PHONY : install
152+ install : manifests kustomize # # Install CRDs into the K8s cluster specified in ~/.kube/config.
153+ $(KUSTOMIZE ) build config/crd | kubectl apply -f -
154+
155+ .PHONY : uninstall
156+ uninstall : manifests kustomize # # Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
157+ $(KUSTOMIZE ) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found ) -f -
158+
159+ # TODO(wallrj): .PHONY ensures that the install file is always regenerated,
160+ # because I this really depends on the checksum of the Docker image and all the
161+ # base Kustomize files.
162+ .PHONY : ${INSTALL_YAML}
163+ ${INSTALL_YAML} : manifests kustomize
164+ mkdir -p $(dir $@ )
165+ rm -rf build/kustomize
166+ mkdir -p build/kustomize
167+ cd build/kustomize
168+ $(KUSTOMIZE ) create --resources ../../config/default
169+ $(KUSTOMIZE ) edit set image controller=${IMG}
170+ cd ${CURDIR}
171+ $(KUSTOMIZE ) build build/kustomize > $@
172+
173+ .PHONY : deploy
174+ deploy : ${INSTALL_YAML} # # Deploy controller to the K8s cluster specified in ~/.kube/config.
175+ kubectl apply -f ${INSTALL_YAML}
176+
177+ .PHONY : undeploy
178+ undeploy : ${INSTALL_YAML} # # Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
179+ kubectl delete -f ${INSTALL_YAML} --ignore-not-found=$(ignore-not-found )
180+
181+ # #@ Build Dependencies
182+
183+ LOCAL_OS := $(shell go env GOOS)
184+ LOCAL_ARCH := $(shell go env GOARCH)
185+
186+ # # Location to install dependencies to
187+ LOCALBIN ?= $(shell pwd) /bin
188+ $(LOCALBIN ) :
189+ mkdir -p $(LOCALBIN )
190+
191+ # # Tool Binaries
192+ KUSTOMIZE ?= $(LOCALBIN ) /kustomize
193+ CONTROLLER_GEN ?= $(LOCALBIN ) /controller-gen
194+ ENVTEST ?= $(LOCALBIN ) /setup-envtest
195+ KIND ?= $(LOCALBIN ) /kind
196+
197+ # # Tool Versions
198+ KUSTOMIZE_VERSION ?= v3.8.7
199+ CONTROLLER_TOOLS_VERSION ?= v0.11.3
200+ KIND_VERSION := 0.18.0
161201
162- ${KIND} : ${BIN}
163- curl -fsSL -o ${KIND} https://github.com/kubernetes-sigs/kind/releases/download/v${KIND_VERSION} /kind-${OS} -${ARCH}
202+ KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
203+ .PHONY : kustomize
204+ kustomize : $(KUSTOMIZE ) # # Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
205+ $(KUSTOMIZE ) : $(LOCALBIN )
206+ @if test -x $(LOCALBIN ) /kustomize && ! $(LOCALBIN ) /kustomize version | grep -q $(KUSTOMIZE_VERSION ) ; then \
207+ echo " $( LOCALBIN) /kustomize version is not expected $( KUSTOMIZE_VERSION) . Removing it before installing." ; \
208+ rm -rf $(LOCALBIN ) /kustomize; \
209+ fi
210+ test -s $(LOCALBIN ) /kustomize || { curl -Ss $( KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $( subst v,,$( KUSTOMIZE_VERSION) ) $( LOCALBIN) ; }
211+
212+ .PHONY : controller-gen
213+ controller-gen : $(CONTROLLER_GEN ) # # Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
214+ $(CONTROLLER_GEN ) : $(LOCALBIN )
215+ test -s $(LOCALBIN ) /controller-gen && $(LOCALBIN ) /controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION ) || \
216+ GOBIN=$(LOCALBIN ) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION )
217+
218+ .PHONY : envtest
219+ envtest : $(ENVTEST ) # # Download envtest-setup locally if necessary.
220+ $(ENVTEST ) : $(LOCALBIN )
221+ test -s $(LOCALBIN ) /setup-envtest || GOBIN=$(LOCALBIN ) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
222+
223+ .PHONY : kind
224+ kind : $(LOCALBIN ) # # Download Kind locally if necessary.
225+ curl -fsSL -o ${KIND} https://github.com/kubernetes-sigs/kind/releases/download/v${KIND_VERSION} /kind-${LOCAL_OS} -${LOCAL_ARCH}
164226 chmod +x ${KIND}
0 commit comments