11
22# Image URL to use all building/pushing image targets
33IMG ?= controller:latest
4+ REGISTRY ?= localhost
45# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
56ENVTEST_K8S_VERSION = 1.25.0
7+ OS ?= $(shell go env GOOS )
8+ ARCH ?= $(shell go env GOARCH )
69
710# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
811ifeq (,$(shell go env GOBIN) )
@@ -19,6 +22,9 @@ SHELL = /usr/bin/env bash -o pipefail
1922.PHONY : all
2023all : build
2124
25+ # kcp specific
26+ APIEXPORT_PREFIX ?= today
27+
2228# #@ General
2329
2430# The help target prints out all targets with their descriptions organized
@@ -46,6 +52,10 @@ manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and Cust
4652generate : controller-gen # # Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
4753 $(CONTROLLER_GEN ) object:headerFile=" hack/boilerplate.go.txt" paths=" ./..."
4854
55+ .PHONY : apiresourceschemas
56+ apiresourceschemas : $(KUSTOMIZE ) # # Convert CRDs from config/crds to APIResourceSchemas. Specify APIEXPORT_PREFIX as needed.
57+ $(KUSTOMIZE ) build config/crd | kubectl kcp crd snapshot -f - --prefix $(APIEXPORT_PREFIX ) > config/kcp/$(APIEXPORT_PREFIX ) .apiresourceschemas.yaml
58+
4959.PHONY : fmt
5060fmt : # # Run go fmt against code.
5161 go fmt ./...
@@ -58,6 +68,62 @@ vet: ## Run go vet against code.
5868test : manifests generate fmt vet envtest # # Run tests.
5969 KUBEBUILDER_ASSETS=" $( shell $( ENVTEST) use $( ENVTEST_K8S_VERSION) --bin-dir $( LOCALBIN) -p path) " go test ./... -coverprofile cover.out
6070
71+ ARTIFACT_DIR ?= .test
72+
73+ .PHONY : test-e2e
74+ test-e2e : $(ARTIFACT_DIR ) /kind.kubeconfig kcp-synctarget run-test-e2e# # Set up prerequisites and run end-to-end tests on a cluster.
75+
76+ .PHONY : run-test-e2e
77+ run-test-e2e : # # Run end-to-end tests on a cluster.
78+ go test ./test/e2e/... --kubeconfig $(abspath $(ARTIFACT_DIR ) /kcp.kubeconfig) --workspace $(shell $(KCP_KUBECTL ) kcp workspace . --short)
79+
80+ .PHONY : kind-image
81+ kind-image : docker-build # # Load the controller-manager image into the kind cluster.
82+ kind load docker-image $(REGISTRY ) /$(IMG ) --name catalog
83+
84+ $(ARTIFACT_DIR ) /kind.kubeconfig : $(ARTIFACT_DIR ) # # Run a kind cluster and generate a $KUBECONFIG for it.
85+ @if ! kind get clusters --quiet | grep --quiet catalog; then kind create cluster --name catalog; fi
86+ kind get kubeconfig --name catalog > $(ARTIFACT_DIR ) /kind.kubeconfig
87+
88+ $(ARTIFACT_DIR ) : # # Create a directory for test artifacts.
89+ mkdir -p $(ARTIFACT_DIR )
90+
91+ KCP_KUBECTL ?= PATH=$(LOCALBIN ) :$(PATH ) KUBECONFIG=$(ARTIFACT_DIR ) /kcp.kubeconfig kubectl
92+ KIND_KUBECTL ?= kubectl --kubeconfig $(ARTIFACT_DIR ) /kind.kubeconfig
93+
94+ .PHONY : kcp-synctarget
95+ kcp-synctarget : kcp-workspace $(ARTIFACT_DIR ) /syncer.yaml $(YQ ) # # Add the kind cluster to kcp as a target for workloads.
96+ $(KIND_KUBECTL ) apply -f $(ARTIFACT_DIR ) /syncer.yaml
97+ $(eval DEPLOYMENT_NAME = $(shell $(YQ ) 'select(.kind=="Deployment") | .metadata.name' < $(ARTIFACT_DIR ) /syncer.yaml ) )
98+ $(eval DEPLOYMENT_NAMESPACE = $(shell $(YQ ) 'select(.kind=="Deployment") | .metadata.namespace' < $(ARTIFACT_DIR ) /syncer.yaml ) )
99+ $(KIND_KUBECTL ) --namespace $(DEPLOYMENT_NAMESPACE ) rollout status deployment/$(DEPLOYMENT_NAME )
100+ @if [[ ! -s $( ARTIFACT_DIR) /syncer.log ]]; then ( $( KIND_KUBECTL) --namespace $( DEPLOYMENT_NAMESPACE) logs deployment/$( DEPLOYMENT_NAME) -f > $( ARTIFACT_DIR) /syncer.log 2>&1 & ); fi
101+ $(KCP_KUBECTL ) wait --for=condition=Ready synctarget/catalog
102+
103+ $(ARTIFACT_DIR ) /syncer.yaml : # # Generate the manifests necessary to register the kind cluster with kcp.
104+ $(KCP_KUBECTL ) kcp workload sync catalog --resources services --syncer-image ghcr.io/kcp-dev/kcp/syncer:v$(KCP_VERSION ) --output-file $(ARTIFACT_DIR ) /syncer.yaml
105+
106+ .PHONY : kcp-workspace
107+ kcp-workspace : $(KUBECTL_KCP ) kcp-server # # Create a workspace in kcp for the controller-manager.
108+ $(KCP_KUBECTL ) kcp workspace use ' ~'
109+ @if ! $(KCP_KUBECTL ) kcp workspace use catalog; then $(KCP_KUBECTL ) kcp workspace create catalog --type universal --enter; fi
110+
111+ .PHONY : kcp-server
112+ kcp-server : $(KCP ) $(ARTIFACT_DIR ) /kcp # # Run the kcp server.
113+ @if [[ ! -s $( ARTIFACT_DIR) /kcp.log ]]; then ( $( KCP) start -v 5 --root-directory $( ARTIFACT_DIR) /kcp --kubeconfig-path $( ARTIFACT_DIR) /kcp.kubeconfig --audit-log-maxsize 1024 --audit-log-mode=batch --audit-log-batch-max-wait=1s --audit-log-batch-max-size=1000 --audit-log-batch-buffer-size=10000 --audit-log-batch-throttle-burst=15 --audit-log-batch-throttle-enable=true --audit-log-batch-throttle-qps=10 --audit-policy-file ./test/e2e/audit-policy.yaml --audit-log-path $( ARTIFACT_DIR) /audit.log > $( ARTIFACT_DIR) /kcp.log 2>&1 & ); fi
114+ @while true ; do if [[ ! -s $( ARTIFACT_DIR) /kcp.kubeconfig ]]; then sleep 0.2; else break ; fi ; done
115+ @while true ; do if ! kubectl --kubeconfig $( ARTIFACT_DIR) /kcp.kubeconfig get --raw /readyz > $( ARTIFACT_DIR) /kcp.probe.log 2>&1 ; then sleep 0.2; else break ; fi ; done
116+
117+ $(ARTIFACT_DIR ) /kcp : # # Create a directory for the kcp server data.
118+ mkdir -p $(ARTIFACT_DIR ) /kcp
119+
120+ .PHONY : test-e2e-cleanup
121+ test-e2e-cleanup : # # Clean up processes and directories from an end-to-end test run.
122+ kind delete cluster --name catalog || true
123+ rm -rf $(ARTIFACT_DIR ) || true
124+ pkill -sigterm kcp || true
125+ pkill -sigterm kubectl || true
126+
61127# #@ Build
62128
63129.PHONY : build
@@ -102,6 +168,8 @@ ifndef ignore-not-found
102168 ignore-not-found = false
103169endif
104170
171+ KUBECONFIG ?= $(abspath ~/.kube/config )
172+
105173.PHONY : install
106174install : manifests kustomize # # Install CRDs into the K8s cluster specified in ~/.kube/config.
107175 $(KUSTOMIZE ) build config/crd | kubectl apply -f -
@@ -110,6 +178,11 @@ install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~
110178uninstall : 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.
111179 $(KUSTOMIZE ) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found ) -f -
112180
181+ .PHONY : deploy-crd
182+ deploy-crd : manifests $(KUSTOMIZE ) # # Deploy controller
183+ cd config/manager && $(KUSTOMIZE ) edit set image controller=${REGISTRY} /${IMG}
184+ $(KUSTOMIZE ) build config/default-crd | kubectl --kubeconfig $(KUBECONFIG ) apply -f - || true
185+
113186.PHONY : deploy
114187deploy : manifests kustomize # # Deploy controller to the K8s cluster specified in ~/.kube/config.
115188 cd config/manager && $(KUSTOMIZE ) edit set image controller=${IMG}
@@ -130,10 +203,15 @@ $(LOCALBIN):
130203KUSTOMIZE ?= $(LOCALBIN ) /kustomize
131204CONTROLLER_GEN ?= $(LOCALBIN ) /controller-gen
132205ENVTEST ?= $(LOCALBIN ) /setup-envtest
206+ KCP ?= $(LOCALBIN ) /kcp
207+ KUBECTL_KCP ?= $(LOCALBIN ) /kubectl-kcp
208+ YQ ?= $(LOCALBIN ) /yq
133209
134210# # Tool Versions
135211KUSTOMIZE_VERSION ?= v3.8.7
136212CONTROLLER_TOOLS_VERSION ?= v0.10.0
213+ KCP_VERSION ?= 0.9.1
214+ YQ_VERSION ?= v4.27.2
137215
138216KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
139217.PHONY : kustomize
@@ -150,3 +228,19 @@ $(CONTROLLER_GEN): $(LOCALBIN)
150228envtest : $(ENVTEST ) # # Download envtest-setup locally if necessary.
151229$(ENVTEST ) : $(LOCALBIN )
152230 test -s $(LOCALBIN ) /setup-envtest || GOBIN=$(LOCALBIN ) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
231+
232+ $(YQ ) : # # Download yq locally if necessary.
233+ mkdir -p $(LOCALBIN )
234+ GOBIN=$(LOCALBIN ) go install github.com/mikefarah/yq/v4@$(YQ_VERSION )
235+
236+ .PHONY : kcp
237+ $(KCP ) : # # Download kcp locally if necessary.
238+ mkdir -p $(LOCALBIN )
239+ curl -L -s -o - https://github.com/kcp-dev/kcp/releases/download/v$(KCP_VERSION ) /kcp_$(KCP_VERSION ) _$(OS ) _$(ARCH ) .tar.gz | tar --directory $(LOCALBIN ) /../ -xvzf - bin/kcp
240+ touch $(KCP ) # we download an "old" file, so make will re-download to refresh it unless we make it newer than the owning dir
241+
242+ $(KUBECTL_KCP ) : # # Download kcp kubectl plugins locally if necessary.
243+ mkdir -p $(LOCALBIN )
244+ curl -L -s -o - https://github.com/kcp-dev/kcp/releases/download/v$(KCP_VERSION ) /kubectl-kcp-plugin_$(KCP_VERSION ) _$(OS ) _$(ARCH ) .tar.gz | tar --directory $(LOCALBIN ) /../ -xvzf - bin
245+ touch $(KUBECTL_KCP ) # we download an "old" file, so make will re-download to refresh it unless we make it newer than the owning dir
246+
0 commit comments