Skip to content

Commit a4f1f80

Browse files
feat(contributing): add support for debugging live pods (#585)
1 parent 762dabb commit a4f1f80

File tree

10 files changed

+244
-16
lines changed

10 files changed

+244
-16
lines changed

Dockerfile

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ ENV VITE_API_BASE_URL=/api
1717
RUN yarn build
1818

1919
# Build the manager binary
20-
FROM docker.io/library/golang:1.23.7@sha256:1acb493b9f9dfdfe705042ce09e8ded908ce4fb342405ecf3ca61ce7f3b168c7 AS builder
20+
FROM docker.io/library/golang:1.23.7-alpine@sha256:e438c135c348bd7677fde18d1576c2f57f265d5dfa1a6b26fca975d4aa40b3bb AS builder
2121
ARG TARGETOS
2222
ARG TARGETARCH
2323
ARG PACKAGE=github.com/padok-team/burrito
2424
ARG COMMIT_HASH
2525
ARG BUILD_TIMESTAMP
26+
ARG BUILD_MODE=Release
2627

2728
WORKDIR /workspace
2829
# Copy the Go Modules manifests
@@ -47,12 +48,24 @@ COPY --from=builder-ui /workspace/dist internal/server/dist
4748
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
4849
ARG VERSION
4950
ENV GOCACHE=/root/.cache/go-build
50-
RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build \
51-
-ldflags="-w -s \
52-
-X ${PACKAGE}/internal/version.Version=${VERSION} \
53-
-X ${PACKAGE}/internal/version.CommitHash=${COMMIT_HASH} \
54-
-X ${PACKAGE}/internal/version.BuildTimestamp=${BUILD_TIMESTAMP}" \
55-
-o bin/burrito main.go
51+
52+
RUN if [ "${BUILD_MODE}" = "Debug" ]; then go install github.com/go-delve/delve/cmd/dlv@latest; fi
53+
54+
# Build with different flags based on debug mode
55+
RUN --mount=type=cache,target=/root/.cache/go-build \
56+
if [ "${BUILD_MODE}" = "Debug" ]; then \
57+
GCFLAGS="all=-N -l"; \
58+
LDFLAGS=""; \
59+
else \
60+
GCFLAGS=""; \
61+
LDFLAGS="-w -s"; \
62+
fi && \
63+
CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build \
64+
-gcflags "${GCFLAGS}" \
65+
-ldflags="${LDFLAGS} -X ${PACKAGE}/internal/version.Version=${VERSION} \
66+
-X ${PACKAGE}/internal/version.CommitHash=${COMMIT_HASH} \
67+
-X ${PACKAGE}/internal/version.BuildTimestamp=${BUILD_TIMESTAMP}" \
68+
-o bin/burrito main.go
5669

5770
FROM docker.io/library/alpine:3.21.3@sha256:a8560b36e8b8210634f77d9f7f9efd7ffa463e380b75e2e74aff4511df3ef88c
5871

@@ -79,11 +92,13 @@ RUN addgroup \
7992
$USER
8093

8194
# Copy the binary to the production image from the builder stage
82-
COPY --from=builder /workspace/bin/burrito /usr/local/bin/burrito
95+
# Copy /go/bin/dlv*: the wildcard makes the copy to work, even if the binary is not present (in Release mode)
96+
COPY --from=builder /workspace/bin/burrito /go/bin/dlv* /usr/local/bin/
8397

8498
RUN mkdir -p /runner/bin
85-
RUN chmod +x /usr/local/bin/burrito
86-
RUN chown -R burrito:burrito /runner
99+
RUN chmod +x /usr/local/bin/*
100+
# /home/burrito/.config is required for debug mode
101+
RUN mkdir -p /home/burrito/.config && chown -R burrito:burrito /runner /home/burrito/.config
87102

88103
# Use an unprivileged user
89104
USER 65532:65532

Makefile

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,35 @@ test: manifests generate fmt vet envtest ## Run tests.
109109

110110
NEW_VERSION := $(shell date +%s)
111111

112+
# Common function for helm upgrades
113+
define upgrade-helm-common
114+
helm upgrade --install -f deploy/charts/burrito/values.yaml -f deploy/charts/burrito/$(1) -n burrito-system --create-namespace burrito-system deploy/charts/burrito
115+
endef
116+
117+
# Common function for kind upgrades
118+
define upgrade-kind-common
119+
docker buildx build --tag burrito:$(NEW_VERSION) --build-arg VERSION=${NEW_VERSION} $(2) .
120+
kind load docker-image burrito:$(NEW_VERSION)
121+
yq e '.global.deployment.image.tag = "$(NEW_VERSION)"' -i deploy/charts/burrito/$(1)
122+
yq e '.config.burrito.runner.image.tag = "$(NEW_VERSION)"' -i deploy/charts/burrito/$(1)
123+
$(call upgrade-helm-common,$(1))
124+
endef
125+
112126
.PHONY: upgrade-dev-kind
113127
upgrade-dev-kind:
114-
docker buildx build --tag burrito:$(NEW_VERSION) --build-arg VERSION=${NEW_VERSION} .
115-
kind load docker-image burrito:$(NEW_VERSION)
116-
yq e '.global.deployment.image.tag = "$(NEW_VERSION)"' -i deploy/charts/burrito/values-dev.yaml
117-
yq e '.config.burrito.runner.image.tag = "$(NEW_VERSION)"' -i deploy/charts/burrito/values-dev.yaml
118-
helm upgrade --install -f deploy/charts/burrito/values.yaml -f deploy/charts/burrito/values-dev.yaml -n burrito-system --create-namespace burrito-system deploy/charts/burrito
128+
$(call upgrade-kind-common,values-dev.yaml,--build-arg BUILD_MODE=Release)
129+
130+
.PHONY: upgrade-debug-kind
131+
upgrade-debug-kind:
132+
$(call upgrade-kind-common,values-debug.yaml,--build-arg BUILD_MODE=Debug)
119133

120134
.PHONY: upgrade-dev-helm
121135
upgrade-dev-helm:
122-
helm upgrade --install -f deploy/charts/burrito/values.yaml -f deploy/charts/burrito/values-dev.yaml -n burrito-system --create-namespace burrito-system deploy/charts/burrito
136+
$(call upgrade-helm-common,values-dev.yaml)
137+
138+
.PHONY: upgrade-debug-helm
139+
upgrade-debug-helm:
140+
$(call upgrade-helm-common,values-debug.yaml)
123141

124142
##@ Build
125143

deploy/charts/burrito/templates/controllers.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ spec:
4646
imagePullPolicy: {{ .deployment.image.pullPolicy }}
4747
ports:
4848
{{- toYaml .deployment.ports | nindent 12 }}
49+
{{- if eq .deployment.mode "Release" }}
4950
livenessProbe:
5051
{{- toYaml .deployment.livenessProbe | nindent 12 }}
5152
readinessProbe:
5253
{{- toYaml .deployment.readinessProbe | nindent 12 }}
54+
{{- end }}
5355
resources:
5456
{{- toYaml .deployment.resources | nindent 12 }}
5557
env:

deploy/charts/burrito/templates/datastore.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ spec:
5050
imagePullPolicy: {{ .deployment.image.pullPolicy }}
5151
ports:
5252
{{- toYaml .deployment.ports | nindent 12 }}
53+
{{- if eq .deployment.mode "Release" }}
5354
livenessProbe:
5455
{{- toYaml .deployment.livenessProbe | nindent 12 }}
5556
readinessProbe:
5657
{{- toYaml .deployment.readinessProbe | nindent 12 }}
58+
{{- end }}
5759
resources:
5860
{{- toYaml .deployment.resources | nindent 12 }}
5961
env:
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
config:
2+
burrito:
3+
runner:
4+
image:
5+
repository: burrito
6+
tag: DEV_TAG
7+
pullPolicy: Never
8+
datastore:
9+
storage:
10+
mock: true
11+
global:
12+
deployment:
13+
image:
14+
repository: burrito
15+
tag: DEV_TAG
16+
pullPolicy: Never
17+
tenants:
18+
- namespace:
19+
create: true
20+
name: "burrito-project"
21+
22+
# controllers:
23+
# deployment:
24+
# mode: Debug
25+
# command: ["/usr/local/bin/dlv"]
26+
# args: ["--listen=0.0.0.0:2345", "--headless=true", "--accept-multiclient", "--api-version=2", "--log", "exec", "/usr/local/bin/burrito", "controllers", "start"]
27+
28+
# datastore:
29+
# deployment:
30+
# mode: Debug
31+
# command: ["/usr/local/bin/dlv"]
32+
# args: ["--listen=0.0.0.0:2347", "--headless=true", "--accept-multiclient", "--api-version=2", "--log", "exec", "/usr/local/bin/burrito", "datastore", "start"]

deploy/charts/burrito/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ global:
205205
app.kubernetes.io/part-of: burrito
206206
annotations: {}
207207
deployment:
208+
mode: Release
208209
autoscaling:
209210
# -- Enable/Disable autoscaling for Burrito pods
210211
enabled: false
99.9 KB
Loading
331 KB
Loading

docs/assets/demo/vscode-debug.png

23 KB
Loading

docs/contributing.md

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,161 @@ It is strongly recommended to create a GitHub token with no specific rights to b
188188

189189
- [Controller-runtime documentation](https://pkg.go.dev/sigs.k8s.io/controller-runtime) (Burrito heavily relies on this package)
190190
- [Burrito documentation](https://docs.burrito.tf/)
191+
192+
## Debugging Burrito
193+
194+
To debug Burrito efficiently in Kubernetes, you can use [Delve](https://github.com/go-delve/delve), optionally with [Visual Studio Code](https://code.visualstudio.com/) (recommended). We have set a few things to help you getting started.
195+
196+
You'll need to follow instructions in [Getting Started: Set Up a Local Development Environment (kind)](#getting-started-set-up-a-local-development-environment-kind) to get a local Kubernetes development instance.
197+
198+
### Enable debugging
199+
200+
First, being by installing dlv: `go install github.com/go-delve/delve/cmd/dlv@latest`
201+
202+
We'll rely on `deploy/charts/burrito/values-debug.yaml` to deploy the configuration to start the debugging session.
203+
204+
By default, the `controller` (that includes the runner) and `datastore` are commented in the Helm values. Indeed, starting the application with dlv server will hang until you connect with the dlv client so it has to be enabled only when you need it.
205+
206+
```yaml
207+
# controllers:
208+
# deployment:
209+
# mode: Debug
210+
# command: ["/usr/local/bin/dlv"]
211+
# args: ["--listen=0.0.0.0:2345", "--headless=true", "--accept-multiclient", "--api-version=2", "--log", "exec", "/usr/local/bin/burrito", "controllers", "start"]
212+
213+
# datastore:
214+
# deployment:
215+
# mode: Debug
216+
# command: ["/usr/local/bin/dlv"]
217+
# args: ["--listen=0.0.0.0:2347", "--headless=true", "--accept-multiclient", "--api-version=2", "--log", "exec", "/usr/local/bin/burrito", "datastore", "start"]
218+
219+
# server:
220+
# deployment:
221+
# mode: Debug
222+
# command: ["/usr/local/bin/dlv"]
223+
# args: ["--listen=0.0.0.0:2348", "--headless=true", "--accept-multiclient", "--api-version=2", "--log", "exec", "/usr/local/bin/burrito", "server", "start"]
224+
```
225+
226+
By default, we'll start the application with the usual command. If you want to debug the controller or the runner, uncomment the required block. This will open a port on the pod on which you'll connect from your computer.
227+
228+
`mode: Debug` is removing liveness and readiness probes: they won't be able to start as dlv will await for you to start the debugging session.
229+
230+
### Deploy/refresh commands
231+
232+
You'll need to deploy the debug container images and config. This is the same command if you need to refresh your deployment.
233+
234+
To build a new local debug image of Burrito, push it into your local Kind cluster, and update the Helm release with the new image tag, run the following:
235+
236+
```bash
237+
make upgrade-debug-kind
238+
```
239+
240+
To refresh the Helm chart with debug values, run:
241+
242+
```bash
243+
make upgrade-debug-helm
244+
```
245+
246+
Check the [Makefile](https://github.com/padok-team/burrito/blob/main/Makefile) for more details about these commands.
247+
248+
### Connect from your computer to the debug session
249+
250+
The debugging port won't be exposed by default so you'll need to port-forward it.
251+
252+
- For the controller:
253+
254+
```bash
255+
kubectl port-forward $(kubectl get pods -n burrito-system | awk '/burrito-controllers.*Running/{print $1}') -n burrito-system 2345:2345
256+
```
257+
258+
- For the runner:
259+
260+
```bash
261+
kubectl port-forward -n burrito-project <layerName> 2346:2345
262+
```
263+
264+
It will listen on the same port than the controller so we're exposing it on port 2346 on your computer so you can debug the controller and the runner if needed.
265+
266+
- For the datastore:
267+
268+
```bash
269+
kubectl port-forward $(kubectl get pods -n burrito-system | awk '/burrito-datastore.*Running/{print $1}') -n burrito-system 2347:2347
270+
```
271+
272+
- For the server:
273+
274+
```bash
275+
kubectl port-forward $(kubectl get pods -n burrito-system | awk '/burrito-server.*Running/{print $1}') -n burrito-system 2348:2348
276+
```
277+
278+
### Start debugging
279+
280+
#### With vscode
281+
282+
!!! note
283+
You can get more information about Vscode+Go debugging [here](https://github.com/golang/vscode-go/blob/master/docs/debugging.md)
284+
285+
If you want to use Vscode to debug the app, you'll need to get the [Go extension](https://marketplace.visualstudio.com/items?itemName=golang.go) and create a `.vscode/launch.json`:
286+
287+
```json
288+
{
289+
"version": "0.2.0",
290+
"configurations": [
291+
{
292+
"name": "Attach to Controller",
293+
"type": "go",
294+
"request": "attach",
295+
"mode": "remote",
296+
"port": 2345,
297+
"host": "127.0.0.1",
298+
"apiVersion": 2
299+
},
300+
{
301+
"name": "Attach to Runner",
302+
"type": "go",
303+
"request": "attach",
304+
"mode": "remote",
305+
"port": 2346,
306+
"host": "127.0.0.1",
307+
"apiVersion": 2
308+
},
309+
{
310+
"name": "Attach to Datastore",
311+
"type": "go",
312+
"request": "attach",
313+
"mode": "remote",
314+
"port": 2347,
315+
"host": "127.0.0.1",
316+
"apiVersion": 2
317+
},
318+
{
319+
"name": "Attach to Server",
320+
"type": "go",
321+
"request": "attach",
322+
"mode": "remote",
323+
"port": 2348,
324+
"host": "127.0.0.1",
325+
"apiVersion": 2
326+
}
327+
]
328+
}
329+
```
330+
331+
!!! question "New to debugging on Vscode?"
332+
For a vscode debug introduction, you can check [Debug code with Visual Studio Code](https://code.visualstudio.com/docs/debugtest/debugging).
333+
334+
Browse your code to set breakpoints by clicking on the left side of your line.
335+
336+
![Vscode breakpoint](assets/demo/vscode-breakpoint.png)
337+
338+
Open the `Run and Debug` pane, select your debugging configuration and hit `F5` to connect to the remove `dlv`.
339+
340+
![Vscode debug configuration](assets/demo/vscode-debug.png)
341+
342+
Once your line is reached, vscode will show you the variables, current stack, etc
343+
344+
![Vscode debug variables](assets/demo/vscode-debug-variables.png)
345+
346+
#### With `dlv`
347+
348+
If you prefer to debug on cli, you can connect with `dlv connect 127.0.0.1:<debuggingPort>` where `<debuggingPort>` is 2345, 2346, 2347 or 2348, depending on what you're debugging.

0 commit comments

Comments
 (0)