forked from k3s-io/k3s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_client.go
182 lines (149 loc) · 5.22 KB
/
fake_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
/*
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 fake
import (
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/testing"
"k8s.io/metrics/pkg/apis/custom_metrics/v1beta2"
cmclient "k8s.io/metrics/pkg/client/custom_metrics"
)
type GetForActionImpl struct {
testing.GetAction
MetricName string
LabelSelector labels.Selector
}
type GetForAction interface {
testing.GetAction
GetMetricName() string
GetLabelSelector() labels.Selector
}
func (i GetForActionImpl) GetMetricName() string {
return i.MetricName
}
func (i GetForActionImpl) GetLabelSelector() labels.Selector {
return i.LabelSelector
}
func (i GetForActionImpl) GetSubresource() string {
return i.MetricName
}
func (i GetForActionImpl) DeepCopy() testing.Action {
var labelSelector labels.Selector
if i.LabelSelector != nil {
labelSelector = i.LabelSelector.DeepCopySelector()
}
return GetForActionImpl{
GetAction: i.GetAction.DeepCopy().(testing.GetAction),
MetricName: i.MetricName,
LabelSelector: labelSelector,
}
}
func NewGetForAction(groupKind schema.GroupKind, namespace, name string, metricName string, labelSelector labels.Selector) GetForActionImpl {
// the version doesn't matter
gvk := groupKind.WithVersion("")
gvr, _ := meta.UnsafeGuessKindToResource(gvk)
groupResourceForKind := schema.GroupResource{
Group: gvr.Group,
Resource: gvr.Resource,
}
resource := schema.GroupResource{
Group: v1beta2.SchemeGroupVersion.Group,
Resource: groupResourceForKind.String(),
}
return GetForActionImpl{
GetAction: testing.NewGetAction(resource.WithVersion(""), namespace, name),
MetricName: metricName,
LabelSelector: labelSelector,
}
}
func NewRootGetForAction(groupKind schema.GroupKind, name string, metricName string, labelSelector labels.Selector) GetForActionImpl {
// the version doesn't matter
gvk := groupKind.WithVersion("")
gvr, _ := meta.UnsafeGuessKindToResource(gvk)
groupResourceForKind := schema.GroupResource{
Group: gvr.Group,
Resource: gvr.Resource,
}
resource := schema.GroupResource{
Group: v1beta2.SchemeGroupVersion.Group,
Resource: groupResourceForKind.String(),
}
return GetForActionImpl{
GetAction: testing.NewRootGetAction(resource.WithVersion(""), name),
MetricName: metricName,
LabelSelector: labelSelector,
}
}
type FakeCustomMetricsClient struct {
testing.Fake
}
func (c *FakeCustomMetricsClient) RootScopedMetrics() cmclient.MetricsInterface {
return &fakeRootScopedMetrics{
Fake: c,
}
}
func (c *FakeCustomMetricsClient) NamespacedMetrics(namespace string) cmclient.MetricsInterface {
return &fakeNamespacedMetrics{
Fake: c,
ns: namespace,
}
}
type fakeNamespacedMetrics struct {
Fake *FakeCustomMetricsClient
ns string
}
func (m *fakeNamespacedMetrics) GetForObject(groupKind schema.GroupKind, name string, metricName string, metricSelector labels.Selector) (*v1beta2.MetricValue, error) {
obj, err := m.Fake.
Invokes(NewGetForAction(groupKind, m.ns, name, metricName, nil), &v1beta2.MetricValueList{})
if obj == nil {
return nil, err
}
objList := obj.(*v1beta2.MetricValueList)
if len(objList.Items) != 1 {
return nil, fmt.Errorf("the custom metrics API server returned %v results when we asked for exactly one", len(objList.Items))
}
return &objList.Items[0], err
}
func (m *fakeNamespacedMetrics) GetForObjects(groupKind schema.GroupKind, selector labels.Selector, metricName string, metricSelector labels.Selector) (*v1beta2.MetricValueList, error) {
obj, err := m.Fake.
Invokes(NewGetForAction(groupKind, m.ns, "*", metricName, selector), &v1beta2.MetricValueList{})
if obj == nil {
return nil, err
}
return obj.(*v1beta2.MetricValueList), err
}
type fakeRootScopedMetrics struct {
Fake *FakeCustomMetricsClient
}
func (m *fakeRootScopedMetrics) GetForObject(groupKind schema.GroupKind, name string, metricName string, metricSelector labels.Selector) (*v1beta2.MetricValue, error) {
obj, err := m.Fake.
Invokes(NewRootGetForAction(groupKind, name, metricName, nil), &v1beta2.MetricValueList{})
if obj == nil {
return nil, err
}
objList := obj.(*v1beta2.MetricValueList)
if len(objList.Items) != 1 {
return nil, fmt.Errorf("the custom metrics API server returned %v results when we asked for exactly one", len(objList.Items))
}
return &objList.Items[0], err
}
func (m *fakeRootScopedMetrics) GetForObjects(groupKind schema.GroupKind, selector labels.Selector, metricName string, metricSelector labels.Selector) (*v1beta2.MetricValueList, error) {
obj, err := m.Fake.
Invokes(NewRootGetForAction(groupKind, "*", metricName, selector), &v1beta2.MetricValueList{})
if obj == nil {
return nil, err
}
return obj.(*v1beta2.MetricValueList), err
}