26

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?

4 Answers 4

30

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.

3
  • Thank you! What is the difference between the clientset in the kubernetes package and the clientset in the client-go package? Jan 22, 2018 at 20:26
  • I don't really know if there is any difference Jan 23, 2018 at 8:36
  • 6
    development is made in the kubernetes repository, changes are "mirrored" once a day to the client-go repository. The last paragraph "Contributing" in the readme of client-go gives a hint about that. Reason is afaik that client-go has always been in the k8s repo, but this way you can use it without needing to download/import all the k8s code. github.com/kubernetes/client-go/blob/master/README.md
    – slintes
    Jan 25, 2018 at 10:04
6

The description given by @Jose Armesto is correct, I would like to support it with a snippet.

package main

import (
    "context"
    "flag"
    "fmt"

    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/tools/clientcmd"
)

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)

    ctx := context.Background()
    // executes GET request to K8s API to get pods 'cart' from 'prepayment' namespace
    pod, err := clientset.CoreV1().Pods("prepayment").Get(ctx, "cart", metav1.GetOptions{})
    fmt.Printf("err %v", err)

    fmt.Printf("pod %v", pod)

}
0

The code is actually the same for the two locations. The Kubernetes project writes and manages all the code in a mono-repo (kubernetes/kubernetes), but then publishes the code they want other projects to consume to other repos to work better with the Go module system. You can see a reference to the details of this publishing process in this table, though I'm not sure where the process is clearly documented.

BTW - you should consume this code through k8s.io/client-go as an import, which will fetch from the github.com/kubernetes/client-go repository.

0

Following jack_t's snippet, I'll add additional snippet regarding the creation of clientset where the Go client authenticate to the K8S API from a pod running inside the cluster:

package main

import (
    "context"
    "fmt"
    "time"

    "k8s.io/apimachinery/pkg/api/errors"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/rest"
)

func main() {
    // creates the in-cluster config
    config, err := rest.InClusterConfig()
    if err != nil {
        panic(err.Error())
    }
    // creates the clientset
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        panic(err.Error())
    }
    for {
        // get pods in all the namespaces by omitting namespace
        // Or specify namespace to get pods in particular namespace
        pods, err := clientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
        if err != nil {
            panic(err.Error())
        }
        fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
    }
}

In this case, the client-go library will use the Service Account token that is mounted inside the Pod at the /var/run/secrets/kubernetes.io/serviceaccount path when the rest.InClusterConfig() is used.

Reference - here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.