1
0
Fork 0
mirror of https://github.com/prometheus-operator/prometheus-operator.git synced 2025-04-21 03:38:43 +00:00

feat: Support add consul filter ()

---------

Signed-off-by: dongjiang1989 <dongjiang1989@126.com>
This commit is contained in:
dongjiang 2024-11-22 18:33:25 +08:00 committed by GitHub
parent 26e72997e8
commit 514f90aabf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 150 additions and 16 deletions

20
Documentation/api.md generated
View file

@ -20898,7 +20898,8 @@ string
</td>
<td>
<em>(Optional)</em>
<p>An optional list of tags used to filter nodes for a given service. Services must contain all tags in the list.</p>
<p>An optional list of tags used to filter nodes for a given service. Services must contain all tags in the list.
Starting with Consul 1.14, it is recommended to use <code>filter</code> with the <code>ServiceTags</code> selector instead.</p>
</td>
</tr>
<tr>
@ -20923,7 +20924,22 @@ map[string]string
</td>
<td>
<em>(Optional)</em>
<p>Node metadata key/value pairs to filter nodes for a given service.</p>
<p>Node metadata key/value pairs to filter nodes for a given service.
Starting with Consul 1.14, it is recommended to use <code>filter</code> with the <code>NodeMeta</code> selector instead.</p>
</td>
</tr>
<tr>
<td>
<code>filter</code><br/>
<em>
string
</em>
</td>
<td>
<em>(Optional)</em>
<p>Filter expression used to filter the catalog results.
See <a href="https://www.consul.io/api-docs/catalog#list-services">https://www.consul.io/api-docs/catalog#list-services</a>
It requires Prometheus &gt;= 3.0.0.</p>
</td>
</tr>
<tr>

17
bundle.yaml generated
View file

@ -45035,6 +45035,13 @@ spec:
Whether to enable HTTP2.
If unset, Prometheus uses its default value.
type: boolean
filter:
description: |-
Filter expression used to filter the catalog results.
See https://www.consul.io/api-docs/catalog#list-services
It requires Prometheus >= 3.0.0.
minLength: 1
type: string
followRedirects:
description: |-
Configure whether HTTP requests follow HTTP 3xx redirects.
@ -45058,8 +45065,9 @@ spec:
nodeMeta:
additionalProperties:
type: string
description: Node metadata key/value pairs to filter nodes for
a given service.
description: |-
Node metadata key/value pairs to filter nodes for a given service.
Starting with Consul 1.14, it is recommended to use `filter` with the `NodeMeta` selector instead.
type: object
x-kubernetes-map-type: atomic
oauth2:
@ -45477,8 +45485,9 @@ spec:
minLength: 1
type: string
tags:
description: An optional list of tags used to filter nodes for
a given service. Services must contain all tags in the list.
description: |-
An optional list of tags used to filter nodes for a given service. Services must contain all tags in the list.
Starting with Consul 1.14, it is recommended to use `filter` with the `ServiceTags` selector instead.
items:
type: string
type: array

View file

@ -337,6 +337,13 @@ spec:
Whether to enable HTTP2.
If unset, Prometheus uses its default value.
type: boolean
filter:
description: |-
Filter expression used to filter the catalog results.
See https://www.consul.io/api-docs/catalog#list-services
It requires Prometheus >= 3.0.0.
minLength: 1
type: string
followRedirects:
description: |-
Configure whether HTTP requests follow HTTP 3xx redirects.
@ -360,8 +367,9 @@ spec:
nodeMeta:
additionalProperties:
type: string
description: Node metadata key/value pairs to filter nodes for
a given service.
description: |-
Node metadata key/value pairs to filter nodes for a given service.
Starting with Consul 1.14, it is recommended to use `filter` with the `NodeMeta` selector instead.
type: object
x-kubernetes-map-type: atomic
oauth2:
@ -779,8 +787,9 @@ spec:
minLength: 1
type: string
tags:
description: An optional list of tags used to filter nodes for
a given service. Services must contain all tags in the list.
description: |-
An optional list of tags used to filter nodes for a given service. Services must contain all tags in the list.
Starting with Consul 1.14, it is recommended to use `filter` with the `ServiceTags` selector instead.
items:
type: string
type: array

View file

