From 218217cdcfb190acafa8e0f4fb8ccec56b22faaf Mon Sep 17 00:00:00 2001 From: dtfranz Date: Tue, 28 Feb 2023 10:49:24 -0800 Subject: [PATCH] Remote debug make target Adds debug make target to rukpak. The debug target is essentially the same as the run target except that, once finished, it exposes a local port to allow for remote debugging in the rukpak core container. Signed-off-by: dtfranz --- Makefile | 22 ++++++++---- README.md | 29 +++++++++++++++ test/tools/remotedebug/kind-config.yaml | 9 +++++ test/tools/remotedebug/kustomization.yaml | 14 ++++++++ .../remotedebug/patch/install_dlv_init.yaml | 35 +++++++++++++++++++ .../patch/remote_debug_command.yaml | 33 +++++++++++++++++ test/tools/remotedebug/resources/service.yaml | 14 ++++++++ 7 files changed, 150 insertions(+), 6 deletions(-) create mode 100644 test/tools/remotedebug/kind-config.yaml create mode 100644 test/tools/remotedebug/kustomization.yaml create mode 100644 test/tools/remotedebug/patch/install_dlv_init.yaml create mode 100644 test/tools/remotedebug/patch/remote_debug_command.yaml create mode 100644 test/tools/remotedebug/resources/service.yaml diff --git a/Makefile b/Makefile index f42544e9..daee0830 100644 --- a/Makefile +++ b/Makefile @@ -109,7 +109,7 @@ e2e: KIND_CLUSTER_NAME=rukpak-e2e e2e: rukpakctl run image-registry local-git kind-load-bundles registry-load-bundles test-e2e kind-cluster-cleanup ## Run e2e tests against an ephemeral kind cluster kind-cluster: kind kind-cluster-cleanup ## Standup a kind cluster - $(KIND) create cluster --name ${KIND_CLUSTER_NAME} + $(KIND) create cluster --name ${KIND_CLUSTER_NAME} ${KIND_CLUSTER_CONFIG} $(KIND) export kubeconfig --name ${KIND_CLUSTER_NAME} kind-cluster-cleanup: kind ## Delete the kind cluster @@ -124,14 +124,15 @@ local-git: ## Setup in-cluster git repository ################### # Install and Run # ################### -.PHONY: install install-manifests wait run cert-mgr uninstall +.PHONY: install install-manifests wait run debug debug-helper cert-mgr uninstall ##@ install/run: install: generate cert-mgr install-manifests wait ## Install rukpak +MANIFESTS_DIR ?= manifests install-manifests: - $(KUBECTL) apply -k manifests + $(KUBECTL) apply -k $(MANIFESTS_DIR) wait: $(KUBECTL) wait --for=condition=Available --namespace=$(RUKPAK_NAMESPACE) deployment/core --timeout=60s @@ -141,12 +142,21 @@ wait: run: build-container kind-cluster kind-load install ## Build image, stop/start a local kind cluster, and run operator in that cluster +debug: MANIFESTS_DIR = test/tools/remotedebug ## Same as 'run' target with the addition of remote debugging available on port 40000 +debug: DEBUG_FLAGS = -gcflags="all=-N -l" +debug: KIND_CLUSTER_CONFIG = --config=./test/tools/remotedebug/kind-config.yaml +debug: run debug-helper + +debug-helper: + @echo '' + @echo "Remote Debugging for '$$($(KUBECTL) -n $(RUKPAK_NAMESPACE) get pods -l app=core -o name)' in namespace '$(RUKPAK_NAMESPACE)' now available through localhost:40000" + cert-mgr: ## Install the certification manager $(KUBECTL) apply -f https://github.com/cert-manager/cert-manager/releases/download/$(CERT_MGR_VERSION)/cert-manager.yaml $(KUBECTL) wait --for=condition=Available --namespace=cert-manager deployment/cert-manager-webhook --timeout=60s uninstall: ## Remove all rukpak resources from the cluster - $(KUBECTL) delete -k manifests + $(KUBECTL) delete -k $(MANIFESTS_DIR) ################## # Build and Load # @@ -165,10 +175,10 @@ VERSION_FLAGS=-ldflags "-X $(VERSION_PATH).GitCommit=$(GIT_COMMIT)" build: $(BINARIES) $(LINUX_BINARIES): - CGO_ENABLED=0 GOOS=linux go build -tags $(GO_BUILD_TAGS) $(VERSION_FLAGS) -o $(BIN_DIR)/$@ ./cmd/$(notdir $@) + CGO_ENABLED=0 GOOS=linux go build $(DEBUG_FLAGS) -tags $(GO_BUILD_TAGS) $(VERSION_FLAGS) -o $(BIN_DIR)/$@ ./cmd/$(notdir $@) $(BINARIES): - CGO_ENABLED=0 go build -tags $(GO_BUILD_TAGS) $(VERSION_FLAGS) -o $(BIN_DIR)/$@ ./cmd/$@ + CGO_ENABLED=0 go build $(DEBUG_FLAGS) -tags $(GO_BUILD_TAGS) $(VERSION_FLAGS) -o $(BIN_DIR)/$@ ./cmd/$@ $(DEBUG_FLAGS) build-container: $(LINUX_BINARIES) ## Builds provisioner container image locally $(CONTAINER_RUNTIME) build -f Dockerfile -t $(IMAGE) $(BIN_DIR)/linux diff --git a/README.md b/README.md index a2a29165..693cfe1c 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,35 @@ make run > Note: RukPak may take some time to become fully operational while its controllers and webhooks are spinning up during installation. As a result, please allow a few moments before creating Bundles/BundleDeployments if you are noticing unexpected failures. +### Debugging + +The following make target is also available to developers that wish to debug the container remotely through `dlv`. Once completed, a remote debugging session may be attached through any preferred IDE on `localhost:40000` : + +```bash +make debug +``` + +As an example, the following `launch.json` file may be used to connect to the debugging session with vscode: +```json +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Remote Debugging", + "type": "go", + "request": "attach", + "mode": "remote", + "remotePath": "", + "port":40000, + "host":"127.0.0.1", + "showLog": true, + "trace": "log", + "logOutput": "rpc" + } + ] +} +``` + There are currently no other supported ways of installing RukPak, although there are plans to add support for other popular packaging formats such as a Helm chart or an OLM bundle. diff --git a/test/tools/remotedebug/kind-config.yaml b/test/tools/remotedebug/kind-config.yaml new file mode 100644 index 00000000..e8bfb379 --- /dev/null +++ b/test/tools/remotedebug/kind-config.yaml @@ -0,0 +1,9 @@ +apiVersion: kind.x-k8s.io/v1alpha4 +kind: Cluster +nodes: +- role: control-plane + extraPortMappings: + - containerPort: 31111 + hostPort: 40000 + listenAddress: "0.0.0.0" # Optional, defaults to "0.0.0.0" + protocol: tcp # Optional, defaults to tcp diff --git a/test/tools/remotedebug/kustomization.yaml b/test/tools/remotedebug/kustomization.yaml new file mode 100644 index 00000000..935d2003 --- /dev/null +++ b/test/tools/remotedebug/kustomization.yaml @@ -0,0 +1,14 @@ +resources: + - ../../../manifests/ + - ./resources/service.yaml + +patchesJson6902: +- target: + group: apps + version: v1 + kind: Deployment + name: core + path: patch/remote_debug_command.yaml + +patchesStrategicMerge: + - patch/install_dlv_init.yaml diff --git a/test/tools/remotedebug/patch/install_dlv_init.yaml b/test/tools/remotedebug/patch/install_dlv_init.yaml new file mode 100644 index 00000000..aeeaa361 --- /dev/null +++ b/test/tools/remotedebug/patch/install_dlv_init.yaml @@ -0,0 +1,35 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + namespace: rukpak-system + name: core +spec: + template: + spec: + securityContext: + runAsNonRoot: false + volumes: + - name: gobindir + emptyDir: {} + initContainers: + - name: install-dlv + env: + - name: CGO_ENABLED + value: "0" + image: golang:1.17 + command: + - go + args: + - install + - -ldflags + - "-s -w -extldflags '-static'" + - github.com/go-delve/delve/cmd/dlv@latest + volumeMounts: + - name: gobindir + mountPath: /go/bin/ + containers: + - name: kube-rbac-proxy + - name: manager + volumeMounts: + - name: gobindir + mountPath: /go/bin/ diff --git a/test/tools/remotedebug/patch/remote_debug_command.yaml b/test/tools/remotedebug/patch/remote_debug_command.yaml new file mode 100644 index 00000000..e4c096cd --- /dev/null +++ b/test/tools/remotedebug/patch/remote_debug_command.yaml @@ -0,0 +1,33 @@ +# Move the original command to the beginning of the args array +- op: copy + from: /spec/template/spec/containers/1/command/0 + path: /spec/template/spec/containers/1/args/0 +# Set the command to /dlv +- op: add + path: /spec/template/spec/containers/1/command + value: ["/go/bin/dlv"] +# Prepend the args array with the following dlv args +- op: add + path: /spec/template/spec/containers/1/args/0 + value: "--listen=:40000" +- op: add + path: /spec/template/spec/containers/1/args/1 + value: "--continue" +- op: add + path: /spec/template/spec/containers/1/args/2 + value: "--headless=true" +- op: add + path: /spec/template/spec/containers/1/args/3 + value: "--api-version=2" +- op: add + path: /spec/template/spec/containers/1/args/4 + value: "--accept-multiclient" +- op: add + path: /spec/template/spec/containers/1/args/5 + value: "--log" +- op: add + path: /spec/template/spec/containers/1/args/6 + value: "exec" +- op: add + path: /spec/template/spec/containers/1/args/7 + value: "--" diff --git a/test/tools/remotedebug/resources/service.yaml b/test/tools/remotedebug/resources/service.yaml new file mode 100644 index 00000000..8f44f441 --- /dev/null +++ b/test/tools/remotedebug/resources/service.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Service +metadata: + name: core-remote-debug + namespace: rukpak-system +spec: + type: NodePort + ports: + - port: 40000 + targetPort: 40000 + nodePort: 31111 + protocol: TCP + selector: + app: core \ No newline at end of file