Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres db helm charts #42

Merged
merged 6 commits into from
Jul 29, 2024
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
File renamed without changes.
5 changes: 5 additions & 0 deletions charts/postgres/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v2
name: postgres
description: A Helm chart to deploy a PostgreSQL
type: application
version: 0.1.0
File renamed without changes.
26 changes: 26 additions & 0 deletions charts/postgres/templates/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: postgres-init
labels:
{{- include "chart.labels" . | nindent 4 }}
data:
init.sql: |
-- Create the telemetry user
SELECT 'CREATE USER telemetry'
WHERE NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = 'telemetry')\gexec

-- Create staging database and grant telemetry user access
SELECT 'CREATE DATABASE staging WITH TEMPLATE template0'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'staging')\gexec
GRANT ALL PRIVILEGES ON DATABASE staging TO telemetry;

-- Create operational database and grant telemetry user access
SELECT 'CREATE DATABASE operational WITH TEMPLATE template0'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname= 'operational')\gexec
GRANT ALL PRIVILEGES ON DATABASE operational TO telemetry;

-- Create telemetry database and grant telemetry user access
SELECT 'CREATE DATABASE telemetry WITH TEMPLATE template0'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname= 'telemetry')\gexec
GRANT ALL PRIVILEGES ON DATABASE telemetry TO telemetry;
57 changes: 57 additions & 0 deletions charts/postgres/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "chart.fullname" . }}
labels:
{{- include "chart.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
selector:
matchLabels:
{{- include "chart.selectorLabels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "chart.labels" . | nindent 8 }}
{{- with .Values.podLabels }}
{{- toYaml . | nindent 8 }}
{{- end }}
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- containerPort: {{ .Values.service.port }}
env:
- name: POSTGRES_USER
valueFrom:
secretKeyRef:
name: postgres-config
key: POSTGRES_USER
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: postgres-config
key: POSTGRES_PASSWORD
- name: POSTGRES_DB
valueFrom:
secretKeyRef:
name: postgres-config
key: POSTGRES_DB
{{- with .Values.volumeMounts }}
volumeMounts:
{{- toYaml . | nindent 12 }}
{{- end }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- with .Values.volumes }}
volumes:
{{- toYaml . | nindent 8 }}
{{- end }}
37 changes: 37 additions & 0 deletions charts/postgres/templates/jobs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: batch/v1
kind: Job
metadata:
name: postgres-init
namespace: {{ .Release.Namespace }}
spec:
template:
spec:
containers:
- name: init-db
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
command:
- sh
- -c
- |
until pg_isready -h postgres -U $POSTGRES_USER; do sleep 1; done
psql -h postgres -U $POSTGRES_USER -f /scripts/init.sql
env:
- name: POSTGRES_USER
valueFrom:
secretKeyRef:
name: postgres-config
key: POSTGRES_USER
- name: PGPASSWORD
valueFrom:
secretKeyRef:
name: postgres-config
key: POSTGRES_PASSWORD
volumeMounts:
- name: init-sql
mountPath: /scripts/init.sql
subPath: init.sql
restartPolicy: OnFailure
volumes:
- name: init-sql
configMap:
name: postgres-init
13 changes: 13 additions & 0 deletions charts/postgres/templates/pvc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: postgres-pv-claim
labels:
{{- include "chart.labels" . | nindent 4 }}
spec:
storageClassName: manual
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
12 changes: 12 additions & 0 deletions charts/postgres/templates/secrets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Secret
metadata:
name: postgres-config
labels:
{{- include "chart.labels" . | nindent 4 }}
namespace: {{ .Release.Namespace }}
type: Opaque
data:
POSTGRES_DB: {{ .Values.postgres.db | b64enc }}
POSTGRES_USER: {{ .Values.postgres.user | b64enc }}
POSTGRES_PASSWORD: {{ .Values.postgres.password | b64enc }}
20 changes: 20 additions & 0 deletions charts/postgres/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: v1
kind: Service
metadata:
{{- if .Values.service.annotations }}
annotations:
{{ toYaml .Values.service.annotations | indent 4 }}
{{- end }}
name: {{ include "chart.fullname" . }}
labels:
{{- include "chart.labels" . | nindent 4 }}
spec:
{{- if .Values.service.type }}
type: {{ .Values.service.type }}
{{- end }}
ports:
- port: {{ .Values.service.port }}
targetPort: {{ .Values.service.port }}
protocol: TCP
selector:
{{- include "chart.selectorLabels" . | nindent 4 }}
14 changes: 14 additions & 0 deletions charts/postgres/templates/storage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
kind: PersistentVolume
apiVersion: v1
metadata:
name: postgres-pv-volume
labels:
{{- include "chart.labels" . | nindent 4 }}
spec:
storageClassName: manual
capacity:
storage: 5Gi
accessModes:
- ReadWriteMany
hostPath:
path: "/mnt/data"
54 changes: 54 additions & 0 deletions charts/postgres/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
replicaCount: 1

# A valid image tag must be provided for the desired postgres version
image:
repository: postgres
pullPolicy: IfNotPresent
tag: "16"
gbuenodevsuse marked this conversation as resolved.
Show resolved Hide resolved

podAnnotations: {}
podLabels: {}

service:
port: 5432
type: ClusterIP

resources:
limits:
cpu: 500m
memory: 256Mi
requests:
cpu: 500m
memory: 256Mi

volumes:
- name: postgredb
persistentVolumeClaim:
claimName: postgres-pv-claim

volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgredb

autoscaling:
enabled: false
minReplicas: 1
maxReplicas: 3
targetCPUUtilizationPercentage: 80

# TODO: this must be passed in as envs within CI/CD
postgres:
db: operational
user: postgres
password: telemetry

# OpenPlatform
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially instead of commenting this out, could we make it conditionally generated based upon a setting that we can override that defaults to disabled for now?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note also that we will potentially have all 3 DB envs served by this one postgres instance, i.e staging (caching), operational and telemetry

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good suggestion. I'll have it addressed in a follow-up PR, together with k3s local dev environment setup.

# postgres:
# persistence:
# enabled: true
# storageClass: ebs
# accessMode: ReadWriteOnce
# size: 10Mi
# volumeMounts:
# - name: postgres-pvc
# mountPath: /var/lib/postgresql/telemetry-server-data
23 changes: 23 additions & 0 deletions charts/telemetry-server/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
File renamed without changes.
File renamed without changes.
62 changes: 62 additions & 0 deletions charts/telemetry-server/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "chart.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "chart.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "chart.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "chart.labels" -}}
helm.sh/chart: {{ include "chart.chart" . }}
{{ include "chart.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "chart.selectorLabels" -}}
app.kubernetes.io/name: {{ include "chart.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "chart.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "chart.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
File renamed without changes.
2 changes: 1 addition & 1 deletion chart/values.yaml → charts/telemetry-server/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ config:
driver: sqlite3
params: /tmp/telemetry/server/staging.db
logging:
level: debug
level: info
location: stderr
style: text
# This is for testing purposes only
Expand Down