Skip to content

Commit

Permalink
Merge pull request #81 from irfanhakim-as/postgres-agent
Browse files Browse the repository at this point in the history
Postgres-Agent: Complete overhaul
  • Loading branch information
irfanhakim-as authored Jun 30, 2024
2 parents d7330ba + 5841b6d commit 579e1d2
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 172 deletions.
4 changes: 2 additions & 2 deletions mika/postgres-agent/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ apiVersion: v2
name: postgres-agent
description: Easily create or delete a database and user pair in a remote PostgreSQL instance.
type: application
version: 0.2.2
appVersion: "13.3"
version: 0.3.0
appVersion: "16.3.0-debian-12-r12"
keywords:
- "postgresql"
- "agent"
Expand Down
151 changes: 101 additions & 50 deletions mika/postgres-agent/README.md
Original file line number Diff line number Diff line change
@@ -1,89 +1,140 @@
# [`postgres-agent`](https://github.com/postgres/postgres)
# Postgres-Agent

Easily create or delete a database and user pair in a remote PostgreSQL instance.

## Prerequisites

> [!NOTE]
> You may refer to [Orked](https://github.com/irfanhakim-as/orked) for help with setting up a Kubernetes cluster that meets all the following prerequisites.
- Kubernetes 1.19+
- Helm 3.2.0+
- Longhorn 1.4.1+

---

## External dependencies

> [!IMPORTANT]
> The following items are required to be set up prior to installing this chart.
**This section does not apply to this chart.**

---

## Recommended configurations

> [!NOTE]
> The following configuration recommendations might not be the default settings for this chart but are **highly recommended**. Please carefully consider them before configuring your installation.
**This section does not apply to this chart.**

---

## Application configurations

> [!NOTE]
> The following configurations are expected or recommended to be set up from within the application after completing the installation.
**This section does not apply to this chart.**

---

## How to add the chart repo

1. Add the repo to your local helm client:

```sh
helm repo add mika https://irfanhakim-as.github.io/charts
```

2. Update the repo to retrieve the latest versions of the packages:

```sh
helm repo update
```

---

## How to install or upgrade a chart release

## Preflight checklist
1. Get the values file of the Postgres-Agent chart or an existing installation (release).

## How to add repo
Get the latest Postgres-Agent chart values file for a new installation:

Add the repo to your local helm client.
```sh
helm show values mika/postgres-agent > values.yaml
```

```sh
helm repo add mika https://irfanhakim-as.github.io/charts
```
Alternatively, get the values file of an existing Postgres-Agent release:

Update the repo to retrieve the latest versions of the packages.
```sh
helm get values ${releaseName} --namespace ${namespace} > values.yaml
```

```sh
helm repo update
```
Replace `${releaseName}` and `${namespace}` accordingly.

## How to install
2. Edit your Postgres-Agent values file with the intended configurations:

### Prepare PostgreSQL host
```sh
nano values.yaml
```

Install [`mika/postgres`](../postgres/). This step can be skipped if you have an existing PostgreSQL server.
Pay extra attention to the descriptions and sample values provided in the chart values file.

### Prepare chart values
3. Install a new release for Postgres-Agent or upgrade an existing Postgres-Agent release:

Copy `values.yaml` from the chart you would like to install. Refer to the [Configurations](#configurations) section for more information.
```sh
helm upgrade --install ${releaseName} mika/postgres-agent --namespace ${namespace} --create-namespace --values values.yaml --wait
```

```sh
cp mika/postgres-agent/values.yaml .
```
Replace `${releaseName}` and `${namespace}` accordingly.

Edit `values.yaml` with the appropriate values. Refer to the [Configurations](#Configurations) section for available options.
4. Verify that your Postgres-Agent release has been installed:

```sh
nano values.yaml
```
```sh
helm ls --namespace ${namespace} | grep "${releaseName}"
```

### Perform installation
Replace `${namespace}` and `${releaseName}` accordingly. This should return the release information if the release has been installed.

Install the desired chart. Replace `$release_name` and `$namespace` accordingly.
---

```sh
helm install $release_name mika/postgres-agent --namespace $namespace --create-namespace --values values.yaml --wait
```
## How to uninstall a chart release

Verify that your chart has been installed. Replace `$namespace` and `$release_name` accordingly.
> [!CAUTION]
> Uninstalling a release will irreversibly delete all the resources associated with the release, including any persistent data.

```sh
helm ls --namespace $namespace | grep "$release_name"
```
1. Uninstall the desired release:

## How to upgrade
```sh
helm uninstall ${releaseName} --namespace ${namespace} --wait
```

After making any necessary changes to the `values.yaml` file, upgrade the desired chart. Replace `$release_name` and `$namespace` accordingly.
Replace `${releaseName}` and `${namespace}` accordingly.

```sh
helm upgrade $release_name mika/postgres-agent --namespace $namespace --values values.yaml --wait
```
2. Verify that the release has been uninstalled:

## How to uninstall
```sh
helm ls --namespace ${namespace} | grep "${releaseName}"
```

Uninstall the desired chart. Replace `$release_name` and `$namespace` accordingly.
Replace `${namespace}` and `${releaseName}` accordingly. This should return nothing if the release has been uninstalled.

```sh
helm uninstall $release_name --namespace $namespace --wait
```
---

## Configurations
## Chart configurations

| Key | Type | Default | Description |
|-----|------|---------|-------------|
| image.postgres.pullPolicy | string | `""` | The policy that determines when Kubernetes should pull the PostgreSQL container image. Default: `"IfNotPresent"`. |
| image.postgres.registry | string | `""` | The registry where the PostgreSQL container image is hosted. Default: `"docker.io"`. |
| image.postgres.repository | string | `""` | The name of the repository that contains the PostgreSQL container image used. Default: `"postgres"`. |
| image.postgres.repository | string | `""` | The name of the repository that contains the PostgreSQL container image used. Default: `"bitnami/postgresql"`. |
| image.postgres.tag | string | `""` | The tag that specifies the version of the PostgreSQL container image used. Default: `Chart appVersion`. |
| imagePullSecrets | list | `[]` | Credentials used to securely authenticate and authorise the pulling of container images from private registries. |
| postgres.databases | list | `[]` | Database configurations array. Elements: `.name`, `.user`, `.password`, `.create`, `.drop`, `.custom`, `.custom_command`. |
| postgres.host | string | `""` | The hostname or IP address of the PostgreSQL database server. |
| postgres.mode.create | bool | `true` | Specifies whether to create a database and user in a remote PostgreSQL instance. |
| postgres.mode.drop | bool | `false` | Specifies whether to delete a database and user in a remote PostgreSQL instance. |
| postgres.name | string | `""` | The name of the intended PostgreSQL database. |
| postgres.password | string | `""` | The password associated with the intended PostgreSQL database user. |
| postgres.root.database | string | `""` | The name of the database of the PostgreSQL database server root user. Default: `$user`. |
| postgres.root.password | string | `""` | The password associated with the PostgreSQL database server root user. |
| postgres.root.user | string | `""` | The username or user account for accessing the PostgreSQL database server as root. Default: `"root"`. |
| postgres.user | string | `""` | The username or user account for accessing the intended PostgreSQL database. |
| postgres.root.user | string | `""` | The username or user account for accessing the PostgreSQL database server as root. Default: `"postgres"`. |
| resources.postgres | object | `{}` | PostgreSQL container resources. |
4 changes: 2 additions & 2 deletions mika/postgres-agent/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
postgres-agent job has been deployed for {{ .Release.Name | toString }}.
Postgres-Agent job has been deployed for {{ .Release.Name | toString }}.

See the values.yaml file of this chart for more configuration options.

Please refer to the official documentation for more information on how to use postgres-agent.
Please refer to the official documentation for more information on how to use Postgres-Agent.
84 changes: 0 additions & 84 deletions mika/postgres-agent/templates/deployment.yaml

This file was deleted.

63 changes: 63 additions & 0 deletions mika/postgres-agent/templates/job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{{- $host := .Values.postgres.host | default "postgres" | toString }}
{{- $registry := .Values.image.postgres.registry | default "docker.io" | toString }}
{{- $repository := .Values.image.postgres.repository | default "bitnami/postgresql" | toString }}
{{- $tag := .Values.image.postgres.tag | default .Chart.AppVersion | toString }}
{{- $pullPolicy := .Values.image.postgres.pullPolicy | default "IfNotPresent" | toString | quote }}
{{- $rootDB := .Values.postgres.root.database | default "$(ROOT_USER)" | toString }}
{{- $databases := .Values.postgres.databases }}
apiVersion: batch/v1
kind: Job
metadata:
name: {{ printf "%s-%s-%s" (index (splitList "." $host) 0) .Release.Name "postgres-agent" }}
labels:
{{- include "postgres-agent.labels" . | nindent 4 }}
spec:
ttlSecondsAfterFinished: 120
backoffLimit: 0
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "postgres-agent.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
containers:
- name: postgres-agent
image: {{ printf "%s/%s:%s" $registry $repository $tag | quote }}
imagePullPolicy: {{ $pullPolicy }}
resources:
{{- toYaml .Values.resources.postgres | nindent 12 }}
command: ["/bin/bash"]
args:
- -c
- >-
psql -h $(DB_HOST) -U $(ROOT_USER) -d {{ $rootDB }} <<EOF
{{- range $databases }}
{{- if .create }}
CREATE USER {{ .user }} WITH ENCRYPTED PASSWORD '{{ .password }}';
CREATE DATABASE {{ .name }};
GRANT ALL ON DATABASE {{ .name }} TO {{ .user }};
ALTER DATABASE {{ .name }} OWNER TO {{ .user }};
GRANT USAGE, CREATE ON SCHEMA PUBLIC TO {{ .user }};
{{- else if .drop }}
DROP DATABASE IF EXISTS {{ .name }};
REVOKE ALL ON SCHEMA PUBLIC FROM {{ .user }};
DROP USER IF EXISTS {{ .user }};
{{- end }}
{{- if and .custom .custom_command }}
\connect {{ .name }};
{{- .custom_command | replace "$.name" .name | replace "$.user" .user | replace "$.password" .password | nindent 16 }}
\connect {{ $rootDB }};
{{- end }}
{{- end }}
EOF
envFrom:
- secretRef:
name: {{ .Release.Name }}-postgres-agent-secret
restartPolicy: Never
10 changes: 2 additions & 8 deletions mika/postgres-agent/templates/secret.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{{- $db_host := .Values.postgres.host | toString | b64enc }}
{{- $db_name := .Values.postgres.name | toString | b64enc }}
{{- $db_user := .Values.postgres.user | toString | b64enc }}
{{- $db_password := .Values.postgres.password | toString | b64enc }}
{{- $root_user := .Values.postgres.root.user | default "root" | toString | b64enc }}
{{- $root_user := .Values.postgres.root.user | default "postgres" | toString | b64enc }}
{{- $root_password := .Values.postgres.root.password | toString | b64enc }}
apiVersion: v1
kind: Secret
Expand All @@ -13,8 +10,5 @@ metadata:
type: Opaque
data:
DB_HOST: {{ $db_host }}
DB_NAME: {{ $db_name }}
DB_USER: {{ $db_user }}
DB_PASS: {{ $db_password }}
ROOT_USER: {{ $root_user }}
ROOT_PASS: {{ $root_password }}
PGPASSWORD: {{ $root_password }}
Loading

0 comments on commit 579e1d2

Please sign in to comment.