From 722b2cf003e4e57dfcec690f383ba43d27583320 Mon Sep 17 00:00:00 2001 From: Tristan Stenner Date: Fri, 30 Jun 2023 13:38:53 +0200 Subject: [PATCH] re-implement `WATCH_NAMESPACE` --- charts/ext-postgres-operator/templates/operator.yaml | 6 ++++-- cmd/main.go | 8 ++++++++ internal/postgres/database.go | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/charts/ext-postgres-operator/templates/operator.yaml b/charts/ext-postgres-operator/templates/operator.yaml index 2c46ba34..1c1a4a87 100644 --- a/charts/ext-postgres-operator/templates/operator.yaml +++ b/charts/ext-postgres-operator/templates/operator.yaml @@ -30,8 +30,6 @@ spec: securityContext: {{- toYaml .Values.securityContext | nindent 12 }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" - command: - - postgres-operator imagePullPolicy: {{ .Values.image.pullPolicy }} envFrom: - secretRef: @@ -47,6 +45,10 @@ spec: valueFrom: fieldRef: fieldPath: metadata.name + {{- if .Values.prefix }} + - name: POSTGRES_OBJECT_PREFIX + value: {{ .Values.prefix }} + {{- end }} - name: OPERATOR_NAME value: {{ include "chart.fullname" . }} {{- range $key, $value := .Values.env }} diff --git a/cmd/main.go b/cmd/main.go index 81031696..cc0ce6c2 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -27,6 +27,7 @@ import ( utilruntime "k8s.io/apimachinery/pkg/util/runtime" clientgoscheme "k8s.io/client-go/kubernetes/scheme" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/log/zap" @@ -66,6 +67,12 @@ func main() { ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) + cacheOpts := cache.Options{} + namespace, found := os.LookupEnv("WATCH_NAMESPACE") + if found { + cacheOpts.Namespaces = []string{namespace} + } + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ Scheme: scheme, MetricsBindAddress: metricsAddr, @@ -73,6 +80,7 @@ func main() { HealthProbeBindAddress: probeAddr, LeaderElection: enableLeaderElection, LeaderElectionID: "c5655ffe.db.movetokube.com", + Cache: cacheOpts, // LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily // when the Manager ends. This requires the binary to immediately end when the // Manager is stopped, otherwise, this setting is unsafe. Setting this significantly diff --git a/internal/postgres/database.go b/internal/postgres/database.go index 52830d6c..131b431d 100644 --- a/internal/postgres/database.go +++ b/internal/postgres/database.go @@ -12,7 +12,7 @@ const ( CREATE_SCHEMA = `CREATE SCHEMA IF NOT EXISTS "%s" AUTHORIZATION "%s"` CREATE_EXTENSION = `CREATE EXTENSION IF NOT EXISTS "%s"` ALTER_DB_OWNER = `ALTER DATABASE "%s" OWNER TO "%s"` - DROP_DATABASE = `DROP DATABASE "%s"` + DROP_DATABASE = `DROP DATABASE IF EXISTS "%s" WITH (FORCE)` GRANT_USAGE_SCHEMA = `GRANT USAGE ON SCHEMA "%s" TO "%s"` GRANT_ALL_TABLES = `GRANT %s ON ALL TABLES IN SCHEMA "%s" TO "%s"` DEFAULT_PRIVS_SCHEMA = `ALTER DEFAULT PRIVILEGES FOR ROLE "%s" IN SCHEMA "%s" GRANT %s ON TABLES TO "%s"`