1
0
mirror of https://github.com/coreos/prometheus-operator.git synced 2026-02-05 15:46:31 +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:
Simon Pasquier
2023-06-14 10:49:22 +02:00
parent 4b5aa9f673
commit 8318c2e336
5 changed files with 29 additions and 16 deletions

View File

@@ -29,6 +29,7 @@ import (
"time" "time"
logging "github.com/prometheus-operator/prometheus-operator/internal/log" 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/prometheus-operator/prometheus-operator/pkg/versionutil"
"github.com/alecthomas/kingpin/v2" "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. 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 defaultReloadTimeout = 30 * time.Second // 30 seconds was the default value
statefulsetOrdinalEnvvar = "STATEFULSET_ORDINAL_NUMBER" statefulsetOrdinalEnvvar = "STATEFULSET_ORDINAL_NUMBER"
statefulsetOrdinalFromEnvvarDefault = "POD_NAME"
) )
func main() { func main() {
@@ -69,7 +69,7 @@ func main() {
createStatefulsetOrdinalFrom := app.Flag( createStatefulsetOrdinalFrom := app.Flag(
"statefulset-ordinal-from-envvar", "statefulset-ordinal-from-envvar",
fmt.Sprintf("parse this environment variable to create %s, containing the statefulset ordinal number", statefulsetOrdinalEnvvar)). 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( listenAddress := app.Flag(
"listen-address", "listen-address",

View File

@@ -23,6 +23,8 @@ import (
"time" "time"
"github.com/go-test/deep" "github.com/go-test/deep"
"github.com/prometheus-operator/prometheus-operator/pkg/operator"
) )
var cases = []struct { var cases = []struct {
@@ -40,8 +42,8 @@ var cases = []struct {
func TestCreateOrdinalEnvVar(t *testing.T) { func TestCreateOrdinalEnvVar(t *testing.T) {
for _, tt := range cases { for _, tt := range cases {
t.Run(tt.in, func(t *testing.T) { t.Run(tt.in, func(t *testing.T) {
os.Setenv(statefulsetOrdinalFromEnvvarDefault, tt.in) os.Setenv(operator.PodNameEnvVar, tt.in)
s := createOrdinalEnvvar(statefulsetOrdinalFromEnvvarDefault) s := createOrdinalEnvvar(operator.PodNameEnvVar)
if os.Getenv(statefulsetOrdinalEnvvar) != tt.out { if os.Getenv(statefulsetOrdinalEnvvar) != tt.out {
t.Errorf("got %v, want %s", s, tt.out) t.Errorf("got %v, want %s", s, tt.out)
} }

View File

@@ -25,7 +25,17 @@ import (
"k8s.io/apimachinery/pkg/util/intstr" "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 // ConfigReloader contains the options to configure
// a config-reloader container // a config-reloader container
@@ -152,7 +162,7 @@ func CreateConfigReloader(name string, options ...ReloaderOption) v1.Container {
args = make([]string, 0) args = make([]string, 0)
envVars = []v1.EnvVar{ envVars = []v1.EnvVar{
{ {
Name: "POD_NAME", Name: PodNameEnvVar,
ValueFrom: &v1.EnvVarSource{ ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{FieldPath: "metadata.name"}, FieldRef: &v1.ObjectFieldSelector{FieldPath: "metadata.name"},
}, },
@@ -225,7 +235,7 @@ func CreateConfigReloader(name string, options ...ReloaderOption) v1.Container {
if configReloader.shard != nil { if configReloader.shard != nil {
envVars = append(envVars, v1.EnvVar{ envVars = append(envVars, v1.EnvVar{
Name: "SHARD", Name: ShardEnvVar,
Value: strconv.Itoa(int(*configReloader.shard)), Value: strconv.Itoa(int(*configReloader.shard)),
}) })
} }

View File

@@ -41,6 +41,8 @@ const (
kubernetesSDRoleEndpointSlice = "endpointslice" kubernetesSDRoleEndpointSlice = "endpointslice"
kubernetesSDRolePod = "pod" kubernetesSDRolePod = "pod"
kubernetesSDRoleIngress = "ingress" kubernetesSDRoleIngress = "ingress"
defaultReplicaExternalLabelName = "prometheus_replica"
) )
var invalidLabelCharRE = regexp.MustCompile(`[^a-zA-Z0-9_]`) 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. // Do not add the external label if the resulting value is empty.
if replicaExternalLabelName != "" { if replicaExternalLabelName != "" {
m[replicaExternalLabelName] = "$(POD_NAME)" m[replicaExternalLabelName] = fmt.Sprintf("$(%s)", operator.PodNameEnvVar)
} }
for n, v := range cpf.ExternalLabels { for n, v := range cpf.ExternalLabels {
@@ -1372,7 +1374,7 @@ func generateAddressShardingRelabelingRulesWithSourceLabel(relabelings []yaml.Ma
{Key: "action", Value: "hashmod"}, {Key: "action", Value: "hashmod"},
}, yaml.MapSlice{ }, yaml.MapSlice{
{Key: "source_labels", Value: []string{"__tmp_hash"}}, {Key: "source_labels", Value: []string{"__tmp_hash"}},
{Key: "regex", Value: "$(SHARD)"}, {Key: "regex", Value: fmt.Sprintf("$(%s)", operator.ShardEnvVar)},
{Key: "action", Value: "keep"}, {Key: "action", Value: "keep"},
}) })
} }

View File

@@ -32,12 +32,11 @@ import (
) )
const ( const (
defaultReplicaExternalLabelName = "prometheus_replica" StorageDir = "/prometheus"
StorageDir = "/prometheus" ConfDir = "/etc/prometheus/config"
ConfDir = "/etc/prometheus/config" ConfOutDir = "/etc/prometheus/config_out"
ConfOutDir = "/etc/prometheus/config_out" WebConfigDir = "/etc/prometheus/web_config"
WebConfigDir = "/etc/prometheus/web_config" tlsAssetsDir = "/etc/prometheus/certs"
tlsAssetsDir = "/etc/prometheus/certs"
//TODO: RulesDir should be moved to the server package, since it is not used by the agent. //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. // It is here at the moment because promcfg uses it, and moving as is will cause import cycle error.
RulesDir = "/etc/prometheus/rules" RulesDir = "/etc/prometheus/rules"