Suraj Narwade
Suraj Narwade
~1 min read

Categories

Tags

With reference to the previous blog post, we are importing clientcmd package to work on kubeconfig which contains essential information like cluster URL, namespace, auth details to talk to cluster.

import k8s.io/client-go/tools/clientcmd

BuildConfigFromFlags function from clientcmd creates config from either master URL or kubeconfig filepath.

config, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath)

if nothing (master URL or kubeconfig file path) is provided, then it will fallback to inClusterConfig which we will see in the next blog post.

if err is occurred that means, kubeconfig is not parsed properly or kubeconfigPath is not correct.

config is of type rest.Config (coming from k8s.io/client-go/rest) which then used to create actual clientsets. we will see that in upcoming blog posts.

if you just want to play around kubeconfig, you can also use,

config, err := clientcmd.LoadFromFile(kubeConfigPath)

where config is of type clientcmdapi.Config which is go struct for kubeconfig.

sample kubeconfig:

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: REDACTED
    server: https://192.168.42.120:8443
  name: 192-168-42-120:8443
contexts:
- context:
    cluster: 192-168-42-120:8443
    namespace: myproject
    user: developer/192-168-42-120:8443
  name: minishift
current-context: minishift
kind: Config
preferences: {}
users:
- name: developer/192-168-42-120:8443
  user:
    token: <token>