- read

Dynamic Kubernetes Deployments with Golang’s text/template

Adam Szpilewicz 116

Photo by Possessed Photography on Unsplash

If you enjoy reading medium articles and are interested in becoming a member, I would be happy to share my referral link with you!

When working with Kubernetes, there might come a time when you need to create dynamic Kubernetes deployments. Such use-cases arise when the deployment specifications vary based on user inputs, runtime parameters, or other business needs. One way to handle this is by using templates, similar to Python’s Jinja.

In Golang, the text/template package provides functionality similar to Jinja in Python. The template package helps to generate textual output with data that can change. With this, we can create dynamic YAML deployment files for Kubernetes based on the values we receive through HTTP POST requests.

In this article, we’ll be using the Gin web framework to set up a simple HTTP server that can accept POST requests, and the text/template package to create dynamic Kubernetes deployment files.

Here is how the flow could look like in production environemnt. We will discuss only part related to parsing deployment template into yaml ready for the deployment in k8s.

Golang code

In the following demonstration, we will be using two powerful Go libraries to set up a dynamic Kubernetes deployment creator: the Gin web framework and the text/template package.

Gin is an incredibly popular web framework in Go due to its speed and simplicity. It’s designed to develop web applications in a much faster and efficient way, leveraging Go’s robustness and high speed.