From 3ae38c7417421ceebdc853926f76702bf90f4553 Mon Sep 17 00:00:00 2001 From: Carson Anderson Date: Fri, 5 Mar 2021 15:15:14 -0700 Subject: [PATCH] Allow metrics to be defined as namespaced: false When set to false, no namespace label will be set by the adapter based on the namespace portion of the url in the request path. This allows individual consumers to set namespace independent of the source kubernetes resource. --- Example: Given an adapter config like this: ``` externalRules: - seriesQuery: 'nsq_topic_depth' resources: namespaced: false ``` An HPA could target a different namespace by setting it in the selector: ``` - type: External external: metric: name: nsq_topic_depth selector: labelSelector: topic: my-topic namespace: nsq ``` This is useful for scaling on metrics from services that run in a differnt namespace than the source resource. --- pkg/config/config.go | 2 ++ pkg/naming/metric_namer.go | 12 ++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index fee4e006..2536604d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -58,6 +58,8 @@ type ResourceMapping struct { // Overrides specifies exceptions to the above template, mapping label names // to group-resources Overrides map[string]GroupResource `json:"overrides,omitempty" yaml:"overrides,omitempty"` + //Namespaced ignores the source namespace of the requester and requires one in the query + Namespaced *bool `json:"namespaced,omitempty" yaml:"namespaced,omitempty"` } // GroupResource represents a Kubernetes group-resource. diff --git a/pkg/naming/metric_namer.go b/pkg/naming/metric_namer.go index 54ac7183..7516a896 100644 --- a/pkg/naming/metric_namer.go +++ b/pkg/naming/metric_namer.go @@ -102,10 +102,15 @@ type metricNamer struct { nameMatches *regexp.Regexp nameAs string seriesMatchers []*ReMatcher + namespaced *bool ResourceConverter } +func (n *metricNamer) isNamespaced() bool { + return (n.namespaced != nil) && *n.namespaced +} + // queryTemplateArgs are the arguments for the metrics query template. func (n *metricNamer) FilterSeries(initialSeries []prom.Series) []prom.Series { if len(n.seriesMatchers) == 0 { @@ -131,8 +136,10 @@ func (n *metricNamer) QueryForSeries(series string, resource schema.GroupResourc } func (n *metricNamer) QueryForExternalSeries(series string, namespace string, metricSelector labels.Selector) (prom.Selector, error) { - //test := prom.Selector() - //return test, nil + if !n.isNamespaced() { + namespace = "" + } + return n.metricsQuery.BuildExternal(series, namespace, "", []string{}, metricSelector) } @@ -207,6 +214,7 @@ func NamersFromConfig(cfg []config.DiscoveryRule, mapper apimeta.RESTMapper) ([] nameMatches: nameMatches, nameAs: nameAs, seriesMatchers: seriesMatchers, + namespaced: rule.Resources.Namespaced, ResourceConverter: resConv, }