Make env var overwrite configmap setting for watching namespaces

This commit is contained in:
Sergey Dudoladov 2018-02-06 16:12:47 +01:00
parent 0ef801f4e0
commit 8b7bbde06e
4 changed files with 23 additions and 14 deletions

View File

@ -27,14 +27,9 @@ func init() {
flag.BoolVar(&config.NoTeamsAPI, "noteamsapi", false, "Disable all access to the teams API")
flag.Parse()
config.Namespace = os.Getenv("WATCH_NAMESPACE")
if config.Namespace == "" {
config.Namespace = "default"
}
configMap := os.Getenv("CONFIG_MAP_NAME")
if configMap != "" {
err := config.ConfigMapName.Decode(configMap)
configMapRawName := os.Getenv("CONFIG_MAP_NAME")
if configMapRawName != "" {
err := config.ConfigMapName.Decode(configMapRawName)
if err != nil {
log.Fatalf("incorrect config map name")
}

View File

@ -4,6 +4,7 @@ metadata:
name: postgres-operator
data:
# assumes the ns exists before the operator starts
# the env var with the same name may overwrite it in the operator pod
# watched_namespace: development
service_account_name: operator
cluster_labels: application:spilo

View File

@ -15,9 +15,10 @@ spec:
image: registry.opensource.zalan.do/acid/postgres-operator:c17aabb
imagePullPolicy: IfNotPresent
env:
- name: WATCH_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
# uncomment to overwrite a similar setting from operator configmap
# - name: WATCHED_NAMESPACE
# valueFrom:
# fieldRef:
# fieldPath: metadata.namespace
- name: CONFIG_MAP_NAME
value: "postgres-operator"

View File

@ -97,9 +97,21 @@ func (c *Controller) initOperatorConfig() {
c.logger.Infoln("no ConfigMap specified. Loading default values")
}
if configMapData["watched_namespace"] == "" { // Namespace in ConfigMap has priority over env var
configMapData["watched_namespace"] = c.config.Namespace
// env var takes priority over the same param from the operator ConfigMap
watchedNamespace := os.Getenv("WATCHED_NAMESPACE")
if watchedNamespace != "" {
c.logger.Infof("Watch the %q namespace specified in the env variable WATCHED_NAMESPACE\n", watchedNamespace)
configMapData["watched_namespace"] = watchedNamespace
}
if configMapData["watched_namespace"] == "" {
c.logger.Infoln("No namespace to watch specified. Fall back to watching the 'default' namespace.")
configMapData["watched_namespace"] = v1.NamespaceDefault
}
// display the actual namespace in case someone checks the value manually
os.Setenv("WATCHED_NAMESPACE", configMapData["watched_namespace"])
if c.config.NoDatabaseAccess {
configMapData["enable_database_access"] = "false"
}