From 38eb90d64b859a34e32f8cdf719b301755269997 Mon Sep 17 00:00:00 2001 From: Artem Bortnikov Date: Thu, 19 Dec 2024 13:21:30 +0200 Subject: [PATCH] add tilt-cleanup make target Signed-off-by: Artem Bortnikov --- Makefile | 50 ++++++++++++++++++++++++++++++++++---------------- Tiltfile | 30 +++++++++++++----------------- 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index 35fc8b3..eb97d4d 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,9 @@ else GOBIN=$(shell go env GOBIN) endif +GOARCH := $(shell go env GOARCH) +GOOS := $(shell go env GOOS) + # CONTAINER_TOOL defines the container tool to be used for building images. # Be aware that the target commands are only tested with Docker which is # scaffolded by default. However, you might want to replace it to use other @@ -174,25 +177,25 @@ ifndef ignore-not-found endif .PHONY: install -install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config. +install: manifests kustomize kubectl ## Install CRDs into the K8s cluster specified in ~/.kube/config. $(KUSTOMIZE) build config/crd | $(KUBECTL) 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. +uninstall: manifests kustomize kubectl ## 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 -n $(NAMESPACE) --ignore-not-found=$(ignore-not-found) -f - .PHONY: deploy -deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config. +deploy: manifests kustomize kubectl ## Deploy controller to the K8s cluster specified in ~/.kube/config. cd config/manager && $(KUSTOMIZE) edit set image ghcr.io/aenix-io/etcd-operator=${IMG} $(KUSTOMIZE) build config/default | $(KUBECTL) -n $(NAMESPACE) apply -f - $(KUBECTL) wait deployment.apps/etcd-operator-controller-manager --for condition=Available --namespace $(NAMESPACE) --timeout 5m .PHONY: undeploy -undeploy: kustomize ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion. +undeploy: kustomize kubectl ## 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 -n $(NAMESPACE) --ignore-not-found=$(ignore-not-found) -f - .PHONY: redeploy -redeploy: deploy ## Redeploy controller with new docker image. +redeploy: deploy kubectl ## Redeploy controller with new docker image. # force recreate pods $(KUBECTL) rollout restart -n $(NAMESPACE) deploy/etcd-operator-controller-manager @@ -216,7 +219,7 @@ kind-delete: kind ## Create kubernetes cluster using Kind. fi .PHONY: kind-prepare -kind-prepare: kind-create +kind-prepare: kind-create kubectl # Install prometheus operator $(KUBECTL) apply --server-side -f "https://github.com/prometheus-operator/prometheus-operator/releases/download/$(PROMETHEUS_OPERATOR_VERSION)/bundle.yaml" $(KUBECTL) wait deployment.apps/prometheus-operator --for condition=Available --namespace default --timeout 5m @@ -237,7 +240,7 @@ $(HELM_PLUGINS): mkdir -p $(HELM_PLUGINS) ## Tool Binaries -KUBECTL ?= kubectl +KUBECTL ?= $(LOCALBIN)/kubectl KUSTOMIZE ?= $(LOCALBIN)/kustomize CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen ENVTEST ?= $(LOCALBIN)/setup-envtest @@ -277,6 +280,13 @@ TILT_VERSION ?= 0.33.21 KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" HELM_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3" +CURL_RETRIES=3 +.PHONY: kubectl +kubectl: $(LOCALBIN) + @test -x $(KUBECTL) && ! $(KUBECTL) version 2>/dev/null | grep -q $(K8S_VERSION) || \ + curl --retry $(CURL_RETRIES) -fsL https://dl.k8s.io/release/$(K8S_VERSION)/bin/$(GOOS)/$(GOARCH)/kubectl -o $(KUBECTL) && \ + chmod +x $(KUBECTL) + .PHONY: kustomize kustomize: $(LOCALBIN) @if test -x $(KUSTOMIZE) && ! $(KUSTOMIZE) version | grep -q $(KUSTOMIZE_VERSION); then \ @@ -341,24 +351,32 @@ ctlptl: $(LOCALBIN) @test -x $(CTLPTL) && $(CTLPTL) version | grep -q $(CTLPTL_VERSION) || \ GOBIN=$(LOCALBIN) go install github.com/tilt-dev/ctlptl/cmd/ctlptl@$(CTLPTL_VERSION) -ifeq (darwin,$(shell go env GOOS)) +ifeq (darwin,$(GOOS)) TILT_OS=mac else -TILT_OS=$(shell go env GOOS) +TILT_OS=$(GOOS) endif -TILT_ARCH ?= $(shell go env GOARCH) +TILT_ARCH ?= $(GOARCH) -TILT_ARCHIVE=tilt.$(TILT_VERSION).$(TILT_OS).$(TILT_ARCH).tar.gz +TILT_ARCHIVE_NAME=tilt.$(TILT_VERSION).$(TILT_OS).$(TILT_ARCH).tar.gz .PHONY: tilt tilt: $(LOCALBIN) - @test -x $(TILT) && $(TILT) version | grep -q $(TILT_VERSION) || \ - rm -f $(TILT) && \ - curl -sL https://github.com/tilt-dev/tilt/releases/download/v$(TILT_VERSION)/$(TILT_ARCHIVE) -o /tmp/$(TILT_ARCHIVE) && \ - tar xzf /tmp/$(TILT_ARCHIVE) -C $(LOCALBIN) && \ - rm -f /tmp/$(TILT_ARCHIVE) + @if ! test -x $(TILT) || ! $(TILT) version | grep -q $(TILT_VERSION); then \ + TMPDIR=$$(mktemp -d) && \ + trap 'rm -rf "$$TMPDIR"' EXIT && \ + curl -sL https://github.com/tilt-dev/tilt/releases/download/v$(TILT_VERSION)/$(TILT_ARCHIVE_NAME) -o "$$TMPDIR/$(TILT_ARCHIVE_NAME)" && \ + curl -sL https://github.com/tilt-dev/tilt/releases/download/v$(TILT_VERSION)/$(TILT_ARCHIVE_NAME).sha256 -o "$$TMPDIR/$(TILT_ARCHIVE_NAME).sha256" && \ + cd "$$TMPDIR" && sha256sum -c "$(TILT_ARCHIVE_NAME).sha256" && \ + tar xzf "$(TILT_ARCHIVE_NAME)" -C $(LOCALBIN) || exit 1; \ + fi .PHONY: tilt-up tilt-up: kustomize kind ctlptl tilt $(CTLPTL) apply -f config/dev/ctlptl-kind.yaml $(TILT) up + +.PHONY: tilt-cleanup +tilt-cleanup: ctlptl + $(TILT) down + $(CTLPTL) delete -f config/dev/ctlptl-kind.yaml diff --git a/Tiltfile b/Tiltfile index 994606a..53fbe07 100644 --- a/Tiltfile +++ b/Tiltfile @@ -1,5 +1,3 @@ -#!/usr/bin/env bash - update_settings(k8s_upsert_timeout_secs=60) # on first tilt up, often can take longer than 30 seconds # tilt settings @@ -7,14 +5,15 @@ settings = { "allowed_contexts": [ "kind-etcd-operator-dev" ], - "kubectl": "/usr/local/bin/kubectl", - "kustomize": "./bin/kustomize", + "kubectl": "bin/kubectl", + "kustomize": "bin/kustomize", "cert_manager_version": "v1.15.3", } # define variables and functions -kubectl = settings.get("kubectl") -kustomize_binary = settings.get("kustomize") +base_path = config.main_dir +kubectl_binary = "{}/{}".format(base_path, settings.get("kubectl")) +kustomize_binary = "{}/{}".format(base_path, settings.get("kustomize")) if "allowed_contexts" in settings: allow_k8s_contexts(settings.get("allowed_contexts")) @@ -22,22 +21,19 @@ if "allowed_contexts" in settings: def deploy_cert_manager(): version = settings.get("cert_manager_version") print("Installing cert-manager") - local("{} apply -f https://github.com/cert-manager/cert-manager/releases/download/{}/cert-manager.yaml".format(kubectl, version), quiet=True, echo_off=True) + local("{} apply -f https://github.com/cert-manager/cert-manager/releases/download/{}/cert-manager.yaml".format(kubectl_binary, version), quiet=True, echo_off=True) print("Waiting for cert-manager to start") - local("{} wait --for=condition=Available --timeout=300s -n cert-manager deployment/cert-manager".format(kubectl), quiet=True, echo_off=True) - local("{} wait --for=condition=Available --timeout=300s -n cert-manager deployment/cert-manager-cainjector".format(kubectl), quiet=True, echo_off=True) - local("{} wait --for=condition=Available --timeout=300s -n cert-manager deployment/cert-manager-webhook".format(kubectl), quiet=True, echo_off=True) + local("{} wait --for=condition=Available --timeout=300s -n cert-manager deployment/cert-manager".format(kubectl_binary), quiet=True, echo_off=True) + local("{} wait --for=condition=Available --timeout=300s -n cert-manager deployment/cert-manager-cainjector".format(kubectl_binary), quiet=True, echo_off=True) + local("{} wait --for=condition=Available --timeout=300s -n cert-manager deployment/cert-manager-webhook".format(kubectl_binary), quiet=True, echo_off=True) -def waitforsystem(): - print("Waiting for metal-operator to start") - local("{} wait --for=condition=ready --timeout=300s -n etcd-operator-system pod --all".format(kubectl), quiet=False, echo_off=True) +def prepare_etcd_operator(): + docker_build('ghcr.io/aenix-io/etcd-operator', '.') + return kustomize("./config/dev", kustomize_bin=kustomize_binary) # deploy everything deploy_cert_manager() -docker_build('ghcr.io/aenix-io/etcd-operator', '.') - -yaml = kustomize("./config/dev", kustomize_bin=kustomize_binary) - +yaml = prepare_etcd_operator() k8s_yaml(yaml)