Explore client-go Informer Patterns

Invoke the Kubernetes resources without overloading the cluster

Stefanie Lai
CodeX

--

from Unsplash @uncle_rickie

In platform development, our cluster runs operators involving multiple teams and various GCP resources, for querying which we often need to write various code including but not limited to bash scripts composed of kubectl, the most commonly used Java fabric8 API lib and go programs like kubectl-plugin written with client-go.

As we all know, client-go is currently the most native support in cluster search as a client-end tool, and the most widely used within the community. Therefore, you can leverage it in daily work by exploring more.

Let’s dissect it from a piece of code, which is the most commonly used client-go code.

namespaces, err := clientset.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
if errors.IsNotFound(err) {
log.Fatal("No namespace in the cluster", err)
} else if err != nil {
log.Fatal("Failed to fetch namespaces in the cluster", err)
}
var names []string
for _, ns := range namespaces.Items {
// code
}

Then to its most commonly used pattern.

clientset.{APIVERSION}.{RESOURCES}.{ACTION}

Essentially, it calls the restclient inside client-go to perform an access to the…

--

--