mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-05 17:27:51 +00:00
Merge pull request #380 from carsonoid/issue-324-carsonoid
Allow metrics to be defined as `namespaced: false`
This commit is contained in:
commit
c893b1140c
6 changed files with 152 additions and 5 deletions
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -131,8 +131,6 @@ 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
|
||||
return n.metricsQuery.BuildExternal(series, namespace, "", []string{}, metricSelector)
|
||||
}
|
||||
|
||||
|
|
@ -155,7 +153,13 @@ func NamersFromConfig(cfg []config.DiscoveryRule, mapper apimeta.RESTMapper) ([]
|
|||
return nil, err
|
||||
}
|
||||
|
||||
metricsQuery, err := NewMetricsQuery(rule.MetricsQuery, resConv)
|
||||
// queries are namespaced by default unless the rule specifically disables it
|
||||
namespaced := true
|
||||
if rule.Resources.Namespaced != nil {
|
||||
namespaced = *rule.Resources.Namespaced
|
||||
}
|
||||
|
||||
metricsQuery, err := NewExternalMetricsQuery(rule.MetricsQuery, resConv, namespaced)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to construct metrics query associated with series query %q: %v", rule.SeriesQuery, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,6 +59,27 @@ func NewMetricsQuery(queryTemplate string, resourceConverter ResourceConverter)
|
|||
return &metricsQuery{
|
||||
resConverter: resourceConverter,
|
||||
template: templ,
|
||||
namespaced: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// NewExternalMetricsQuery constructs a new MetricsQuery by compiling the given Go template.
|
||||
// The delimiters on the template are `<<` and `>>`, and it may use the following fields:
|
||||
// - Series: the series in question
|
||||
// - LabelMatchers: a pre-stringified form of the label matchers for the resources in the query
|
||||
// - LabelMatchersByName: the raw map-form of the above matchers
|
||||
// - GroupBy: the group-by clause to use for the resources in the query (stringified)
|
||||
// - GroupBySlice: the raw slice form of the above group-by clause
|
||||
func NewExternalMetricsQuery(queryTemplate string, resourceConverter ResourceConverter, namespaced bool) (MetricsQuery, error) {
|
||||
templ, err := template.New("metrics-query").Delims("<<", ">>").Parse(queryTemplate)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to parse metrics query template %q: %v", queryTemplate, err)
|
||||
}
|
||||
|
||||
return &metricsQuery{
|
||||
resConverter: resourceConverter,
|
||||
template: templ,
|
||||
namespaced: namespaced,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
@ -68,6 +89,7 @@ func NewMetricsQuery(queryTemplate string, resourceConverter ResourceConverter)
|
|||
type metricsQuery struct {
|
||||
resConverter ResourceConverter
|
||||
template *template.Template
|
||||
namespaced bool
|
||||
}
|
||||
|
||||
// queryTemplateArgs contains the arguments for the template used in metricsQuery.
|
||||
|
|
@ -150,7 +172,7 @@ func (q *metricsQuery) BuildExternal(seriesName string, namespace string, groupB
|
|||
// Build up the query parts from the selector.
|
||||
queryParts = append(queryParts, q.createQueryPartsFromSelector(metricSelector)...)
|
||||
|
||||
if namespace != "" {
|
||||
if q.namespaced && namespace != "" {
|
||||
namespaceLbl, err := q.resConverter.LabelForResource(NsGroupResource)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
|
|||
|
|
@ -272,7 +272,15 @@ func TestBuildSelector(t *testing.T) {
|
|||
|
||||
func TestBuildExternalSelector(t *testing.T) {
|
||||
mustNewQuery := func(queryTemplate string) MetricsQuery {
|
||||
mq, err := NewMetricsQuery(queryTemplate, &resourceConverterMock{true})
|
||||
mq, err := NewExternalMetricsQuery(queryTemplate, &resourceConverterMock{true}, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return mq
|
||||
}
|
||||
|
||||
mustNewNonNamespacedQuery := func(queryTemplate string) MetricsQuery {
|
||||
mq, err := NewExternalMetricsQuery(queryTemplate, &resourceConverterMock{true}, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
@ -348,6 +356,19 @@ func TestBuildExternalSelector(t *testing.T) {
|
|||
hasSelector("default [foo bar]"),
|
||||
),
|
||||
},
|
||||
{
|
||||
name: "multiple GroupBySlice values with namespace disabled",
|
||||
|
||||
mq: mustNewNonNamespacedQuery(`<<index .LabelValuesByName "namespaces">> <<.GroupBySlice>>`),
|
||||
namespace: "default",
|
||||
groupBySlice: []string{"foo", "bar"},
|
||||
metricSelector: labels.NewSelector(),
|
||||
|
||||
check: checks(
|
||||
hasError(nil),
|
||||
hasSelector(" [foo bar]"),
|
||||
),
|
||||
},
|
||||
{
|
||||
name: "single LabelMatchers value",
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue