mirror of
https://github.com/coreos/prometheus-operator.git
synced 2026-02-05 06:45:27 +01:00
chore: use constants for env variables
The SHARD and POD_NAME variables were defined in different places. Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
@@ -29,6 +29,7 @@ import (
|
||||
"time"
|
||||
|
||||
logging "github.com/prometheus-operator/prometheus-operator/internal/log"
|
||||
"github.com/prometheus-operator/prometheus-operator/pkg/operator"
|
||||
"github.com/prometheus-operator/prometheus-operator/pkg/versionutil"
|
||||
|
||||
"github.com/alecthomas/kingpin/v2"
|
||||
@@ -47,8 +48,7 @@ const (
|
||||
defaultRetryInterval = 5 * time.Second // 5 seconds was the value previously hardcoded in github.com/thanos-io/thanos/pkg/reloader.
|
||||
defaultReloadTimeout = 30 * time.Second // 30 seconds was the default value
|
||||
|
||||
statefulsetOrdinalEnvvar = "STATEFULSET_ORDINAL_NUMBER"
|
||||
statefulsetOrdinalFromEnvvarDefault = "POD_NAME"
|
||||
statefulsetOrdinalEnvvar = "STATEFULSET_ORDINAL_NUMBER"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -69,7 +69,7 @@ func main() {
|
||||
createStatefulsetOrdinalFrom := app.Flag(
|
||||
"statefulset-ordinal-from-envvar",
|
||||
fmt.Sprintf("parse this environment variable to create %s, containing the statefulset ordinal number", statefulsetOrdinalEnvvar)).
|
||||
Default(statefulsetOrdinalFromEnvvarDefault).String()
|
||||
Default(operator.PodNameEnvVar).String()
|
||||
|
||||
listenAddress := app.Flag(
|
||||
"listen-address",
|
||||
|
||||
@@ -23,6 +23,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/go-test/deep"
|
||||
|
||||
"github.com/prometheus-operator/prometheus-operator/pkg/operator"
|
||||
)
|
||||
|
||||
var cases = []struct {
|
||||
@@ -40,8 +42,8 @@ var cases = []struct {
|
||||
func TestCreateOrdinalEnvVar(t *testing.T) {
|
||||
for _, tt := range cases {
|
||||
t.Run(tt.in, func(t *testing.T) {
|
||||
os.Setenv(statefulsetOrdinalFromEnvvarDefault, tt.in)
|
||||
s := createOrdinalEnvvar(statefulsetOrdinalFromEnvvarDefault)
|
||||
os.Setenv(operator.PodNameEnvVar, tt.in)
|
||||
s := createOrdinalEnvvar(operator.PodNameEnvVar)
|
||||
if os.Getenv(statefulsetOrdinalEnvvar) != tt.out {
|
||||
t.Errorf("got %v, want %s", s, tt.out)
|
||||
}
|
||||
|
||||
@@ -25,7 +25,17 @@ import (
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
)
|
||||
|
||||
const configReloaderPort = 8080
|
||||
const (
|
||||
configReloaderPort = 8080
|
||||
|
||||
// ShardEnvVar is the name of the environment variable injected into the
|
||||
// config-reloader container that contains the shard number.
|
||||
ShardEnvVar = "SHARD"
|
||||
|
||||
// PodNameEnvVar is the name of the environment variable injected in the
|
||||
// config-reloader container that contains the pod name.
|
||||
PodNameEnvVar = "POD_NAME"
|
||||
)
|
||||
|
||||
// ConfigReloader contains the options to configure
|
||||
// a config-reloader container
|
||||
@@ -152,7 +162,7 @@ func CreateConfigReloader(name string, options ...ReloaderOption) v1.Container {
|
||||
args = make([]string, 0)
|
||||
envVars = []v1.EnvVar{
|
||||
{
|
||||
Name: "POD_NAME",
|
||||
Name: PodNameEnvVar,
|
||||
ValueFrom: &v1.EnvVarSource{
|
||||
FieldRef: &v1.ObjectFieldSelector{FieldPath: "metadata.name"},
|
||||
},
|
||||
@@ -225,7 +235,7 @@ func CreateConfigReloader(name string, options ...ReloaderOption) v1.Container {
|
||||
|
||||
if configReloader.shard != nil {
|
||||
envVars = append(envVars, v1.EnvVar{
|
||||
Name: "SHARD",
|
||||
Name: ShardEnvVar,
|
||||
Value: strconv.Itoa(int(*configReloader.shard)),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -41,6 +41,8 @@ const (
|
||||
kubernetesSDRoleEndpointSlice = "endpointslice"
|
||||
kubernetesSDRolePod = "pod"
|
||||
kubernetesSDRoleIngress = "ingress"
|
||||
|
||||
defaultReplicaExternalLabelName = "prometheus_replica"
|
||||
)
|
||||
|
||||
var invalidLabelCharRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
|
||||
@@ -397,7 +399,7 @@ func (cg *ConfigGenerator) buildExternalLabels() yaml.MapSlice {
|
||||
|
||||
// Do not add the external label if the resulting value is empty.
|
||||
if replicaExternalLabelName != "" {
|
||||
m[replicaExternalLabelName] = "$(POD_NAME)"
|
||||
m[replicaExternalLabelName] = fmt.Sprintf("$(%s)", operator.PodNameEnvVar)
|
||||
}
|
||||
|
||||
for n, v := range cpf.ExternalLabels {
|
||||
@@ -1372,7 +1374,7 @@ func generateAddressShardingRelabelingRulesWithSourceLabel(relabelings []yaml.Ma
|
||||
{Key: "action", Value: "hashmod"},
|
||||
}, yaml.MapSlice{
|
||||
{Key: "source_labels", Value: []string{"__tmp_hash"}},
|
||||
{Key: "regex", Value: "$(SHARD)"},
|
||||
{Key: "regex", Value: fmt.Sprintf("$(%s)", operator.ShardEnvVar)},
|
||||
{Key: "action", Value: "keep"},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -32,12 +32,11 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
defaultReplicaExternalLabelName = "prometheus_replica"
|
||||
StorageDir = "/prometheus"
|
||||
ConfDir = "/etc/prometheus/config"
|
||||
ConfOutDir = "/etc/prometheus/config_out"
|
||||
WebConfigDir = "/etc/prometheus/web_config"
|
||||
tlsAssetsDir = "/etc/prometheus/certs"
|
||||
StorageDir = "/prometheus"
|
||||
ConfDir = "/etc/prometheus/config"
|
||||
ConfOutDir = "/etc/prometheus/config_out"
|
||||
WebConfigDir = "/etc/prometheus/web_config"
|
||||
tlsAssetsDir = "/etc/prometheus/certs"
|
||||
//TODO: RulesDir should be moved to the server package, since it is not used by the agent.
|
||||
// It is here at the moment because promcfg uses it, and moving as is will cause import cycle error.
|
||||
RulesDir = "/etc/prometheus/rules"
|
||||
|
||||
Reference in New Issue
Block a user