From 50a400d25b09142b5f188078924a08d2a81e4d72 Mon Sep 17 00:00:00 2001 From: Josh Robson Chase Date: Mon, 3 Jul 2023 16:57:43 -0400 Subject: [PATCH 1/4] update development docs for kind --- docs/developer-guide/README.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/developer-guide/README.md b/docs/developer-guide/README.md index abc74702..33da98f4 100644 --- a/docs/developer-guide/README.md +++ b/docs/developer-guide/README.md @@ -11,10 +11,13 @@ Have a look at the architecture guide on the internal workings of the ingress co - [Go 1.20](https://go.dev/dl/) - [Helm](https://helm.sh/docs/intro/install/) -Both of these can be obtained via [nix-direnv](https://github.com/nix-community/nix-direnv), which will automatically configure your shell for you. +Both of these can be obtained via [nix-direnv](https://github.com/nix-community/nix-direnv), which will automatically configure your shell for you. Note that it will also set `KUBECONFIG` to a file "owned" by direnv, so as not to pollute the configuration in your `$HOME`. -- A k8s cluster is available via your kubectl client. This can be a remote cluster or a local cluster like [minikube](https://minikube.sigs.k8s.io/docs/start/) - - NOTE: Depending on your cluster, you may have to take additional steps to make the image available. For example with minikube, you may need to run `eval $(minikube docker-env)` in each terminal session to make the image from `make deploy` available to the cluster. +A k8s cluster is available via your kubectl client. This can be a remote cluster or a local cluster like [kind](https://kind.sigs.k8s.io/) or [minikube](https://minikube.sigs.k8s.io/docs/start/). + +Depending on your cluster, you may have to take additional steps to make the image available. For example with minikube, you may need to run `eval $(minikube docker-env)` in each terminal session to make the image from `make deploy` available to the cluster. + +Support for `kind` is provided out of the box. `scripts/kind-up.sh` will create a kind cluster and an accompanying docker registry, and `scripts/kind-down.sh` will tear both down. The `docker-push` and `deploy` tasks in the provided `Makefile` are configured to use this registry. ### Setup From 62449fa2566b8e9dfd5da8afe7105edc7f788513 Mon Sep 17 00:00:00 2001 From: Josh Robson Chase Date: Mon, 3 Jul 2023 13:13:00 -0400 Subject: [PATCH 2/4] add support for kind clusters --- .envrc | 1 + Makefile | 10 ++++--- flake.nix | 2 ++ scripts/kind-down.sh | 4 +++ scripts/kind-up.sh | 64 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 4 deletions(-) create mode 100755 scripts/kind-down.sh create mode 100755 scripts/kind-up.sh diff --git a/.envrc b/.envrc index 94c685d2..3e646636 100644 --- a/.envrc +++ b/.envrc @@ -1,3 +1,4 @@ layout go dotenv_if_exists +export KUBECONFIG=$(direnv_layout_dir)/kubeconfig use flake diff --git a/Makefile b/Makefile index 0113259b..ae8ad170 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,4 @@ +REGISTRY ?= localhost:5001 # Image URL to use all building/pushing image targets IMG ?= kubernetes-ingress-controller @@ -94,11 +95,11 @@ run: manifests generate fmt vet ## Run a controller from your host. .PHONY: docker-build docker-build: test ## Build docker image with the manager. - DOCKER_BUILDKIT=1 docker build -t ${IMG} . + DOCKER_BUILDKIT=1 docker build -t ${REGISTRY}/${IMG} . .PHONY: docker-push -docker-push: ## Push docker image with the manager. - docker push ${IMG} +docker-push: docker-build ## Push docker image with the manager. + docker push ${REGISTRY}/${IMG} ##@ Deployment @@ -115,10 +116,11 @@ uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified $(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f - .PHONY: deploy -deploy: _deploy-check-env-vars docker-build manifests kustomize _helm_setup ## Deploy controller to the K8s cluster specified in ~/.kube/config. +deploy: _deploy-check-env-vars docker-push manifests kustomize _helm_setup ## Deploy controller to the K8s cluster specified in ~/.kube/config. helm upgrade ngrok-ingress-controller $(HELM_CHART_DIR) --install \ --namespace ngrok-ingress-controller \ --create-namespace \ + --set image.registry=$(REGISTRY) \ --set image.repository=$(IMG) \ --set image.tag="latest" \ --set podAnnotations."k8s\.ngrok\.com/test"="\{\"env\": \"local\"\}" \ diff --git a/flake.nix b/flake.nix index c97e47bd..f258295d 100644 --- a/flake.nix +++ b/flake.nix @@ -15,6 +15,8 @@ gotools go-tools golangci-lint + kind + kubectl kubernetes-helm kubebuilder jq diff --git a/scripts/kind-down.sh b/scripts/kind-down.sh new file mode 100755 index 00000000..2e759342 --- /dev/null +++ b/scripts/kind-down.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +kind delete cluster +docker stop kind-registry | xargs docker rm \ No newline at end of file diff --git a/scripts/kind-up.sh b/scripts/kind-up.sh new file mode 100755 index 00000000..b1efe1f0 --- /dev/null +++ b/scripts/kind-up.sh @@ -0,0 +1,64 @@ +#!/usr/bin/env bash +set -o errexit + +# 1. Create registry container unless it already exists +reg_name='kind-registry' +reg_port='5001' +if [ "$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)" != 'true' ]; then + docker run \ + -d --restart=always -p "127.0.0.1:${reg_port}:5000" --name "${reg_name}" \ + registry:2 +fi + +# 2. Create kind cluster with containerd registry config dir enabled +# TODO: kind will eventually enable this by default and this patch will +# be unnecessary. +# +# See: +# https://github.com/kubernetes-sigs/kind/issues/2875 +# https://github.com/containerd/containerd/blob/main/docs/cri/config.md#registry-configuration +# See: https://github.com/containerd/containerd/blob/main/docs/hosts.md +cat < Date: Mon, 3 Jul 2023 16:13:49 -0400 Subject: [PATCH 3/4] set image pull policy to Always for development --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index ae8ad170..ff444f1d 100644 --- a/Makefile +++ b/Makefile @@ -123,6 +123,7 @@ deploy: _deploy-check-env-vars docker-push manifests kustomize _helm_setup ## De --set image.registry=$(REGISTRY) \ --set image.repository=$(IMG) \ --set image.tag="latest" \ + --set image.pullPolicy="Always" \ --set podAnnotations."k8s\.ngrok\.com/test"="\{\"env\": \"local\"\}" \ --set credentials.apiKey=$(NGROK_API_KEY) \ --set credentials.authtoken=$(NGROK_AUTHTOKEN) \ From 476ea9c15f4b282b32beee9a06b01a7b0cd616a1 Mon Sep 17 00:00:00 2001 From: Josh Robson Chase Date: Mon, 3 Jul 2023 16:48:27 -0400 Subject: [PATCH 4/4] namespace ngrok kind cluster --- scripts/kind-down.sh | 2 +- scripts/kind-up.sh | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/scripts/kind-down.sh b/scripts/kind-down.sh index 2e759342..3b0519c0 100755 --- a/scripts/kind-down.sh +++ b/scripts/kind-down.sh @@ -1,4 +1,4 @@ #!/usr/bin/env bash -kind delete cluster +kind delete cluster --name ngrok-ingress-controller docker stop kind-registry | xargs docker rm \ No newline at end of file diff --git a/scripts/kind-up.sh b/scripts/kind-up.sh index b1efe1f0..99061001 100755 --- a/scripts/kind-up.sh +++ b/scripts/kind-up.sh @@ -1,6 +1,8 @@ #!/usr/bin/env bash set -o errexit +cluster_name=ngrok-ingress-controller + # 1. Create registry container unless it already exists reg_name='kind-registry' reg_port='5001' @@ -18,7 +20,7 @@ fi # https://github.com/kubernetes-sigs/kind/issues/2875 # https://github.com/containerd/containerd/blob/main/docs/cri/config.md#registry-configuration # See: https://github.com/containerd/containerd/blob/main/docs/hosts.md -cat <