Skip to content

Commit 83e7f71

Browse files
committed
docs: add debugging controllers documentation
Issue #8876. Signed-off-by: Stanislav Jakuschevskij <[email protected]>
1 parent 3f1322a commit 83e7f71

File tree

1 file changed

+99
-1
lines changed

1 file changed

+99
-1
lines changed

DEVELOPMENT.md

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ First, you may want to [Ramp up](#ramp-up) on Kubernetes and Custom Resource Def
1919
1. [Managing Tekton Objects using `ko`](#managing-tekton-objects-using-ko) in Kubernetes
2020
1. [Accessing logs](#accessing-logs)
2121
1. [Adding new CRD types](#adding-new-crd-types)
22+
1. [Debugging](#debugging)
23+
1. [Change Controller](#change-controller)
24+
1. [Forward Debugging Port](#forward-debugging-port)
25+
1. [Add VSCode Configuration](#add-vscode-configuration)
2226

2327
---
2428

@@ -115,6 +119,10 @@ You must install these tools:
115119
request. To ensure your work does not contain offensive language, you may
116120
want to install and run this tool locally.
117121

122+
1. (Optional)
123+
[`delve`](https://github.com/go-delve/delve/tree/master/Documentation/installation) is needed if you want to setup
124+
a debugging of the Tekton controller in VSCode or your IDE of choice.
125+
118126
### Configure environment
119127

120128
To [build, deploy and run your Tekton Objects with `ko`](#install-pipeline), you'll need to set these environment variables:
@@ -464,7 +472,6 @@ This script will cause `ko` to:
464472
465473
It will also update the default system namespace used for K8s `deployments` to the new value for all subsequent `kubectl` commands.
466474
467-
468475
---
469476
470477
### Accessing logs
@@ -505,3 +512,94 @@ If you need to add a new CRD type, you will need to add:
505512
[list of known types](./pkg/apis/pipeline/v1alpha1/register.go)
506513
507514
_See [the API compatibility policy](api_compatibility_policy.md)._
515+
516+
## Debugging
517+
518+
`ko` has build in support for the `delve` debugger. To use it you need to build your controller image with the `--debug` and `--disable-optimizations` flag.
519+
520+
### Change Controller
521+
522+
If you want to debug your controller you first need to update its deployment configuration and comment the probes. If you don't do that the short probe timeouts will crash the pod:
523+
524+
```yaml
525+
# config/controller.yaml
526+
527+
# livenessProbe:
528+
# httpGet:
529+
# path: /health
530+
# port: probes
531+
# scheme: HTTP
532+
# initialDelaySeconds: 5
533+
# periodSeconds: 10
534+
# timeoutSeconds: 5
535+
# readinessProbe:
536+
# httpGet:
537+
# path: /readiness
538+
# port: probes
539+
# scheme: HTTP
540+
# initialDelaySeconds: 5
541+
# periodSeconds: 10
542+
# timeoutSeconds: 5
543+
```
544+
545+
Then you need to rebuild the the controller in debug mode:
546+
547+
```sh
548+
ko apply -f config/controller.yaml --debug --disable-optimizations
549+
```
550+
551+
### Forward Debugging Port
552+
553+
Now the controller will be re-started with `delve` in headless mode and you can attach a client to it on port `40000` but first you need to forward the port from the controller pod:
554+
555+
```sh
556+
kubectl port-forward -n tekton-pipelines deployments/tekton-pipelines-controller 40000:40000
557+
```
558+
559+
### Add VSCode Configuration
560+
561+
In VSCode add the following `launch.json` configuration for go remote debugging:
562+
563+
```json
564+
{
565+
"version": "0.2.0",
566+
"configurations": [
567+
{
568+
"name": "Attach to Delve (Tekton Controller)",
569+
"type": "go",
570+
"request": "attach",
571+
"mode": "remote",
572+
"port": 40000,
573+
"host": "127.0.0.1",
574+
"apiVersion": 2,
575+
"substitutePath": [
576+
{
577+
"from": "${workspaceFolder}",
578+
"to": "github.com/tektoncd/pipeline"
579+
}
580+
]
581+
}
582+
]
583+
}
584+
```
585+
586+
Now you ar able to attach to `delve` in VSCode. Set breakpoints e.g. in `pipelinerun.go` in the `reconcile` method and execute a `PipelineRun` and VSCode should stop at the breakpoint. You can use this sample `PipelineRun` for testing:
587+
588+
```sh
589+
kubectl create -f - <<EOF
590+
apiVersion: tekton.dev/v1
591+
kind: PipelineRun
592+
metadata:
593+
generateName: hello-world-run-
594+
spec:
595+
pipelineSpec:
596+
tasks:
597+
- name: hello-task
598+
taskSpec:
599+
steps:
600+
- name: hello-step
601+
image: alpine
602+
script: |
603+
echo "Hello world!"
604+
EOF
605+
```

0 commit comments

Comments
 (0)