Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a Kubernetes client-go "clientset"?

Tags:

go

kubernetes

In the kubernetes go client, what is a clientset?

It is defined in multiple places.

  1. In the client-go package. https://github.com/kubernetes/client-go/blob/62b2cb756b8cea8fba00764ff123993eb44dbd48/kubernetes/clientset.go#L120

  2. 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?

like image 726
Charles Holbrow Avatar asked Jan 22 '18 18:01

Charles Holbrow


2 Answers

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.

like image 83
Jose Armesto Avatar answered Sep 27 '22 15:09

Jose Armesto


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{})
}
like image 33
jack_t Avatar answered Sep 27 '22 16:09

jack_t