如何从.kube/config中设置的当前上下文中获取名称空间 [英] How to get namespace from current-context set in .kube/config

查看:68
本文介绍了如何从.kube/config中设置的当前上下文中获取名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式从〜/.kube/config中获取当前上下文的名称空间Go.

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\n", config.Namespace())
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
    log.Fatal(err)
}

但是仍然不知道clientset是否可以给我我想要的名称空间.从这个线程: 如何获取in的当前名称空间-集群去Kubernetes客户端

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

它说了一些要做的事情: kubeconfig.Namespace()

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

推荐答案

我找到了使用NewDefaultClientConfigLoadingRules然后加载规则的解决方案.如果您的配置可以使用默认的客户端配置加载规则进行加载,则此方法有效.

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.

示例:

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

为您提供 https://godoc.org/k8s.io/client-go/tools/clientcmd/api#Config ,其中包含当前上下文及其名称空间.

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",

供您参考,ClientConfigLoadingRules是具有不同属性的结构,用于告诉cliclient从何处加载配置.默认值将使用KUBECONFIG环境变量中Precedence字段中的路径.

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

这篇关于如何从.kube/config中设置的当前上下文中获取名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