Learning Golang writing kubernetes controller using kubebuilder
- Aniket Patel
- Kubernetes , Go , Devops
- January 17, 2024
Hey, Kubernetes crew!
I’m going to share with you my journey of learning Go and Kubebuilder.
I joined Explorex beginning of this year we are using microservice architecture and all services are built using Go. I joined as Devops Engineer. just like the term pure-veg
, I would call myself pure-pythonist
as you can see on my github profile most of my projects are built using Python. But most of the tools I use as a devops engineer are all written in Go. In the past, I faced blockers when developers who maintains these tools is not adding the feature which I can see can be easily be built if only I knew Go. These are enough reasons for me to start learning Go.
I knew it would be too boring to learn Go only by going through tutorials, watching videos on YouTube and books. So, I decided that I’ll build a kubernetes controller that has two CRDs BroadcastJob and CronBroadcastJob. I called it tugboat as it is inspired by the tool that already exists called kruise. Tugboat would do a similar job but has a smaller deployment footprint. Tugboat is a hobby project so please use kruise if you have similar requirements. It’s a great tool.
Tugboat: https://github.com/mehuaniket/tugboat
Step 1: Go Bootcamp – Pointers, Panics, and Paradise
My Go journey started like any newbie’s: struggling with “A Tour of Go,” confused by pointers and error handling. But with each challenge (hello, concurrency!), I discovered Go’s beauty and simplicity. Interface became my abstraction layers as Kubebuilder uses interface heavily to define CRDs which are eventually auto-generate. I learned to use concurrency while writing a logic to delete a job after its status changed to completed
.
Step 2: Kubernetes Dojo – Mastering the Controller Way of Life
With a beginner level of Go skills in my arsenal, I entered the Kubernetes controller realm. The Kubebuilder tool became my weapon, cutting through the complexity of controllers, custom resources, and reconciliation loops. Informers, handlers, actuators – these became my building blocks for crafting controllers.
Step 3: BroadcastJob – Job version of daemonset
My first creation, BroadcastJob, solved a real-world problem: parallel job execution across nodes. Think of it as a Kubernetes Job on steroids – define tasks, run them on all nodes, and monitor the controller’s status, ensuring every pod completes the job before declaring success.
Step 4: CronBroadcastJob – Scheduling Perfection with Time-Traveling Tricks
But I wasn’t done. Meet CronBroadcastJob, the time-traveling sibling of BroadcastJob. This controller lets you schedule recurring BroadcastJobs. Daily config updates? Nightly data backups? CronBroadcastJob makes them effortless! Think of it as putting your deployments on autopilot – pure automation bliss.
Reflections and Revelations
This journey has been a code-fueled adventure. I’ve learned complex concepts, Found a reason why people love statically typed language and learned Kubebuilder which helped me rethink the examples from my past which I could have done differently if I had known Go and Kubebuilder. The satisfaction of seeing my code automate tasks. I’m looking forward to contributing to the Kubernetes ecosystem – these are the rewards that ignite my coding passion.
To my fellow Kubernetes enthusiasts:
Don’t be afraid of the Go and Kubebuilder learning curve! Dive in, embrace the difficulties, and celebrate every achievement. And if you ever need a coding buddy, hit me up! I’ll be glad to share my experience and support you in your own controller-making quest.
P.S. Stay tuned for the next post, where I’ll explain the technical details of BroadcastJob and CronBroadcastJob, sharing code snippets and best practices to help you learn how to build a Kubernetes controller!
Remember, the code speaks for itself: Go + Kubebuilder + Automation = Epic Win. So, brew your code-infused coffee, sharpen your keyboard katana, and let’s write the next chapter in the story of Kubernetes mastery!
Happy coding, Kubernetes comrades!
References: