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

chore: refactor postgresql addon and add scripts ut #984

Merged
merged 15 commits into from
Sep 3, 2024
17 changes: 16 additions & 1 deletion addons-cluster/postgresql/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ Define postgresql ComponentSpec with ComponentDefinition.
clusterDefinitionRef: postgresql
topology: {{ .Values.mode }}
componentSpecs:
- name: postgresql
- name: {{ include "postgresql-cluster.component-name" . }}
labels:
{{- include "postgresql-cluster.patroni-scope-label" . | indent 8 }}
Copy link
Collaborator

Choose a reason for hiding this comment

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

how to specify the pg version when install the chart?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

how to specify the pg version when install the chart?

This is not within the scope of this PR. The version will be specified through serviceVersion in the future, but the management of serviceVersion requires further design.

{{- include "postgresql-cluster.replicaCount" . | indent 6 }}
enabledLogs:
- running
Expand Down Expand Up @@ -50,3 +52,16 @@ Define postgresql ComponentSpec with ComponentDefinition.
{{- end }}
{{- end -}}

{{/*
Define postgresql componentName
*/}}
{{- define "postgresql-cluster.component-name" -}}
postgresql
{{- end }}

{{/*
Define patroni scope label which postgresql cluster depends on, the named pattern is `apps.kubeblocks.postgres.patroni/scope: <clusterName>-<componentName>`
*/}}
{{- define "postgresql-cluster.patroni-scope-label" }}
apps.kubeblocks.postgres.patroni/scope: {{ include "kblib.clusterName" . }}-{{ include "postgresql-cluster.component-name" . }}
{{- end -}}
1 change: 0 additions & 1 deletion addons-cluster/postgresql/templates/cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ metadata:
labels: {{ include "kblib.clusterLabels" . | nindent 4 }}
spec:
terminationPolicy: {{ .Values.extra.terminationPolicy }}
{{- include "kblib.affinity" . | indent 2 }}
{{- include "postgresql-cluster.componentSpec" . }}

1 change: 0 additions & 1 deletion addons-cluster/postgresql/templates/rbac.yaml

This file was deleted.

7 changes: 7 additions & 0 deletions addons/postgresql/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ version: 1.0.0-alpha.0
# appVersion should be consistent with the highest PostgreSQL (with Patroni HA) kernel version.
appVersion: "15.7.0"

# Add a dependency to the kubeblocks definition library chart
dependencies:
- name: kblib
version: 0.1.0
repository: file://../kblib
alias: extra

home: https://kubeblocks.io/
icon: https://kubeblocks.io/img/logo.png

Expand Down
3 changes: 1 addition & 2 deletions addons/postgresql/config/pgbouncer-ini.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ ignore_startup_parameters = extra_float_digits
{{- end }}
max_client_conn = {{ $max_client_conn }}
admin_users = postgres
;;; [database]
;;; config default database in pgbouncer_setup.sh
;;; [database]
87 changes: 87 additions & 0 deletions addons/postgresql/scripts-ut-spec/pgbouncer_setup_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# shellcheck shell=bash
# shellcheck disable=SC2034

# validate_shell_type_and_version defined in shellspec/spec_helper.sh used to validate the expected shell type and version this script needs to run.
if ! validate_shell_type_and_version "bash" 4 &>/dev/null; then
echo "pgbouncer_setup_spec.sh skip all cases because dependency bash version 4 or higher is not installed."
exit 0
fi

source ./utils.sh

# The unit test needs to rely on the common library functions defined in kblib.
# Therefore, we first dynamically generate the required common library files from the kblib library chart.
common_library_file="./common.sh"
generate_common_library $common_library_file

Describe "PgBouncer Configuration and Startup Script Tests"

Include ../scripts/pgbouncer-setup.sh
Include $common_library_file

