Reference

Debug Kubernetes Operator

Tanul
4 min readFeb 12, 2024

--

Using bridge to Kubernetes plugin for real time debugging of operator code

Bridge to Kubernetes is intended for use in development and testing scenarios only. It is not intended or supported for use with production clusters or live services in active use.

Kubernetes operators sometimes needs intensive debugging especially if it's an existing opensource code or hosted as a custom metrics server. An easy and real-time solution for such cases is to debug the code using Microsoft’s Bridge to Kubernetes.

Pre-requisites

  1. Basic understanding of Kubectl
  2. Understanding of Kubernetes operators
  3. Bridge to Kubernetes extension in vscode

Tools Required

  1. Vs code
  2. Bridge to Kubernetes extension

Hope we already have the Kubernetes operator and the extension installed in vscode otherwise, read other sections of the article first.

Start Debugging

  • Run the kubectl command to select the correct context.
kubectl config set-context — current — namespace=<namespace-name>

For detailed understanding/configuration, please check this article

  • Now, press CTRL+SHIFT+P and select Bridge to Kubernetes: configure
  • Select the service
  • If the application is multi container then select the desire one
  • Specify the port of your local machine for redirection
  • Select run without (or default) launch configuration. If wanted to specify your own launch configuration then create a new config
  • If need to run in the isolation mode, then select yes else no. Yes is preferable when we need to call the service via url. Read here for more details
  • Once it is configured, click the Kubernetes icon at the bottom and select connect to cluster.
  • Once connected, icon will start appearing like this

And the plugin is connected to cluster. Here it will create a new pod in the cluster which perform redirection.

Now, go to main.go and click start debugging. The moment we create a custom resource in Kubernetes, debug shall activate and will reach the debug point in the reconcile function.

TL;DR;

Setup Kubernetes operator

  1. Just go through this article for complete understanding
  2. To save time, here are the ready commands to setup demo with readily available files.
> git clone https://github.com/codereliant/sample-tenant-operator?ref=codereliant.io

> cd sample-tenant-operator

> docker build <your_repo_name>/controller:v0.0.1 .

> docker push <your_repo_name>/controller:v0.0.1 .

> make install

> make IMG=<your_repo_name>/controller:v0.0.1 deploy

If root privileges error occur in this demo while debugging, change this runAsNonRoot to false. Not recommended

# Run this to create the resource and hit the debug point
> kubectl apply -f config/samples/multitenancy_v1_tenant.yaml

It will create the resource and hit the debug point in reconcile function.

Install Bridge to kubernetes

This article helps in installing the extension. Just search for the extension and click install button.

For detailed understanding, please go through this article.

Routing capabilities

For routing capabilities like running the application in isolation mode so that other users are not impacted, check this article.

Configuration

Consider these situations,

  • Prevent this plugin from mapping specific ports of local machine with Kubernetes.
  • Or enable environment variables while debugging.
  • Or mount the pod volume in local machine while debugging.
    Note: - This step will start downloading the whole volume

For such cases, please read this article.

Suggestion

Once plugin execute, it might update the host file. So make sure to backup the host file before executing especially if working in an organization environment.

--

--