In this article, I describe how to write a Kubernetes client in Go using the dynamic client in k8s.io/client-go package. During the course, you can learn the following things:
- The difference between typed clients and the dynamic client.
- Reading YAML manifests into
unstructured.Unstructured
.
- Discovering the REST API endpoint for a Group-Version-Kind.
- Creating and updating resources using Server Side Apply.
You should have a basic knowledge of Kubernetes and Go programming.
The examples in this article depend on k8s.io/client-go@v0.18.1.
- Background: Server Side Apply
- Kubernetes API Basics
- Mapping between GVK and GVR
- Go client libraries
- Typed clients
- Dynamic client
- Using the dynamic client to implement SSA
Background: Server Side Apply
Recently, I wrote a program that applies Kubernetes resources using Server Side Apply.
Server Side Apply, or SSA, is a new way to create or update resources in Kubernetes API server added as a beta feature to Kubernetes 1.16.
One of the advantage of SSA is that it introduces better patching strategy than Strategic Merge Patch. For example, if a Service has two ports sharing the same port number but with different protocol, Strategic Merge Patch could not identify which port should be updated because it uses port
as the key.
apiVersion: v1
kind: Service
metadata:
name: mydns
spec:
selector:
app: mydns
ports:
- protocol: TCP
port: 53
- protocol: UDP
port: 53
Read more