Skip to content

Commit c6101ab

Browse files
authored
Merge pull request kubernetes#80392 from tedyu/ver-client-cast
Check whether metricObj can be converted to *v1beta2.MetricValueList
2 parents 758af11 + 5fe5dee commit c6101ab

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

staging/src/k8s.io/metrics/pkg/client/custom_metrics/versioned_client.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ package custom_metrics
1818

1919
import (
2020
"fmt"
21+
"reflect"
2122

2223
"k8s.io/apimachinery/pkg/api/meta"
2324
"k8s.io/apimachinery/pkg/labels"
2425
"k8s.io/apimachinery/pkg/runtime/schema"
25-
serializer "k8s.io/apimachinery/pkg/runtime/serializer"
26+
"k8s.io/apimachinery/pkg/runtime/serializer"
2627
"k8s.io/client-go/rest"
2728
"k8s.io/client-go/util/flowcontrol"
2829

@@ -122,7 +123,11 @@ func (m *rootScopedMetrics) getForNamespace(namespace string, metricName string,
122123
return nil, err
123124
}
124125

125-
res := metricObj.(*v1beta2.MetricValueList)
126+
var res *v1beta2.MetricValueList
127+
var ok bool
128+
if res, ok = metricObj.(*v1beta2.MetricValueList); !ok {
129+
return nil, fmt.Errorf("the custom metrics API server didn't return MetricValueList, the type is %v", reflect.TypeOf(metricObj))
130+
}
126131
if len(res.Items) != 1 {
127132
return nil, fmt.Errorf("the custom metrics API server returned %v results when we asked for exactly one", len(res.Items))
128133
}
@@ -160,7 +165,11 @@ func (m *rootScopedMetrics) GetForObject(groupKind schema.GroupKind, name string
160165
return nil, err
161166
}
162167

163-
res := metricObj.(*v1beta2.MetricValueList)
168+
var res *v1beta2.MetricValueList
169+
var ok bool
170+
if res, ok = metricObj.(*v1beta2.MetricValueList); !ok {
171+
return nil, fmt.Errorf("the custom metrics API server didn't return MetricValueList, the type is %v", reflect.TypeOf(metricObj))
172+
}
164173
if len(res.Items) != 1 {
165174
return nil, fmt.Errorf("the custom metrics API server returned %v results when we asked for exactly one", len(res.Items))
166175
}
@@ -199,7 +208,11 @@ func (m *rootScopedMetrics) GetForObjects(groupKind schema.GroupKind, selector l
199208
return nil, err
200209
}
201210

202-
res := metricObj.(*v1beta2.MetricValueList)
211+
var res *v1beta2.MetricValueList
212+
var ok bool
213+
if res, ok = metricObj.(*v1beta2.MetricValueList); !ok {
214+
return nil, fmt.Errorf("the custom metrics API server didn't return MetricValueList, the type is %v", reflect.TypeOf(metricObj))
215+
}
203216
return res, nil
204217
}
205218

@@ -234,7 +247,11 @@ func (m *namespacedMetrics) GetForObject(groupKind schema.GroupKind, name string
234247
return nil, err
235248
}
236249

237-
res := metricObj.(*v1beta2.MetricValueList)
250+
var res *v1beta2.MetricValueList
251+
var ok bool
252+
if res, ok = metricObj.(*v1beta2.MetricValueList); !ok {
253+
return nil, fmt.Errorf("the custom metrics API server didn't return MetricValueList, the type is %v", reflect.TypeOf(metricObj))
254+
}
238255
if len(res.Items) != 1 {
239256
return nil, fmt.Errorf("the custom metrics API server returned %v results when we asked for exactly one", len(res.Items))
240257
}
@@ -269,6 +286,10 @@ func (m *namespacedMetrics) GetForObjects(groupKind schema.GroupKind, selector l
269286
return nil, err
270287
}
271288

272-
res := metricObj.(*v1beta2.MetricValueList)
289+
var res *v1beta2.MetricValueList
290+
var ok bool
291+
if res, ok = metricObj.(*v1beta2.MetricValueList); !ok {
292+
return nil, fmt.Errorf("the custom metrics API server didn't return MetricValueList, the type is %v", reflect.TypeOf(metricObj))
293+
}
273294
return res, nil
274295
}

0 commit comments

Comments
 (0)