-
Notifications
You must be signed in to change notification settings - Fork 39.2k
/
converter.go
122 lines (102 loc) · 4.01 KB
/
converter.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
/*
Copyright 2018 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/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/rest"
cmint "k8s.io/metrics/pkg/apis/custom_metrics"
cmv1beta1 "k8s.io/metrics/pkg/apis/custom_metrics/v1beta1"
cmv1beta2 "k8s.io/metrics/pkg/apis/custom_metrics/v1beta2"
"k8s.io/metrics/pkg/client/custom_metrics/scheme"
)
var (
// MetricVersions is the set of metric versions accepted by the converter.
MetricVersions = []schema.GroupVersion{
cmv1beta2.SchemeGroupVersion,
cmv1beta1.SchemeGroupVersion,
cmint.SchemeGroupVersion,
}
)
// MetricConverter knows how to convert between external MetricValue versions.
type MetricConverter struct {
scheme *runtime.Scheme
codecs serializer.CodecFactory
internalVersioner runtime.GroupVersioner
}
// NewMetricConverter creates a MetricConverter which knows how to convert objects
// between different versions of the custom metrics api.
func NewMetricConverter() *MetricConverter {
return &MetricConverter{
scheme: scheme.Scheme,
codecs: serializer.NewCodecFactory(scheme.Scheme),
internalVersioner: runtime.NewMultiGroupVersioner(
scheme.SchemeGroupVersion,
schema.GroupKind{Group: cmint.GroupName, Kind: ""},
schema.GroupKind{Group: cmv1beta1.GroupName, Kind: ""},
schema.GroupKind{Group: cmv1beta2.GroupName, Kind: ""},
),
}
}
// Scheme returns the scheme used by this metric converter.
func (c *MetricConverter) Scheme() *runtime.Scheme {
return c.scheme
}
// Codecs returns the codecs used by this metric converter
func (c *MetricConverter) Codecs() serializer.CodecFactory {
return c.codecs
}
// ConvertListOptionsToVersion converts converts a set of MetricListOptions
// to the provided GroupVersion.
func (c *MetricConverter) ConvertListOptionsToVersion(opts *cmint.MetricListOptions, version schema.GroupVersion) (runtime.Object, error) {
paramObj, err := c.UnsafeConvertToVersionVia(opts, version)
if err != nil {
return nil, err
}
return paramObj, nil
}
// ConvertResultToVersion converts a Result to the provided GroupVersion
func (c *MetricConverter) ConvertResultToVersion(res rest.Result, gv schema.GroupVersion) (runtime.Object, error) {
if err := res.Error(); err != nil {
return nil, err
}
metricBytes, err := res.Raw()
if err != nil {
return nil, err
}
decoder := c.codecs.UniversalDecoder(MetricVersions...)
rawMetricObj, err := runtime.Decode(decoder, metricBytes)
if err != nil {
return nil, err
}
metricObj, err := c.UnsafeConvertToVersionVia(rawMetricObj, gv)
if err != nil {
return nil, err
}
return metricObj, nil
}
// unsafeConvertToVersionVia is like Scheme.UnsafeConvertToVersion, but it does so via an internal version first.
// We use it here to work with the v1beta2 client internally, while preserving backwards compatibility for existing custom metrics adapters
func (c *MetricConverter) UnsafeConvertToVersionVia(obj runtime.Object, externalVersion schema.GroupVersion) (runtime.Object, error) {
objInt, err := c.scheme.UnsafeConvertToVersion(obj, schema.GroupVersion{Group: externalVersion.Group, Version: runtime.APIVersionInternal})
if err != nil {
return nil, fmt.Errorf("failed to convert the given object to the internal version: %v", err)
}
objExt, err := c.scheme.UnsafeConvertToVersion(objInt, externalVersion)
if err != nil {
return nil, fmt.Errorf("failed to convert the given object back to the external version: %v", err)
}
return objExt, err
}