5.26 Kubernetes Client Object Interaction
Kubernetes Client Object Interaction allows users to manage cluster resources via structured API calls and object models.
Kubernetes Client Object Interaction is the practical layer of how actual client software, ranging from the kubectl command-line tool to generated typed libraries and dynamic, schema-agnostic clients, connects to a cluster, authenticates, and constructs the correctly shaped requests needed to create, inspect, and manipulate objects according to the API and object model underlying the entire system.
Establishing a Connection
kubeconfig as the Connection Description
Most client tools locate and connect to a cluster using a kubeconfig file, which bundles cluster endpoint information, certificate authority data, and user authentication credentials into named contexts, allowing a single client installation to switch between multiple clusters simply by selecting a different context.
In-Cluster Configuration
Workloads running inside the cluster itself, such as controllers deployed as Pods, typically use an in-cluster configuration mode instead, reading the API server address from environment variables and a mounted ServiceAccount token, avoiding the need for an external kubeconfig file when the client is already running within the environment it needs to talk to.
Typed Clients
Generated Clientsets
Language-specific client libraries, such as Go's client-go, generate strongly typed clientsets from the API's known types, offering method calls like Create, Get, Update, and Delete scoped to a specific kind, giving compile-time type safety and IDE auto-completion for interacting with well-known, built-in resources.
Generated Clients for Custom Resources
Custom Resource authors commonly generate their own typed clientsets using the same code generation tooling the project itself uses for built-in types, producing a client experience for their custom resource that is indistinguishable in ergonomics from working with a native Kubernetes type.
The Dynamic Client
Schema-Agnostic Interaction
A dynamic client operates on unstructured data, typically represented as a generic map-like structure keyed by field name, allowing a single piece of client code to interact with any resource type, known or unknown at compile time, purely by specifying its GroupVersionResource at runtime.
Trade-Offs Versus Typed Clients
The dynamic client trades compile-time type safety for flexibility, making it well suited for generic tools, such as kubectl itself for many operations, or controllers that need to operate across a configurable, not-known-in-advance set of resource types, at the cost of requiring more careful runtime handling of field access and type assertions.
Authentication and Credential Handling
Client Certificates and Bearer Tokens
Clients authenticate to the API server using one of several supported mechanisms, commonly client TLS certificates, static or dynamically issued bearer tokens, or, for workloads running inside the cluster, an automatically mounted, time-limited ServiceAccount token, with the specific mechanism configured through the kubeconfig or in-cluster environment.
Credential Plugins
For cloud-managed clusters, kubeconfig entries often reference an external credential plugin, invoked by the client at connection time to obtain short-lived, dynamically generated authentication tokens from a cloud identity provider, avoiding the need to store long-lived static credentials directly in the kubeconfig file.
Constructing Well-Formed Requests
Serialization Format Negotiation
Client libraries handle content negotiation transparently, typically defaulting to Protocol Buffers for typed, built-in resource interactions when supported, and falling back to JSON for custom resources or when working through the dynamic client, since custom resource schemas are not known ahead of time in a way that supports efficient binary encoding.
Automatic Retry and Backoff
Well-implemented clients incorporate automatic retry logic with exponential backoff for transient failures, such as temporary network issues or the API server signaling it is under load, improving the resilience of automation without requiring every piece of calling code to implement its own retry handling.
Higher-Level Interaction Patterns
Informers and Listers
Building on the list-and-watch model, client libraries provide informer and lister abstractions that maintain a continuously synchronized local cache of a resource collection, letting controller code query current state instantly from memory rather than issuing a live API request for every read.
Client-Side Rate Limiting
To avoid overwhelming the API server, client libraries commonly implement client-side rate limiting and request queuing, smoothing out bursts of requests a controller might otherwise generate when reconciling many objects in quick succession, complementing the API server's own server-side rate limiting mechanisms.