forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
251 lines (207 loc) · 6.49 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package custom_metrics
import (
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
serializer "k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/util/flowcontrol"
"k8s.io/metrics/pkg/apis/custom_metrics/v1beta1"
)
type customMetricsClient struct {
client rest.Interface
mapper meta.RESTMapper
}
func New(client rest.Interface) CustomMetricsClient {
return &customMetricsClient{
client: client,
}
}
func NewForConfig(c *rest.Config) (CustomMetricsClient, error) {
configShallowCopy := *c
if configShallowCopy.RateLimiter == nil && configShallowCopy.QPS > 0 {
configShallowCopy.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(configShallowCopy.QPS, configShallowCopy.Burst)
}
configShallowCopy.APIPath = "/apis"
if configShallowCopy.UserAgent == "" {
configShallowCopy.UserAgent = rest.DefaultKubernetesUserAgent()
}
configShallowCopy.GroupVersion = &v1beta1.SchemeGroupVersion
configShallowCopy.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: scheme.Codecs}
client, err := rest.RESTClientFor(&configShallowCopy)
if err != nil {
return nil, err
}
return New(client), nil
}
func NewForConfigOrDie(c *rest.Config) CustomMetricsClient {
client, err := NewForConfig(c)
if err != nil {
panic(err)
}
return client
}
// NewForMapper constucts the client with a RESTMapper, which allows more
// accurate translation from GroupVersionKind to GroupVersionResource.
func NewForMapper(client rest.Interface, mapper meta.RESTMapper) CustomMetricsClient {
return &customMetricsClient{
client: client,
mapper: mapper,
}
}
func (c *customMetricsClient) RootScopedMetrics() MetricsInterface {
return &rootScopedMetrics{c}
}
func (c *customMetricsClient) NamespacedMetrics(namespace string) MetricsInterface {
return &namespacedMetrics{
client: c,
namespace: namespace,
}
}
func (c *customMetricsClient) qualResourceForKind(groupKind schema.GroupKind) (string, error) {
if c.mapper == nil {
// the version doesn't matter
gvk := groupKind.WithVersion("")
gvr, _ := meta.UnsafeGuessKindToResource(gvk)
gr := gvr.GroupResource()
return gr.String(), nil
}
// use the mapper if it's available
mapping, err := c.mapper.RESTMapping(groupKind)
if err != nil {
return "", fmt.Errorf("unable to map kind %s to resource: %v", groupKind.String(), err)
}
groupResource := schema.GroupResource{
Group: mapping.GroupVersionKind.Group,
Resource: mapping.Resource,
}
return groupResource.String(), nil
}
type rootScopedMetrics struct {
client *customMetricsClient
}
func (m *rootScopedMetrics) getForNamespace(namespace string, metricName string) (*v1beta1.MetricValue, error) {
res := &v1beta1.MetricValueList{}
err := m.client.client.Get().
Resource("metrics").
Namespace(namespace).
Name(metricName).
Do().
Into(res)
if err != nil {
return nil, err
}
if len(res.Items) != 1 {
return nil, fmt.Errorf("the custom metrics API server returned %v results when we asked for exactly one", len(res.Items))
}
return &res.Items[0], nil
}
func (m *rootScopedMetrics) GetForObject(groupKind schema.GroupKind, name string, metricName string) (*v1beta1.MetricValue, error) {
// handle namespace separately
if groupKind.Kind == "Namespace" && groupKind.Group == "" {
return m.getForNamespace(name, metricName)
}
resourceName, err := m.client.qualResourceForKind(groupKind)
if err != nil {
return nil, err
}
res := &v1beta1.MetricValueList{}
err = m.client.client.Get().
Resource(resourceName).
Name(name).
SubResource(metricName).
Do().
Into(res)
if err != nil {
return nil, err
}
if len(res.Items) != 1 {
return nil, fmt.Errorf("the custom metrics API server returned %v results when we asked for exactly one", len(res.Items))
}
return &res.Items[0], nil
}
func (m *rootScopedMetrics) GetForObjects(groupKind schema.GroupKind, selector labels.Selector, metricName string) (*v1beta1.MetricValueList, error) {
// we can't wildcard-fetch for namespaces
if groupKind.Kind == "Namespace" && groupKind.Group == "" {
return nil, fmt.Errorf("cannot fetch metrics for multiple namespaces at once")
}
resourceName, err := m.client.qualResourceForKind(groupKind)
if err != nil {
return nil, err
}
res := &v1beta1.MetricValueList{}
err = m.client.client.Get().
Resource(resourceName).
Name(v1beta1.AllObjects).
SubResource(metricName).
VersionedParams(&metav1.ListOptions{
LabelSelector: selector.String(),
}, metav1.ParameterCodec).
Do().
Into(res)
if err != nil {
return nil, err
}
return res, nil
}
type namespacedMetrics struct {
client *customMetricsClient
namespace string
}
func (m *namespacedMetrics) GetForObject(groupKind schema.GroupKind, name string, metricName string) (*v1beta1.MetricValue, error) {
resourceName, err := m.client.qualResourceForKind(groupKind)
if err != nil {
return nil, err
}
res := &v1beta1.MetricValueList{}
err = m.client.client.Get().
Resource(resourceName).
Namespace(m.namespace).
Name(name).
SubResource(metricName).
Do().
Into(res)
if err != nil {
return nil, err
}
if len(res.Items) != 1 {
return nil, fmt.Errorf("the custom metrics API server returned %v results when we asked for exactly one", len(res.Items))
}
return &res.Items[0], nil
}
func (m *namespacedMetrics) GetForObjects(groupKind schema.GroupKind, selector labels.Selector, metricName string) (*v1beta1.MetricValueList, error) {
resourceName, err := m.client.qualResourceForKind(groupKind)
if err != nil {
return nil, err
}
res := &v1beta1.MetricValueList{}
err = m.client.client.Get().
Resource(resourceName).
Namespace(m.namespace).
Name(v1beta1.AllObjects).
SubResource(metricName).
VersionedParams(&metav1.ListOptions{
LabelSelector: selector.String(),
}, metav1.ParameterCodec).
Do().
Into(res)
if err != nil {
return nil, err
}
return res, nil
}