init() {
pgbouncer_conf_dir="./conf"
pgbouncer_log_dir="./logs"
pgbouncer_tmp_dir="./tmp"
pgbouncer_conf_file="./conf/pgbouncer.ini"
pgbouncer_user_list_file="./conf/userlist.txt"
pgbouncer_template_conf_file="./pgbouncer.ini"
touch $pgbouncer_template_conf_file
echo "[pgbouncer]
listen_addr = *
listen_port = 6432
unix_socket_dir = /tmp/
unix_socket_mode = 0777
auth_file = /opt/bitnami/pgbouncer/conf/userlist.txt
auth_user = postgres
auth_query = SELECT usename, passwd FROM pg_shadow WHERE usename=$1
pidfile =/opt/bitnami/pgbouncer/tmp/pgbouncer.pid
logfile =/opt/bitnami/pgbouncer/logs/pgbouncer.log
auth_type = md5
pool_mode = session
ignore_startup_parameters = extra_float_digits
admin_users = postgres
;;; [database]" > $pgbouncer_template_conf_file
}
BeforeAll "init"

cleanup() {
rm -rf ./conf ./logs ./tmp
rm -f $common_library_file
rm -f $pgbouncer_template_conf_file
}
AfterAll 'cleanup'

Describe "build_pgbouncer_conf()"
setup() {
POSTGRESQL_USERNAME="testuser"
POSTGRESQL_PASSWORD="testpassword"
CURRENT_POD_IP="127.0.0.1"
}
Before 'setup'

un_setup() {
unset POSTGRESQL_USERNAME
unset POSTGRESQL_PASSWORD
unset CURRENT_POD_IP
}
After 'un_setup'

It "builds the PgBouncer configuration files"
When call build_pgbouncer_conf
The path "$pgbouncer_conf_dir" should be directory
The path "$pgbouncer_log_dir" should be directory
The path "$pgbouncer_tmp_dir" should be directory
The contents of file "$pgbouncer_user_list_file" should include "\"testuser\" \"testpassword\""
The contents of file "$pgbouncer_conf_file" should include "listen_addr = *"
The contents of file "$pgbouncer_conf_file" should include "listen_port = 6432"
The contents of file "$pgbouncer_conf_file" should include "admin_users = postgres"
The contents of file "$pgbouncer_conf_file" should include "[databases]"
The contents of file "$pgbouncer_conf_file" should include "postgres=host=127.0.0.1 port=5432 dbname=postgres"
The contents of file "$pgbouncer_conf_file" should include "*=host=127.0.0.1 port=5432"
# ignore useradd commands
The status should be failure
The stderr should be present
End
End
End
44 changes: 44 additions & 0 deletions addons/postgresql/scripts-ut-spec/postgres_pre_setup_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# shellcheck shell=bash
# shellcheck disable=SC2034

# validate_shell_type_and_version defined in shellspec/spec_helper.sh used to validate the expected shell type and version this script needs to run.
if ! validate_shell_type_and_version "bash" 4 &>/dev/null; then
echo "postgres_pre_setup_spec.sh skip all cases because dependency bash version 4 or higher is not installed."
exit 0
fi

Describe "PostgreSQL Configuration Script Tests"

Include ../scripts/postgres-pre-setup.sh

init() {
postgres_template_conf_file="./postgresql.conf"
postgres_conf_dir="./pgdata/"
postgres_conf_file="./pgdata/postgresql.conf"
touch $postgres_template_conf_file
echo "listen_addresses = '*'
port = '5432'
archive_command = '/bin/true'
archive_mode = 'on'
auto_explain.log_analyze = 'False'
auto_explain.log_buffers = 'False'" > $postgres_template_conf_file
}
BeforeAll "init"

cleanup() {
rm -rf $postgres_template_conf_file $postgres_conf_dir $postgres_conf_file
}
AfterAll 'cleanup'

Describe "build_real_postgres_conf()"
It "builds the PostgreSQL configuration file"
When call build_real_postgres_conf
The status should be success
The path "$postgres_conf_dir" should be directory
The path "$postgres_conf_file" should be file
The contents of file "$postgres_conf_file" should include "listen_addresses = '*'"
The contents of file "$postgres_conf_file" should include "port = '5432'"
The contents of file "$postgres_conf_file" should include "archive_command = '/bin/true'"
End
End
End
117 changes: 117 additions & 0 deletions addons/postgresql/scripts-ut-spec/postgres_setup_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# shellcheck shell=bash
# shellcheck disable=SC2034

