DEBUG A KUBERNETES OPERATOR WRITTEN IN GO
Tags: go, operatorsdk, kubernetes, delve
Published: 2022-03-03 15:48:13

---
title: DEBUG A KUBERNETES OPERATOR WRITTEN IN GO
published: true
description: In this blog post I want to share how to debug a GO Operator on your local machine on macOS.
tags: #go, #operatorsdk, #kubernetes, #delve
cover_image: https://suedbroecker.files.wordpress.com/2021/04/scan-3963099_1920.jpg?w=1640&h=624&crop=1
---

In this blog post I want to share how to debug a GO Operator on your local machine on macOS. [Adam de Leeuw](https://www.linkedin.com/in/deleeuwa/) and I verified it in different GO operator projects. Sometimes you find on Google information which uses the [older Operator SDK](https://auscunningham.medium.com/debug-kubernetes-operator-sdk-locally-using-vscode-a233aa7c750e). The following instructions worked for us in March 2022. 😉

The blog post is structured in four sections:

1. Prerequisites
2. Basics
3. Configure Visual Studio Code to debug the operator
4. [Debug your GO operator](https://sdk.operatorframework.io/docs/building-operators/golang/)


### 1. PREREQUISITES

You need to …

- … ensure you have the Operator SDK installed ([that also installs GO when you use brew for the installation](https://suedbroecker.net/2022/02/15/fata0009-failed-to-create-api-unable-to-run-post-scaffold-tasks-of-base-go-kubebuilder-io-v3-exit-status-2/))
- … setup the [Visual Studio Code extension for GO](https://code.visualstudio.com/docs/languages/go)
- … install [DELVE debugger for GO](https://github.com/go-delve/delve)
- … setup the integration of the [DELVE debugger for Visual Studio Code](https://github.com/golang/vscode-go)
- … run an example [GO operator](https://github.com/thomassuedbroecker/multi-tenancy-frontend-operator) 😉

### 2. BASICS

Please keep in mind some essentially basics when you run your operator locally with the **make** command:

```bash
make install run
```

That `make install run` command does …

1. … **deploy the needed manifests (`yaml's`)** to the Kubernetes cluster you use and create the connection.
2. … it starts the local GO application with the file called `main.go` 
   (in the last step inside make file with the command `go run ./main.go` )

### 3. CONFIGURE VISUAL STUDIO CODE TO DEBUG THE OPERATOR

With that in mind we know how to debug.

1. First, you to run the `make install run` command to ensure you operator will find all need `manifests` configurations on your cluster.
2. Now you need to configure the DELVE GO debug in Visual Studio Code `.vscode/launch.json` configurations file in your workspace.

### STEP 1: CREATE A `.VSCODE/LAUNCH.JSON` FILE IN VISUAL STUDIO CODE WITH A GO CONFIGURATION[¶](http://127.0.0.1:8000/openshift-security/debug-go-operator-sdk/#step-1-create-a-vscodelaunchjson-with-go)

The following short gif shows the creation of a `.vscode/launch.json` file. In that case it uses the folder where my workspace file is located and you see that **`go package`** was used as debug a configuration.

![img](https://suedbroecker.files.wordpress.com/2022/03/operator-debug-01.gif?w=619)

That’s the example result you saw in the gif.

```json
"configurations": [   {      
  "name" :  "Launch Package" ,      
  "type" :  "go" ,      
  "request" :  "launch" ,      
  "mode" :  "auto" ,      
  "program" :  "${fileDirname}"    
} ]
```

### STEP 2: NOW YOU CAN OPTIONAL CONFIGURE THE LOCATION WHERE YOU WANT TO START THE `MAIN.GO` FILE.

Just to ensure the right `operator` will be started. The gif below show how to customize the `.vscode/launch.json` file.

![img](https://suedbroecker.files.wordpress.com/2022/03/operator-debug-02-2.gif?w=619)

That’s the example result you saw in the gif.

```json
{    // Use IntelliSense to learn about possible attributes.    
     // Hover to view descriptions of existing attributes.    
     // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387    "version" :  "0.2.0" ,    "configurations" : [      
  {        	"name" :  "Launch Frontend Operator" ,        
  					"type" :  "go" ,        
            "request" :  "launch" ,
            "mode" :  "auto" ,
             "program" :  "/Users/thomassuedbroecker/Downloads/dev/multi-tenancy-frontend-operator/frontendOperator"      
} ] }
```

### 4. DEBUG YOUR GO OPERATOR

### STEP 1: EXECUTE THE[ `MAKE INSTALL RUN`] FOR YOUR OPERATOR

You see the my example operator I used for my blog post and live stream [Develop a simple operator to deploy a web application using the GO Operator SDK¶](https://suedbroecker.net/2022/02/18/start-to-develop-a-simple-operator-to-deploy-the-frontend-application-of-the-open-source-multi-cloud-asset-to-build-saas¶/).

```
make install run
```

- Example output

```shell
/Users/thomassuedbroecker/Downloads/dev/multi-tenancy-frontend-operator/frontendOperator/bin/controller-gen
rbac:roleName=manager-role crd webhook paths= "./..."` `output:crd:artifacts:config=config /crd/bases /Users/thomassuedbroecker/Downloads/dev/multi-tenancy-frontend-operator/frontendOperator/bin/kustomize` `build config /crd | kubectl apply -f - customresourcedefinition.apiextensions.k8s.io /tenancyfrontends .multitenancy.example.net configured /Users/thomassuedbroecker/Downloads/dev/multi-tenancy-frontend-operator/frontendOperator/bin/controller-gen` `object:headerFile= "hack/boilerplate.go.txt" 
paths= "./..." go  fmt` `./... go vet ./... go run . /main .go 1.6461312240985138e+09 INFO  controller-runtime.metrics   Metrics server is starting to listen   { "addr" :  ":8080" }
1.646131224098974e+09  INFO  setup  starting manager 1.646131224099315e+09  INFO  Starting server { "path" :  "/metrics" ,  "kind" :  "metrics" ,  "addr" :  "[::]:8080" }
1.646131224099315e+09  INFO  Starting server { "kind" :  "health probe" ,  "addr" :  "[::]:8081" }
1.6461312240993888e+09 INFO  controller.tenancyfrontend   Starting EventSource   { "reconciler group" :  "multitenancy.example.net" ,  "reconciler kind" :  "TenancyFrontend" ,  "source" :  "kind source: *v1alpha1.TenancyFrontend" }
1.6461312240994549e+09 INFO  controller.tenancyfrontend   Starting Controller    { "reconciler group" :  "multitenancy.example.net" ,  "reconciler kind" :  "TenancyFrontend" }
1.646131224200895e+09  INFO  controller.tenancyfrontend   Starting workers { "reconciler group" :  "multitenancy.example.net" ,  "reconciler kind" :  "TenancyFrontend" ,  "worker count" : 1}
```

### STEP 2: STOP THE OPERATOR EXECUTION

### STEP 3: ADD A BREAK POINT TO THE CONTROLLER AND START THE DEBUGGING FOR THE GO OPERATOR[¶]

The gif below shows how create a break point and start the debugging. You need to take a look in the debug console.

![img](https://suedbroecker.files.wordpress.com/2022/03/operator-debug-03.gif?w=619)

In the following image you see a screen shot of the running debugging.

Take a short look at

- Run and debug
  - Start the debug session with the just defined GO configuration for the Operator
- Observe
  - Variables
  - Watch
  - Stack
- Debug console
  - Show the output we would see, when we would have started operator with a [make](https://en.wikipedia.org/wiki/Make_(software)) command

![img](https://suedbroecker.files.wordpress.com/2022/03/operator-debug-02.jpg?w=1024)

# SUMMARY

It’s awesome to use the powerful [DELVE debugger for GO](https://github.com/go-delve/delve) inside [Visual Studio Code](https://github.com/golang/vscode-go) when you build a GO Operator. 🙂

------

I hope this was useful for you and let’s see what’s next?

Greetings,

Thomas

Source: [www.suedbroecker.net](https://suedbroecker.net/2022/03/01/debug-a-kubernetes-operator-written-in-go/#more-4460)

#operator, #go, #operatorsdk, #kubernetes, #delve, #buildlabs4saas