Skip to content

Commit

Permalink
Merge pull request #348 from dgrisonnet/populate-selector
Browse files Browse the repository at this point in the history
Populate metric selector for custom metrics
  • Loading branch information
s-urbaniak authored Dec 15, 2020
2 parents aa77eca + 76020f6 commit f33fc94
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions pkg/custom-provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func NewPrometheusProvider(mapper apimeta.RESTMapper, kubeClient dynamic.Interfa
}, lister
}

func (p *prometheusProvider) metricFor(value pmodel.SampleValue, name types.NamespacedName, info provider.CustomMetricInfo) (*custom_metrics.MetricValue, error) {
func (p *prometheusProvider) metricFor(value pmodel.SampleValue, name types.NamespacedName, info provider.CustomMetricInfo, metricSelector labels.Selector) (*custom_metrics.MetricValue, error) {
ref, err := helpers.ReferenceFor(p.mapper, name, info)
if err != nil {
return nil, err
Expand All @@ -90,18 +90,29 @@ func (p *prometheusProvider) metricFor(value pmodel.SampleValue, name types.Name
} else {
q = resource.NewMilliQuantity(int64(value*1000.0), resource.DecimalSI)
}
return &custom_metrics.MetricValue{

metric := &custom_metrics.MetricValue{
DescribedObject: ref,
Metric: custom_metrics.MetricIdentifier{
Name: info.Metric,
},
// TODO(directxman12): use the right timestamp
Timestamp: metav1.Time{time.Now()},
Value: *q,
}, nil
}

if !metricSelector.Empty() {
sel, err := metav1.ParseToLabelSelector(metricSelector.String())
if err != nil {
return nil, err
}
metric.Metric.Selector = sel
}

return metric, nil
}

func (p *prometheusProvider) metricsFor(valueSet pmodel.Vector, info provider.CustomMetricInfo, namespace string, names []string) (*custom_metrics.MetricValueList, error) {
func (p *prometheusProvider) metricsFor(valueSet pmodel.Vector, namespace string, names []string, info provider.CustomMetricInfo, metricSelector labels.Selector) (*custom_metrics.MetricValueList, error) {
values, found := p.MatchValuesToNames(info, valueSet)
if !found {
return nil, provider.NewMetricNotFoundError(info.GroupResource, info.Metric)
Expand All @@ -113,7 +124,7 @@ func (p *prometheusProvider) metricsFor(valueSet pmodel.Vector, info provider.Cu
continue
}

value, err := p.metricFor(values[name], types.NamespacedName{Namespace: namespace, Name: name}, info)
value, err := p.metricFor(values[name], types.NamespacedName{Namespace: namespace, Name: name}, info, metricSelector)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -175,7 +186,7 @@ func (p *prometheusProvider) GetMetricByName(name types.NamespacedName, info pro
}

// return the resulting metric
return p.metricFor(resultValue, name, info)
return p.metricFor(resultValue, name, info, metricSelector)
}

func (p *prometheusProvider) GetMetricBySelector(namespace string, selector labels.Selector, info provider.CustomMetricInfo, metricSelector labels.Selector) (*custom_metrics.MetricValueList, error) {
Expand All @@ -194,7 +205,7 @@ func (p *prometheusProvider) GetMetricBySelector(namespace string, selector labe
}

// return the resulting metrics
return p.metricsFor(queryResults, info, namespace, resourceNames)
return p.metricsFor(queryResults, namespace, resourceNames, info, metricSelector)
}

type cachingMetricsLister struct {
Expand Down

0 comments on commit f33fc94

Please sign in to comment.