[英]Use kubectl context in kubernetes client-go
How can I use a normal context to configure the kubernetes client-go?如何使用普通上下文来配置 kubernetes client-go?
package kube
import (
"fmt"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// GetKubeClient creates a Kubernetes config and client for a given kubeconfig context.
func GetKubeClient(context string) (*rest.Config, kubernetes.Interface, error) {
config, err := configForContext(context)
if err != nil {
return nil, nil, err
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, nil, fmt.Errorf("could not get Kubernetes client: %s", err)
}
return config, client, nil
}
// configForContext creates a Kubernetes REST client configuration for a given kubeconfig context.
func configForContext(context string) (*rest.Config, error) {
config, err := getConfig(context).ClientConfig()
if err != nil {
return nil, fmt.Errorf("could not get Kubernetes config for context %q: %s", context, err)
}
return config, nil
}
// getConfig returns a Kubernetes client config for a given context.
func getConfig(context string) clientcmd.ClientConfig {
rules := clientcmd.NewDefaultClientConfigLoadingRules()
rules.DefaultClientConfig = &clientcmd.DefaultClientConfig
overrides := &clientcmd.ConfigOverrides{ClusterDefaults: clientcmd.ClusterDefaults}
if context != "" {
overrides.CurrentContext = context
}
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, overrides)
}
If I try this code (got it from helm), the api server is not correctly set and the client wants to connect to the default host localhost:8080
.如果我尝试此代码(从 helm 获取),则 api 服务器未正确设置,并且客户端想要连接到默认主机
localhost:8080
。
Found the problem.发现问题了。 The implementation of
github.com/imdario/mergo
changed in a newer version and breaks the actual behavior of generating the client config. github.com/imdario/mergo
的实现在较新的版本中发生了变化,并破坏了生成客户端配置的实际行为。 So just only use revision 6633656539c1639d9d78127b7d47c622b5d7b6dc
like in the official kubernetes cient-go repository.所以只使用
6633656539c1639d9d78127b7d47c622b5d7b6dc
修订版,就像在官方 kubernetes cient-go 存储库中一样。
https://github.com/kubernetes/client-go/issues/415 https://github.com/kubernetes/client-go/issues/415
Currently the example recommends doing something like this:目前该示例建议执行以下操作:
kconf, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, err
}
However that won't allow you to specify the context you want to use.但是,这不允许您指定要使用的上下文。 If you look at the source code you'll see that BuildConfigFromFlags is a thin wrapper around NewNonInteractiveDeferredLoadingClientConfig.
如果您查看源代码,您会看到 BuildConfigFromFlags 是一个围绕 NewNonInteractiveDeferredLoadingClientConfig 的瘦包装器。
If you use NewNonInteractiveDeferredLoadingClientConfig instead you can specify the context like this:如果您使用 NewNonInteractiveDeferredLoadingClientConfig 代替,您可以像这样指定上下文:
configLoadingRules := &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig}
configOverrides := &clientcmd.ConfigOverrides{CurrentContext: "dev-cluster"}
kconf, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(configLoadingRules, configOverrides).ClientConfig()
if err != nil {
return nil, err
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.