-
Notifications
You must be signed in to change notification settings - Fork 24
/
client.go
86 lines (78 loc) · 2.72 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Package k8s provides datagatherers for different parts of the Kubernetes API.
package k8s
import (
"github.com/pkg/errors"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// NewDynamicClient creates a new 'dynamic' clientset using the provided kubeconfig.
// If kubeconfigPath is not set/empty, it will attempt to load configuration using
// the default loading rules.
func NewDynamicClient(kubeconfigPath string) (dynamic.Interface, error) {
cfg, err := loadRESTConfig(kubeconfigPath)
if err != nil {
return nil, errors.WithStack(err)
}
cl, err := dynamic.NewForConfig(cfg)
if err != nil {
return nil, errors.WithStack(err)
}
return cl, nil
}
// NewDiscoveryClient creates a new 'discovery' client using the provided
// kubeconfig. If kubeconfigPath is not set/empty, it will attempt to load
// configuration using the default loading rules.
func NewDiscoveryClient(kubeconfigPath string) (discovery.DiscoveryClient, error) {
var discoveryClient *discovery.DiscoveryClient
cfg, err := loadRESTConfig(kubeconfigPath)
if err != nil {
return *discoveryClient, errors.WithStack(err)
}
discoveryClient, err = discovery.NewDiscoveryClientForConfig(cfg)
if err != nil {
return *discoveryClient, errors.WithStack(err)
}
return *discoveryClient, nil
}
// NewClientSet creates a new kubernetes clientset using the provided kubeconfig.
// If kubeconfigPath is not set/empty, it will attempt to load configuration using
// the default loading rules.
func NewClientSet(kubeconfigPath string) (kubernetes.Interface, error) {
var clientset *kubernetes.Clientset
cfg, err := loadRESTConfig(kubeconfigPath)
if err != nil {
return nil, errors.WithStack(err)
}
clientset, err = kubernetes.NewForConfig(cfg)
if err != nil {
return nil, errors.WithStack(err)
}
return clientset, nil
}
func loadRESTConfig(path string) (*rest.Config, error) {
switch path {
// If the kubeconfig path is not provided, use the default loading rules
// so we read the regular KUBECONFIG variable or create a non-interactive
// client for agents running in cluster
case "":
loadingrules := clientcmd.NewDefaultClientConfigLoadingRules()
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
loadingrules, &clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return nil, errors.WithStack(err)
}
return cfg, nil
// Otherwise use the explicitly named kubeconfig file.
default:
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: path},
&clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return nil, errors.WithStack(err)
}
return cfg, nil
}
}