mirror of https://github.com/k3s-io/k3s
Add utility functions for getting kubernetes client
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
(cherry picked from commit 3c324335b2
)
pull/6925/head
parent
631847536c
commit
b88c3b8c95
|
@ -40,10 +40,8 @@ import (
|
||||||
"k8s.io/apimachinery/pkg/labels"
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/watch"
|
"k8s.io/apimachinery/pkg/watch"
|
||||||
"k8s.io/client-go/kubernetes"
|
|
||||||
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
|
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
|
||||||
"k8s.io/client-go/tools/cache"
|
"k8s.io/client-go/tools/cache"
|
||||||
"k8s.io/client-go/tools/clientcmd"
|
|
||||||
toolswatch "k8s.io/client-go/tools/watch"
|
toolswatch "k8s.io/client-go/tools/watch"
|
||||||
app2 "k8s.io/kubernetes/cmd/kube-proxy/app"
|
app2 "k8s.io/kubernetes/cmd/kube-proxy/app"
|
||||||
kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
|
kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
|
||||||
|
@ -137,7 +135,7 @@ func run(ctx context.Context, cfg cmds.Agent, proxy proxy.Proxy) error {
|
||||||
return errors.Wrap(err, "failed to wait for apiserver ready")
|
return errors.Wrap(err, "failed to wait for apiserver ready")
|
||||||
}
|
}
|
||||||
|
|
||||||
coreClient, err := coreClient(nodeConfig.AgentConfig.KubeConfigKubelet)
|
coreClient, err := util.GetClientSet(nodeConfig.AgentConfig.KubeConfigKubelet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -213,15 +211,6 @@ func getConntrackConfig(nodeConfig *daemonconfig.Node) (*kubeproxyconfig.KubePro
|
||||||
return ctConfig, nil
|
return ctConfig, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func coreClient(cfg string) (kubernetes.Interface, error) {
|
|
||||||
restConfig, err := clientcmd.BuildConfigFromFlags("", cfg)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return kubernetes.NewForConfig(restConfig)
|
|
||||||
}
|
|
||||||
|
|
||||||
// RunStandalone bootstraps the executor, but does not run the kubelet or containerd.
|
// RunStandalone bootstraps the executor, but does not run the kubelet or containerd.
|
||||||
// This allows other bits of code that expect the executor to be set up properly to function
|
// This allows other bits of code that expect the executor to be set up properly to function
|
||||||
// even when the agent is disabled. It will only return in case of error or context
|
// even when the agent is disabled. It will only return in case of error or context
|
||||||
|
|
|
@ -56,12 +56,7 @@ func (p *podEntry) Network() net.IPNet {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Setup(ctx context.Context, config *daemonconfig.Node, proxy proxy.Proxy) error {
|
func Setup(ctx context.Context, config *daemonconfig.Node, proxy proxy.Proxy) error {
|
||||||
restConfig, err := clientcmd.BuildConfigFromFlags("", config.AgentConfig.KubeConfigK3sController)
|
client, err := util.GetClientSet(config.AgentConfig.KubeConfigK3sController)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
client, err := kubernetes.NewForConfig(restConfig)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/k3s-io/k3s/pkg/datadir"
|
||||||
|
clientset "k8s.io/client-go/kubernetes"
|
||||||
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetKubeConfigPath can be used to search for a kubeconfig in standard
|
||||||
|
// locations if an empty string is passed. If a non-empty string is passed,
|
||||||
|
// that path is used.
|
||||||
|
func GetKubeConfigPath(file string) string {
|
||||||
|
if file != "" {
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
rules := clientcmd.NewDefaultClientConfigLoadingRules()
|
||||||
|
rules.Precedence = append([]string{datadir.GlobalConfig}, rules.Precedence...)
|
||||||
|
return rules.GetDefaultFilename()
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetClientSet creates a Kubernetes client from the kubeconfig at the provided path.
|
||||||
|
func GetClientSet(file string) (clientset.Interface, error) {
|
||||||
|
restConfig, err := clientcmd.BuildConfigFromFlags("", file)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return clientset.NewForConfig(restConfig)
|
||||||
|
}
|
Loading…
Reference in New Issue