【问题标题】:Can't override Kubernetes config in Kubernetes Go client无法在 Kubernetes Go 客户端中覆盖 Kubernetes 配置
【发布时间】:2020-04-29 16:07:48
【问题描述】:

我想使用 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"

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

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

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

【问题讨论】:

    标签: go kubernetes kubernetes-go-client


    【解决方案1】:

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

    根据

    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)
    

    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
    }
    

    只返回没有覆盖的配置。你可以在我的补丁中看到一个可能的解决方案

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

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

    仅使用 context.AuthInfo (string) 中提供的用户名键覆盖 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
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-01
      • 1970-01-01
      • 2017-09-10
      • 2019-03-13
      • 2021-09-30
      • 2020-03-04
      • 2018-07-24
      • 2020-08-22
      • 2017-12-26
      相关资源
      最近更新 更多