|
2 | 2 |
|
3 | 3 | A `kubectl` plugin to debug Pods with an IDE rather than the CLI.
|
4 | 4 |
|
5 |
| - |
| 5 | +:warning: This plugin is in its alpha stage and is missing some important feature compared to `kubectl debug`. Running |
| 6 | +the IDE in an ephemeral debugging container (rather than copying the target Pod) is not supported. Workloads other than |
| 7 | +`Pods` aren't supported either. |
| 8 | + |
| 9 | +## How does `kubectl debug-ide` work? |
6 | 10 |
|
7 |
| -## Prerequirements |
| 11 | +Running `kubectl debug-ide` is similar to running `kubectl debug` (same flags) but an IDE is started and a git |
| 12 | +repository is cloned within the debugging container. |
8 | 13 |
|
9 |
| -The [DevWorkspace Operator](https://github.com/devfile/devworkspace-operator/tree/main) needs to be installed in the |
10 |
| -Kubernetes cluster. |
| 14 | + |
11 | 15 |
|
12 |
| -## Details |
| 16 | +The command returns the URL of the remote IDE (local IDE support is not implemented yet). Opening the IDE URL in a |
| 17 | +browser allow debugging a process in the target container. Note that the IDE runs in the debugging container, but |
| 18 | +"local" debugging is possible as the debugging container and the target container share the same process namespace. |
13 | 19 |
|
14 |
| -The plugin uses the [client-go library](https://github.com/kubernetes/client-go/tree/master/tools/clientcmd) to create a DevWorkspace in the current namespace. |
| 20 | + |
15 | 21 |
|
16 |
| -It accepts the same flags as the command `kubectl debug` which it tries to mimic as much as possible, but including an |
17 |
| -IDE for source code debugging. |
| 22 | +## Examples |
18 | 23 |
|
19 |
| -## Running |
| 24 | +The following command creates a copy of the Pod `$TARGET_POD` with an extra container using image |
| 25 | +`$DEBUGGING_CONTAINER_IMG` where `$GIT_REPO` is cloned and an IDE is started. |
20 | 26 |
|
21 | 27 | ```sh
|
22 |
| -$ go install cmd/kubectl-debug_ide.go |
| 28 | +TARGET_POD="outyet" |
| 29 | +DEBUGGING_CONTAINER_IMG="ghcr.io/l0rd/outyet-dev:latest" |
| 30 | +TARGET_POD_COPY="outyet-debug" |
| 31 | +GIT_REPO="https://github.com/l0rd/outyet.git" |
| 32 | + |
| 33 | +kubectl debug-ide $TARGET_POD \ |
| 34 | + --image $DEBUGGING_CONTAINER_IMG \ |
| 35 | + --copy-to $TARGET_POD_COPY \ |
| 36 | + --share-processes \ |
| 37 | + --git-repository $GIT_REPO |
| 38 | +``` |
23 | 39 |
|
24 |
| -# assumes you have a working KUBECONFIG |
25 |
| -# you can now begin using this plugin as a regular kubectl command: |
26 |
| -# start debugging the pod `outyet` |
27 |
| -$ kubectl debug-ide outyet \ |
28 |
| - --image ghcr.io/l0rd/outyet-dev:latest \ |
29 |
| - --copy-to outyet-debug \ |
| 40 | +:mega: The containers in the copy of target Pod share the PID namespace. This is helpful to attach the IDE debugger to |
| 41 | +the target process as they run in separate containers. |
| 42 | + |
| 43 | +Delete the `DevWorkspace` Custom resource to stop the debugging session and cleanup the Kubernetes resources created by |
| 44 | +`kubectl debug-id`: |
| 45 | + |
| 46 | +```sh |
| 47 | +TARGET_POD="outyet" |
| 48 | +DEBUGGING_CONTAINER_IMG="ghcr.io/l0rd/outyet-dev:latest" |
| 49 | +TARGET_POD_COPY="outyet-debug" |
| 50 | +GIT_REPO="https://github.com/l0rd/outyet.git" |
| 51 | + |
| 52 | +kubectl debug-ide $TARGET_POD \ |
| 53 | + --image $DEBUGGING_CONTAINER_IMG \ |
| 54 | + --copy-to $TARGET_POD_COPY \ |
30 | 55 | --share-processes \
|
31 |
| - --git-repository https://github.com/l0rd/outyet.git |
| 56 | + --git-repository $GIT_REPO |
32 | 57 | ```
|
33 | 58 |
|
34 |
| -## Shell completion |
| 59 | +:mega: `kubectl delete pod` won't work in this case as the DevWorkspace Operator will restart the Pod. |
35 | 60 |
|
36 |
| -This plugin supports shell completion when used through kubectl. To enable shell completion for the plugin |
37 |
| -you must copy the file `./kubectl_complete-cde` somewhere on `$PATH` and give it executable permissions. |
| 61 | +## Requirements |
38 | 62 |
|
39 |
| -The `./kubectl_complete-cde` script shows a hybrid approach to providing completions: |
40 |
| -1. it uses the builtin `__complete` command provided by [Cobra](https://github.com/spf13/cobra) for flags |
41 |
| -1. it calls `kubectl` to obtain the list of pods to complete arguments (note that a more elegant approach would be to |
42 |
| -have the `kubectl-cde` program itself provide completion of arguments by implementing Cobra's `ValidArgsFunction` to |
43 |
| -fetch the list of pods, but it would then be a less varied example) |
| 63 | +Running `kubectl debug-ide` requires the [DevWorkspace Operator](https://github.com/devfile/devworkspace-operator/tree/main). |
| 64 | +`kubectl debug-ide` creates Custom Resources of type `DevWorkspace`. |
44 | 65 |
|
45 |
| -One can then do things like: |
46 |
| -``` |
47 |
| -$ kubectl debug-ide <TAB> |
48 |
| -outyet |
49 |
| -
|
50 |
| -$ kubectl debug-ide --<TAB> |
51 |
| ---copy-to |
52 |
| ---image |
53 |
| ---git-repository |
54 |
| ---share-processes |
55 |
| -[...] |
| 66 | +Building (and currently installing too) requires [Go](https://go.dev/dl/). |
| 67 | + |
| 68 | +## Installation |
| 69 | + |
| 70 | +To install `kubectl debug-ide` clone this git repository and run `go install`: |
| 71 | + |
| 72 | +```sh |
| 73 | +$ go install cmd/kubectl-debug_ide.go |
56 | 74 | ```
|
57 | 75 |
|
58 |
| -Note: kubectl v1.26 or higher is required for shell completion to work for plugins. |
| 76 | +## Shell completion |
| 77 | + |
| 78 | +Copy the file `./kubectl_complete-ide` somewhere on `$PATH` and give it executable permissions to enable shell |
| 79 | +completion for the plugin. |
| 80 | + |
| 81 | +:mega: kubectl v1.26 or higher is required for shell completion to work for plugins. |
59 | 82 |
|
60 | 83 | ## Cleanup
|
61 | 84 |
|
62 | 85 | You can "uninstall" this plugin from kubectl by simply removing it from your PATH:
|
63 | 86 |
|
64 |
| - $ rm /usr/local/bin/kubectl-debug_ide |
| 87 | + $ rm $GOBIN/kubectl-debug_ide |
| 88 | + |
| 89 | +## Acknowledgments |
| 90 | + |
| 91 | +The awesome [sample-cli-plugin](https://github.com/kubernetes/sample-cli-plugin) was used to kick off this plugin. |
| 92 | + |
| 93 | +`kubectl debug-ide` is just a generator of DevWorkspace Custom Resources. The heavy lifting, the Cloud Development |
| 94 | +Environment provisioning, is done by the [DevWorkspace Operator](https://github.com/devfile/devworkspace-operator). |
0 commit comments