Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populate metric selector for custom metrics #348

Merged
merged 1 commit into from
Dec 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: I changed the order of the parameters here to match the metricFor function

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