@ -338,6 +338,13 @@ spec:
Whether to enable HTTP2.
If unset, Prometheus uses its default value.
type: boolean
filter:
description: |-
Filter expression used to filter the catalog results.
See https://www.consul.io/api-docs/catalog#list-services
It requires Prometheus >= 3.0.0.
minLength: 1
type: string
followRedirects:
description: |-
Configure whether HTTP requests follow HTTP 3xx redirects.
@ -361,8 +368,9 @@ spec:
nodeMeta:
additionalProperties:
type: string
description: Node metadata key/value pairs to filter nodes for
a given service.
description: |-
Node metadata key/value pairs to filter nodes for a given service.
Starting with Consul 1.14, it is recommended to use `filter` with the `NodeMeta` selector instead.
type: object
x-kubernetes-map-type: atomic
oauth2:
@ -780,8 +788,9 @@ spec:
minLength: 1
type: string
tags:
description: An optional list of tags used to filter nodes for
a given service. Services must contain all tags in the list.
description: |-
An optional list of tags used to filter nodes for a given service. Services must contain all tags in the list.
Starting with Consul 1.14, it is recommended to use `filter` with the `ServiceTags` selector instead.
items:
type: string
type: array

View file

@ -307,6 +307,11 @@
"description": "Whether to enable HTTP2.\nIf unset, Prometheus uses its default value.",
"type": "boolean"
},
"filter": {
"description": "Filter expression used to filter the catalog results.\nSee https://www.consul.io/api-docs/catalog#list-services\nIt requires Prometheus >= 3.0.0.",
"minLength": 1,
"type": "string"
},
"followRedirects": {
"description": "Configure whether HTTP requests follow HTTP 3xx redirects.\nIf unset, Prometheus uses its default value.",
"type": "boolean"
@ -324,7 +329,7 @@
"additionalProperties": {
"type": "string"
},
"description": "Node metadata key/value pairs to filter nodes for a given service.",
"description": "Node metadata key/value pairs to filter nodes for a given service.\nStarting with Consul 1.14, it is recommended to use `filter` with the `NodeMeta` selector instead.",
"type": "object",
"x-kubernetes-map-type": "atomic"
},
@ -720,7 +725,7 @@
"type": "string"
},
"tags": {
"description": "An optional list of tags used to filter nodes for a given service. Services must contain all tags in the list.",
"description": "An optional list of tags used to filter nodes for a given service. Services must contain all tags in the list.\nStarting with Consul 1.14, it is recommended to use `filter` with the `ServiceTags` selector instead.",
"items": {
"type": "string"
},

View file

@ -504,6 +504,7 @@ type ConsulSDConfig struct {
// +optional
Services []string `json:"services,omitempty"`
// An optional list of tags used to filter nodes for a given service. Services must contain all tags in the list.
// Starting with Consul 1.14, it is recommended to use `filter` with the `ServiceTags` selector instead.
// +listType:=set
// +optional
Tags []string `json:"tags,omitempty"`
@ -513,9 +514,16 @@ type ConsulSDConfig struct {
// +optional
TagSeparator *string `json:"tagSeparator,omitempty"`
// Node metadata key/value pairs to filter nodes for a given service.
// Starting with Consul 1.14, it is recommended to use `filter` with the `NodeMeta` selector instead.
// +mapType:=atomic
// +optional
NodeMeta map[string]string `json:"nodeMeta,omitempty"`
// Filter expression used to filter the catalog results.
// See https://www.consul.io/api-docs/catalog#list-services
// It requires Prometheus >= 3.0.0.
// +kubebuilder:validation:MinLength=1
// +optional
Filter *string `json:"filter,omitempty"`
// Allow stale Consul results (see https://www.consul.io/api/features/consistency.html). Will reduce load on Consul.
// If unset, Prometheus uses its default value.
// +optional

View file

@ -241,6 +241,11 @@ func (in *ConsulSDConfig) DeepCopyInto(out *ConsulSDConfig) {
(*out)[key] = val
}
}
if in.Filter != nil {
in, out := &in.Filter, &out.Filter
*out = new(string)
**out = **in
}
if in.AllowStale != nil {
in, out := &in.AllowStale, &out.AllowStale
*out = new(bool)

View file

@ -36,6 +36,7 @@ type ConsulSDConfigApplyConfiguration struct {
Tags []string `json:"tags,omitempty"`
TagSeparator *string `json:"tagSeparator,omitempty"`
NodeMeta map[string]string `json:"nodeMeta,omitempty"`
Filter *string `json:"filter,omitempty"`
AllowStale *bool `json:"allowStale,omitempty"`
RefreshInterval *monitoringv1.Duration `json:"refreshInterval,omitempty"`
BasicAuth *applyconfigurationmonitoringv1.BasicAuthApplyConfiguration `json:"basicAuth,omitempty"`
@ -151,6 +152,14 @@ func (b *ConsulSDConfigApplyConfiguration) WithNodeMeta(entries map[string]strin
return b
}
// WithFilter sets the Filter field in the declarative configuration to the given value
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
// If called multiple times, the Filter field is set to the value of the last call.
func (b *ConsulSDConfigApplyConfiguration) WithFilter(value string) *ConsulSDConfigApplyConfiguration {
b.Filter = &value
return b
}
// WithAllowStale sets the AllowStale field in the declarative configuration to the given value
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
// If called multiple times, the AllowStale field is set to the value of the last call.

View file

@ -3273,6 +3273,13 @@ func (cg *ConfigGenerator) generateScrapeConfig(
})
}
if config.Filter != nil {
configs[i] = append(configs[i], yaml.MapItem{
Key: "filter",
Value: config.Filter,
})
}
if config.AllowStale != nil {
configs[i] = append(configs[i], yaml.MapItem{
Key: "allow_stale",

View file

@ -6993,6 +6993,18 @@ func TestScrapeConfigSpecConfigWithConsulSD(t *testing.T) {
},
},
golden: "ConsulScrapeConfig.golden",
}, {
name: "consul_scrape_config_with_filter",
version: "3.0.0",
scSpec: monitoringv1alpha1.ScrapeConfigSpec{
ConsulSDConfigs: []monitoringv1alpha1.ConsulSDConfig{
{
Server: "localhost",
Filter: ptr.To("Meta.env == \"qa\""),
},
},
},
golden: "ConsulScrapeConfigWithFilter.golden",
}, {
name: "consul_scrape_config_basic_auth",
scSpec: monitoringv1alpha1.ScrapeConfigSpec{

View file

@ -1035,6 +1035,10 @@ func (rs *ResourceSelector) validateConsulSDConfigs(ctx context.Context, sc *mon
return fmt.Errorf("field `config.Namespace` is only supported for Prometheus version >= 2.28.0")
}
if config.Filter != nil && rs.version.Major < 3 {
return fmt.Errorf("field `config.Filter` is only supported for Prometheus version >= 3.0.0")
}
if err := rs.store.AddBasicAuth(ctx, sc.GetNamespace(), config.BasicAuth); err != nil {
return fmt.Errorf("[%d]: %w", i, err)
}

View file

@ -2105,6 +2105,32 @@ func TestSelectScrapeConfigs(t *testing.T) {
},
selected: true,
},
{
scenario: "Consul SD config with filter",
updateSpec: func(sc *monitoringv1alpha1.ScrapeConfigSpec) {
sc.ConsulSDConfigs = []monitoringv1alpha1.ConsulSDConfig{
{
Server: "example.com",
Filter: ptr.To("Meta.env == \"qa\""),
},
}
},
selected: true,
promVersion: "3.0.0",
},
{
scenario: "Consul SD config with filter but unsupported prometheus version",
updateSpec: func(sc *monitoringv1alpha1.ScrapeConfigSpec) {
sc.ConsulSDConfigs = []monitoringv1alpha1.ConsulSDConfig{
{
Server: "example.com",
Filter: ptr.To("Meta.env == \"qa\""),
},
}
},
selected: false,
promVersion: "2.55.0",
},
{
scenario: "Consul SD proxy config with invalid secret key",
updateSpec: func(sc *monitoringv1alpha1.ScrapeConfigSpec) {

View file

@ -0,0 +1,15 @@
global:
scrape_interval: 30s
external_labels:
prometheus: default/test
prometheus_replica: $(POD_NAME)
evaluation_interval: 30s
scrape_configs:
- job_name: scrapeConfig/default/testscrapeconfig1
consul_sd_configs:
- server: localhost
filter: Meta.env == "qa"
relabel_configs:
- source_labels:
- job
target_label: __tmp_prometheus_job_name