Exploring client-go Clientsets
We've successfully obtained a *rest.Config
object, which holds the API server address and our authentication credentials. Think of this config
as the key and address to the Kubernetes API building. Now, we need a way to navigate the building and interact with specific "departments" (API Groups) and "offices" (Resource Kinds) within it. This is where Clientsets come in.
A Clientset
in client-go
is essentially a collection of clients, grouped by API Group and Version. It provides a convenient, type-safe way to interact with the different parts of the Kubernetes API we discussed earlier (Core v1
, apps/v1
, networking.k8s.io/v1
, etc.).
Instead of constructing raw HTTP requests, you use the methods provided by the specific client within the clientset, which handles the details of serialization (converting Go structs to JSON), setting headers, making the HTTP call, and deserialization (converting the JSON response back into Go structs).
Creating a Clientset
Creating a clientset is straightforward once you have your *rest.Config
. You simply pass the config object to the kubernetes.NewForConfig
function:
The clientset
variable now holds an instance of *kubernetes.Clientset
. This object acts as our gateway to the various API groups and versions supported by client-go
.
Accessing Specific API Groups/Versions
The clientset
object provides methods corresponding to the different API Groups and Versions. The naming convention is generally GroupNameVersion()
.
Core V1 API: For fundamental resources like Pods, Services, Nodes, Namespaces. Accessed via
clientset.CoreV1()
.Apps V1 API: For workload resources like Deployments, StatefulSets, DaemonSets. Accessed via
clientset.AppsV1()
.Networking V1 API: For networking resources like Ingress and NetworkPolicy. Accessed via
clientset.NetworkingV1()
.RBAC V1 API: For Role-Based Access Control resources like Roles, ClusterRoles, RoleBindings. Accessed via
clientset.RbacV1()
.And many others...
client-go
covers most built-in Kubernetes APIs.
Once you select a specific group/version client (e.g., clientset.CoreV1()
), you get another object that has methods for interacting with the resources within that group/version. The convention here is usually the plural, CamelCase name of the resource (e.g., Pods
, Services
, Nodes
).
Let's see a quick example of accessing the Pods client:
Important Note: When you call a method like coreV1Client.Pods("some-namespace")
, you get back an interface specific to managing Pods within that namespace. This interface provides the standard RESTful operations (Get
, List
, Create
, Update
, Delete
, Watch
, Patch
).
This structure – Clientset -> GroupVersionClient -> ResourceInterface
– provides a clear and type-safe way to navigate the Kubernetes API using Go.
Beyond Typed Clientsets: While we'll primarily use these standard, type-safe clientsets in this book, it's worth knowing that client-go
also offers other client types like the DynamicClient
(for working with arbitrary resources, including CRDs, without compile-time type checking) and the DiscoveryClient
(for finding out which API groups, versions, and resources are supported by the cluster). We might touch upon these later if needed.
Now that we have our clientset
and know how to access interfaces for specific resources, let's finally perform some basic operations, like listing Pods and Nodes in our cluster!
Last updated
Was this helpful?