Debian Codegraph @

pkg/client/custom_metrics/versioned_client.go

raw
1 /*
2 Copyright 2017 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package custom_metrics
18
19 import (
20 "context"
21 "fmt"
22 "reflect"
23
24 "k8s.io/apimachinery/pkg/api/meta"
25 "k8s.io/apimachinery/pkg/labels"
26 "k8s.io/apimachinery/pkg/runtime/schema"
27 "k8s.io/client-go/rest"
28 "k8s.io/client-go/util/flowcontrol"
29
30 cmint "k8s.io/metrics/pkg/apis/custom_metrics"
31 "k8s.io/metrics/pkg/apis/custom_metrics/v1beta1"
32 "k8s.io/metrics/pkg/apis/custom_metrics/v1beta2"
33 "k8s.io/metrics/pkg/client/custom_metrics/scheme"
34 )
35
36 var versionConverter = NewMetricConverter()
37
38 type customMetricsClient struct {
39 client rest.Interface
40 version schema.GroupVersion
41 mapper meta.RESTMapper
42 }
43
44 // NewForVersion returns a new CustomMetricsClient for a particular api version.
45 func NewForVersion(client rest.Interface, mapper meta.RESTMapper, version schema.GroupVersion) CustomMetricsClient {
46 return &customMetricsClient{
47 client: client,
48 version: version,
49 mapper: mapper,
50 }
51 }
52
53 // NewForVersionForConfig returns a new CustomMetricsClient for a particular api version and base configuration.
54 func NewForVersionForConfig(c *rest.Config, mapper meta.RESTMapper, version schema.GroupVersion) (CustomMetricsClient, error) {
55 configShallowCopy := *c
56 if configShallowCopy.RateLimiter == nil && configShallowCopy.QPS > 0 {
57 if configShallowCopy.Burst <= 0 {
58 return nil, fmt.Errorf("burst is required to be greater than 0 when RateLimiter is not set and QPS is set to greater than 0")
59 }
60 configShallowCopy.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(configShallowCopy.QPS, configShallowCopy.Burst)
61 }
62 configShallowCopy.APIPath = "/apis"
63 if configShallowCopy.UserAgent == "" {
64 configShallowCopy.UserAgent = rest.DefaultKubernetesUserAgent()
65 }
66 configShallowCopy.GroupVersion = &version
67 configShallowCopy.NegotiatedSerializer = scheme.Codecs.WithoutConversion()
68
69 client, err := rest.RESTClientFor(&configShallowCopy)
70 if err != nil {
71 return nil, err
72 }
73
74 return NewForVersion(client, mapper, version), nil
75 }
76
77 func (c *customMetricsClient) RootScopedMetrics() MetricsInterface {
78 return &rootScopedMetrics{c}
79 }
80
81 func (c *customMetricsClient) NamespacedMetrics(namespace string) MetricsInterface {
82 return &namespacedMetrics{
83 client: c,
84 namespace: namespace,
85 }
86 }
87
88 // qualResourceForKind returns the string format of a qualified group resource for the specified GroupKind
89 func (c *customMetricsClient) qualResourceForKind(groupKind schema.GroupKind) (string, error) {
90 mapping, err := c.mapper.RESTMapping(groupKind)
91 if err != nil {
92 return "", fmt.Errorf("unable to map kind %s to resource: %v", groupKind.String(), err)
93 }
94
95 gr := mapping.Resource.GroupResource()
96 return gr.String(), nil
97 }
98
99 type rootScopedMetrics struct {
100 client *customMetricsClient
101 }
102
103 func (m *rootScopedMetrics) getForNamespace(namespace string, metricName string, metricSelector labels.Selector) (*v1beta2.MetricValue, error) {
104 params, err := versionConverter.ConvertListOptionsToVersion(&cmint.MetricListOptions{
105 MetricLabelSelector: metricSelector.String(),
106 }, m.client.version)
107 if err != nil {
108 return nil, err
109 }
110
111 result := m.client.client.Get().
112 Resource("metrics").
113 Namespace(namespace).
114 Name(metricName).
115 VersionedParams(params, scheme.ParameterCodec).
116 Do(context.TODO())
117
118 metricObj, err := versionConverter.ConvertResultToVersion(result, v1beta2.SchemeGroupVersion)
119 if err != nil {
120 return nil, err
121 }
122
123 var res *v1beta2.MetricValueList
124 var ok bool
125 if res, ok = metricObj.(*v1beta2.MetricValueList); !ok {
126 return nil, fmt.Errorf("the custom metrics API server didn't return MetricValueList, the type is %v", reflect.TypeOf(metricObj))
127 }
128 if len(res.Items) != 1 {
129 return nil, fmt.Errorf("the custom metrics API server returned %v results when we asked for exactly one", len(res.Items))
130 }
131
132 return &res.Items[0], nil
133 }
134
135 func (m *rootScopedMetrics) GetForObject(groupKind schema.GroupKind, name string, metricName string, metricSelector labels.Selector) (*v1beta2.MetricValue, error) {
136 // handle namespace separately
137 if groupKind.Kind == "Namespace" && groupKind.Group == "" {
138 return m.getForNamespace(name, metricName, metricSelector)
139 }
140
141 resourceName, err := m.client.qualResourceForKind(groupKind)
142 if err != nil {
143 return nil, err
144 }
145
146 params, err := versionConverter.ConvertListOptionsToVersion(&cmint.MetricListOptions{
147 MetricLabelSelector: metricSelector.String(),
148 }, m.client.version)
149 if err != nil {
150 return nil, err
151 }
152
153 result := m.client.client.Get().
154 Resource(resourceName).
155 Name(name).
156 SubResource(metricName).
157 VersionedParams(params, scheme.ParameterCodec).
158 Do(context.TODO())
159
160 metricObj, err := versionConverter.ConvertResultToVersion(result, v1beta2.SchemeGroupVersion)
161 if err != nil {
162 return nil, err
163 }
164
165 var res *v1beta2.MetricValueList
166 var ok bool
167 if res, ok = metricObj.(*v1beta2.MetricValueList); !ok {
168 return nil, fmt.Errorf("the custom metrics API server didn't return MetricValueList, the type is %v", reflect.TypeOf(metricObj))
169 }
170 if len(res.Items) != 1 {
171 return nil, fmt.Errorf("the custom metrics API server returned %v results when we asked for exactly one", len(res.Items))
172 }
173
174 return &res.Items[0], nil
175 }
176
177 func (m *rootScopedMetrics) GetForObjects(groupKind schema.GroupKind, selector labels.Selector, metricName string, metricSelector labels.Selector) (*v1beta2.MetricValueList, error) {
178 // we can't wildcard-fetch for namespaces
179 if groupKind.Kind == "Namespace" && groupKind.Group == "" {
180 return nil, fmt.Errorf("cannot fetch metrics for multiple namespaces at once")
181 }
182
183 resourceName, err := m.client.qualResourceForKind(groupKind)
184 if err != nil {
185 return nil, err
186 }
187
188 params, err := versionConverter.ConvertListOptionsToVersion(&cmint.MetricListOptions{
189 LabelSelector: selector.String(),
190 MetricLabelSelector: metricSelector.String(),
191 }, m.client.version)
192 if err != nil {
193 return nil, err
194 }
195
196 result := m.client.client.Get().
197 Resource(resourceName).
198 Name(v1beta1.AllObjects).
199 SubResource(metricName).
200 VersionedParams(params, scheme.ParameterCodec).
201 Do(context.TODO())
202
203 metricObj, err := versionConverter.ConvertResultToVersion(result, v1beta2.SchemeGroupVersion)
204 if err != nil {
205 return nil, err
206 }
207
208 var res *v1beta2.MetricValueList
209 var ok bool
210 if res, ok = metricObj.(*v1beta2.MetricValueList); !ok {
211 return nil, fmt.Errorf("the custom metrics API server didn't return MetricValueList, the type is %v", reflect.TypeOf(metricObj))
212 }
213 return res, nil
214 }
215
216 type namespacedMetrics struct {
217 client *customMetricsClient
218 namespace string
219 }
220
221 func (m *namespacedMetrics) GetForObject(groupKind schema.GroupKind, name string, metricName string, metricSelector labels.Selector) (*v1beta2.MetricValue, error) {
222 resourceName, err := m.client.qualResourceForKind(groupKind)
223 if err != nil {
224 return nil, err
225 }
226
227 params, err := versionConverter.ConvertListOptionsToVersion(&cmint.MetricListOptions{
228 MetricLabelSelector: metricSelector.String(),
229 }, m.client.version)
230 if err != nil {
231 return nil, err
232 }
233
234 result := m.client.client.Get().
235 Resource(resourceName).
236 Namespace(m.namespace).
237 Name(name).
238 SubResource(metricName).
239 VersionedParams(params, scheme.ParameterCodec).
240 Do(context.TODO())
241
242 metricObj, err := versionConverter.ConvertResultToVersion(result, v1beta2.SchemeGroupVersion)
243 if err != nil {
244 return nil, err
245 }
246
247 var res *v1beta2.MetricValueList
248 var ok bool
249 if res, ok = metricObj.(*v1beta2.MetricValueList); !ok {
250 return nil, fmt.Errorf("the custom metrics API server didn't return MetricValueList, the type is %v", reflect.TypeOf(metricObj))
251 }
252 if len(res.Items) != 1 {
253 return nil, fmt.Errorf("the custom metrics API server returned %v results when we asked for exactly one", len(res.Items))
254 }
255
256 return &res.Items[0], nil
257 }
258
259 func (m *namespacedMetrics) GetForObjects(groupKind schema.GroupKind, selector labels.Selector, metricName string, metricSelector labels.Selector) (*v1beta2.MetricValueList, error) {
260 resourceName, err := m.client.qualResourceForKind(groupKind)
261 if err != nil {
262 return nil, err
263 }
264
265 params, err := versionConverter.ConvertListOptionsToVersion(&cmint.MetricListOptions{
266 LabelSelector: selector.String(),
267 MetricLabelSelector: metricSelector.String(),
268 }, m.client.version)
269 if err != nil {
270 return nil, err
271 }
272
273 result := m.client.client.Get().
274 Resource(resourceName).
275 Namespace(m.namespace).
276 Name(v1beta1.AllObjects).
277 SubResource(metricName).
278 VersionedParams(params, scheme.ParameterCodec).
279 Do(context.TODO())
280
281 metricObj, err := versionConverter.ConvertResultToVersion(result, v1beta2.SchemeGroupVersion)
282 if err != nil {
283 return nil, err
284 }
285
286 var res *v1beta2.MetricValueList
287 var ok bool
288 if res, ok = metricObj.(*v1beta2.MetricValueList); !ok {
289 return nil, fmt.Errorf("the custom metrics API server didn't return MetricValueList, the type is %v", reflect.TypeOf(metricObj))
290 }
291 return res, nil
292 }