Top Related Projects
Official Python client library for kubernetes
Repo for the controller-runtime subproject of kubebuilder (sig-apimachinery)
Customization of kubernetes YAML configurations
Kubebuilder - SDK for building Kubernetes APIs using CRDs
Complete container management platform
The Kubernetes Package Manager
Quick Overview
Kubernetes/client-go is the official Go client library for Kubernetes. It provides a set of tools and APIs to interact with Kubernetes clusters programmatically, allowing developers to create, manage, and automate Kubernetes resources and operations using Go programming language.
Pros
- Official and well-maintained library with regular updates
- Comprehensive coverage of Kubernetes API features
- Supports both in-cluster and out-of-cluster authentication
- Provides helper utilities for common tasks and patterns
Cons
- Steep learning curve for newcomers to Kubernetes
- Large dependency footprint
- API changes between versions can require code updates
- Documentation can be sparse for some advanced use cases
Code Examples
- Creating a Kubernetes clientset:
import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
config, err := rest.InClusterConfig()
if err != nil {
// handle error
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
// handle error
}
- Listing pods in a namespace:
pods, err := clientset.CoreV1().Pods("default").List(context.TODO(), metav1.ListOptions{})
if err != nil {
// handle error
}
for _, pod := range pods.Items {
fmt.Printf("Pod: %s\n", pod.Name)
}
- Creating a deployment:
deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "example-deployment",
},
Spec: appsv1.DeploymentSpec{
Replicas: pointer.Int32Ptr(3),
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "example",
},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": "example",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "example-container",
Image: "nginx:latest",
},
},
},
},
},
}
result, err := clientset.AppsV1().Deployments("default").Create(context.TODO(), deployment, metav1.CreateOptions{})
if err != nil {
// handle error
}
fmt.Printf("Created deployment %q.\n", result.GetObjectMeta().GetName())
Getting Started
-
Install the client-go library:
go get k8s.io/client-go@latest
-
Import the necessary packages in your Go code:
import ( "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" )
-
Create a Kubernetes client:
// For in-cluster configuration config, err := rest.InClusterConfig() // For out-of-cluster configuration // config, err := clientcmd.BuildConfigFromFlags("", "/path/to/kubeconfig") if err != nil { // handle error } clientset, err := kubernetes.NewForConfig(config) if err != nil { // handle error }
-
Use the clientset to interact with the Kubernetes API.
Competitor Comparisons
Official Python client library for kubernetes
Pros of python
- Easier to learn and use for Python developers
- More concise syntax for common operations
- Better integration with Python-based tools and frameworks
Cons of python
- Generally slower performance compared to Go
- Less mature and feature-complete than client-go
- Smaller community and ecosystem
Code comparison
client-go:
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
pods, err := clientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
python:
from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
pods = v1.list_pod_for_all_namespaces()
The python client offers a more concise syntax for common operations, making it easier to read and write. However, client-go provides more fine-grained control and better performance for complex operations.
Both libraries offer similar functionality, but client-go is generally considered more mature and feature-complete. It also benefits from Go's strong typing and concurrency support, making it a better choice for large-scale production environments.
The python client is ideal for scripting, prototyping, and integration with Python-based tools, while client-go is better suited for building robust, high-performance applications and services.
Repo for the controller-runtime subproject of kubebuilder (sig-apimachinery)
Pros of controller-runtime
- Higher-level abstractions for building Kubernetes controllers and operators
- Built-in support for leader election, metrics, and health probes
- Simplified reconciliation loop implementation with predefined patterns
Cons of controller-runtime
- Steeper learning curve for developers new to Kubernetes controllers
- Less flexibility for custom implementations compared to client-go
- Potential overhead for simpler use cases that don't require all features
Code Comparison
client-go example:
clientset, err := kubernetes.NewForConfig(config)
pod, err := clientset.CoreV1().Pods(namespace).Get(context.TODO(), name, metav1.GetOptions{})
controller-runtime example:
var pod corev1.Pod
err := r.Get(ctx, client.ObjectKey{Namespace: namespace, Name: name}, &pod)
controller-runtime provides a more concise API for common operations, while client-go offers lower-level access to Kubernetes resources.
Both libraries are essential for Kubernetes development, with client-go serving as a foundation and controller-runtime building upon it to simplify controller creation. The choice between them depends on the specific requirements of your project and your familiarity with Kubernetes concepts.
Customization of kubernetes YAML configurations
Pros of Kustomize
- Simplifies Kubernetes resource management with a template-free approach
- Allows for easy customization of base configurations without modifying original files
- Integrates well with existing kubectl workflows and GitOps practices
Cons of Kustomize
- Limited to YAML manipulation and doesn't provide programmatic access to Kubernetes API
- May require additional tooling for complex scenarios or dynamic resource generation
- Learning curve for users unfamiliar with Kustomize-specific concepts and syntax
Code Comparison
Kustomize example:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
client-go example:
clientset, err := kubernetes.NewForConfig(config)
deployment, err := clientset.AppsV1().Deployments(namespace).Get(context.TODO(), name, metav1.GetOptions{})
Key Differences
- Kustomize focuses on declarative configuration management
- client-go provides programmatic access to Kubernetes API
- Kustomize is typically used for resource templating and patching
- client-go is used for building custom controllers and operators
Use Cases
- Kustomize: CI/CD pipelines, GitOps workflows, environment-specific configurations
- client-go: Custom Kubernetes controllers, operators, and automation tools
Community and Ecosystem
- Both projects are part of the Kubernetes ecosystem
- Kustomize has been integrated into kubectl, increasing its adoption
- client-go is widely used in Kubernetes-native applications and tools
Kubebuilder - SDK for building Kubernetes APIs using CRDs
Pros of Kubebuilder
- Simplifies the process of creating Kubernetes operators and custom resources
- Provides scaffolding and code generation for faster development
- Includes built-in testing frameworks and utilities
Cons of Kubebuilder
- Steeper learning curve for developers new to Kubernetes concepts
- Less flexibility for fine-grained control compared to direct client-go usage
- May generate unnecessary boilerplate code for simple use cases
Code Comparison
client-go example:
clientset, err := kubernetes.NewForConfig(config)
pod, err := clientset.CoreV1().Pods("default").Get(context.TODO(), "mypod", metav1.GetOptions{})
Kubebuilder example:
type MyCustomResource struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec MyCustomResourceSpec `json:"spec,omitempty"`
Status MyCustomResourceStatus `json:"status,omitempty"`
}
Summary
client-go is a low-level Kubernetes API client library, offering direct access to Kubernetes resources. Kubebuilder, on the other hand, is a framework for building Kubernetes APIs and controllers, providing higher-level abstractions and tools. While client-go offers more flexibility and control, Kubebuilder simplifies the development of custom resources and operators at the cost of some complexity and potential overhead.
Complete container management platform
Pros of Rancher
- Provides a complete container management platform with a user-friendly GUI
- Offers multi-cluster management and simplified Kubernetes deployment
- Includes built-in monitoring, logging, and security features
Cons of Rancher
- Adds complexity and overhead compared to using client-go directly
- May have a steeper learning curve for users new to container orchestration
- Potential vendor lock-in with Rancher-specific features
Code Comparison
client-go example:
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
clientset, err := kubernetes.NewForConfig(config)
pods, err := clientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
Rancher API example:
client, err := rancher.NewClient(url, token)
cluster, err := client.Cluster.ByID(clusterID)
pods, err := cluster.CoreV1().Pods("").List(metav1.ListOptions{})
Summary
client-go is a Kubernetes client library for Go, providing direct access to the Kubernetes API. Rancher is a complete container management platform that simplifies Kubernetes deployment and management. While client-go offers more flexibility and direct control, Rancher provides a more user-friendly experience with additional features for cluster management and monitoring.
The Kubernetes Package Manager
Pros of Helm
- Simplifies Kubernetes application deployment and management
- Provides templating and package management for Kubernetes resources
- Offers a higher-level abstraction for managing complex applications
Cons of Helm
- Steeper learning curve for users new to Kubernetes
- Potential security risks if not properly configured
- Less granular control over individual Kubernetes resources
Code Comparison
Helm (Chart.yaml):
apiVersion: v2
name: my-app
description: A Helm chart for my application
version: 0.1.0
appVersion: 1.16.0
client-go (main.go):
import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
config, err := rest.InClusterConfig()
clientset, err := kubernetes.NewForConfig(config)
Helm focuses on packaging and deploying applications using charts, while client-go provides a low-level API for interacting with Kubernetes resources programmatically. Helm is better suited for application deployment and management, while client-go is more appropriate for building custom Kubernetes controllers or automation tools.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
client-go
Go clients for talking to a kubernetes cluster.
We recommend using the v0.x.y
tags for Kubernetes releases >= v1.17.0
and
kubernetes-1.x.y
tags for Kubernetes releases < v1.17.0
.
The fastest way to add this library to a project is to run go get k8s.io/client-go@latest
with go1.16+.
See INSTALL.md for detailed installation instructions and troubleshooting.
Table of Contents
- What's included
- Versioning
- Kubernetes tags
- How to get it
- How to use it
- Dependency management
- Contributing code
What's included
- The
kubernetes
package contains the clientset to access Kubernetes API. - The
discovery
package is used to discover APIs supported by a Kubernetes API server. - The
dynamic
package contains a dynamic client that can perform generic operations on arbitrary Kubernetes API objects. - The
plugin/pkg/client/auth
packages contain optional authentication plugins for obtaining credentials from external sources. - The
transport
package is used to set up auth and start a connection. - The
tools/cache
package is useful for writing controllers.
Versioning
-
For each
v1.x.y
Kubernetes release, the major version (first digit) would remain0
. -
Bugfixes will result in the patch version (third digit) changing. PRs that are cherry-picked into an older Kubernetes release branch will result in an update to the corresponding branch in
client-go
, with a corresponding new tag changing the patch version.
Branches and tags.
We will create a new branch and tag for each increment in the minor version number. We will create only a new tag for each increment in the patch version number. See semver for definitions of major, minor, and patch.
The HEAD of the master branch in client-go will track the HEAD of the master branch in the main Kubernetes repo.
Compatibility: your code <-> client-go
The v0.x.y
tags indicate that go APIs may change in incompatible ways in
different versions.
See INSTALL.md for guidelines on requiring a specific version of client-go.
Compatibility: client-go <-> Kubernetes clusters
Since Kubernetes is backwards compatible with clients, older client-go
versions will work with many different Kubernetes cluster versions.
We will backport bugfixes--but not new features--into older versions of
client-go
.
Compatibility matrix
Kubernetes 1.23 | Kubernetes 1.24 | Kubernetes 1.25 | Kubernetes 1.26 | Kubernetes 1.27 | Kubernetes 1.28 | |
---|---|---|---|---|---|---|
kubernetes-1.23.0 /v0.23.0 | â | +- | +- | +- | +- | +- |
kubernetes-1.24.0 /v0.24.0 | +- | â | +- | +- | +- | +- |
kubernetes-1.25.0 /v0.25.0 | +- | +- | â | +- | +- | +- |
kubernetes-1.26.0 /v0.26.0 | +- | +- | +- | â | +- | +- |
kubernetes-1.27.0 /v0.27.0 | +- | +- | +- | +- | â | +- |
kubernetes-1.28.0 /v0.28.0 | +- | +- | +- | +- | +- | â |
HEAD | +- | +- | +- | +- | +- | +- |
Key:
â
Exactly the same features / API objects in both client-go and the Kubernetes version.+
client-go has features or API objects that may not be present in the Kubernetes cluster, either due to that client-go has additional new API, or that the server has removed old API. However, everything they have in common (i.e., most APIs) will work. Please note that alpha APIs may vanish or change significantly in a single release.-
The Kubernetes cluster has features the client-go library can't use, either due to the server has additional new API, or that client-go has removed old API. However, everything they share in common (i.e., most APIs) will work.
See the CHANGELOG for a detailed description of changes between client-go versions.
Branch | Canonical source code location | Maintenance status |
---|---|---|
release-1.19 | Kubernetes main repo, 1.19 branch | =- |
release-1.20 | Kubernetes main repo, 1.20 branch | =- |
release-1.21 | Kubernetes main repo, 1.21 branch | =- |
release-1.22 | Kubernetes main repo, 1.22 branch | =- |
release-1.23 | Kubernetes main repo, 1.23 branch | =- |
release-1.24 | Kubernetes main repo, 1.24 branch | =- |
release-1.25 | Kubernetes main repo, 1.25 branch | â |
release-1.26 | Kubernetes main repo, 1.26 branch | â |
release-1.27 | Kubernetes main repo, 1.27 branch | â |
release-1.28 | Kubernetes main repo, 1.28 branch | â |
client-go HEAD | Kubernetes main repo, master branch | â |
Key:
â
Changes in main Kubernetes repo are actively published to client-go by a bot=
Maintenance is manual, only severe security bugs will be patched.-
Deprecated; please upgrade.
Deprecation policy
We will maintain branches for at least six months after their first stable tag is cut. (E.g., the clock for the release-2.0 branch started ticking when we tagged v2.0.0, not when we made the first alpha.) This policy applies to every version greater than or equal to 2.0.
Why do the 1.4 and 1.5 branch contain top-level folder named after the version?
For the initial release of client-go, we thought it would be easiest to keep separate directories for each minor version. That soon proved to be a mistake. We are keeping the top-level folders in the 1.4 and 1.5 branches so that existing users won't be broken.
Kubernetes tags
This repository is still a mirror of k8s.io/kubernetes/staging/src/client-go, the code development is still done in the staging area.
Since Kubernetes v1.8.0
, when syncing the code from the staging area,
we also sync the Kubernetes version tags to client-go, prefixed with
kubernetes-
. From Kubernetes v1.17.0
, we also create matching semver
v0.x.y
tags for each v1.x.y
Kubernetes release.
For example, if you check out the kubernetes-1.17.0
or the v0.17.0
tag in
client-go, the code you get is exactly the same as if you check out the v1.17.0
tag in Kubernetes, and change directory to staging/src/k8s.io/client-go
.
The purpose is to let users quickly find matching commits among published repos, like sample-apiserver, apiextension-apiserver, etc. The Kubernetes version tag does NOT claim any backwards compatibility guarantees for client-go. Please check the semantic versions if you care about backwards compatibility.
How to get it
To get the latest version, use go1.16+ and fetch using the go get
command. For example:
go get k8s.io/client-go@latest
To get a specific version, use go1.11+ and fetch the desired version using the go get
command. For example:
go get k8s.io/client-go@v0.20.4
See INSTALL.md for detailed instructions and troubleshooting.
How to use it
If your application runs in a Pod in the cluster, please refer to the in-cluster example, otherwise please refer to the out-of-cluster example.
Dependency management
For details on how to correctly use a dependency management for installing client-go, please see INSTALL.md.
Contributing code
Please send pull requests against the client packages in the Kubernetes main repository. Changes in the staging area will be published to this repository every day.
Top Related Projects
Official Python client library for kubernetes
Repo for the controller-runtime subproject of kubebuilder (sig-apimachinery)
Customization of kubernetes YAML configurations
Kubebuilder - SDK for building Kubernetes APIs using CRDs
Complete container management platform
The Kubernetes Package Manager
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot