doucan957495 2019-03-23 13:22
浏览 1003
已采纳

如何从.kube / config中设置的当前上下文获取名称空间

I am trying to get programmatically in Go, the namespace of the current-context from ~/.kube/config.

So far what I tried is from these modules:

    "k8s.io/client-go/tools/clientcmd"
    "k8s.io/client-go/kubernetes"



kubeconfig := filepath.Join(
     os.Getenv("HOME"), ".kube", "config",
)
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
    log.Fatal(err)
}

fmt.Printf("Namespace: %s
", config.Namespace())
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
    log.Fatal(err)
}

But still no clue if clientset can give me the namespace I am looking for. From this thread: How to get current namespace of an in-cluster go Kubernetes client

It says something of this to be done: kubeconfig.Namespace()

  • 写回答

2条回答 默认 最新

  • dqfxao2898 2019-03-23 16:28
    关注

    I found a solution using NewDefaultClientConfigLoadingRules and then loading the rules. This works if your config is loadable with the default client config loading rules.

    Example:

    package main
    
    import (
            "github.com/davecgh/go-spew/spew"
            "k8s.io/client-go/tools/clientcmd"
    )
    
    func main() {
            clientCfg, err := clientcmd.NewDefaultClientConfigLoadingRules().Load()
            spew.Dump(clientCfg, err)
    }
    

    Gives you a https://godoc.org/k8s.io/client-go/tools/clientcmd/api#Config which contains the current context including its namespace.

    Contexts: (map[string]*api.Context) (len=1) {
      (string) (len=17) "xxx.xxxxx.xxx": (*api.Context)(0xc0001b2b40)({
       LocationOfOrigin: (string) (len=30) "/path/to/.kube/config",
       Cluster: (string) (len=17) "xxx.xxxxx.xxx",
       AuthInfo: (string) (len=29) "xxxx@xxxx.com",
       Namespace: (string) (len=7) "default",
       Extensions: (map[string]runtime.Object) {
       }
      })
     },
     CurrentContext: (string) (len=17) "xxx.xxxxx.xxx",
    

    For your information, ClientConfigLoadingRules is a structure with different properties to tell the cliclient where to load the config from. The default one will use the path in your KUBECONFIG environment variable in the Precedence field.

    (*clientcmd.ClientConfigLoadingRules)(0xc0000a31d0)({
     ExplicitPath: (string) "",
     Precedence: ([]string) (len=1 cap=1) {
      (string) (len=30) "/path/to/.kube/config"
     },
     MigrationRules: (map[string]string) (len=1) {
      (string) (len=30) "/path/to/.kube/config": (string) (len=35) "/path/to/.kube/.kubeconfig"
     },
     DoNotResolvePaths: (bool) false,
     DefaultClientConfig: (clientcmd.ClientConfig) <nil>
    })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大