[Operator-Framework] – How to debug a Golang operator project?

IntelliJ IDEA/Golang

Following the steps that should be executed from the root dir of your operator project.

# Export the ENV VAR WATCH_NAMESPACE 
$ export WATCH_NAMESPACE={{mynamespace}}

# Create the namespace which the operator will be deployed 
$ kubectl create namespace {{mynamespace}}

# Install the CRD's of your operator project
$ kubectl create -f deploy/crds/myoperator_v1alpha1_kind_crd.yaml

# Go to the dir where has the main.go file
$ cd cmd/manager/

# Run the following command
$ dlv debug --headless --listen=:2345 --api-version=2

In the IDEA you should set up and use the option “Go remote” as follows.

Screenshot 2019-05-04 at 16.14.42.png

Now, you can just run and you will be able to debug it. Following an example.

56869102-b4097980-69f3-11e9-8588-b92812900e4f.png

Visual Code

Following the steps that should be executed from the root dir of your operator project.

# Export the ENV VAR WATCH_NAMESPACE 
$ export WATCH_NAMESPACE={{mynamespace}}

# Create the namespace which the operator will be deployed 
$ kubectl create namespace {{mynamespace}}

# Install the CRD's of your operator project
$ kubectl create -f deploy/crds/myoperator_v1alpha1_kind_crd.yaml

# Run the following command
$ dlv --listen=:2345 --headless=true --api-version=2 exec ./build/_output/bin/{{my-operator}}-local  --

Debug the project using the following Visual Code launch config.

{
    // 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": "test",
            "type": "go",
            "request": "launch",
            "mode": "remote",
            "remotePath": "${workspaceFolder}/cmd/manager/main.go",
            "port": 2345,
            "host": "127.0.0.1",
            "program": "${workspaceFolder}",
            "env": {},
            "args": []
        }
    ]
}

NOTE: In order to get the latest changes you need to build the project.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s