How to Create and Delete deployment and service using the Unstructured Dynamic Client in Go
Introduction
In this article, we will explore how to use the Go dynamic client to create and manage Kubernetes deployments and services. The unstructured client allows us to work with Kubernetes resources without having to define strong types for each resource. This can be useful when dealing with custom resources or when you want to write generic code that can handle multiple resource types.
The first step in working with the unstructured client is to set up a dynamic Kubernetes client. In the code snippet, we define a K8sDynamicClient struct with a dynamic.Interface field. The dynamic.Interface is part of the Go Kubernetes client library and allows us to work with third-party and unstructured resources.
In the getK8sClient function, we build a dynamic client using the clientcmd package and the user’s kubeconfig file. The kubeconfig file is typically found in the user’s home directory under the .kube folder. We create an instance of the K8sDynamicClient struct and return it.
Create a deployment and service using the unstructured client
To create a deployment using the unstructured client, we define the CreateDeployment function. This function accepts the name, namespace, image, port, and replicas for the deployment. Using the provided parameters, we create a unstructured.Unstructured object with the required fields and use the dynamic client to create the deployment in the specified namespace.
Similarly, we define the CreateService function to create a service using the unstructured client. This function accepts the name, selector, namespace, port, and target port for the service. We create a unstructured.Unstructured object with the required fields and use the dynamic client to create the service in the specified namespace.
Delete the created deployment and service
The DeleteDeployment and DeleteService functions demonstrate how to delete a deployment and service using the unstructured client. We create a unstructured.Unstructured object with the required metadata and use the dynamic client to delete the deployment or service in the specified namespace.
The Main Function
In the main function, we initialize the dynamic client and create a deployment and service using the CreateDeployment and CreateService functions. We then delete the created deployment and service using the DeleteDeployment and DeleteService functions.
Assembling the Pieces
After putting all the pieces together, we have a main.go file containing all the code we discussed previously. Secondly, we have a go.mod file containing the dependencies.
Conclusion
This article has demonstrated how to use the Go unstructured client to create and manage Kubernetes deployments and services. By using the unstructured client, you can write code that works with Kubernetes resources without having to define strong types for each resource. This can be especially helpful when dealing with custom resources or when you want to write generic code that can handle multiple resource types.
Subscribe to Faizan Bashir
Get the latest posts delivered right to your inbox