From 6bdda324840feb7787c1f5c8fd10f4677a2c3493 Mon Sep 17 00:00:00 2001 From: Andrew Chen Date: Tue, 14 Jul 2020 12:09:01 -0700 Subject: [PATCH 1/5] External DB compatbility --- ts-obs/cmd/pgx.go | 53 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/ts-obs/cmd/pgx.go b/ts-obs/cmd/pgx.go index 832be026..0ba8efba 100644 --- a/ts-obs/cmd/pgx.go +++ b/ts-obs/cmd/pgx.go @@ -5,11 +5,13 @@ import ( "errors" "os" "strconv" + "strings" "github.com/jackc/pgx/v4/pgxpool" ) func OpenConnectionToDB(namespace, name, user, dbname string, remote int) (*pgxpool.Pool, error) { + var pool *pgxpool.Pool var err error // Suppress output @@ -17,6 +19,24 @@ func OpenConnectionToDB(namespace, name, user, dbname string, remote int) (*pgxp os.Stdout = nil defer func() { os.Stdout = stdout }() + tspromPods, err := KubeGetPods(namespace, map[string]string{"app": name + "-timescale-prometheus"}) + if err != nil { + return nil, err + } + + envs := tspromPods[0].Spec.Containers[0].Env + + var port, host, sslmode string + for _, env := range envs { + if env.Name == "TS_PROM_DB_PORT" { + port = env.Value + } else if env.Name == "TS_PROM_DB_HOST" { + host = env.Value + } else if env.Name == "TS_PROM_DB_SSL_MODE" { + sslmode = env.Value + } + } + secret, err := KubeGetSecret(namespace, name+"-timescaledb-passwords") if err != nil { return nil, err @@ -29,22 +49,29 @@ func OpenConnectionToDB(namespace, name, user, dbname string, remote int) (*pgxp return nil, errors.New("user not found") } - podName, err := KubeGetPodName(namespace, map[string]string{"release": name, "role": "master"}) - if err != nil { - return nil, err - } + if strings.HasSuffix(host, ".svc.cluster.local") { + podName, err := KubeGetPodName(namespace, map[string]string{"release": name, "role": "master"}) + if err != nil { + return nil, err + } - pf, err := KubePortForwardPod(namespace, podName, 0, remote) - if err != nil { - return nil, err - } + pf, err := KubePortForwardPod(namespace, podName, 0, remote) + if err != nil { + return nil, err + } - ports, err := pf.GetPorts() - local := int(ports[0].Local) + ports, err := pf.GetPorts() + local := int(ports[0].Local) - pool, err := pgxpool.Connect(context.Background(), "postgres://"+user+":"+pass+"@localhost:"+strconv.Itoa(local)+"/"+dbname) - if err != nil { - return nil, err + pool, err = pgxpool.Connect(context.Background(), "postgres://"+user+":"+pass+"@localhost:"+strconv.Itoa(local)+"/"+dbname) + if err != nil { + return nil, err + } + } else { + pool, err = pgxpool.Connect(context.Background(), "postgres://"+user+":"+pass+"@"+host+":"+port+"/tsdb?sslmode="+sslmode) + if err != nil { + return nil, err + } } return pool, nil From 88c76b9673391674566686dfa34ed9da27ed96ff Mon Sep 17 00:00:00 2001 From: Andrew Chen Date: Wed, 15 Jul 2020 13:12:58 -0700 Subject: [PATCH 2/5] Fix templates for external db --- chart/templates/grafana-db-sec.yaml | 5 ++++- chart/templates/grafana-db-user-conf.yaml | 3 ++- chart/templates/grafana-db-user-job.yaml | 7 ++++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/chart/templates/grafana-db-sec.yaml b/chart/templates/grafana-db-sec.yaml index d5acbff5..75ddf637 100644 --- a/chart/templates/grafana-db-sec.yaml +++ b/chart/templates/grafana-db-sec.yaml @@ -11,9 +11,12 @@ metadata: type: Opaque data: {{- $root := . -}} +{{- $host := tpl .Values.grafana.timescale.database.host $root -}} +{{- $port := index .Values "timescale-prometheus" "connection" "port" | int -}} +{{- $fullhost := printf "%s:%d" $host $port -}} {{- with .Values.grafana.timescale.database }} GF_DATABASE_TYPE: {{ "postgres" | b64enc }} - GF_DATABASE_HOST: {{ tpl .host $root | b64enc }} + GF_DATABASE_HOST: {{ $fullhost | b64enc }} GF_DATABASE_NAME: {{ .dbName | b64enc }} GF_DATABASE_USER: {{ .user | b64enc }} GF_DATABASE_PASSWORD: {{ .pass | b64enc }} diff --git a/chart/templates/grafana-db-user-conf.yaml b/chart/templates/grafana-db-user-conf.yaml index 8fe63dec..43e65be3 100644 --- a/chart/templates/grafana-db-user-conf.yaml +++ b/chart/templates/grafana-db-user-conf.yaml @@ -21,6 +21,7 @@ data: RAISE NOTICE 'role {{ $timescaleAsDB.user }} already exists, skipping create'; END $$; + GRANT {{ $timescaleAsDB.user }} TO {{ .Values.grafana.timescale.adminUser }}; CREATE SCHEMA IF NOT EXISTS {{ $timescaleAsDB.schema }} AUTHORIZATION {{ $timescaleAsDB.user }}; ALTER ROLE {{ $timescaleAsDB.user }} SET search_path = {{ $timescaleAsDB.schema }}; {{- end }} @@ -58,4 +59,4 @@ data: echo Checking if ${PGHOST} is up done {{- end -}} -{{- end -}} \ No newline at end of file +{{- end -}} diff --git a/chart/templates/grafana-db-user-job.yaml b/chart/templates/grafana-db-user-job.yaml index 3a25e8b7..fa4acb07 100644 --- a/chart/templates/grafana-db-user-job.yaml +++ b/chart/templates/grafana-db-user-job.yaml @@ -13,6 +13,7 @@ spec: template: spec: {{- $root := . -}} +{{- $vals := .Values -}} {{- with .Values.grafana.timescale }} containers: - name: {{ $root.Chart.Name }}-grafana-db @@ -23,7 +24,7 @@ spec: subPath: add-users.sql env: - name: PGPORT - value: "5432" + value: {{ index $vals "timescale-prometheus" "connection" "port" | quote }} - name: PGUSER value: {{ .adminUser }} - name: PGPASSWORD @@ -33,7 +34,7 @@ spec: key: {{ .adminUser }} - name: PGHOST value: {{ tpl .database.host $root }} - command: ['psql', '-f', '/add-users.sql'] + command: ['psql', '-d', {{ .database.dbName }}, '-f', '/add-users.sql'] restartPolicy: OnFailure volumes: - name: sql-volume @@ -52,4 +53,4 @@ spec: command: ['sh', '/wait-for-ts.sh'] {{- end -}} {{- end -}} -{{- end -}} \ No newline at end of file +{{- end -}} From 89d7e5c3ea958883936f380f9fee34b5472222fb Mon Sep 17 00:00:00 2001 From: Andrew Chen Date: Thu, 16 Jul 2020 08:59:12 -0700 Subject: [PATCH 3/5] Add port to Grafana datasource url --- chart/templates/grafana-datasources-sec.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/chart/templates/grafana-datasources-sec.yaml b/chart/templates/grafana-datasources-sec.yaml index 7d8a1b83..2f0014dc 100644 --- a/chart/templates/grafana-datasources-sec.yaml +++ b/chart/templates/grafana-datasources-sec.yaml @@ -31,9 +31,11 @@ stringData: {{ if $tsEnabled -}} {{- $isDefault := not $promEnabled -}} {{- $root := . -}} +{{- $host := tpl .Values.grafana.timescale.database.host $root -}} +{{- $port := index .Values "timescale-prometheus" "connection" "port" | int -}} {{ with .Values.grafana.timescale.datasource }} - name: TimescaleDB - url: {{ tpl .host $root }} + url: {{ printf "%s:%d" $host $port }} type: postgres isDefault: {{ $isDefault }} access: proxy From 8b307b2d502053a0d8860f2966298e663212cda0 Mon Sep 17 00:00:00 2001 From: Andrew Chen Date: Fri, 17 Jul 2020 09:28:13 -0700 Subject: [PATCH 4/5] Add key for grafana database port --- chart/templates/grafana-dashboards-conf.yaml | 5 ++--- chart/templates/grafana-datasources-sec.yaml | 5 ++--- chart/templates/grafana-db-sec.yaml | 5 ++--- chart/templates/grafana-db-user-job.yaml | 13 ++++++------- chart/values.yaml | 3 +++ 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/chart/templates/grafana-dashboards-conf.yaml b/chart/templates/grafana-dashboards-conf.yaml index 57e5367b..aa5f73b5 100644 --- a/chart/templates/grafana-dashboards-conf.yaml +++ b/chart/templates/grafana-dashboards-conf.yaml @@ -11,11 +11,10 @@ metadata: chart: {{ template "timescale-observability.chart" . }} release: {{ .Release.Name }} data: -{{- $root := . -}} {{ range $dash := .Values.grafana.sidecar.dashboards.files }} {{ base $dash -}}: |- -{{ $root.Files.Get $dash | indent 4 }} +{{ $.Files.Get $dash | indent 4 }} {{- end }} {{- end -}} {{- end -}} -{{- end -}} \ No newline at end of file +{{- end -}} diff --git a/chart/templates/grafana-datasources-sec.yaml b/chart/templates/grafana-datasources-sec.yaml index 2f0014dc..b9f202c0 100644 --- a/chart/templates/grafana-datasources-sec.yaml +++ b/chart/templates/grafana-datasources-sec.yaml @@ -30,9 +30,8 @@ stringData: {{- end -}} {{ if $tsEnabled -}} {{- $isDefault := not $promEnabled -}} -{{- $root := . -}} -{{- $host := tpl .Values.grafana.timescale.database.host $root -}} -{{- $port := index .Values "timescale-prometheus" "connection" "port" | int -}} +{{- $host := tpl .Values.grafana.timescale.database.host $ -}} +{{- $port := .Values.grafana.timescale.database.port | int -}} {{ with .Values.grafana.timescale.datasource }} - name: TimescaleDB url: {{ printf "%s:%d" $host $port }} diff --git a/chart/templates/grafana-db-sec.yaml b/chart/templates/grafana-db-sec.yaml index 75ddf637..2a492c44 100644 --- a/chart/templates/grafana-db-sec.yaml +++ b/chart/templates/grafana-db-sec.yaml @@ -10,9 +10,8 @@ metadata: release: {{ .Release.Name }} type: Opaque data: -{{- $root := . -}} -{{- $host := tpl .Values.grafana.timescale.database.host $root -}} -{{- $port := index .Values "timescale-prometheus" "connection" "port" | int -}} +{{- $host := tpl .Values.grafana.timescale.database.host $ -}} +{{- $port := .Values.grafana.timescale.database.port | int -}} {{- $fullhost := printf "%s:%d" $host $port -}} {{- with .Values.grafana.timescale.database }} GF_DATABASE_TYPE: {{ "postgres" | b64enc }} diff --git a/chart/templates/grafana-db-user-job.yaml b/chart/templates/grafana-db-user-job.yaml index fa4acb07..c154cdb9 100644 --- a/chart/templates/grafana-db-user-job.yaml +++ b/chart/templates/grafana-db-user-job.yaml @@ -12,11 +12,10 @@ metadata: spec: template: spec: -{{- $root := . -}} {{- $vals := .Values -}} {{- with .Values.grafana.timescale }} containers: - - name: {{ $root.Chart.Name }}-grafana-db + - name: {{ $.Chart.Name }}-grafana-db image: postgres:12-alpine volumeMounts: - name: sql-volume @@ -24,22 +23,22 @@ spec: subPath: add-users.sql env: - name: PGPORT - value: {{ index $vals "timescale-prometheus" "connection" "port" | quote }} + value: {{ .database.port | quote }} - name: PGUSER value: {{ .adminUser }} - name: PGPASSWORD valueFrom: secretKeyRef: - name: {{ tpl .adminPassSecret $root }} + name: {{ tpl .adminPassSecret $ }} key: {{ .adminUser }} - name: PGHOST - value: {{ tpl .database.host $root }} + value: {{ tpl .database.host $ }} command: ['psql', '-d', {{ .database.dbName }}, '-f', '/add-users.sql'] restartPolicy: OnFailure volumes: - name: sql-volume configMap: - name: {{ $root.Release.Name }}-grafana-db + name: {{ $.Release.Name }}-grafana-db initContainers: - name: init-db image: busybox:1.28 @@ -49,7 +48,7 @@ spec: subPath: wait-for-ts.sh env: - name: PGHOST - value: {{ tpl .database.host $root }} + value: {{ tpl .database.host $ }} command: ['sh', '/wait-for-ts.sh'] {{- end -}} {{- end -}} diff --git a/chart/values.yaml b/chart/values.yaml index a55aef25..506ff3f6 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -36,6 +36,7 @@ timescale-prometheus: # Host name (templated) of the database instance, default # to service created in timescaledb-single nameTemplate: &dbHost "{{ .Release.Name }}.{{ .Release.Namespace }}.svc.cluster.local" + port: 5432 # configuration options for the service exposed by timescale-prometheus service: @@ -111,6 +112,7 @@ grafana: database: enabled: true host: *dbHost + port: 5432 user: grafanadb pass: grafanadb dbName: *metricDB @@ -125,6 +127,7 @@ grafana: # By default the url/host is set to the db instance deployed # with this chart host: *dbHost + port: 5432 adminUser: postgres adminPassSecret: *dbPassSecret From 23a49bfa6e6181b6dc64b7641e70bbdaeb5c89c9 Mon Sep 17 00:00:00 2001 From: Andrew Chen Date: Fri, 17 Jul 2020 11:05:33 -0700 Subject: [PATCH 5/5] Check if pod exists instead of checking host --- ts-obs/cmd/kubectl.go | 8 ++++++++ ts-obs/cmd/pgx.go | 13 ++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/ts-obs/cmd/kubectl.go b/ts-obs/cmd/kubectl.go index bdddd8cd..b2b6a673 100644 --- a/ts-obs/cmd/kubectl.go +++ b/ts-obs/cmd/kubectl.go @@ -56,6 +56,10 @@ func KubeGetPodName(namespace string, labelmap map[string]string) (string, error return "", err } + if len(pods.Items) == 0 { + return "", nil + } + return pods.Items[0].Name, nil } @@ -74,6 +78,10 @@ func KubeGetServiceName(namespace string, labelmap map[string]string) (string, e return "", err } + if len(services.Items) == 0 { + return "", nil + } + return services.Items[0].Name, nil } diff --git a/ts-obs/cmd/pgx.go b/ts-obs/cmd/pgx.go index 0ba8efba..3d444b52 100644 --- a/ts-obs/cmd/pgx.go +++ b/ts-obs/cmd/pgx.go @@ -5,7 +5,6 @@ import ( "errors" "os" "strconv" - "strings" "github.com/jackc/pgx/v4/pgxpool" ) @@ -49,13 +48,13 @@ func OpenConnectionToDB(namespace, name, user, dbname string, remote int) (*pgxp return nil, errors.New("user not found") } - if strings.HasSuffix(host, ".svc.cluster.local") { - podName, err := KubeGetPodName(namespace, map[string]string{"release": name, "role": "master"}) - if err != nil { - return nil, err - } + tsdbPods, err := KubeGetPods(namespace, map[string]string{"release": name, "role": "master"}) + if err != nil { + return nil, err + } - pf, err := KubePortForwardPod(namespace, podName, 0, remote) + if len(tsdbPods) != 0 { + pf, err := KubePortForwardPod(namespace, tsdbPods[0].Name, 0, remote) if err != nil { return nil, err }