In the kubernetes go client, what is a clientset
?
It is defined in multiple places.
In the client-go
package. https://github.com/kubernetes/client-go/blob/62b2cb756b8cea8fba00764ff123993eb44dbd48/kubernetes/clientset.go#L120
In the kubernetes
package https://github.com/kubernetes/kubernetes/blob/80e344644e2b6222296f2f03551a8d0273c7cbce/pkg/client/clientset_generated/internalclientset/clientset.go#L64
The documentation says the same thing for both of them:
Clientset contains the clients for groups. Each group has exactly one version included in a Clientset.
This is confusing. What is a group?
Every resource type in Kubernetes (Pods, Deployments, Services and so on) is a member of an API group. These logically "group" the different types. Some examples of groups are
core
extensions
batch
apps
authentication
autoscaling
Groups also contain versions. Versions allow developers to introduce breaking changes to APIs, and manage them as they do. Some examples of versions inside a group
core/v1
extensions/v1beta
apps/v1beta1
batch/v1
, batch/v2alpha1
(notice the two versions inside the same group)authentication/v1
, authentication/v1beta1
autoscaling/v1
, autoscaling/v2alpha1
So the client documentation is saying that it's creating a different client for every group.
The description given by @Jose Armesto is correct, I would like to support it with a snippet.
package main
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/kubernetes"
)
var kubeconfig string
func init() {
// kubeconfig file parsing
flag.StringVar(&kubeconfig, "kubeconfig", "", "path to Kubernetes config file")
flag.Parse()
}
func main() {
// create the config object from kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
// create clientset (set of muliple clients) for each Group (e.g. Core),
// the Version (V1) of Group and Kind (e.g. Pods) so GVK.
clientset, err := kubernetes.NewForConfig(config)
// executes GET request to K8s API to get pods 'cart' from 'prepayment' namespace
pod, err := clientset.CoreV1().Pods("prepayment").Get("cart", metav1.GetOptions{})
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With