Skip to content
This repository has been archived by the owner on Jun 22, 2023. It is now read-only.

feat(validation): Add catalog entry validation in controller #5

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 81 additions & 71 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,60 @@

# Image URL to use all building/pushing image targets
IMG ?= controller:latest
REGISTRY ?= localhost
IMG ?= controller:latest
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.25.0

##@ Build Dependencies

## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin

## Tool Binaries
KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
KCP ?= $(LOCALBIN)/kcp
KUBECTL_KCP ?= $(LOCALBIN)/kubectl-kcp
YQ ?= $(LOCALBIN)/yq

## Tool Versions
KUSTOMIZE_VERSION ?= v4.5.7
CONTROLLER_TOOLS_VERSION ?= v0.10.0
KCP_VERSION ?= 0.9.1
YQ_VERSION ?= v4.27.2

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
$(KUSTOMIZE): ## Download kustomize locally if necessary.
mkdir -p $(LOCALBIN)
curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN)
touch $(KUSTOMIZE) # we download an "old" file, so make will re-download to refresh it unless we make it newer than the owning dir

$(CONTROLLER_GEN): ## Download controller-gen locally if necessary.
mkdir -p $(LOCALBIN)
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)

$(YQ): ## Download yq locally if necessary.
mkdir -p $(LOCALBIN)
GOBIN=$(LOCALBIN) go install github.com/mikefarah/yq/v4@$(YQ_VERSION)

OS ?= $(shell go env GOOS )
ARCH ?= $(shell go env GOARCH )

$(KCP): ## Download kcp locally if necessary.
mkdir -p $(LOCALBIN)
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
touch $(KCP) # we download an "old" file, so make will re-download to refresh it unless we make it newer than the owning dir

$(KUBECTL_KCP): ## Download kcp kubectl plugins locally if necessary.
mkdir -p $(LOCALBIN)
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
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

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

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
Expand All @@ -23,7 +71,7 @@ SHELL = /usr/bin/env bash -o pipefail
all: build

# kcp specific
APIEXPORT_PREFIX ?= catalog
APIEXPORT_PREFIX ?= today

##@ General

Expand All @@ -45,11 +93,11 @@ help: ## Display this help.
##@ Development

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

.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
generate: $(CONTROLLER_GEN) ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

.PHONY: apiresourceschemas
Expand All @@ -65,18 +113,29 @@ vet: ## Run go vet against code.
go vet ./...

.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
test: manifests generate fmt vet $(ENVTEST) ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./controllers/... -coverprofile cover.out

ARTIFACT_DIR ?= .test

.PHONY: test-e2e
test-e2e: $(ARTIFACT_DIR)/kind.kubeconfig kcp-synctarget run-test-e2e## Set up prerequisites and run end-to-end tests on a cluster.
test-e2e: $(ARTIFACT_DIR)/kind.kubeconfig kcp-synctarget ready-deployment run-test-e2e## Set up prerequisites and run end-to-end tests on a cluster.

.PHONY: run-test-e2e
run-test-e2e: ## Run end-to-end tests on a cluster.
go test ./test/e2e/... --kubeconfig $(abspath $(ARTIFACT_DIR)/kcp.kubeconfig) --workspace $(shell $(KCP_KUBECTL) kcp workspace . --short)

.PHONY: ready-deployment
ready-deployment: KUBECONFIG = $(ARTIFACT_DIR)/kcp.kubeconfig
ready-deployment: kind-image install deploy apibinding ## Deploy the controller-manager and wait for it to be ready.
$(KCP_KUBECTL) --namespace "catalog-system" rollout status deployment/catalog-controller-manager

.PHONY: apibinding
apibinding:
$( eval WORKSPACE = $(shell $(KCP_KUBECTL) kcp workspace . --short))
sed 's/WORKSPACE/$(WORKSPACE)/' ./test/e2e/apibinding.yaml | $(KCP_KUBECTL) apply -f -
$(KCP_KUBECTL) wait --for=condition=Ready apibinding/catalog.kcp.dev

.PHONY: kind-image
kind-image: docker-build ## Load the controller-manager image into the kind cluster.
kind load docker-image $(REGISTRY)/$(IMG) --name catalog
Expand Down Expand Up @@ -130,20 +189,23 @@ test-e2e-cleanup: ## Clean up processes and directories from an end-to-end test
build: manifests generate fmt vet ## Build manager binary.
go build -o bin/manager main.go

NAME_PREFIX ?= catalog-
APIEXPORT_NAME ?= catalog.kcp.dev

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
go run ./main.go
go run ./main.go --api-export-name $(APIEXPORT_NAME)

