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.
This commit is contained in:
Carson Anderson 2021-03-05 15:15:14 -07:00 committed by Carson Anderson
parent 7e11fe30ee
commit 3ae38c7417
2 changed files with 12 additions and 2 deletions

View file

@ -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.

View file

@ -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,
}