Debug Kubernetes Operator-sdk locally using Vscode
I recently got started with Kubernetes Operators on Openshift 4.1 . A lot of lessons learned but have more to learn.
What are Operators
Kubernetes Operator are a way of leveraging the Kubernetties API to create your own Kubernetes custom resources.
The project I am working on Integreatly is using the Operator-sdk framework to build operators, this takes some of the complexity out of building a Kubernetes Operators.
I won’t go into the basic of installing a operator that’s documented in the Operator-sdk docs.
Running Locally
You can run the Operator-sdk locally, and point it at the namespace for your operator
operator-sdk up local --namespace=integreatly-operator
# command changed as off v0.15.0
operator-sdk run --local --namespace=integreatly-operator
What this means that instead of having to build a container image and push it to a app registry like quay.io or dockerhub.com, you can just use your local code base as the source instead of a container image and use it to deploy your operator to your Openshift 4 cluster.
Setup Vscode to debug
I use Vscode so how do I debug using operator-sdk up local?
Delve is a debug tool for Golang, it can be downloaded here https://github.com/go-delve/delve/tree/master/Documentation/installation or by just using go
go get -u github.com/go-delve/delve/cmd/dlv
Delve is used by this Vscode Go plugin https://marketplace.visualstudio.com/items?itemName=ms-vscode.Go
You need to run delve with the command line switch — enable-delve
on the up local command
e.g. The operator I am working on is called integreatly-opeartor
so the commands to run it are as follows
NOTE: `WATCH_NAMESPACE` should always point to the namespace for your operator
You will need a launch json for Vscode to interact with this headless mode of delve
{
“version”: “0.2.0”,
“configurations”: [
{
“name”: “Integreatly Operator”,
“type”: “go”,
“request”: “launch”,
“mode”: “auto”,
“program”: “${workspaceFolder}/cmd/manager/main.go”,
“env”: {
“WATCH_NAMESPACE”: “integreatly-operator”
},
“args”: []
}
]
}
Start the Vscode debugger. Open the Debug console and you will see the standard output from the operator-sdk up local command, Your debugger will stop on the set breakpoints.