Skip to content

Commit

Permalink
Setup remote debugging of manager while it's running in cluster (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Hodgetts authored Nov 2, 2021
1 parent ad07418 commit d1d59bd
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
29 changes: 29 additions & 0 deletions Dockerfile-debug
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Build the manager binary
FROM golang:1.16 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download

RUN CGO_ENABLED=0 go get github.com/go-delve/delve/cmd/dlv

# Copy the go source
COPY . .

# Build
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/go/pkg CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -gcflags "all=-N -l" -v -o manager main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/static:nonroot
WORKDIR /
COPY --from=builder /workspace/manager .
COPY --from=builder /go/bin/dlv .
USER 65532:65532

ENTRYPOINT ["/dlv", "--listen=:40000", "--headless=true", "--api-version=2", "exec", "/manager"]

9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

# Image URL to use all building/pushing image targets
IMG ?= kusk-gateway:dev

# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"

Expand Down Expand Up @@ -73,6 +74,8 @@ run: install-local generate fmt vet ## Run a controller from your host, proxying
docker-build: ## Build docker image with the manager.
DOCKER_BUILDKIT=1 docker build -t ${IMG} .

docker-build-debug: ## Build docker image with the manager and debugger.
DOCKER_BUILDKIT=1 docker build -t "${IMG}-debug" -f ./Dockerfile-debug .
docker-push: ## Push docker image with the manager.
docker push ${IMG}

Expand All @@ -94,6 +97,12 @@ deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/default | kubectl apply -f -

deploy-debug: manifests kustomize ## Deploy controller with debugger to the K8s cluster specified in ~/.kube/config.
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}-debug
cd config/default && $(KUSTOMIZE) edit add patch --path ./manager_debug_patch.yaml
$(KUSTOMIZE) build config/default | kubectl apply -f -
cd config/default && $(KUSTOMIZE) edit remove patch --path ./manager_debug_patch.yaml

undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/default | kubectl delete -f -

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,13 @@ curl -v -X GET 'http://localhost:8080/petstore/api/v3/pet/findByStatus?status=av
```

For the convenience you can use provided petshop-openapi-short-with-kusk.yaml or petshop-openapi-short-with-kusk-and-mock.yaml files in ./development.

# Remote debugging contoller-manager in cluster
- Set up remote debugging for your IDE pointed at localhost:40000
- [Goland](https://www.jetbrains.com/help/go/attach-to-running-go-processes-with-debugger.html#attach-to-a-process-in-the-docker-container)
- [VSCode](https://github.com/golang/vscode-go/blob/master/docs/debugging.md#configure)
- Run `make create-env`
- When the make script is waiting for kusk-controller-manager to become healthy, run `kubectl port-forward deployment/kusk-controller-manager -n kusk-system 40000:40000`
- Run your debug configuration from your IDE. The pod won't become healthy until you do this as Delve waits for a connection on :40000.
- When the script completes, you can now deploy httpbin with `kubectl apply -f examples/httpbin`
- Place breakpoints in the code and debug as normal
31 changes: 31 additions & 0 deletions config/default/manager_debug_patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- name: manager
resources: null
command:
- /dlv
- --listen=:40000
- --headless=true
- --api-version=2
- exec
- /manager
- --
- "--health-probe-bind-address=:8081"
- "--metrics-bind-address=127.0.0.1:8080"
- "--leader-elect"
args: []
securityContext:
capabilities:
add:
- SYS_PTRACE
ports:
- containerPort: 40000
name: debug
protocol: TCP
2 changes: 1 addition & 1 deletion development/cluster/create-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ echo "========> building control-plane docker image and installing into cluster"

SHELL=/bin/bash
eval $(minikube docker-env --profile "kgw")
make docker-build deploy
make docker-build-debug deploy-debug

kubectl rollout status -w deployment/kusk-controller-manager -n kusk-system

Expand Down

0 comments on commit d1d59bd

Please sign in to comment.