diff --git a/README.md b/README.md index 325c2aa..bc3970e 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,8 @@ This is what the output of another Pod's startup event would look like: ## Configuration The application is configurable via the environment variables: -| Environment Variable | Description | Default Value | -|:--------------------:|------------------------------------------------------------------|:-------------:| -| TZ | Timezone of the logged event's timestamp (UTC for example) | | -| LOG_LEVEL | The application's internal logging level (not related to events) | INFO | \ No newline at end of file +| Environment Variable | Description | Default Value | +|:--------------------:|-----------------------------------------------------------------------------------------------------------------------------------------------------|:-------------:| +| TZ | Timezone of the logged event's timestamp (UTC for example) | UTC | +| NAMESPACE | Namespace in which this application is running. If not set, tries to detect it automatically from the injected environment variable "POD_NAMESPACE" | event-logging | +| LOG_LEVEL | The application's internal logging level (not related to events) | INFO | \ No newline at end of file diff --git a/persistence/timestamp_persister.go b/persistence/timestamp_persister.go index c66b466..d45dd7c 100644 --- a/persistence/timestamp_persister.go +++ b/persistence/timestamp_persister.go @@ -6,11 +6,12 @@ import ( v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" + "os" "strconv" "time" ) -var namespace = "event-logging" +var namespace string var timestampConfigMapName = "timestamp" type TimestampPersister struct { @@ -22,6 +23,7 @@ type TimestampPersister struct { } func Init(client *kubernetes.Clientset, logger *logging.Logger) *TimestampPersister { + namespace = getConfiguredNamespace() configMap, err := getConfigMap(client) var currentTimestamp time.Time @@ -59,6 +61,20 @@ func Init(client *kubernetes.Clientset, logger *logging.Logger) *TimestampPersis return persister } +// getConfiguredNamespace returns the namespace in which the ConfigMap should be updated. +// It returns the value in the following priority order: +// Environment variable "POD_NAMESPACE", Environment variable "NAMESPACE" or else "event-logging" by default +func getConfiguredNamespace() string { + if podNamespace, isSet := os.LookupEnv("POD_NAMESPACE"); isSet { + return podNamespace + } + if namespace, isSet := os.LookupEnv("NAMESPACE"); isSet { + return namespace + } + + return "event-logging" +} + func (s *TimestampPersister) runScheduledUpdates() { for { select {