From d960601ee02a38ba75ec29a7ba6204959628c05b Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Thu, 11 Jul 2024 10:16:21 +0000 Subject: [PATCH] postgres helm --- charts/{postgresssql => postgres}/.helmignore | 0 charts/postgres/Chart.yaml | 6 ++ charts/postgres/templates/NOTES.txt | 24 +++++++ .../templates/_helpers.tpl | 33 +++++++--- charts/postgres/templates/configmap.yaml | 8 +++ charts/postgres/templates/deployment.yaml | 37 +++++++++++ charts/postgres/templates/secrets.yaml | 8 +++ charts/postgres/templates/service.yaml | 12 ++++ .../templates/tests/test-connection.yaml | 15 +++++ charts/postgres/values.yaml | 32 ++++++++++ charts/postgresssql/Chart.yaml | 9 --- charts/postgresssql/custom-values.yaml | 2 - charts/postgresssql/templates/NOTES.txt | 1 - charts/postgresssql/templates/configmap.yaml | 12 ---- charts/postgresssql/templates/hpa.yaml | 29 --------- charts/postgresssql/templates/ingress.yaml | 62 ------------------- charts/postgresssql/templates/pvc.yaml | 12 ---- charts/postgresssql/templates/secret.yaml | 7 --- charts/postgresssql/templates/service.yaml | 11 ---- .../templates/serviceaccount.yaml | 11 ---- .../postgresssql/templates/statefullset.yaml | 46 -------------- charts/postgresssql/templates/svc.yaml | 13 ---- .../templates/tests/test-connection.yaml | 13 ---- charts/postgresssql/values.yaml | 47 -------------- 24 files changed, 166 insertions(+), 284 deletions(-) rename charts/{postgresssql => postgres}/.helmignore (100%) create mode 100644 charts/postgres/Chart.yaml create mode 100644 charts/postgres/templates/NOTES.txt rename charts/{postgresssql => postgres}/templates/_helpers.tpl (50%) create mode 100644 charts/postgres/templates/configmap.yaml create mode 100644 charts/postgres/templates/deployment.yaml create mode 100644 charts/postgres/templates/secrets.yaml create mode 100644 charts/postgres/templates/service.yaml create mode 100644 charts/postgres/templates/tests/test-connection.yaml create mode 100644 charts/postgres/values.yaml delete mode 100644 charts/postgresssql/Chart.yaml delete mode 100644 charts/postgresssql/custom-values.yaml delete mode 100644 charts/postgresssql/templates/NOTES.txt delete mode 100644 charts/postgresssql/templates/configmap.yaml delete mode 100644 charts/postgresssql/templates/hpa.yaml delete mode 100644 charts/postgresssql/templates/ingress.yaml delete mode 100644 charts/postgresssql/templates/pvc.yaml delete mode 100644 charts/postgresssql/templates/secret.yaml delete mode 100644 charts/postgresssql/templates/service.yaml delete mode 100644 charts/postgresssql/templates/serviceaccount.yaml delete mode 100644 charts/postgresssql/templates/statefullset.yaml delete mode 100644 charts/postgresssql/templates/svc.yaml delete mode 100644 charts/postgresssql/templates/tests/test-connection.yaml delete mode 100644 charts/postgresssql/values.yaml diff --git a/charts/postgresssql/.helmignore b/charts/postgres/.helmignore similarity index 100% rename from charts/postgresssql/.helmignore rename to charts/postgres/.helmignore diff --git a/charts/postgres/Chart.yaml b/charts/postgres/Chart.yaml new file mode 100644 index 00000000..20ab0e9b --- /dev/null +++ b/charts/postgres/Chart.yaml @@ -0,0 +1,6 @@ +apiVersion: v2 +name: postgresql-helm-chart +description: A Helm chart for PostgreSQL +version: 0.1.0 +appVersion: "12.6" + diff --git a/charts/postgres/templates/NOTES.txt b/charts/postgres/templates/NOTES.txt new file mode 100644 index 00000000..dbb0997e --- /dev/null +++ b/charts/postgres/templates/NOTES.txt @@ -0,0 +1,24 @@ +{{- if .Values.ingress }} + {{- if .Values.ingress.enabled }} + {{- range $host := .Values.ingress.hosts }} + {{- range .paths }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }} + {{- end }} + {{- end }} + {{- end }} +{{- else if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "postgres.fullname" . }}) + export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") + echo http://$NODE_IP:$NODE_PORT +{{- else if contains "LoadBalancer" .Values.service.type }} + NOTE: It may take a few minutes for the LoadBalancer IP to be available. + You can watch its status by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "postgres.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "postgres.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") + echo http://$SERVICE_IP:{{ .Values.service.port }} +{{- else if contains "ClusterIP" .Values.service.type }} + export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "postgres.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") + export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}") + echo "Visit http://127.0.0.1:8080 to use your application" + kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT +{{- end }} + diff --git a/charts/postgresssql/templates/_helpers.tpl b/charts/postgres/templates/_helpers.tpl similarity index 50% rename from charts/postgresssql/templates/_helpers.tpl rename to charts/postgres/templates/_helpers.tpl index e5c96a81..d0bfd036 100644 --- a/charts/postgresssql/templates/_helpers.tpl +++ b/charts/postgres/templates/_helpers.tpl @@ -1,14 +1,16 @@ {{/* Expand the name of the chart. */}} -{{- define "postgresql-helm.name" -}} +{{- define "postgres.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 "postgresql-helm.fullname" -}} +{{- define "postgres.fullname" -}} {{- if .Values.fullnameOverride }} {{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} {{- else }} @@ -24,24 +26,37 @@ Create a default fully qualified app name. {{/* Create chart name and version as used by the chart label. */}} -{{- define "postgresql-helm.chart" -}} +{{- define "postgres.chart" -}} {{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} {{- end }} {{/* Common labels */}} -{{- define "postgresql-helm.labels" -}} -app.kubernetes.io/name: {{ include "postgresql-helm.name" . }} -helm.sh/chart: {{ include "postgresql-helm.chart" . }} -app.kubernetes.io/instance: {{ .Release.Name }} +{{- define "postgres.labels" -}} +helm.sh/chart: {{ include "postgres.chart" . }} +{{ include "postgres.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} app.kubernetes.io/managed-by: {{ .Release.Service }} {{- end }} {{/* Selector labels */}} -{{- define "postgresql-helm.selectorLabels" -}} -app.kubernetes.io/name: {{ include "postgresql-helm.name" . }} +{{- define "postgres.selectorLabels" -}} +app.kubernetes.io/name: {{ include "postgres.name" . }} app.kubernetes.io/instance: {{ .Release.Name }} {{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "postgres.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "postgres.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/charts/postgres/templates/configmap.yaml b/charts/postgres/templates/configmap.yaml new file mode 100644 index 00000000..343d1214 --- /dev/null +++ b/charts/postgres/templates/configmap.yaml @@ -0,0 +1,8 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: postgresql-config +data: + POSTGRES_DB: {{ default (randAlphaNum 8) .Values.configMap.databaseName }} + POSTGRES_USER: {{ default (randAlphaNum 8) .Values.configMap.user }} + diff --git a/charts/postgres/templates/deployment.yaml b/charts/postgres/templates/deployment.yaml new file mode 100644 index 00000000..fb148819 --- /dev/null +++ b/charts/postgres/templates/deployment.yaml @@ -0,0 +1,37 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: postgresql +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + app: postgresql + template: + metadata: + labels: + app: postgresql + spec: + containers: + - name: postgresql + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + env: + - name: POSTGRES_DB + valueFrom: + configMapKeyRef: + name: postgresql-config + key: POSTGRES_DB + - name: POSTGRES_USER + valueFrom: + configMapKeyRef: + name: postgresql-config + key: POSTGRES_USER + - name: POSTGRES_PASSWORD + valueFrom: + secretKeyRef: + name: postgresql-secret + key: password + ports: + - containerPort: 5432 + diff --git a/charts/postgres/templates/secrets.yaml b/charts/postgres/templates/secrets.yaml new file mode 100644 index 00000000..2d429450 --- /dev/null +++ b/charts/postgres/templates/secrets.yaml @@ -0,0 +1,8 @@ +apiVersion: v1 +kind: Secret +metadata: + name: postgresql-secret +type: Opaque +data: + password: {{ default (randAlphaNum 16) .Values.secret.password | b64enc | quote }} + diff --git a/charts/postgres/templates/service.yaml b/charts/postgres/templates/service.yaml new file mode 100644 index 00000000..e22359b6 --- /dev/null +++ b/charts/postgres/templates/service.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Service +metadata: + name: postgresql-headless +spec: + clusterIP: None + ports: + - port: {{ .Values.service.port }} + targetPort: 5432 + selector: + app: postgresql + diff --git a/charts/postgres/templates/tests/test-connection.yaml b/charts/postgres/templates/tests/test-connection.yaml new file mode 100644 index 00000000..8289a4fd --- /dev/null +++ b/charts/postgres/templates/tests/test-connection.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: "{{ include "postgres.fullname" . }}-test-connection" + labels: + {{- include "postgres.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": test +spec: + containers: + - name: wget + image: busybox + command: ['wget'] + args: ['{{ include "postgres.fullname" . }}:{{ .Values.service.port }}'] + restartPolicy: Never diff --git a/charts/postgres/values.yaml b/charts/postgres/values.yaml new file mode 100644 index 00000000..d729e3dd --- /dev/null +++ b/charts/postgres/values.yaml @@ -0,0 +1,32 @@ +replicaCount: 1 + +image: + repository: postgres + tag: "12.6" + pullPolicy: IfNotPresent + +service: + type: ClusterIP + port: 5432 + +configMap: + databaseName: mydatabase + user: myuser + +secret: + password: changeme + +resources: + requests: + memory: "512Mi" + cpu: "500m" + limits: + memory: "10Gi" + cpu: "1" + +nodeSelector: {} + +tolerations: [] + +affinity: {} + diff --git a/charts/postgresssql/Chart.yaml b/charts/postgresssql/Chart.yaml deleted file mode 100644 index 69f387a4..00000000 --- a/charts/postgresssql/Chart.yaml +++ /dev/null @@ -1,9 +0,0 @@ -apiVersion: v2 -name: postgresql -description: A Helm chart for Kubernetes - -type: application - -version: 0.1.0 - -appVersion: "1.0.0" diff --git a/charts/postgresssql/custom-values.yaml b/charts/postgresssql/custom-values.yaml deleted file mode 100644 index f6ccc628..00000000 --- a/charts/postgresssql/custom-values.yaml +++ /dev/null @@ -1,2 +0,0 @@ -ingress: - enabled: true diff --git a/charts/postgresssql/templates/NOTES.txt b/charts/postgresssql/templates/NOTES.txt deleted file mode 100644 index 891f2d36..00000000 --- a/charts/postgresssql/templates/NOTES.txt +++ /dev/null @@ -1 +0,0 @@ -created by Khushi Malhotra diff --git a/charts/postgresssql/templates/configmap.yaml b/charts/postgresssql/templates/configmap.yaml deleted file mode 100644 index a40133ed..00000000 --- a/charts/postgresssql/templates/configmap.yaml +++ /dev/null @@ -1,12 +0,0 @@ -apiVersion: v1 -kind: ConfigMap -metadata: - name: {{ include "postgresql-helm.name" . }}-configmap -data: - postgresql.conf: |- - listen_addresses = '*' - port = 5432 - pg_hba.conf: |- - # TYPE DATABASE USER ADDRESS METHOD - host all all 0.0.0.0/0 md5 - diff --git a/charts/postgresssql/templates/hpa.yaml b/charts/postgresssql/templates/hpa.yaml deleted file mode 100644 index fb8dc5b0..00000000 --- a/charts/postgresssql/templates/hpa.yaml +++ /dev/null @@ -1,29 +0,0 @@ -{{- if .Values.autoscaling.enabled }} -apiVersion: autoscaling/v2beta1 -kind: HorizontalPodAutoscaler -metadata: - name: {{ include "postgresql-helm.name" . }}-hpa - labels: - {{- include "postgresql-helm.labels" . | nindent 4 }} -spec: - scaleTargetRef: - apiVersion: apps/v1 - kind: Deployment - name: {{ include "postgresql-helm.fullname" . }} - minReplicas: {{ .Values.autoscaling.minReplicas | default 1 }} - maxReplicas: {{ .Values.autoscaling.maxReplicas | default 10 }} - metrics: - {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} - - type: Resource - resource: - name: cpu - targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} - {{- end }} - {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }} - - type: Resource - resource: - name: memory - targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }} - {{- end }} -{{- end }} - diff --git a/charts/postgresssql/templates/ingress.yaml b/charts/postgresssql/templates/ingress.yaml deleted file mode 100644 index a9d7a4ad..00000000 --- a/charts/postgresssql/templates/ingress.yaml +++ /dev/null @@ -1,62 +0,0 @@ -{{- if .Values.ingress.enabled -}} -{{- $fullName := include "postgresql-helm.name" . -}} -{{- $svcPort := .Values.service.port -}} -{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} -{{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} -{{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} -{{- end }} -{{- end }} -{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} -apiVersion: networking.k8s.io/v1 -{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} -apiVersion: networking.k8s.io/v1beta1 -{{- else -}} -apiVersion: extensions/v1beta1 -{{- end }} -kind: Ingress -metadata: - name: {{ $fullName }}-ingress - labels: - {{- include "postgresql-helm.labels" . | nindent 4 }} - {{- with .Values.ingress.annotations }} - annotations: - {{- toYaml . | nindent 4 }} - {{- end }} -spec: - {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} - ingressClassName: {{ .Values.ingress.className }} - {{- end }} - {{- if .Values.ingress.tls }} - tls: - {{- range .Values.ingress.tls }} - - hosts: - {{- range .hosts }} - - {{ . | quote }} - {{- end }} - secretName: {{ .secretName }} - {{- end }} - {{- end }} - rules: - {{- range .Values.ingress.hosts }} - - host: {{ .host | quote }} - http: - paths: - {{- range .paths }} - - path: {{ .path }} - {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} - pathType: {{ .pathType }} - {{- end }} - backend: - {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} - service: - name: {{ $fullName }} - port: - number: {{ $svcPort }} - {{- else }} - serviceName: {{ $fullName }} - servicePort: {{ $svcPort }} - {{- end }} - {{- end }} - {{- end }} -{{- end }} - diff --git a/charts/postgresssql/templates/pvc.yaml b/charts/postgresssql/templates/pvc.yaml deleted file mode 100644 index 04bbbb92..00000000 --- a/charts/postgresssql/templates/pvc.yaml +++ /dev/null @@ -1,12 +0,0 @@ -apiVersion: v1 -kind: PersistentVolumeClaim -metadata: - name: {{ include "postgresql-helm.fullname" . }} -spec: - accessModes: - - {{ .Values.persistence.accessMode }} - resources: - requests: - storage: {{ .Values.persistence.size }} - storageClassName: {{ .Values.persistence.storageClass }} - diff --git a/charts/postgresssql/templates/secret.yaml b/charts/postgresssql/templates/secret.yaml deleted file mode 100644 index f99404f4..00000000 --- a/charts/postgresssql/templates/secret.yaml +++ /dev/null @@ -1,7 +0,0 @@ -apiVersion: v1 -kind: Secret -metadata: - name: {{ include "postgresql-helm.fullname" . }}-secret -type: Opaque -data: - postgresql-password: {{ if .Values.postgresql.generatePassword }}{{ randAlphaNum 16 | b64enc }}{{ else }}{{ .Values.postgresql.password | b64enc }}{{ end }} diff --git a/charts/postgresssql/templates/service.yaml b/charts/postgresssql/templates/service.yaml deleted file mode 100644 index 2292ca8a..00000000 --- a/charts/postgresssql/templates/service.yaml +++ /dev/null @@ -1,11 +0,0 @@ -apiVersion: v1 -kind: Service -metadata: - name: {{ include "postgresql-helm.fullname" . }} - labels: - {{- include "postgresql-helm.labels" . | nindent 4 }} -spec: - ports: - - port: 5432 - selector: - app: {{ include "postgresql-helm.name" . }} diff --git a/charts/postgresssql/templates/serviceaccount.yaml b/charts/postgresssql/templates/serviceaccount.yaml deleted file mode 100644 index 82c23ae6..00000000 --- a/charts/postgresssql/templates/serviceaccount.yaml +++ /dev/null @@ -1,11 +0,0 @@ -apiVersion: v1 -kind: ServiceAccount -metadata: - name: {{ include "postgresql-helm.fullname" . }} - labels: - {{- include "postgresql-helm.labels" . | nindent 4 }} -{{- if .Values.serviceAccount.annotations }} - annotations: - {{- toYaml .Values.serviceAccount.annotations | nindent 4 }} -{{- end }} - diff --git a/charts/postgresssql/templates/statefullset.yaml b/charts/postgresssql/templates/statefullset.yaml deleted file mode 100644 index ee3529a0..00000000 --- a/charts/postgresssql/templates/statefullset.yaml +++ /dev/null @@ -1,46 +0,0 @@ -apiVersion: apps/v1 -kind: StatefulSet -metadata: - name: {{ include "postgresql-helm.fullname" . }} - labels: - {{- include "postgresql-helm.labels" . | nindent 4 }} -spec: - serviceName: {{ include "postgresql-helm.fullname" . }} - replicas: {{ .Values.replicaCount }} - selector: - matchLabels: - app: {{ include "postgresql-helm.name" . }} - template: - metadata: - labels: - app: {{ include "postgresql-helm.name" . }} - spec: - containers: - - name: {{ .Chart.Name }} - image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" - env: - - name: POSTGRES_DB - value: {{ .Values.postgresql.database }} - - name: POSTGRES_USER - value: {{ .Values.postgresql.username }} - - name: POSTGRES_PASSWORD - valueFrom: - secretKeyRef: - name: {{ include "postgresql-helm.fullname" . }}-secret - key: postgresql-password - ports: - - containerPort: 5432 - volumeMounts: - - name: data - mountPath: /var/lib/postgresql/data - volumeClaimTemplates: - - metadata: - name: data - labels: - {{- include "postgresql-helm.labels" . | nindent 8 }} - spec: - accessModes: [ "{{ .Values.persistence.accessMode }}" ] - resources: - requests: - storage: {{ .Values.persistence.size }} - storageClassName: {{ .Values.persistence.storageClass | quote }} diff --git a/charts/postgresssql/templates/svc.yaml b/charts/postgresssql/templates/svc.yaml deleted file mode 100644 index b0900450..00000000 --- a/charts/postgresssql/templates/svc.yaml +++ /dev/null @@ -1,13 +0,0 @@ -apiVersion: v1 -kind: Service -metadata: - name: {{ .Release.Name }}-service -spec: - type: {{ .Values.service.type | default "ClusterIP" }} - selector: - app: {{ .Release.Name }} - ports: - - protocol: TCP - port: 5432 - targetPort: 5432 - diff --git a/charts/postgresssql/templates/tests/test-connection.yaml b/charts/postgresssql/templates/tests/test-connection.yaml deleted file mode 100644 index d124a80e..00000000 --- a/charts/postgresssql/templates/tests/test-connection.yaml +++ /dev/null @@ -1,13 +0,0 @@ -apiVersion: v1 -kind: Pod -metadata: - name: "{{ include "postgresql-helm.fullname" . }}-test-connection" - labels: - {{- include "postgresql-helm.labels" . | nindent 4 }} -spec: - containers: - - name: {{ .Chart.Name }}-test-connection - image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" - command: ['sh', '-c', 'until pg_isready -h {{ include "postgresql-helm.fullname" . }} -U {{ .Values.postgresql.username }} -d {{ .Values.postgresql.database }}; do echo waiting for database; sleep 2; done;'] - restartPolicy: Never - diff --git a/charts/postgresssql/values.yaml b/charts/postgresssql/values.yaml deleted file mode 100644 index b6961592..00000000 --- a/charts/postgresssql/values.yaml +++ /dev/null @@ -1,47 +0,0 @@ -service: - type: ClusterIP # or LoadBalancer, NodePort, etc. - -mode: standalone # Options: standalone, cluster - -replicaCount: 2 - -image: - repository: postgres - tag: "13" - pullPolicy: IfNotPresent - -postgresql: - database: mydatabase - username: myuser - generatePassword: true - -autoscaling: - enabled: false - minReplicas: 1 - maxReplicas: 10 - targetCPUUtilizationPercentage: 80 - -serviceAccount: - create: true - -ingress: - enabled: false - -resources: - limits: - cpu: 100m - memory: 128Mi - requests: - cpu: 100m - memory: 128Mi - -nodeSelector: {} - -tolerations: [] - -affinity: {} - -persistence: - accessMode: ReadWriteOnce - size: 10Gi - storageClass: standard