[Infrastructure as Code (IaC)] Pulumi Generate Kubernetes YAML with Golang (Go)
Rendering Kubernetes YAML from Golang (Go)
Pulumi is a Modern Infrastructure as Code (IaC) to create, deploy, and manage infrastructure on any cloud using familiar programming languages and tools.
Pulumi can generate Kubernetes manifests that easily integrate into existing CI/CD workflows with your familiar programming languages.
This article is about how to use Pulumi and Go SDK to manager Namespace and Deployment within Kubernetes (K8S).
Pulumi has excellent support for deploying and updating Kubernetes resources on a cluster, many users have asked for the option to render YAML that they can integrate into existing workflows. pulumi-kubernetes adds the renderYamlToDirectory option, which enables this feature. This option is available in every Pulumi-supported language, including TypeScript/JavaScript, Python, Go, and .NET.
Aside from easily templating configuration across resources, using a familiar programming language allows you to write and consume libraries, and easily mix in infrastructure configuration (e.g., managed database endpoints, object storage, etc.), all in the same program.
Prerequisites
- Kubernetes (K8S)
Kubernetes (K8s) is an open-source system for automating deployment, scaling, and management of containerized applications.
Install Pulumi and Go
Pulumi
Install the Pulumi - https://www.pulumi.com/ CLI.
1 | Mac OS X |
Go Language Runtime
Install Go - https://golang.org/ .
1 | Mac OS X |
Configure Kubernetes
By default, Pulumi will look for a kubeconfig file in the following locations, just like kubectl:
-
The environment variable: $KUBECONFIG,
-
Or in current user’s default kubeconfig directory: ~/.kube/config
If the kubeconfig file is not in either of these locations, Pulumi will not find it, and it will fail to authenticate against the cluster. Set one of these locations to a valid kubeconfig file, if you have not done so already.
Pulumi New
Create the workspace directory.
1 | mkdir -p col-k8s-yaml-gen-example |
Pulumi login into local file system.
1 | pulumi login file://. |
Pulumi new a project with go SDK.
1 | pulumi new kubernetes-go |
The above command will create some files within the current directory.
1 | tree . |
See and modify main.go file.
1 | package main |
Pulumi Update
Then, run pulumi update to generate Kubernetes YAML manifests.
1 | pulumi update |
It will generate Kubernetes YAML manifests into kubernetes directory.
1 | tree kubernetes/ |
See deployment-nginx.yaml
file.
1 | apiVersion: apps/v1 |
Note that CustomResourceDefinition resources need to be applied first, so they are rendered in a separate subdirectory. (This example doesn’t include any CRDs, so the directory is empty). You could deploy the rendered manifests with kubectl like this:
1 | kubectl apply -f kubernetes/0-crd |
Caveats
There are two important caveats to note about YAML rendering support:
-
The YAML-rendered resources are not created on a Kubernetes cluster, so information that is computed server-side will not be available in your program. For example, a Service will not have IP assignments, so attempting to export these values will not work as usual (i.e., the value will be undefined).
-
Any Secret values will appear in plaintext in the rendered manifests. This includes any values marked as secret in Pulumi. A warning will be printed for any secret values being rendered to YAML, but it is your responsibility to protect the rendered files.
References
[2] Kubernetes Package | Pulumi - https://www.pulumi.com/docs/reference/pkg/kubernetes/