mirror of
https://github.com/prometheus-operator/prometheus-operator.git
synced 2025-04-21 11:48:53 +00:00
pkg/prometheus: make replicas label configurable
This change enables existing Thanos deployments which adopted any label name other than `prometheus_replica` to migrate to Prometheus Operator without losing of Thanos deduplication features. Defaults to `prometheus_replica` for backward compatibility.
This commit is contained in:
parent
633daa0f3c
commit
2ad1caf3ea
7 changed files with 43 additions and 17 deletions
Documentation
example/prometheus-operator-crd
jsonnet/prometheus-operator
pkg
|
@ -274,6 +274,7 @@ PrometheusSpec is a specification of the desired behavior of the Prometheus clus
|
|||
| baseImage | Base image to use for a Prometheus deployment. | string | false |
|
||||
| imagePullSecrets | An optional list of references to secrets in the same namespace to use for pulling prometheus and alertmanager images from registries see http://kubernetes.io/docs/user-guide/images#specifying-imagepullsecrets-on-a-pod | [][v1.LocalObjectReference](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.11/#localobjectreference-v1-core) | false |
|
||||
| replicas | Number of instances to deploy for a Prometheus deployment. | *int32 | false |
|
||||
| replicaExternalLabelName | Name of Prometheus external label used to denote replica name. Defaults to the value of `prometheus_replica`. | string | false |
|
||||
| retention | Time duration Prometheus shall retain data for. Default is '24h', and must match the regular expression `[0-9]+(ms\|s\|m\|h\|d\|w\|y)` (milliseconds seconds minutes hours days weeks years). | string | false |
|
||||
| logLevel | Log level for Prometheus to be configured with. | string | false |
|
||||
| logFormat | Log format for Prometheus to be configured with. | string | false |
|
||||
|
|
|
@ -2147,6 +2147,10 @@ spec:
|
|||
required:
|
||||
- url
|
||||
type: array
|
||||
replicaExternalLabelName:
|
||||
description: Name of Prometheus external label used to denote replica
|
||||
name. Defaults to the value of `prometheus_replica`.
|
||||
type: string
|
||||
replicas:
|
||||
description: Number of instances to deploy for a Prometheus deployment.
|
||||
format: int32
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1298,6 +1298,13 @@ func schema_pkg_apis_monitoring_v1_PrometheusSpec(ref common.ReferenceCallback)
|
|||
Format: "int32",
|
||||
},
|
||||
},
|
||||
"replicaExternalLabelName": {
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Description: "Name of Prometheus external label used to denote replica name. Defaults to the value of `prometheus_replica`.",
|
||||
Type: []string{"string"},
|
||||
Format: "",
|
||||
},
|
||||
},
|
||||
"retention": {
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Description: "Time duration Prometheus shall retain data for. Default is '24h', and must match the regular expression `[0-9]+(ms|s|m|h|d|w|y)` (milliseconds seconds minutes hours days weeks years).",
|
||||
|
|
|
@ -109,6 +109,9 @@ type PrometheusSpec struct {
|
|||
ImagePullSecrets []v1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
|
||||
// Number of instances to deploy for a Prometheus deployment.
|
||||
Replicas *int32 `json:"replicas,omitempty"`
|
||||
// Name of Prometheus external label used to denote replica name.
|
||||
// Defaults to the value of `prometheus_replica`.
|
||||
ReplicaExternalLabelName string `json:"replicaExternalLabelName,omitempty"`
|
||||
// Time duration Prometheus shall retain data for. Default is '24h',
|
||||
// and must match the regular expression `[0-9]+(ms|s|m|h|d|w|y)` (milliseconds seconds minutes hours days weeks years).
|
||||
Retention string `json:"retention,omitempty"`
|
||||
|
|
|
@ -91,7 +91,13 @@ func buildExternalLabels(p *v1.Prometheus) yaml.MapSlice {
|
|||
m := map[string]string{}
|
||||
|
||||
m["prometheus"] = fmt.Sprintf("%s/%s", p.Namespace, p.Name)
|
||||
m["prometheus_replica"] = "$(POD_NAME)"
|
||||
|
||||
replicaExternalLabelName := p.Spec.ReplicaExternalLabelName
|
||||
if replicaExternalLabelName == "" {
|
||||
replicaExternalLabelName = defaultReplicaExternalLabelName
|
||||
}
|
||||
|
||||
m[replicaExternalLabelName] = "$(POD_NAME)"
|
||||
|
||||
for n, v := range p.Spec.ExternalLabels {
|
||||
m[n] = v
|
||||
|
@ -195,11 +201,15 @@ func (cg *configGenerator) generateConfig(
|
|||
var alertRelabelConfigs []yaml.MapSlice
|
||||
|
||||
// action 'labeldrop' is not supported <= v1.4.1
|
||||
replicaExternalLabelName := p.Spec.ReplicaExternalLabelName
|
||||
if replicaExternalLabelName == "" {
|
||||
replicaExternalLabelName = defaultReplicaExternalLabelName
|
||||
}
|
||||
if version.GT(semver.MustParse("1.4.1")) {
|
||||
// Drop 'prometheus_replica' label, to make alerts from two Prometheus replicas alike
|
||||
// Drop replica label, to make alerts from multiple Prometheus replicas alike
|
||||
alertRelabelConfigs = append(alertRelabelConfigs, yaml.MapSlice{
|
||||
{Key: "action", Value: "labeldrop"},
|
||||
{Key: "regex", Value: "prometheus_replica"},
|
||||
{Key: "regex", Value: regexp.QuoteMeta(replicaExternalLabelName)},
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -33,19 +33,20 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
governingServiceName = "prometheus-operated"
|
||||
DefaultPrometheusVersion = "v2.7.1"
|
||||
DefaultThanosVersion = "v0.2.1"
|
||||
defaultRetention = "24h"
|
||||
storageDir = "/prometheus"
|
||||
confDir = "/etc/prometheus/config"
|
||||
confOutDir = "/etc/prometheus/config_out"
|
||||
rulesDir = "/etc/prometheus/rules"
|
||||
secretsDir = "/etc/prometheus/secrets/"
|
||||
configmapsDir = "/etc/prometheus/configmaps/"
|
||||
configFilename = "prometheus.yaml.gz"
|
||||
configEnvsubstFilename = "prometheus.env.yaml"
|
||||
sSetInputHashName = "prometheus-operator-input-hash"
|
||||
governingServiceName = "prometheus-operated"
|
||||
DefaultPrometheusVersion = "v2.7.1"
|
||||
DefaultThanosVersion = "v0.2.1"
|
||||
defaultRetention = "24h"
|
||||
defaultReplicaExternalLabelName = "prometheus_replica"
|
||||
storageDir = "/prometheus"
|
||||
confDir = "/etc/prometheus/config"
|
||||
confOutDir = "/etc/prometheus/config_out"
|
||||
rulesDir = "/etc/prometheus/rules"
|
||||
secretsDir = "/etc/prometheus/secrets/"
|
||||
configmapsDir = "/etc/prometheus/configmaps/"
|
||||
configFilename = "prometheus.yaml.gz"
|
||||
configEnvsubstFilename = "prometheus.env.yaml"
|
||||
sSetInputHashName = "prometheus-operator-input-hash"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue