Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
Remote debug make target
Browse files Browse the repository at this point in the history
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 <dfranz@redhat.com>
  • Loading branch information
dtfranz committed Mar 3, 2023
1 parent e520f0e commit 218217c
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 6 deletions.
22 changes: 16 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 #
Expand All @@ -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
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
9 changes: 9 additions & 0 deletions test/tools/remotedebug/kind-config.yaml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions test/tools/remotedebug/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions test/tools/remotedebug/patch/install_dlv_init.yaml
Original file line number Diff line number Diff line change
@@ -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/
33 changes: 33 additions & 0 deletions test/tools/remotedebug/patch/remote_debug_command.yaml
Original file line number Diff line number Diff line change
@@ -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: "--"
14 changes: 14 additions & 0 deletions test/tools/remotedebug/resources/service.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 218217c

Please sign in to comment.