无法覆盖 Kubernetes Go 客户端中的 Kubernetes 配置

Hed*_*dge 5 go kubernetes kubernetes-go-client

我想使用 Kubernetes Go 客户端在集群中执行各种操作。我正在加载kubeconfig包含多个集群和上下文的本地。默认上下文是prod,我要覆盖的配置值之一是CurrentContext

    clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
        &clientcmd.ClientConfigLoadingRules{ExplicitPath: "/Users/me/.kube/config"},
        &clientcmd.ConfigOverrides{
            CurrentContext: "stage",
        })

    rawConfig, _ := clientConfig.RawConfig()
    log.Printf(rawConfig.CurrentContext) // outputs "prod" instead of "stage"
Run Code Online (Sandbox Code Playgroud)

当我检查 RawConfig()当前上下文时,仍然是“prod”而不是“stage”。为什么配置覆盖不起作用?

AuthInfo 等的覆盖如何工作?覆盖只接受一个,AuthInfo而配置包含一个映射AuthInfo

GitHub 相关问题https://github.com/kubernetes/client-go/issues/735

Vit*_*dny 6

为什么配置覆盖不起作用?

根据

https://github.com/kubernetes/client-go/blob/a432bd9ba7da427ae0a38a6889d72136bce4c4ea/tools/clientcmd/client_config.go#L57-L58

// ClientConfig is used to make it easy to get an api server client
type ClientConfig interface {
    // RawConfig returns the merged result of all overrides
    RawConfig() (clientcmdapi.Config, error)
Run Code Online (Sandbox Code Playgroud)

RawConfig应该返回带有覆盖的配置,但实际上没有

https://github.com/kubernetes/client-go/blob/a432bd9ba7da427ae0a38a6889d72136bce4c4ea/tools/clientcmd/client_config.go#L122-L124

func (config *DirectClientConfig) RawConfig() (clientcmdapi.Config, error) {
    return config.config, nil
}
Run Code Online (Sandbox Code Playgroud)

只需返回配置而不覆盖。您可以在我的补丁中看到可能的解决方案

https://github.com/vvelikodny/kubernetes-client-go/pull/1/files

另外 AuthInfo 等的覆盖如何工作?覆盖仅接受单个 AuthInfo,而配置包含 AuthInfo 等的映射。

仅使用 context.AuthInfo(字符串)中显示的用户名密钥覆盖 AuthInfo。

https://github.com/kubernetes/client-go/blob/a432bd9ba7da427ae0a38a6889d72136bce4c4ea/tools/clientcmd/client_config.go#L424-L437

https://github.com/kubernetes/client-go/blob/a432bd9ba7da427ae0a38a6889d72136bce4c4ea/tools/clientcmd/client_config.go#L388-L394

// getAuthInfoName returns a string containing the current authinfo name for the current context,
// and a boolean indicating  whether the default authInfo name is overwritten by a user-set flag, or
// left as its default value
func (config *DirectClientConfig) getAuthInfoName() (string, bool) {
    if len(config.overrides.Context.AuthInfo) != 0 {
        return config.overrides.Context.AuthInfo, true
    }
    context, _ := config.getContext()
    return context.AuthInfo, false
}
Run Code Online (Sandbox Code Playgroud)