client-go Documentation

repository·master·Indexed 27 days ago

https://github.com/kubernetes/client-go

The official Go client library for interacting with Kubernetes clusters. It provides tools for API access via typed (kubernetes.Clientset) and dynamic (dynamic.DynamicClient) clients, service discovery, and controller development using Informers and WorkQueues. The library includes support for in-cluster and out-of-cluster configuration, server-side apply configurations for resources like DaemonSets and Deployments, and structured API error handling.

Tokens
5.2K
Snippets
5
Records
40
Agent score
94%

What's inside client-go

  1. Overview of client-go packages

    master

    The client-go library provides several packages for interacting with Kubernetes:

    • kubernetes: Contains the clientset to access the Kubernetes API.
    • discovery: Used to discover APIs supported by a Kubernetes API server.
    • dynamic: Provides a dynamic client for performing generic operations on arbitrary Kubernetes API objects.
    • plugin/pkg/client/auth: Contains optional authentication plugins for obtaining credentials from external sources.
    • transport: Used to set up authentication and establish connections.
    • tools/cache: Useful for writing Kubernetes controllers.
  2. Install a specific version of client-go

    master

    When working with specific Kubernetes clusters, you should match your client-go version to your Kubernetes version using the appropriate tag format:

    • For Kubernetes >= v1.17.0: Use the v0.x.y tag format. (e.g., Kubernetes v1.20.4 uses k8s.io/client-go@v0.20.4).
    • For Kubernetes < v1.17.0: Use the kubernetes-1.x.y tag format. (e.g., Kubernetes v1.16.3 uses k8s.io/client-go@kubernetes-1.16.3).

    After running the command, run go mod tidy to finalize the dependency in your go.mod file.

  3. Client-go versioning and compatibility

    master

    Versioning Convention

    • For each v1.x.y Kubernetes release, the client-go major version (first digit) remains 0 (e.g., v0.x.y).
    • Bugfixes update the patch version (third digit).
    • New minor versions result in new branches and tags.

    Compatibility

    • Your code <-> client-go: The v0.x.y tags indicate that Go APIs may change in incompatible ways between versions. Use specific version requirements in your dependency management to avoid breaking changes.
    • client-go <-> Kubernetes clusters: Kubernetes is backwards compatible with clients. Older client-go versions will work with many different Kubernetes cluster versions. Bugfixes are backported to older versions, but new features are not.
  4. Use Server-Side Apply for declarative object mutation

    master
    Instead of the traditional get-modify-update pattern, use Server-Side Apply to allow multiple controllers to safely co-manage the same object. The applyconfigurations package provides a generated, type-safe builder API to construct these declarative patches.
  5. Implement the Controller pattern with Informers and WorkQueues

    master

    To build resilient controllers, follow the event-driven, cached model provided by tools/cache. This pattern decouples event detection from reconciliation:

    1. Reflector: Performs a LIST and then a WATCH to stream changes.
    2. Informer/Indexer: Populates a local cache (Indexer) and triggers Event Handlers.
    3. WorkQueue: Event handlers should only add the object's key to a util/workqueue. This prevents blocking the watch stream.
    4. Controller: The controller processes keys from the WorkQueue and uses a Lister to read data from the Indexer cache.

    This architecture allows the controller to perform retries with exponential backoff without impacting the underlying WATCH stream.

  6. Configure client-go for In-Cluster or Out-of-Cluster usage

    master

    The method for configuring the client depends on where your application is running:

    • In-Cluster: If your application runs inside a Pod within a Kubernetes cluster, use the in-cluster configuration pattern.
    • Out-of-Cluster: If your application runs outside the cluster (e.g., on your local machine), use the out-of-cluster configuration pattern.

    Refer to the specific examples in the repository for implementation details: examples/in-cluster-client-configuration and examples/out-of-cluster-client-configuration.

  7. Configure client authentication and settings with rest.Config

    master

    The rest.Config object is the in-memory representation of your Kubernetes client configuration. To produce this object, use the tools/clientcmd package, which handles the complexity of parsing kubeconfig files, merging contexts, and managing external authentication providers like OIDC.

    To control client-side rate limiting and interact with the server's API Priority and Fairness system, configure the following fields in your rest.Config:

    • QPS: The client-side rate limit.
    • Burst: The burst capacity for requests.
  8. Install the latest version of client-go

    master

    To use the latest version of client-go, ensure you are using Go 1.16 or later and run the go get command. This will add k8s.io/client-go to your Go module. You can then run go mod tidy or perform a build/test/run to download dependencies and update your go.mod file.

    go get k8s.io/client-go@latest
  9. Ensure High Availability with leader election

    master
    To ensure single-writer semantics in a high-availability setup, use the tools/leaderelection package. This allows multiple replicas to compete for a lock on a shared Lease object, ensuring only one instance acts as the leader at a time.
  10. Install client-go

    master

    To add client-go to your Go project, use the go get command. Ensure you are using Go 1.16+ for the latest version or Go 1.11+ for specific versions.

    Install the latest version

    go get k8s.io/client-go@latest

    Install a specific version

    go get k8s.io/client-go@v0.20.4
  11. Use ClientWithContext for contextual logging and cancellation

    master
    When working with the openapi package, always prefer the ClientWithContext interface and its associated methods over the base Client interface. This allows your application to properly handle request timeouts and cancellations via context.Context.
  12. Troubleshoot Go versions prior to 1.16

    master

    If you encounter the error module k8s.io/client-go@latest found (v1.5.2), but does not contain package k8s.io/client-go/..., it indicates you are using a Go version older than 1.16. You must explicitly specify a version instead of using @latest.

    go get k8s.io/client-go@v0.20.4