From 5fe5dee8bc8780662bd1e4989bf00a6a8ff5c01a Mon Sep 17 00:00:00 2001 From: Ted Yu Date: Wed, 24 Jul 2019 12:56:36 -0700 Subject: [PATCH] Check whether metricObj can be converted to *v1beta2.MetricValueList --- .../client/custom_metrics/versioned_client.go | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/staging/src/k8s.io/metrics/pkg/client/custom_metrics/versioned_client.go b/staging/src/k8s.io/metrics/pkg/client/custom_metrics/versioned_client.go index bfe9ff2922d..7965d2d9a9d 100644 --- a/staging/src/k8s.io/metrics/pkg/client/custom_metrics/versioned_client.go +++ b/staging/src/k8s.io/metrics/pkg/client/custom_metrics/versioned_client.go @@ -18,11 +18,12 @@ package custom_metrics import ( "fmt" + "reflect" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime/schema" - serializer "k8s.io/apimachinery/pkg/runtime/serializer" + "k8s.io/apimachinery/pkg/runtime/serializer" "k8s.io/client-go/rest" "k8s.io/client-go/util/flowcontrol" @@ -122,7 +123,11 @@ func (m *rootScopedMetrics) getForNamespace(namespace string, metricName string, return nil, err } - res := metricObj.(*v1beta2.MetricValueList) + var res *v1beta2.MetricValueList + var ok bool + if res, ok = metricObj.(*v1beta2.MetricValueList); !ok { + return nil, fmt.Errorf("the custom metrics API server didn't return MetricValueList, the type is %v", reflect.TypeOf(metricObj)) + } if len(res.Items) != 1 { return nil, fmt.Errorf("the custom metrics API server returned %v results when we asked for exactly one", len(res.Items)) } @@ -160,7 +165,11 @@ func (m *rootScopedMetrics) GetForObject(groupKind schema.GroupKind, name string return nil, err } - res := metricObj.(*v1beta2.MetricValueList) + var res *v1beta2.MetricValueList + var ok bool + if res, ok = metricObj.(*v1beta2.MetricValueList); !ok { + return nil, fmt.Errorf("the custom metrics API server didn't return MetricValueList, the type is %v", reflect.TypeOf(metricObj)) + } if len(res.Items) != 1 { return nil, fmt.Errorf("the custom metrics API server returned %v results when we asked for exactly one", len(res.Items)) } @@ -199,7 +208,11 @@ func (m *rootScopedMetrics) GetForObjects(groupKind schema.GroupKind, selector l return nil, err } - res := metricObj.(*v1beta2.MetricValueList) + var res *v1beta2.MetricValueList + var ok bool + if res, ok = metricObj.(*v1beta2.MetricValueList); !ok { + return nil, fmt.Errorf("the custom metrics API server didn't return MetricValueList, the type is %v", reflect.TypeOf(metricObj)) + } return res, nil } @@ -234,7 +247,11 @@ func (m *namespacedMetrics) GetForObject(groupKind schema.GroupKind, name string return nil, err } - res := metricObj.(*v1beta2.MetricValueList) + var res *v1beta2.MetricValueList + var ok bool + if res, ok = metricObj.(*v1beta2.MetricValueList); !ok { + return nil, fmt.Errorf("the custom metrics API server didn't return MetricValueList, the type is %v", reflect.TypeOf(metricObj)) + } if len(res.Items) != 1 { return nil, fmt.Errorf("the custom metrics API server returned %v results when we asked for exactly one", len(res.Items)) } @@ -269,6 +286,10 @@ func (m *namespacedMetrics) GetForObjects(groupKind schema.GroupKind, selector l return nil, err } - res := metricObj.(*v1beta2.MetricValueList) + var res *v1beta2.MetricValueList + var ok bool + if res, ok = metricObj.(*v1beta2.MetricValueList); !ok { + return nil, fmt.Errorf("the custom metrics API server didn't return MetricValueList, the type is %v", reflect.TypeOf(metricObj)) + } return res, nil }