Sai Vamsi
Sai Vamsi

Reputation: 141

How to use inclusterconfig of kubernetes in go

I am using kubernetes-client. Here, I am trying to load my config file using the filepath. I want to create the client using inclusterconfig without having to load a kubeconfig file. How do I go about doing this?

var kube_config_path = "/home/saivamsi/.kube/config"    
var config, conferr = clientcmd.BuildConfigFromFlags("", kube_config_path)
var clientset, cler = kubernetes.NewForConfig(config)

Upvotes: 0

Views: 9500

Answers (2)

Miguel Elias
Miguel Elias

Reputation: 1

If you must only use the base client package, then the above answer works just fine. But if you're ok on importing controller-runtime, then this one is a much more elegant solution:

package k8s

import (
    "k8s.io/client-go/kubernetes"
    cruntimeconfig "sigs.k8s.io/controller-runtime/pkg/client/config"
)

func SetupClient() *kubernetes.Clientset {
    return kubernetes.NewForConfigOrDie(cruntimeconfig.GetConfigOrDie())
}

This works for both in and out cluster authentication and also honours the KUBECONFIG env var.

Fun fact: This basically how kubebuilder internally connects to the cluster.

Upvotes: 0

Arghya Sadhu
Arghya Sadhu

Reputation: 44677

Here is an example

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"
    //
    // Uncomment to load all auth plugins
    // _ "k8s.io/client-go/plugin/pkg/client/auth"
    //
    // Or uncomment to load specific auth plugins
    // _ "k8s.io/client-go/plugin/pkg/client/auth/azure"
    // _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
    // _ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
    // _ "k8s.io/client-go/plugin/pkg/client/auth/openstack"
)

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))

        // Examples for error handling:
        // - Use helper functions e.g. errors.IsNotFound()
        // - And/or cast to StatusError and use its properties like e.g. ErrStatus.Message
        _, err = clientset.CoreV1().Pods("default").Get(context.TODO(), "example-xxxxx", metav1.GetOptions{})
        if errors.IsNotFound(err) {
            fmt.Printf("Pod example-xxxxx not found in default namespace\n")
        } else if statusError, isStatus := err.(*errors.StatusError); isStatus {
            fmt.Printf("Error getting pod %v\n", statusError.ErrStatus.Message)
        } else if err != nil {
            panic(err.Error())
        } else {
            fmt.Printf("Found example-xxxxx pod in default namespace\n")
        }

        time.Sleep(10 * time.Second)
    }
}

Upvotes: 4

Related Questions