# validate_shell_type_and_version defined in shellspec/spec_helper.sh used to validate the expected shell type and version this script needs to run.
if ! validate_shell_type_and_version "bash" 4 &>/dev/null; then
echo "postgres_setup_spec.sh skip all cases because dependency bash version 4 or higher is not installed."
exit 0
fi

source ./utils.sh

# The unit test needs to rely on the common library functions defined in kblib.
# Therefore, we first dynamically generate the required common library files from the kblib library chart.
common_library_file="./common.sh"
generate_common_library $common_library_file

Describe "PostgreSQL Initialization Script Tests"

Include ../scripts/postgres-setup.sh
Include $common_library_file

cleanup() {
rm -f $common_library_file
rm -f ./tmp_patroni.yaml
}
AfterAll 'cleanup'

Describe "init_etcd_dcs_config_if_needed()"
Context "when PATRONI_DCS_ETCD_SERVICE_ENDPOINT is set"
setup() {
PATRONI_DCS_ETCD_SERVICE_ENDPOINT="http://etcd-cluster:2379"
}
Before 'setup'

un_setup() {
unset PATRONI_DCS_ETCD_SERVICE_ENDPOINT
unset ETCDCTL_API
unset DCS_ENABLE_KUBERNETES_API
unset ETCD3_HOSTS
unset ETCD_HOSTS
}
After 'un_setup'

It "sets the etcd configuration"
When call init_etcd_dcs_config_if_needed
The variable ETCDCTL_API should equal "2"
The variable DCS_ENABLE_KUBERNETES_API should equal ""
The variable ETCD_HOSTS should equal "http://etcd-cluster:2379"
The variable ETCD3_HOSTS should be undefined
The output should include "PATRONI_DCS_ETCD_SERVICE_ENDPOINT is set. Use etcd as DCS backend and unset DCS_ENABLE_KUBERNETES_API"
End
End

Context "when PATRONI_DCS_ETCD_VERSION is set to 3"
setup() {
PATRONI_DCS_ETCD_VERSION="3"
PATRONI_DCS_ETCD_SERVICE_ENDPOINT="http://etcd-cluster:2380"
}
Before 'setup'

un_setup() {
unset PATRONI_DCS_ETCD_VERSION
unset PATRONI_DCS_ETCD_SERVICE_ENDPOINT
unset ETCDCTL_API
unset DCS_ENABLE_KUBERNETES_API
unset ETCD3_HOSTS
unset ETCD_HOSTS
}
After 'un_setup'

It "sets the etcd configuration for version 3"
When call init_etcd_dcs_config_if_needed
The variable ETCDCTL_API should equal "3"
The variable DCS_ENABLE_KUBERNETES_API should equal ""
The variable ETCD3_HOSTS should equal "http://etcd-cluster:2380"
The variable ETCD_HOSTS should be undefined
The output should include "PATRONI_DCS_ETCD_SERVICE_ENDPOINT is set. Use etcd as DCS backend and unset DCS_ENABLE_KUBERNETES_API"
End
End
End

Describe "regenerate_spilo_configuration_and_start_postgres()"
setup() {
tmp_patroni_yaml="./tmp_patroni.yaml"
touch $tmp_patroni_yaml
}
Before 'setup'

un_setup() {
unset RESTORE_DATA_DIR
unset SPILO_CONFIGURATION
rm -f $tmp_patroni_yaml
}
After 'un_setup'

It "regenerates the Spilo configuration and starts PostgreSQL"
# mock python3 /kb-scripts/generate_patroni_yaml.py tmp_patroni.yaml
python3() {
echo "bootstrap:
initdb:
- auth-host: md5
- auth-local: trust" > "$tmp_patroni_yaml"
}
exec() {
:
}
When call regenerate_spilo_configuration_and_start_postgres
The status should be success
The file "tmp_patroni.yaml" should be exist
The contents of file "tmp_patroni.yaml" should include "bootstrap:"
The contents of file "tmp_patroni.yaml" should include "auth-host: md5"
The variable SPILO_CONFIGURATION should not be undefined
The variable SPILO_CONFIGURATION should include "bootstrap:"
The variable SPILO_CONFIGURATION should include "auth-host: md5"
End
End
End
Loading
Loading