Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions chart/templates/grafana-dashboards-conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
{{- end -}}
5 changes: 3 additions & 2 deletions chart/templates/grafana-datasources-sec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions chart/templates/grafana-db-sec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
3 changes: 2 additions & 1 deletion chart/templates/grafana-db-user-conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -58,4 +59,4 @@ data:
echo Checking if ${PGHOST} is up
done
{{- end -}}
{{- end -}}
{{- end -}}
18 changes: 9 additions & 9 deletions chart/templates/grafana-db-user-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@ 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
mountPath: /add-users.sql
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
Expand All @@ -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 -}}
{{- end -}}
3 changes: 3 additions & 0 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -111,6 +112,7 @@ grafana:
database:
enabled: true
host: *dbHost
port: 5432
user: grafanadb
pass: grafanadb
dbName: *metricDB
Expand All @@ -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

8 changes: 8 additions & 0 deletions ts-obs/cmd/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
46 changes: 36 additions & 10 deletions ts-obs/cmd/pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,32 @@ import (
)

func OpenConnectionToDB(namespace, name, user, dbname string, remote int) (*pgxpool.Pool, error) {
var pool *pgxpool.Pool
var err error

// Suppress output
stdout := os.Stdout
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
Expand All @@ -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
Expand Down