Debian Codegraph @

pkg/client/custom_metrics/fake/fake_client.go

raw symbols SCIP
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 fake
18
19 import (
20 "fmt"
21
22 "k8s.io/apimachinery/pkg/api/meta"
23 "k8s.io/apimachinery/pkg/labels"
24 "k8s.io/apimachinery/pkg/runtime/schema"
25 "k8s.io/client-go/testing"
26 "k8s.io/metrics/pkg/apis/custom_metrics/v1beta2"
27 cmclient "k8s.io/metrics/pkg/client/custom_metrics"
28 )
29
30 type GetForActionImpl struct {
31 testing.GetAction
32 MetricName string
33 LabelSelector labels.Selector
34 }
35
36 type GetForAction interface {
37 testing.GetAction
38 GetMetricName() string
39 GetLabelSelector() labels.Selector
40 }
41
42 func (i GetForActionImpl) GetMetricName() string {
43 return i.MetricName
44 }
45
46 func (i GetForActionImpl) GetLabelSelector() labels.Selector {
47 return i.LabelSelector
48 }
49
50 func (i GetForActionImpl) GetSubresource() string {
51 return i.MetricName
52 }
53
54 func (i GetForActionImpl) DeepCopy() testing.Action {
55 var labelSelector labels.Selector
56 if i.LabelSelector != nil {
57 labelSelector = i.LabelSelector.DeepCopySelector()
58 }
59 return GetForActionImpl{
60 GetAction: i.GetAction.DeepCopy().(testing.GetAction),
61 MetricName: i.MetricName,
62 LabelSelector: labelSelector,
63 }
64 }
65
66 func NewGetForAction(groupKind schema.GroupKind, namespace, name string, metricName string, labelSelector labels.Selector) GetForActionImpl {
67 // the version doesn't matter
68 gvk := groupKind.WithVersion("")
69 gvr, _ := meta.UnsafeGuessKindToResource(gvk)
70 groupResourceForKind := schema.GroupResource{
71 Group: gvr.Group,
72 Resource: gvr.Resource,
73 }
74 resource := schema.GroupResource{
75 Group: v1beta2.SchemeGroupVersion.Group,
76 Resource: groupResourceForKind.String(),
77 }
78 return GetForActionImpl{
79 GetAction: testing.NewGetAction(resource.WithVersion(""), namespace, name),
80 MetricName: metricName,
81 LabelSelector: labelSelector,
82 }
83 }
84
85 func NewRootGetForAction(groupKind schema.GroupKind, name string, metricName string, labelSelector labels.Selector) GetForActionImpl {
86 // the version doesn't matter
87 gvk := groupKind.WithVersion("")
88 gvr, _ := meta.UnsafeGuessKindToResource(gvk)
89 groupResourceForKind := schema.GroupResource{
90 Group: gvr.Group,
91 Resource: gvr.Resource,
92 }
93 resource := schema.GroupResource{
94 Group: v1beta2.SchemeGroupVersion.Group,
95 Resource: groupResourceForKind.String(),
96 }
97 return GetForActionImpl{
98 GetAction: testing.NewRootGetAction(resource.WithVersion(""), name),
99 MetricName: metricName,
100 LabelSelector: labelSelector,
101 }
102 }
103
104 type FakeCustomMetricsClient struct {
105 testing.Fake
106 }
107
108 func (c *FakeCustomMetricsClient) RootScopedMetrics() cmclient.MetricsInterface {
109 return &fakeRootScopedMetrics{
110 Fake: c,
111 }
112 }
113
114 func (c *FakeCustomMetricsClient) NamespacedMetrics(namespace string) cmclient.MetricsInterface {
115 return &fakeNamespacedMetrics{
116 Fake: c,
117 ns: namespace,
118 }
119 }
120
121 type fakeNamespacedMetrics struct {
122 Fake *FakeCustomMetricsClient
123 ns string
124 }
125
126 func (m *fakeNamespacedMetrics) GetForObject(groupKind schema.GroupKind, name string, metricName string, metricSelector labels.Selector) (*v1beta2.MetricValue, error) {
127 obj, err := m.Fake.
128 Invokes(NewGetForAction(groupKind, m.ns, name, metricName, nil), &v1beta2.MetricValueList{})
129
130 if obj == nil {
131 return nil, err
132 }
133
134 objList := obj.(*v1beta2.MetricValueList)
135 if len(objList.Items) != 1 {
136 return nil, fmt.Errorf("the custom metrics API server returned %v results when we asked for exactly one", len(objList.Items))
137 }
138
139 return &objList.Items[0], err
140 }
141
142 func (m *fakeNamespacedMetrics) GetForObjects(groupKind schema.GroupKind, selector labels.Selector, metricName string, metricSelector labels.Selector) (*v1beta2.MetricValueList, error) {
143 obj, err := m.Fake.
144 Invokes(NewGetForAction(groupKind, m.ns, "*", metricName, selector), &v1beta2.MetricValueList{})
145
146 if obj == nil {
147 return nil, err
148 }
149
150 return obj.(*v1beta2.MetricValueList), err
151 }
152
153 type fakeRootScopedMetrics struct {
154 Fake *FakeCustomMetricsClient
155 }
156
157 func (m *fakeRootScopedMetrics) GetForObject(groupKind schema.GroupKind, name string, metricName string, metricSelector labels.Selector) (*v1beta2.MetricValue, error) {
158 obj, err := m.Fake.
159 Invokes(NewRootGetForAction(groupKind, name, metricName, nil), &v1beta2.MetricValueList{})
160
161 if obj == nil {
162 return nil, err
163 }
164
165 objList := obj.(*v1beta2.MetricValueList)
166 if len(objList.Items) != 1 {
167 return nil, fmt.Errorf("the custom metrics API server returned %v results when we asked for exactly one", len(objList.Items))
168 }
169
170 return &objList.Items[0], err
171 }
172
173 func (m *fakeRootScopedMetrics) GetForObjects(groupKind schema.GroupKind, selector labels.Selector, metricName string, metricSelector labels.Selector) (*v1beta2.MetricValueList, error) {
174 obj, err := m.Fake.
175 Invokes(NewRootGetForAction(groupKind, "*", metricName, selector), &v1beta2.MetricValueList{})
176
177 if obj == nil {
178 return nil, err
179 }
180
181 return obj.(*v1beta2.MetricValueList), err
182 }