From 3ad012d72f137e46fe05622f7f95ccad9dd84731 Mon Sep 17 00:00:00 2001 From: Akash LM Date: Thu, 8 Feb 2024 14:34:31 +0530 Subject: [PATCH] updated helm chart to support existing secret for postgresql --- charts/scsctl/.helmignore | 1 - charts/scsctl/Chart.yaml | 2 +- charts/scsctl/templates/deployment.yaml | 78 ++++++++++++--------- charts/scsctl/values.sample.yaml | 91 ------------------------- 4 files changed, 47 insertions(+), 125 deletions(-) delete mode 100644 charts/scsctl/values.sample.yaml diff --git a/charts/scsctl/.helmignore b/charts/scsctl/.helmignore index e0409c7..0e8a0eb 100644 --- a/charts/scsctl/.helmignore +++ b/charts/scsctl/.helmignore @@ -21,4 +21,3 @@ .idea/ *.tmproj .vscode/ -values.sample.yaml \ No newline at end of file diff --git a/charts/scsctl/Chart.yaml b/charts/scsctl/Chart.yaml index 14f969e..e117734 100644 --- a/charts/scsctl/Chart.yaml +++ b/charts/scsctl/Chart.yaml @@ -15,7 +15,7 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. # Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 0.1.0 +version: 0.1.1 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. Versions are not expected to diff --git a/charts/scsctl/templates/deployment.yaml b/charts/scsctl/templates/deployment.yaml index 4187d80..45b9dd9 100644 --- a/charts/scsctl/templates/deployment.yaml +++ b/charts/scsctl/templates/deployment.yaml @@ -27,6 +27,29 @@ spec: serviceAccountName: {{ include "scsctl.serviceAccountName" . }} securityContext: {{- toYaml .Values.podSecurityContext | nindent 8 }} + initContainers: + - name: create-postgres-datbase + image: "docker.io/bitnami/postgresql:16.0.0-debian-11-r13" + imagePullPolicy: Always + command: + - "/bin/sh" + - "-c" + - > + echo "Creating database {{ .Values.postgresql.database }} if it does not exist..." && + psql -h {{ .Values.postgresql.host }} -p {{ .Values.postgresql.port }} -U {{ .Values.postgresql.username }} -lqt | cut -d \| -f 1 | grep -qw {{ .Values.postgresql.database }} || psql -h {{ .Values.postgresql.host }} -p {{ .Values.postgresql.port }} -U {{ .Values.postgresql.username }} -c "CREATE DATABASE {{ .Values.postgresql.database }};" && + echo "Database {{ .Values.postgresql.database }} created or already exists. Listing all databases:" && + psql -h {{ .Values.postgresql.host }} -p {{ .Values.postgresql.port }} -U {{ .Values.postgresql.username }} -l && + sleep 5 + env: + - name: PGPASSWORD + {{- if not .Values.postgresql.existingSecret }} + value: "{{ .Values.postgresql.password }}" + {{- else }} + valueFrom: + secretKeyRef: + name: {{ .Values.postgresql.existingSecret.name }} + key: {{ .Values.postgresql.existingSecret.passwordKey }} + {{- end }} containers: - name: {{ .Chart.Name }} securityContext: @@ -37,48 +60,39 @@ spec: - name: http containerPort: {{ .Values.service.port }} protocol: TCP + args: + - server env: - - name: SCSCTL_PG_HOST - value: "{{ .Values.env.SCSCTL_PG_HOST }}" - - name: SCSCTL_PG_PORT - value: "{{ .Values.env.SCSCTL_PG_PORT }}" - - name: SCSCTL_PG_USER - value: "{{ .Values.env.SCSCTL_PG_USER }}" - - name: SCSCTL_PG_PASSWORD - value: "{{ .Values.env.SCSCTL_PG_PASSWORD }}" - - name: SCSCTL_PG_DATABASE - value: "{{ .Values.env.SCSCTL_PG_DATABASE }}" - - name: SCSCTL_ENVIRONMENT - value: "{{ .Values.env.SCSCTL_ENVIRONMENT }}" + - name: SCSCTL_PG_HOST + value: "{{ .Values.postgresql.host }}" + - name: SCSCTL_PG_PORT + value: "{{ .Values.postgresql.port }}" + - name: SCSCTL_PG_USER + value: "{{ .Values.postgresql.username }}" + - name: SCSCTL_PG_PASSWORD + {{- if not .Values.postgresql.existingSecret }} + value: "{{ .Values.postgresql.password }}" + {{- else }} + valueFrom: + secretKeyRef: + name: {{ .Values.postgresql.existingSecret.name }} + key: {{ .Values.postgresql.existingSecret.passwordKey }} + {{- end }} + - name: SCSCTL_PG_DATABASE + value: "{{ .Values.postgresql.database }}" + - name: SCSCTL_ENVIRONMENT + value: "{{ .Values.env.SCSCTL_ENVIRONMENT }}" livenessProbe: httpGet: - path: / + path: /healthz port: http readinessProbe: httpGet: - path: / + path: /ready port: http resources: {{- toYaml .Values.resources | nindent 12 }} - initContainers: - - name: create-postgres-datbase - image: "{{ .Values.initContainer.image }}" - imagePullPolicy: {{ .Values.initContainer.imagePullPolicy }} - command: ['sh', '-c', 'until pg_isready -h {{ .Values.env.SCSCTL_PG_HOST }} -p {{ .Values.env.SCSCTL_PG_PORT }}; do echo waiting for database; sleep 2; done; psql -h {{ .Values.env.SCSCTL_PG_HOST }} -p {{ .Values.env.SCSCTL_PG_PORT }} -U {{ .Values.env.SCSCTL_PG_USER }} -d {{ .Values.env.SCSCTL_PG_DATABASE }} -c "CREATE DATABASE {{ .Values.env.SCSCTL_PG_DATABASE }}";'] - env: - - name: SCSCTL_PG_HOST - value: "{{ .Values.env.SCSCTL_PG_HOST }}" - - name: SCSCTL_PG_PORT - value: "{{ .Values.env.SCSCTL_PG_PORT }}" - - name: SCSCTL_PG_USER - value: "{{ .Values.env.SCSCTL_PG_USER }}" - - name: SCSCTL_PG_PASSWORD - value: "{{ .Values.env.SCSCTL_PG_PASSWORD }}" - - name: SCSCTL_PG_DATABASE - value: "{{ .Values.env.SCSCTL_PG_DATABASE }}" - resources: - {{- toYaml .Values.initContainer.resources | nindent 12 }} {{- with .Values.nodeSelector }} nodeSelector: {{- toYaml . | nindent 8 }} diff --git a/charts/scsctl/values.sample.yaml b/charts/scsctl/values.sample.yaml deleted file mode 100644 index 8811a86..0000000 --- a/charts/scsctl/values.sample.yaml +++ /dev/null @@ -1,91 +0,0 @@ -# Default values for scsctl. -# This is a YAML-formatted file. -# Declare variables to be passed into your templates. - -replicaCount: 1 - -image: - repository: ghcr.io/jegathintelops/scsctl - pullPolicy: IfNotPresent - # Overrides the image tag whose default is the chart appVersion. - tag: "latest" - -imagePullSecrets: [] -nameOverride: "" -fullnameOverride: "" - -serviceAccount: - # Specifies whether a service account should be created - create: true - # Annotations to add to the service account - annotations: {} - # The name of the service account to use. - # If not set and create is true, a name is generated using the fullname template - name: "" - -podAnnotations: {} - -podSecurityContext: {} - # fsGroup: 2000 - -securityContext: {} - # capabilities: - # drop: - # - ALL - # readOnlyRootFilesystem: true - # runAsNonRoot: true - # runAsUser: 1000 - -service: - type: ClusterIP - port: 5000 - -ingress: - enabled: false - className: "" - annotations: {} - # kubernetes.io/ingress.class: nginx - # kubernetes.io/tls-acme: "true" - hosts: - - host: chart-example.local - paths: - - path: / - pathType: ImplementationSpecific - tls: [] - # - secretName: chart-example-tls - # hosts: - # - chart-example.local - -resources: {} - # We usually recommend not to specify default resources and to leave this as a conscious - # choice for the user. This also increases chances charts run on environments with little - # resources, such as Minikube. If you do want to specify resources, uncomment the following - # lines, adjust them as necessary, and remove the curly braces after 'resources:'. - # limits: - # cpu: 100m - # memory: 128Mi - # requests: - # cpu: 100m - # memory: 128Mi - -autoscaling: - enabled: false - minReplicas: 1 - maxReplicas: 100 - targetCPUUtilizationPercentage: 80 - # targetMemoryUtilizationPercentage: 80 - -nodeSelector: {} - -tolerations: [] - -affinity: {} - -# Configure the environment variables for the application -env: - SCSCTL_PG_HOST: "" - SCSCTL_PG_PORT: "" - SCSCTL_PG_USER: "" - SCSCTL_PG_PASSWORD: "" - SCSCTL_PG_DATABASE: "" - SCSCTL_ENVIRONMENT: ""