# If you wish built the manager image targeting other platforms you can use the --platform flag.
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
.PHONY: docker-build
docker-build: test ## Build docker image with the manager.
docker build -t ${IMG} .
docker build -t ${REGISTRY}/${IMG} .

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
docker push ${IMG}
docker push ${REGISTRY}/${IMG}

# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
Expand All @@ -169,78 +231,26 @@ ifndef ignore-not-found
endif

KUBECONFIG ?= $(abspath ~/.kube/config )
KCPKUBECONFIG ?= $(ARTIFACT_DIR)/kcp.kubeconfig

.PHONY: install
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl apply -f -
install: manifests $(KUSTOMIZE) ## Install CRDs into the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/kcp | kubectl --kubeconfig $(KCPKUBECONFIG) apply -f -

.PHONY: uninstall
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.
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
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.
$(KUSTOMIZE) build config/crd | kubectl --kubeconfig $(KCPKUBECONFIG) delete --ignore-not-found=$(ignore-not-found) -f -

.PHONY: deploy-crd
deploy-crd: manifests $(KUSTOMIZE) ## Deploy controller
cd config/manager && $(KUSTOMIZE) edit set image controller=${REGISTRY}/${IMG}
$(KUSTOMIZE) build config/default-crd | kubectl --kubeconfig $(KUBECONFIG) apply -f - || true
$(KUSTOMIZE) build config/default-crd | kubectl --kubeconfig $(KCPKUBECONFIG) apply -f - || true

.PHONY: deploy
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
deploy: manifests $(KUSTOMIZE) ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/default | kubectl apply -f -
$(KUSTOMIZE) build config/default | kubectl --kubeconfig $(KCPKUBECONFIG) apply -f -

.PHONY: undeploy
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -

##@ Build Dependencies

## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)

## Tool Binaries
KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
KCP ?= $(LOCALBIN)/kcp
KUBECTL_KCP ?= $(LOCALBIN)/kubectl-kcp
YQ ?= $(LOCALBIN)/yq

## Tool Versions
KUSTOMIZE_VERSION ?= v3.8.7
CONTROLLER_TOOLS_VERSION ?= v0.10.0
KCP_VERSION ?= 0.9.1
YQ_VERSION ?= v4.27.2

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)
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 || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)

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

$(YQ): ## Download yq locally if necessary.
mkdir -p $(LOCALBIN)
GOBIN=$(LOCALBIN) go install github.com/mikefarah/yq/v4@$(YQ_VERSION)

.PHONY: kcp
$(KCP): ## Download kcp locally if necessary.
mkdir -p $(LOCALBIN)
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
touch $(KCP) # we download an "old" file, so make will re-download to refresh it unless we make it newer than the owning dir

$(KUBECTL_KCP): ## Download kcp kubectl plugins locally if necessary.
mkdir -p $(LOCALBIN)
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
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

$(KUSTOMIZE) build config/default | kubectl --kubeconfig $(KCPKUBECONFIG) delete --ignore-not-found=$(ignore-not-found) -f -
2 changes: 1 addition & 1 deletion config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namePrefix: catalog-
# someName: someValue

bases:
- ../crd
- ../kcp
- ../rbac
- ../manager
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
Expand Down
1 change: 1 addition & 0 deletions config/default/manager_auth_proxy_patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ spec:
memory: 64Mi
- name: manager
args:
- "--api-export-name=catalog.kcp.dev"
- "--health-probe-bind-address=:8081"
- "--metrics-bind-address=127.0.0.1:8080"
- "--leader-elect"
7 changes: 7 additions & 0 deletions config/kcp/catalogentry.apiexport.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: apis.kcp.dev/v1alpha1
kind: APIExport
metadata:
name: catalog.kcp.dev
spec:
latestResourceSchemas:
- today.catalogentries.catalog.kcp.dev
21 changes: 21 additions & 0 deletions config/kcp/clusterrole.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
creationTimestamp: null
name: kcp-manager-role
rules:
- apiGroups:
- apis.kcp.dev
resources:
- apiexports
verbs:
- get
- list
- watch
- apiGroups:
- apis.kcp.dev
resources:
- apiexports/content
verbs:
- '*'
12 changes: 12 additions & 0 deletions config/kcp/clusterrolebinding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kcp-manager-rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kcp-manager-role
subjects:
- kind: ServiceAccount
name: controller-manager
namespace: system
8 changes: 8 additions & 0 deletions config/kcp/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resources:
- today.apiresourceschemas.yaml
- catalogentry.apiexport.yaml
- clusterrole.yaml
- clusterrolebinding.yaml

configurations:
- kustomizeconfig.yaml
5 changes: 5 additions & 0 deletions config/kcp/kustomizeconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
nameReference:
- kind: APIResourceSchema
fieldSpecs:
- kind: APIExport
path: spec/latestResourceSchemas
Loading