|
| 1 | +# Debug NetObserv Operator Locally |
| 2 | + |
| 3 | +This command prepares the environment for debugging the operator locally. |
| 4 | + |
| 5 | +**Prerequisites:** You must have already set up the development environment using `/setup-dev-env` before running this command. |
| 6 | + |
| 7 | +## Steps |
| 8 | + |
| 9 | +### 1. Scale down the in-cluster operator |
| 10 | + |
| 11 | +Scale down the operator running in the cluster to avoid conflicts: |
| 12 | + |
| 13 | +```bash |
| 14 | +kubectl scale deployment netobserv-controller-manager -n netobserv --replicas=0 |
| 15 | +``` |
| 16 | + |
| 17 | +### 2. Remove the validating webhook |
| 18 | + |
| 19 | +Delete the webhook to allow free modification of the FlowCollector CR during local development: |
| 20 | + |
| 21 | +```bash |
| 22 | +kubectl delete validatingwebhookconfiguration netobserv-validating-webhook-configuration |
| 23 | +``` |
| 24 | + |
| 25 | +### 3. Run the operator locally |
| 26 | + |
| 27 | +Use the AskUserQuestion tool to ask which debugging method to use: |
| 28 | + |
| 29 | +**Question:** "How do you want to run the operator locally?" |
| 30 | +- **Header:** "Debug method" |
| 31 | +- **Options:** |
| 32 | + 1. "go run - Simple execution without debugging" |
| 33 | + 2. "delve interactive - Terminal debugging with dlv commands" |
| 34 | + 3. "delve headless + VSCode - UI debugging with breakpoints" |
| 35 | + |
| 36 | +**Based on the user's selection, execute the corresponding command:** |
| 37 | + |
| 38 | +#### Option 1: go run |
| 39 | +```bash |
| 40 | +go run ./main.go \ |
| 41 | + -ebpf-agent-image=quay.io/netobserv/netobserv-ebpf-agent:main \ |
| 42 | + -flowlogs-pipeline-image=quay.io/netobserv/flowlogs-pipeline:main \ |
| 43 | + -console-plugin-image=quay.io/netobserv/network-observability-console-plugin:main \ |
| 44 | + -namespace=netobserv |
| 45 | +``` |
| 46 | + |
| 47 | +#### Option 2: delve interactive |
| 48 | +```bash |
| 49 | +dlv debug ./main.go -- \ |
| 50 | + -ebpf-agent-image=quay.io/netobserv/netobserv-ebpf-agent:main \ |
| 51 | + -flowlogs-pipeline-image=quay.io/netobserv/flowlogs-pipeline:main \ |
| 52 | + -console-plugin-image=quay.io/netobserv/network-observability-console-plugin:main \ |
| 53 | + -namespace=netobserv |
| 54 | +``` |
| 55 | + |
| 56 | +After starting, inform the user they can use these delve commands: |
| 57 | +- `break main.main` - set breakpoint |
| 58 | +- `continue` - continue execution |
| 59 | +- `next` - step to next line |
| 60 | +- `print <variable>` - inspect variables |
| 61 | + |
| 62 | +#### Option 3: delve headless + VSCode |
| 63 | + |
| 64 | +**Step 1:** Start Delve in headless mode: |
| 65 | +```bash |
| 66 | +dlv debug ./main.go --headless --listen=:2345 --api-version=2 --accept-multiclient -- \ |
| 67 | + -ebpf-agent-image=quay.io/netobserv/netobserv-ebpf-agent:main \ |
| 68 | + -flowlogs-pipeline-image=quay.io/netobserv/flowlogs-pipeline:main \ |
| 69 | + -console-plugin-image=quay.io/netobserv/network-observability-console-plugin:main \ |
| 70 | + -namespace=netobserv |
| 71 | +``` |
| 72 | + |
| 73 | +**Step 2:** Inform the user: |
| 74 | + |
| 75 | +"Delve is now running in headless mode on port 2345. To connect from VSCode: |
| 76 | +1. Open the Debug view (Ctrl+Shift+D / Cmd+Shift+D) |
| 77 | +2. Select 'Connect to Delve (Operator Debug)' from the dropdown |
| 78 | +3. Press F5 or click 'Start Debugging' |
| 79 | + |
| 80 | +You can now set breakpoints in VSCode and debug the operator." |
| 81 | + |
| 82 | +### 4. Example: Debug FlowCollector reconciliation |
| 83 | + |
| 84 | +**To help the user get started with debugging:** |
| 85 | + |
| 86 | +The main reconciliation logic is in `internal/controller/flowcollector/flowcollector_controller.go` (look for the `Reconcile` function around line 100-150). |
| 87 | + |
| 88 | +**If using VSCode debugging:** |
| 89 | +1. Open `internal/controller/flowcollector/flowcollector_controller.go` |
| 90 | +2. Click the left margin next to the line number inside the `Reconcile` function to set a breakpoint |
| 91 | +3. Trigger a reconciliation by running: |
| 92 | +```bash |
| 93 | +kubectl patch flowcollector cluster --type=merge -p '{"spec":{"processor":{"logLevel":"debug"}}}' |
| 94 | +``` |
| 95 | + |
| 96 | +The debugger will pause at your breakpoint, allowing you to inspect the FlowCollector object and step through the reconciliation logic. |
| 97 | + |
| 98 | +## Cleanup |
| 99 | + |
| 100 | +When finished debugging: |
| 101 | + |
| 102 | +1. Stop the local operator (Ctrl+C in terminal, or stop debugging in VSCode) |
| 103 | + |
| 104 | +2. Scale the in-cluster operator back up: |
| 105 | +```bash |
| 106 | +kubectl scale deployment netobserv-controller-manager -n netobserv --replicas=1 |
| 107 | +``` |
| 108 | + |
| 109 | +**Note:** The validating webhook will be automatically recreated by the operator. |
0 commit comments