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 7d8a1b83..b9f202c0 100644 --- a/chart/templates/grafana-datasources-sec.yaml +++ b/chart/templates/grafana-datasources-sec.yaml @@ -30,10 +30,11 @@ stringData: {{- end -}} {{ if $tsEnabled -}} {{- $isDefault := not $promEnabled -}} -{{- $root := . -}} +{{- $host := tpl .Values.grafana.timescale.database.host $ -}} +{{- $port := .Values.grafana.timescale.database.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 diff --git a/chart/templates/grafana-db-sec.yaml b/chart/templates/grafana-db-sec.yaml index d5acbff5..2a492c44 100644 --- a/chart/templates/grafana-db-sec.yaml +++ b/chart/templates/grafana-db-sec.yaml @@ -10,10 +10,12 @@ metadata: release: {{ .Release.Name }} type: Opaque data: -{{- $root := . -}} +{{- $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 }} - 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..c154cdb9 100644 --- a/chart/templates/grafana-db-user-job.yaml +++ b/chart/templates/grafana-db-user-job.yaml @@ -12,10 +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 @@ -23,22 +23,22 @@ spec: subPath: add-users.sql env: - name: PGPORT - value: "5432" + 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 }} - command: ['psql', '-f', '/add-users.sql'] + 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 @@ -48,8 +48,8 @@ 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 -}} -{{- end -}} \ No newline at end of file +{{- 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 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 832be026..3d444b52 100644 --- a/ts-obs/cmd/pgx.go +++ b/ts-obs/cmd/pgx.go @@ -10,6 +10,7 @@ import ( ) func OpenConnectionToDB(namespace, name, user, dbname string, remote int) (*pgxpool.Pool, error) { + var pool *pgxpool.Pool var err error // Suppress output @@ -17,6 +18,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 +48,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"}) + 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 err != nil { - return nil, err - } + if len(tsdbPods) != 0 { + pf, err := KubePortForwardPod(namespace, tsdbPods[0].Name, 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