Safeguard against int64 Overflow and default to MaxInt64

This commit is contained in:
AshMenhennett 2019-11-02 00:05:02 +11:00
parent 718cb2400e
commit dce5b81642

View file

@ -16,6 +16,7 @@ package provider
import ( import (
"errors" "errors"
"fmt" "fmt"
"math"
prom "github.com/directxman12/k8s-prometheus-adapter/pkg/client" prom "github.com/directxman12/k8s-prometheus-adapter/pkg/client"
"github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/provider" "github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/provider"
@ -55,12 +56,21 @@ func (c *metricConverter) Convert(info provider.ExternalMetricInfo, queryResult
func (c *metricConverter) convertSample(info provider.ExternalMetricInfo, sample *model.Sample) (*external_metrics.ExternalMetricValue, error) { func (c *metricConverter) convertSample(info provider.ExternalMetricInfo, sample *model.Sample) (*external_metrics.ExternalMetricValue, error) {
labels := c.convertLabels(sample.Metric) labels := c.convertLabels(sample.Metric)
intQuantity := int64(0)
if float64(sample.Value) == math.Inf(+1) {
intQuantity = math.MaxInt64
} else if float64(sample.Value) == math.Inf(-1) {
intQuantity = math.MinInt64
} else {
intQuantity = int64(sample.Value * 1000.0)
}
singleMetric := external_metrics.ExternalMetricValue{ singleMetric := external_metrics.ExternalMetricValue{
MetricName: info.Metric, MetricName: info.Metric,
Timestamp: metav1.Time{ Timestamp: metav1.Time{
sample.Timestamp.Time(), sample.Timestamp.Time(),
}, },
Value: *resource.NewMilliQuantity(int64(sample.Value*1000.0), resource.DecimalSI), Value: *resource.NewMilliQuantity(intQuantity, resource.DecimalSI),
MetricLabels: labels, MetricLabels: labels,
} }