-
Notifications
You must be signed in to change notification settings - Fork 176
/
provider.go
330 lines (284 loc) · 10.7 KB
/
provider.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
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 provider
import (
"context"
"net/http"
"sync"
"github.com/emicklei/go-restful/v3"
apierr "k8s.io/apimachinery/pkg/api/errors"
apimeta "k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
"k8s.io/klog/v2"
"k8s.io/metrics/pkg/apis/custom_metrics"
"k8s.io/metrics/pkg/apis/external_metrics"
"sigs.k8s.io/custom-metrics-apiserver/pkg/provider"
"sigs.k8s.io/custom-metrics-apiserver/pkg/provider/defaults"
"sigs.k8s.io/custom-metrics-apiserver/pkg/provider/helpers"
)
// CustomMetricResource wraps provider.CustomMetricInfo in a struct which stores the Name and Namespace of the resource
// So that we can accurately store and retrieve the metric as if this were an actual metrics server.
type CustomMetricResource struct {
provider.CustomMetricInfo
types.NamespacedName
}
// externalMetric provides examples for metrics which would otherwise be reported from an external source
// TODO (damemi): add dynamic external metrics instead of just hardcoded examples
type externalMetric struct {
info provider.ExternalMetricInfo
labels map[string]string
value external_metrics.ExternalMetricValue
}
var (
testingExternalMetrics = []externalMetric{
{
info: provider.ExternalMetricInfo{
Metric: "my-external-metric",
},
labels: map[string]string{"foo": "bar"},
value: external_metrics.ExternalMetricValue{
MetricName: "my-external-metric",
MetricLabels: map[string]string{
"foo": "bar",
},
Value: *resource.NewQuantity(42, resource.DecimalSI),
},
},
{
info: provider.ExternalMetricInfo{
Metric: "my-external-metric",
},
labels: map[string]string{"foo": "baz"},
value: external_metrics.ExternalMetricValue{
MetricName: "my-external-metric",
MetricLabels: map[string]string{
"foo": "baz",
},
Value: *resource.NewQuantity(43, resource.DecimalSI),
},
},
{
info: provider.ExternalMetricInfo{
Metric: "other-external-metric",
},
labels: map[string]string{},
value: external_metrics.ExternalMetricValue{
MetricName: "other-external-metric",
MetricLabels: map[string]string{},
Value: *resource.NewQuantity(44, resource.DecimalSI),
},
},
}
)
type metricValue struct {
labels labels.Set
value resource.Quantity
timestamp metav1.Time
}
var _ provider.MetricsProvider = &testingProvider{}
// testingProvider is a sample implementation of provider.MetricsProvider which stores a map of fake metrics
type testingProvider struct {
defaults.DefaultCustomMetricsProvider
defaults.DefaultExternalMetricsProvider
client dynamic.Interface
mapper apimeta.RESTMapper
valuesLock sync.RWMutex
values map[CustomMetricResource]metricValue
externalMetrics []externalMetric
}
// NewFakeProvider returns an instance of testingProvider, along with its restful.WebService that opens endpoints to post new fake metrics
func NewFakeProvider(client dynamic.Interface, mapper apimeta.RESTMapper) (provider.MetricsProvider, *restful.WebService) {
provider := &testingProvider{
client: client,
mapper: mapper,
values: make(map[CustomMetricResource]metricValue),
externalMetrics: testingExternalMetrics,
}
return provider, provider.webService()
}
// webService creates a restful.WebService with routes set up for receiving fake metrics
// These writing routes have been set up to be identical to the format of routes which metrics are read from.
// There are 3 metric types available: namespaced, root-scoped, and namespaces.
// (Note: Namespaces, we're assuming, are themselves namespaced resources, but for consistency with how metrics are retreived they have a separate route)
func (p *testingProvider) webService() *restful.WebService {
ws := new(restful.WebService)
ws.Path("/write-metrics")
// Namespaced resources
ws.Route(ws.POST("/namespaces/{namespace}/{resourceType}/{name}/{metric}").To(p.updateMetric).
Param(ws.BodyParameter("value", "value to set metric").DataType("integer").DefaultValue("0")))
// Root-scoped resources
ws.Route(ws.POST("/{resourceType}/{name}/{metric}").To(p.updateMetric).
Param(ws.BodyParameter("value", "value to set metric").DataType("integer").DefaultValue("0")))
// Namespaces, where {resourceType} == "namespaces" to match API
ws.Route(ws.POST("/{resourceType}/{name}/metrics/{metric}").To(p.updateMetric).
Param(ws.BodyParameter("value", "value to set metric").DataType("integer").DefaultValue("0")))
return ws
}
// updateMetric writes the metric provided by a restful request and stores it in memory
func (p *testingProvider) updateMetric(request *restful.Request, response *restful.Response) {
p.valuesLock.Lock()
defer p.valuesLock.Unlock()
namespace := request.PathParameter("namespace")
resourceType := request.PathParameter("resourceType")
namespaced := false
if len(namespace) > 0 || resourceType == "namespaces" {
namespaced = true
}
name := request.PathParameter("name")
metricName := request.PathParameter("metric")
value := new(resource.Quantity)
err := request.ReadEntity(value)
if err != nil {
if err := response.WriteErrorString(http.StatusBadRequest, err.Error()); err != nil {
klog.Errorf("Error writing error: %s", err)
}
return
}
groupResource := schema.ParseGroupResource(resourceType)
metricLabels := labels.Set{}
sel := request.QueryParameter("labels")
if len(sel) > 0 {
metricLabels, err = labels.ConvertSelectorToLabelsMap(sel)
if err != nil {
if err := response.WriteErrorString(http.StatusBadRequest, err.Error()); err != nil {
klog.Errorf("Error writing error: %s", err)
}
return
}
}
info := provider.CustomMetricInfo{
GroupResource: groupResource,
Metric: metricName,
Namespaced: namespaced,
}
info, _, err = info.Normalized(p.mapper)
if err != nil {
klog.Errorf("Error normalizing info: %s", err)
}
namespacedName := types.NamespacedName{
Name: name,
Namespace: namespace,
}
metricInfo := CustomMetricResource{
CustomMetricInfo: info,
NamespacedName: namespacedName,
}
p.values[metricInfo] = metricValue{
labels: metricLabels,
value: *value,
timestamp: metav1.Now(),
}
}
// valueFor is a helper function to get just the value of a specific metric
func (p *testingProvider) valueFor(info provider.CustomMetricInfo, name types.NamespacedName, metricSelector labels.Selector) (metricValue, error) {
info, _, err := info.Normalized(p.mapper)
if err != nil {
return metricValue{}, err
}
metricInfo := CustomMetricResource{
CustomMetricInfo: info,
NamespacedName: name,
}
value, found := p.values[metricInfo]
if !found {
return metricValue{}, provider.NewMetricNotFoundForError(info.GroupResource, info.Metric, name.Name)
}
if !metricSelector.Matches(value.labels) {
return metricValue{}, provider.NewMetricNotFoundForSelectorError(info.GroupResource, info.Metric, name.Name, metricSelector)
}
return value, nil
}
// metricFor is a helper function which formats a value, metric, and object info into a MetricValue which can be returned by the metrics API
func (p *testingProvider) metricFor(value metricValue, name types.NamespacedName, _ labels.Selector, info provider.CustomMetricInfo, metricSelector labels.Selector) (*custom_metrics.MetricValue, error) {
objRef, err := helpers.ReferenceFor(p.mapper, name, info)
if err != nil {
return nil, err
}
metric := &custom_metrics.MetricValue{
DescribedObject: objRef,
Metric: custom_metrics.MetricIdentifier{
Name: info.Metric,
},
Timestamp: value.timestamp,
Value: value.value,
}
if len(metricSelector.String()) > 0 {
sel, err := metav1.ParseToLabelSelector(metricSelector.String())
if err != nil {
return nil, err
}
metric.Metric.Selector = sel
}
return metric, nil
}
// metricsFor is a wrapper used by GetMetricBySelector to format several metrics which match a resource selector
func (p *testingProvider) metricsFor(namespace string, selector labels.Selector, info provider.CustomMetricInfo, metricSelector labels.Selector) (*custom_metrics.MetricValueList, error) {
names, err := helpers.ListObjectNames(p.mapper, p.client, namespace, selector, info)
if err != nil {
return nil, err
}
res := make([]custom_metrics.MetricValue, 0, len(names))
for _, name := range names {
namespacedName := types.NamespacedName{Name: name, Namespace: namespace}
value, err := p.valueFor(info, namespacedName, metricSelector)
if err != nil {
if apierr.IsNotFound(err) {
continue
}
return nil, err
}
metric, err := p.metricFor(value, namespacedName, selector, info, metricSelector)
if err != nil {
return nil, err
}
res = append(res, *metric)
}
return &custom_metrics.MetricValueList{
Items: res,
}, nil
}
func (p *testingProvider) GetMetricByName(_ context.Context, name types.NamespacedName, info provider.CustomMetricInfo, metricSelector labels.Selector) (*custom_metrics.MetricValue, error) {
p.valuesLock.RLock()
defer p.valuesLock.RUnlock()
value, err := p.valueFor(info, name, metricSelector)
if err != nil {
return nil, err
}
return p.metricFor(value, name, labels.Everything(), info, metricSelector)
}
func (p *testingProvider) GetMetricBySelector(_ context.Context, namespace string, selector labels.Selector, info provider.CustomMetricInfo, metricSelector labels.Selector) (*custom_metrics.MetricValueList, error) {
p.valuesLock.RLock()
defer p.valuesLock.RUnlock()
return p.metricsFor(namespace, selector, info, metricSelector)
}
func (p *testingProvider) GetExternalMetric(_ context.Context, _ string, metricSelector labels.Selector, info provider.ExternalMetricInfo) (*external_metrics.ExternalMetricValueList, error) {
p.valuesLock.RLock()
defer p.valuesLock.RUnlock()
matchingMetrics := []external_metrics.ExternalMetricValue{}
for _, metric := range p.externalMetrics {
if metric.info.Metric == info.Metric &&
metricSelector.Matches(labels.Set(metric.labels)) {
metricValue := metric.value
metricValue.Timestamp = metav1.Now()
matchingMetrics = append(matchingMetrics, metricValue)
}
}
return &external_metrics.ExternalMetricValueList{
Items: matchingMetrics,
}, nil
}