Weikun Lin
Weikun Lin

Reputation: 1

How to init config from memory on client go

    var kubeconfig *string
    if home := homedir.HomeDir(); home != "" {
        kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
    } else {
        kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
    }
    flag.Parse()

    config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
    if err != nil {
        panic(err)
    }

The above code snippet demonstrates building the kubernetes config from input flags using client-go, but is there a way to construct the config from a kubeconfig in memory?

Upvotes: 0

Views: 1041

Answers (1)

Algebra8
Algebra8

Reputation: 1375

Try RESTConfigFromKubeConfig.

// RESTConfigFromKubeConfig is a convenience method to give back a restconfig from your kubeconfig bytes.
// For programmatic access, this is what you want 80% of the time
func RESTConfigFromKubeConfig(configBytes []byte) (*restclient.Config, error)
// Get the kubeconfig bytes from somewhere.
kubeConfigBytes := []byte("...")

config, err := clientcmd.RESTConfigFromKubeConfig(kubeConfigBytes)
if err != nil {
    log.Fatal(err)
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
   log.Fatal(err)
}

// Do something with clientset.
fmt.Println(clientset)

Upvotes: 1

Related Questions