-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.go
150 lines (133 loc) · 3.76 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package kube
import (
"errors"
"net/http"
"net/url"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/kubernetes"
clischeme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/remotecommand"
"k8s.io/metrics/pkg/client/clientset/versioned"
)
const (
defaultAPIURIPath = "/api"
defaultAPIsURIPath = "/apis"
)
var ErrorMissingNamespace = errors.New("missing namespace")
// Client represents kubernetes Client.
type Client struct {
client kubernetes.Interface
metrics *versioned.Clientset
cfg *Config
proxy func(request *http.Request) (*url.URL, error)
inCluster bool
}
// New returns a new Client for the given Config.
func New(cfg *Config) *Client {
return &Client{cfg: cfg}
}
// NewDefault returns a new Client with default kubeconfig.
func NewDefault() *Client {
flags := genericclioptions.NewConfigFlags(UsePersistentConfig)
return &Client{cfg: NewConfig(flags)}
}
// NewInCluster returns a new Client with InClusterConfig.
func NewInCluster() *Client {
return &Client{inCluster: true}
}
// WithProxy set proxy. Proxy is the proxy func to be used for all requests made by this client.
// If Proxy is nil, http.ProxyFromEnvironment is used. If Proxy returns a nil *URL, no proxy is used.
func (c *Client) WithProxy(fn func(request *http.Request) (*url.URL, error)) *Client {
c.proxy = fn
return c
}
// DialMetrics returns a new versioned.Clientset to the metrics server.
func (c *Client) DialMetrics() (*versioned.Clientset, error) {
if c.metrics != nil {
return c.metrics, nil
}
cfg, err := c.RestConfig()
if err != nil {
return nil, err
}
if c.metrics, err = versioned.NewForConfig(cfg); err != nil {
return nil, err
}
return c.metrics, nil
}
// Dial returns a new kubernetes.Clientset to the kubernetes server.
func (c *Client) Dial() (kubernetes.Interface, error) {
if c.client != nil {
return c.client, nil
}
cfg, err := c.RestConfig()
if err != nil {
return nil, err
}
if c.client, err = kubernetes.NewForConfig(cfg); err != nil {
return nil, err
}
return c.client, nil
}
// RestConfig returns a complete rest client config.
func (c *Client) RestConfig() (*rest.Config, error) {
var (
cfg *rest.Config
err error
)
if c.inCluster {
cfg, err = rest.InClusterConfig()
if err != nil {
return nil, err
}
} else {
cfg, err = c.cfg.RESTConfig()
if err != nil {
return nil, err
}
}
if c.proxy != nil {
cfg.Proxy = c.proxy
}
cfg.Timeout = c.cfg.Timeout()
return cfg, nil
}
// ResourceClient returns a client for the given schema.GroupVersion.
func (c *Client) ResourceClient(gv schema.GroupVersion) (rest.Interface, error) {
cfg, err := c.RestConfig()
if err != nil {
return nil, err
}
cfg.ContentConfig = resource.UnstructuredPlusDefaultContentConfig()
cfg.GroupVersion = &gv
if len(gv.Group) == 0 {
cfg.APIPath = defaultAPIURIPath
} else {
cfg.APIPath = defaultAPIsURIPath
}
return rest.RESTClientFor(cfg)
}
// RemoteExecRequest returns a client for the given schema.GroupVersion.
func (c *Client) RemoteExecRequest(method ExecRequestMethod, pod, namespace string, options *corev1.PodExecOptions) *rest.Request {
return c.client.CoreV1().RESTClient().Verb(string(method)).
Resource("pods").Name(pod).
Namespace(namespace).
SubResource("exec").
VersionedParams(options, clischeme.ParameterCodec)
}
// RemoteExecutor returns a client for the given schema.GroupVersion.
func (c *Client) RemoteExecutor(req *rest.Request) (remotecommand.Executor, error) {
restcfg, err := c.RestConfig()
if err != nil {
return nil, err
}
executor, err := remotecommand.NewSPDYExecutor(restcfg, "POST", req.URL())
if err != nil {
return nil, err
}
return executor, nil
}