Skip to content

Commit

Permalink
Make env var overwrite configmap setting for watching namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Dudoladov committed Feb 6, 2018
1 parent 0ef801f commit 8b7bbde
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
11 changes: 3 additions & 8 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
1 change: 1 addition & 0 deletions manifests/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions manifests/postgres-operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
16 changes: 14 additions & 2 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down

0 comments on commit 8b7bbde

Please sign in to comment.