From 87d47180365d4f7910a4ecd203e35645d8ad87b4 Mon Sep 17 00:00:00 2001 From: Y-Rookie Date: Tue, 27 Aug 2024 15:34:58 +0800 Subject: [PATCH 01/12] refactor pg addon --- .../postgresql/templates/_helpers.tpl | 17 +- .../postgresql/templates/cluster.yaml | 1 - addons-cluster/postgresql/templates/rbac.yaml | 1 - addons/postgresql/Chart.yaml | 7 + addons/postgresql/scripts/etcd-clean.sh | 2 +- addons/postgresql/scripts/init_container.sh | 41 ++++- addons/postgresql/scripts/pgbouncer_setup.sh | 73 ++++++-- addons/postgresql/scripts/setup.sh | 103 ++++++------ addons/postgresql/scripts/switchover.sh | 92 +++++++++++ .../templates/componentdefinition-12.yaml | 154 ++++++++++------- .../templates/componentdefinition-14.yaml | 156 +++++++++++------- .../templates/componentdefinition-15.yaml | 148 +++++++++++------ addons/postgresql/templates/configmap-14.yaml | 2 +- addons/postgresql/templates/scripts.yaml | 11 +- addons/postgresql/values.yaml | 9 +- addons/redis/templates/scripts.yaml | 2 +- 16 files changed, 563 insertions(+), 256 deletions(-) delete mode 100644 addons-cluster/postgresql/templates/rbac.yaml create mode 100644 addons/postgresql/scripts/switchover.sh diff --git a/addons-cluster/postgresql/templates/_helpers.tpl b/addons-cluster/postgresql/templates/_helpers.tpl index d285d561d..8022e37d8 100644 --- a/addons-cluster/postgresql/templates/_helpers.tpl +++ b/addons-cluster/postgresql/templates/_helpers.tpl @@ -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 }} {{- include "postgresql-cluster.replicaCount" . | indent 6 }} enabledLogs: - running @@ -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: -` +*/}} +{{- define "postgresql-cluster.patroni-scope-label" }} +apps.kubeblocks.postgres.patroni/scope: {{ include "kblib.clusterName" . }}-{{ include "postgresql-cluster.component-name" . }} +{{- end -}} \ No newline at end of file diff --git a/addons-cluster/postgresql/templates/cluster.yaml b/addons-cluster/postgresql/templates/cluster.yaml index db8b3c506..8a80d0cad 100644 --- a/addons-cluster/postgresql/templates/cluster.yaml +++ b/addons-cluster/postgresql/templates/cluster.yaml @@ -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" . }} diff --git a/addons-cluster/postgresql/templates/rbac.yaml b/addons-cluster/postgresql/templates/rbac.yaml deleted file mode 100644 index 08875e8bf..000000000 --- a/addons-cluster/postgresql/templates/rbac.yaml +++ /dev/null @@ -1 +0,0 @@ -{{- include "kblib.rbac" . }} \ No newline at end of file diff --git a/addons/postgresql/Chart.yaml b/addons/postgresql/Chart.yaml index 1b8e346f7..24352c215 100644 --- a/addons/postgresql/Chart.yaml +++ b/addons/postgresql/Chart.yaml @@ -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 diff --git a/addons/postgresql/scripts/etcd-clean.sh b/addons/postgresql/scripts/etcd-clean.sh index ed996fbdb..64f02a0bb 100644 --- a/addons/postgresql/scripts/etcd-clean.sh +++ b/addons/postgresql/scripts/etcd-clean.sh @@ -28,7 +28,7 @@ else fi sleep 10 -export ETCDCTL_API=${ETCD_API:-'2'} +export ETCDCTL_API=${PATRONI_DCS_ETCD_VERSION:-'2'} endpoints=${ETCD_SERVER:-'127.0.0.1:2379'} diff --git a/addons/postgresql/scripts/init_container.sh b/addons/postgresql/scripts/init_container.sh index 5f4a3232f..449cc9710 100755 --- a/addons/postgresql/scripts/init_container.sh +++ b/addons/postgresql/scripts/init_container.sh @@ -1,7 +1,36 @@ #!/bin/bash -set -o errexit -set -e -mkdir -p /home/postgres/pgdata/conf -chmod +777 -R /home/postgres/pgdata/conf -cp /home/postgres/conf/postgresql.conf /home/postgres/pgdata/conf -chmod +777 /home/postgres/pgdata/conf/postgresql.conf + +# This is magic for shellspec ut framework. "test" is a `test [expression]` well known as a shell command. +# Normally test without [expression] returns false. It means that __() { :; } +# function is defined if this script runs directly. +# +# shellspec overrides the test command and returns true *once*. It means that +# __() function defined internally by shellspec is called. +# +# In other words. If not in test mode, __ is just a comment. If test mode, __ +# is a interception point. +# you should set ut_mode="true" when you want to run the script in shellspec file. +# shellcheck disable=SC2034 +ut_mode="false" +test || __() { + # when running in non-unit test mode, set the options "set -ex". + set -ex; +} + +build_real_postgres_conf() { + # /home/postgres/conf is the mounted path which postgres config template defined in the cmpd.spec.scripts + mkdir -p /home/postgres/pgdata/conf + chmod +777 -R /home/postgres/pgdata/conf + cp /home/postgres/conf/postgresql.conf /home/postgres/pgdata/conf + chmod +777 /home/postgres/pgdata/conf/postgresql.conf +} + +# This is magic for shellspec ut framework. +# Sometime, functions are defined in a single shell script. +# You will want to test it. but you do not want to run the script. +# When included from shellspec, __SOURCED__ variable defined and script +# end here. The script path is assigned to the __SOURCED__ variable. +${__SOURCED__:+false} : || return 0 + +# main +build_real_postgres_conf diff --git a/addons/postgresql/scripts/pgbouncer_setup.sh b/addons/postgresql/scripts/pgbouncer_setup.sh index 78ba59216..08c727702 100755 --- a/addons/postgresql/scripts/pgbouncer_setup.sh +++ b/addons/postgresql/scripts/pgbouncer_setup.sh @@ -1,15 +1,60 @@ #!/bin/bash -set -o errexit -set -e -mkdir -p /opt/bitnami/pgbouncer/conf/ /opt/bitnami/pgbouncer/logs/ /opt/bitnami/pgbouncer/tmp/ -cp /home/pgbouncer/conf/pgbouncer.ini /opt/bitnami/pgbouncer/conf/ -echo "\"$POSTGRESQL_USERNAME\" \"$POSTGRESQL_PASSWORD\"" > /opt/bitnami/pgbouncer/conf/userlist.txt -# shellcheck disable=SC2129 -echo -e "\\n[databases]" >> /opt/bitnami/pgbouncer/conf/pgbouncer.ini -echo "postgres=host=$KB_POD_IP port=5432 dbname=postgres" >> /opt/bitnami/pgbouncer/conf/pgbouncer.ini -echo "*=host=$KB_POD_IP port=5432" >> /opt/bitnami/pgbouncer/conf/pgbouncer.ini -chmod +777 /opt/bitnami/pgbouncer/conf/pgbouncer.ini -chmod +777 /opt/bitnami/pgbouncer/conf/userlist.txt -useradd pgbouncer -chown -R pgbouncer:pgbouncer /opt/bitnami/pgbouncer/conf/ /opt/bitnami/pgbouncer/logs/ /opt/bitnami/pgbouncer/tmp/ -/opt/bitnami/scripts/pgbouncer/run.sh + +# This is magic for shellspec ut framework. "test" is a `test [expression]` well known as a shell command. +# Normally test without [expression] returns false. It means that __() { :; } +# function is defined if this script runs directly. +# +# shellspec overrides the test command and returns true *once*. It means that +# __() function defined internally by shellspec is called. +# +# In other words. If not in test mode, __ is just a comment. If test mode, __ +# is a interception point. +# you should set ut_mode="true" when you want to run the script in shellspec file. +# shellcheck disable=SC2034 +ut_mode="false" +test || __() { + # when running in non-unit test mode, set the options "set -ex". + set -ex; +} + +load_common_library() { + # the common.sh scripts is mounted to the same path which is defined in the cmpd.spec.scripts + common_library_file="/kb-scripts/common.sh" + # shellcheck disable=SC1090 + source "${common_library_file}" +} + +build_pgbouncer_conf() { + if is_empty "$POSTGRESQL_USERNAME" || is_empty "$POSTGRESQL_PASSWORD" || is_empty "$CURRENT_POD_IP"; then + echo "POSTGRESQL_USERNAME, POSTGRESQL_PASSWORD or CURRENT_POD_IP is not set. Exiting..." + exit 1 + fi + + mkdir -p /opt/bitnami/pgbouncer/conf/ /opt/bitnami/pgbouncer/logs/ /opt/bitnami/pgbouncer/tmp/ + cp /home/pgbouncer/conf/pgbouncer.ini /opt/bitnami/pgbouncer/conf/ + echo "\"$POSTGRESQL_USERNAME\" \"$POSTGRESQL_PASSWORD\"" > /opt/bitnami/pgbouncer/conf/userlist.txt + # shellcheck disable=SC2129 + echo -e "\\n[databases]" >> /opt/bitnami/pgbouncer/conf/pgbouncer.ini + echo "postgres=host=$CURRENT_POD_IP port=5432 dbname=postgres" >> /opt/bitnami/pgbouncer/conf/pgbouncer.ini + echo "*=host=$CURRENT_POD_IP port=5432" >> /opt/bitnami/pgbouncer/conf/pgbouncer.ini + chmod +777 /opt/bitnami/pgbouncer/conf/pgbouncer.ini + chmod +777 /opt/bitnami/pgbouncer/conf/userlist.txt + useradd pgbouncer + chown -R pgbouncer:pgbouncer /opt/bitnami/pgbouncer/conf/ /opt/bitnami/pgbouncer/logs/ /opt/bitnami/pgbouncer/tmp/ +} + +start_pgbouncer() { + /opt/bitnami/scripts/pgbouncer/run.sh +} + +# This is magic for shellspec ut framework. +# Sometime, functions are defined in a single shell script. +# You will want to test it. but you do not want to run the script. +# When included from shellspec, __SOURCED__ variable defined and script +# end here. The script path is assigned to the __SOURCED__ variable. +${__SOURCED__:+false} : || return 0 + +# main +load_common_library +build_pgbouncer_conf +start_pgbouncer diff --git a/addons/postgresql/scripts/setup.sh b/addons/postgresql/scripts/setup.sh index ceb57ece0..0dfd5883b 100755 --- a/addons/postgresql/scripts/setup.sh +++ b/addons/postgresql/scripts/setup.sh @@ -1,60 +1,61 @@ #!/bin/bash -set -o errexit -set -e -export ETCDCTL_API=${ETCD_API:-'2'} +# This is magic for shellspec ut framework. "test" is a `test [expression]` well known as a shell command. +# Normally test without [expression] returns false. It means that __() { :; } +# function is defined if this script runs directly. +# +# shellspec overrides the test command and returns true *once*. It means that +# __() function defined internally by shellspec is called. +# +# In other words. If not in test mode, __ is just a comment. If test mode, __ +# is a interception point. +# you should set ut_mode="true" when you want to run the script in shellspec file. +# shellcheck disable=SC2034 +ut_mode="false" +test || __() { + # when running in non-unit test mode, set the options "set -ex". + set -ex; +} -if [ -n "$ETCD_SERVER" ]; then - echo "ETCD_SERVER is set. Use etcd" - export DCS_ENABLE_KUBERNETES_API="" - if [ "$ETCDCTL_API" = "3" ]; then - export ETCD3_HOSTS=$ETCD_SERVER - else - export ETCD_HOSTS=$ETCD_SERVER - fi -fi +load_common_library() { + # the common.sh scripts is mounted to the same path which is defined in the cmpd.spec.scripts + common_library_file="/kb-scripts/common.sh" + # shellcheck disable=SC1090 + source "${common_library_file}" +} -# usage: retry -# e.g. retry pg_isready -U postgres -h $primary_fqdn -p 5432 -function retry { - local max_attempts=10 - local attempt=1 - until "$@" || [ $attempt -eq $max_attempts ]; do - echo "Command '$*' failed. Attempt $attempt of $max_attempts. Retrying in 5 seconds..." - attempt=$((attempt + 1)) - sleep 5 - done - if [ $attempt -eq $max_attempts ]; then - echo "Command '$*' failed after $max_attempts attempts. Exiting..." - exit 1 +init_etcd_dcs_config_if_needed() { + if ! is_empty "$PATRONI_DCS_ETCD_SERVICE_ENDPOINT"; then + echo "PATRONI_DCS_ETCD_SERVICE_ENDPOINT is set. Use etcd as DCS backend and unset DCS_ENABLE_KUBERNETES_API" + export ETCDCTL_API=${PATRONI_DCS_ETCD_VERSION:-'2'} + export DCS_ENABLE_KUBERNETES_API="" + if [ "$ETCDCTL_API" = "3" ]; then + export ETCD3_HOSTS=$PATRONI_DCS_ETCD_SERVICE_ENDPOINT + else + export ETCD_HOSTS=$PATRONI_DCS_ETCD_SERVICE_ENDPOINT + fi fi } -if [ -f /kb-podinfo/primary-pod ]; then - # Waiting for primary pod information from the DownwardAPI annotation to be available, with a maximum of 5 attempts - attempt=1 - max_attempts=10 - while [ $attempt -le $max_attempts ] && [ -z "$(cat /kb-podinfo/primary-pod)" ]; do - sleep 3 - attempt=$((attempt + 1)) - done - primary=$(cat /kb-podinfo/primary-pod) - echo "DownwardAPI get primary=$primary" >> /home/postgres/pgdata/.kb_set_up.log - echo "KB_POD_NAME=$KB_POD_NAME" >> /home/postgres/pgdata/.kb_set_up.log -else - echo "DownwardAPI get /kb-podinfo/primary-pod is empty" >> /home/postgres/pgdata/.kb_set_up.log -fi +regenerate_spilo_configuration_and_start_postgres() { + if [ -f "${RESTORE_DATA_DIR}"/kb_restore.signal ]; then + chown -R postgres "${RESTORE_DATA_DIR}" + fi + python3 /kb-scripts/generate_patroni_yaml.py tmp_patroni.yaml + # SPILO_CONFIGURATION is defined by spilo image + SPILO_CONFIGURATION=$(cat tmp_patroni.yaml) + export SPILO_CONFIGURATION + exec /launch.sh init +} -if [ ! -z "$primary" ] && [ "$primary" != "$KB_POD_NAME" ]; then - primary_fqdn="$primary.$KB_CLUSTER_NAME-$KB_COMP_NAME-headless.$KB_NAMESPACE.svc" - echo "primary_fqdn=$primary_fqdn" >> /home/postgres/pgdata/.kb_set_up.log - # waiting for the primary to be ready, if the wait time exceeds the maximum number of retries, then the script will fail and exit. - retry pg_isready -U "postgres" -h $primary_fqdn -p 5432 -fi +# This is magic for shellspec ut framework. +# Sometime, functions are defined in a single shell script. +# You will want to test it. but you do not want to run the script. +# When included from shellspec, __SOURCED__ variable defined and script +# end here. The script path is assigned to the __SOURCED__ variable. +${__SOURCED__:+false} : || return 0 -if [ -f ${RESTORE_DATA_DIR}/kb_restore.signal ]; then - chown -R postgres ${RESTORE_DATA_DIR} -fi -python3 /kb-scripts/generate_patroni_yaml.py tmp_patroni.yaml -export SPILO_CONFIGURATION=$(cat tmp_patroni.yaml) -exec /launch.sh init +# main +load_common_library +init_etcd_dcs_config_if_needed +regenerate_spilo_configuration_and_start_postgres diff --git a/addons/postgresql/scripts/switchover.sh b/addons/postgresql/scripts/switchover.sh new file mode 100644 index 000000000..f718be26a --- /dev/null +++ b/addons/postgresql/scripts/switchover.sh @@ -0,0 +1,92 @@ +#!/bin/bash + +# This is magic for shellspec ut framework. "test" is a `test [expression]` well known as a shell command. +# Normally test without [expression] returns false. It means that __() { :; } +# function is defined if this script runs directly. +# +# shellspec overrides the test command and returns true *once*. It means that +# __() function defined internally by shellspec is called. +# +# In other words. If not in test mode, __ is just a comment. If test mode, __ +# is a interception point. +# you should set ut_mode="true" when you want to run the script in shellspec file. +# shellcheck disable=SC2034 +ut_mode="false" +test || __() { + # when running in non-unit test mode, set the options "set -ex". + set -ex; +} + +load_common_library() { + # the common.sh scripts is mounted to the same path which is defined in the cmpd.spec.scripts + common_library_file="/kb-scripts/common.sh" + # shellcheck disable=SC1090 + source "${common_library_file}" +} + +switchoverWithCandidate() { + local current_pod_fqdn=$1 + local current_primary_pod_fqdn=$2 + local candidate_pod_fqdn=$3 + # shellcheck disable=SC2016 + curl -s http://$(current_pod_fqdn):8008/switchover -XPOST -d '{"leader":"$(current_primary_pod_fqdn)","candidate":"$(candidate_pod_fqdn)"}' +} + +switchoverWithoutCandidate() { + local current_pod_fqdn=$1 + local current_primary_pod_fqdn=$2 + # shellcheck disable=SC2016 + curl -s http://$(current_pod_fqdn):8008/switchover -XPOST -d '{"leader":"$(current_primary_pod_fqdn)"}' +} + +switchover() { + # CURRENT_POD_NAME defined in the switchover action env and POSTGRES_PRIMARY_POD_NAME defined in the cmpd.spec.vars + if is_empty "$CURRENT_POD_NAME" || is_empty "$POSTGRES_PRIMARY_POD_NAME"; then + echo "CURRENT_POD_NAME or POSTGRES_PRIMARY_POD_NAME is not set. Exiting..." + exit 1 + fi + + # shellcheck disable=SC2207 + primary_pod_name_list=($(split "$POSTGRES_PRIMARY_POD_NAME" ",")) + # if primary_pod_name_list length is not 1, it means the primary pod is not unique. + if [ "${#primary_pod_name_list[@]}" -ne 1 ]; then + echo "Error: POSTGRES_PRIMARY_POD_NAME should be a unique pod name. Exiting." + exit 1 + fi + + # POSTGRES_POD_NAME_LIST and POSTGRES_POD_FQDN_LIST defined in the cmpd.spec.vars + if is_empty "$POSTGRES_POD_NAME_LIST" || is_empty "$POSTGRES_POD_FQDN_LIST" ; then + echo "POSTGRES_POD_NAME_LIST or POSTGRES_POD_FQDN_LIST is not set. Exiting..." + exit 1 + fi + + current_pod_fqdn=$(get_target_pod_fqdn_from_pod_fqdn_vars "$POSTGRES_POD_FQDN_LIST" "$CURRENT_POD_NAME") + if is_empty "$current_pod_fqdn"; then + echo "Error: Failed to get current pod: $CURRENT_POD_NAME fqdn from postgres pod fqdn list: $POSTGRES_POD_FQDN_LIST. Exiting." + exit 1 + fi + + current_primary_pod_fqdn=$(get_target_pod_fqdn_from_pod_fqdn_vars "$POSTGRES_POD_FQDN_LIST" "$CURRENT_PRIMARY_POD_NAME") + if is_empty "$current_primary_pod_fqdn"; then + echo "Error: Failed to get current primary pod fqdn: $CURRENT_PRIMARY_POD_NAME from postgres pod fqdn list: $POSTGRES_POD_FQDN_LIST. Exiting." + exit 1 + fi + + # KB_SWITCHOVER_CANDIDATE_FQDN is built-in env in the switchover action injected by the KubeBlocks controller + if ! is_empty "$KB_SWITCHOVER_CANDIDATE_FQDN"; then + switchoverWithCandidate "$current_pod_fqdn" "$current_primary_pod_fqdn" "$KB_SWITCHOVER_CANDIDATE_FQDN" + else + switchoverWithoutCandidate "$current_pod_fqdn" "$current_primary_pod_fqdn" + fi +} + +# This is magic for shellspec ut framework. +# Sometime, functions are defined in a single shell script. +# You will want to test it. but you do not want to run the script. +# When included from shellspec, __SOURCED__ variable defined and script +# end here. The script path is assigned to the __SOURCED__ variable. +${__SOURCED__:+false} : || return 0 + +# main +load_common_library +switchover \ No newline at end of file diff --git a/addons/postgresql/templates/componentdefinition-12.yaml b/addons/postgresql/templates/componentdefinition-12.yaml index 94309130b..b18ac9b1f 100644 --- a/addons/postgresql/templates/componentdefinition-12.yaml +++ b/addons/postgresql/templates/componentdefinition-12.yaml @@ -14,6 +14,7 @@ spec: serviceRefDeclarationSpecs: - serviceKind: etcd serviceVersion: "^*" + optional: true services: - name: postgresql serviceName: postgresql @@ -37,8 +38,6 @@ spec: - name: secondary serviceable: false writable: false - labels: - apps.kubeblocks.postgres.patroni/scope: "$(KB_CLUSTER_NAME)-$(KB_COMP_NAME)-patroni$(KB_CLUSTER_UID_POSTFIX_8)" logConfigs: {{- range $name,$pattern := .Values.logConfigs }} - name: {{ $name }} @@ -55,7 +54,7 @@ spec: - postgresql.conf namespace: {{ .Release.Namespace }} volumeName: postgresql-config - defaultMode: 0444 + defaultMode: 0777 reRenderResourceTypes: - vscale - name: pgbouncer-configuration @@ -64,7 +63,7 @@ spec: - pgbouncer.ini namespace: {{ .Release.Namespace }} volumeName: pgbouncer-config - defaultMode: 0444 + defaultMode: 0777 - name: postgresql-custom-metrics templateRef: postgresql12-custom-metrics namespace: {{ .Release.Namespace }} @@ -77,13 +76,30 @@ spec: volumeName: scripts defaultMode: 0555 vars: - - name: ETCD_API - value: "3" - - name: ETCD_SERVER + - name: CLUSTER_NAMESPACE + valueFrom: + clusterVarRef: + namespace: Required + - name: CLUSTER_NAME + valueFrom: + clusterVarRef: + clusterName: Required + - name: POSTGRES_COMPONENT_NAME + valueFrom: + componentVarRef: + compDef: {{ include "postgresql.compDefPostgresql12" . }} + optional: false + componentName: Required + ## the etcd version if postgres patroni use etcd as DCS + - name: PATRONI_DCS_ETCD_VERSION + value: "3" + ## the etcd service reference endpoint if postgres patroni use etcd as DCS + - name: PATRONI_DCS_ETCD_SERVICE_ENDPOINT valueFrom: serviceRefVarRef: name: etcd endpoint: Required + optional: true - name: POSTGRES_USER valueFrom: credentialVarRef: @@ -98,6 +114,29 @@ spec: name: postgres optional: false password: Required + ## the postgres pod name list for each pod, the value format is "pod1,pod2,..." + - name: POSTGRES_POD_NAME_LIST + valueFrom: + componentVarRef: + compDef: {{ include "postgresql.compDefPostgresql12" . }} + optional: false + podNames: Required + ## the postgres pod fqdn list for each pod, the value format is "pod1FQDN,pod2FQDN,..." + - name: POSTGRES_POD_FQDN_LIST + valueFrom: + componentVarRef: + compDef: {{ include "postgresql.compDefPostgresql12" . }} + optional: false + podFQDNs: Required + ## the postgres primary pod name which is dynamically selected, caution to use it + - name: POSTGRES_PRIMARY_POD_NAME + valueFrom: + componentVarRef: + compDef: {{ include "postgresql.compDefPostgresql12" . }} + optional: true + podNamesForRole: + role: primary + option: Optional systemAccounts: - name: postgres initAccount: true @@ -138,22 +177,16 @@ spec: - postgresql - getrole switchover: - withCandidate: - exec: - image: {{ include "postgresql.image.major12.minor150" . }} - command: - - /bin/bash - - -c - args: - - curl -s http://$(KB_REPLICATION_PRIMARY_POD_FQDN):8008/switchover -XPOST -d '{"leader":"$(KB_REPLICATION_PRIMARY_POD_NAME)","candidate":"$(KB_SWITCHOVER_CANDIDATE_NAME)"}' - withoutCandidate: - exec: - image: {{ include "postgresql.image.major12.minor150" . }} - command: - - /bin/bash - - -c - args: - - curl -s http://$(KB_REPLICATION_PRIMARY_POD_FQDN):8008/switchover -XPOST -d '{"leader":"$(KB_REPLICATION_PRIMARY_POD_NAME)"}' + exec: + container: postgresql + command: + - ["/kb-scripts/switchover.sh"] + env: + - name: CURRENT_POD_NAME + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: metadata.name accountProvision: builtinHandler: postgresql {{/* @@ -173,7 +206,7 @@ spec: */}} preTerminate: exec: - image: {{ include "postgresql.image.major12.minor150" . }} + container: postgresql command: ["/kb-scripts/etcd-clean.sh"] runtime: securityContext: @@ -194,8 +227,6 @@ spec: mountPath: /home/postgres/conf - name: scripts mountPath: /kb-scripts - - name: pod-info - mountPath: /kb-podinfo - command: - cp - -r @@ -216,10 +247,6 @@ spec: runAsUser: 0 command: - /kb-scripts/setup.sh - lifecycle: - preStop: - exec: - command: ["/kb-scripts/etcd-clean.sh"] readinessProbe: failureThreshold: 3 initialDelaySeconds: 10 @@ -243,8 +270,6 @@ spec: mountPath: /home/postgres/conf - name: scripts mountPath: /kb-scripts - - name: pod-info - mountPath: /kb-podinfo - mountPath: /tools name: tools ports: @@ -258,13 +283,13 @@ spec: - name: KUBERNETES_USE_CONFIGMAPS value: "true" - name: SCOPE - value: "$(KB_CLUSTER_NAME)-$(KB_COMP_NAME)-patroni$(KB_CLUSTER_UID_POSTFIX_8)" + value: "$(CLUSTER_NAME)-$(POSTGRES_COMPONENT_NAME)" - name: KUBERNETES_SCOPE_LABEL value: "apps.kubeblocks.postgres.patroni/scope" - name: KUBERNETES_ROLE_LABEL value: "apps.kubeblocks.postgres.patroni/role" - name: KUBERNETES_LABELS - value: '{"app.kubernetes.io/instance":"$(KB_CLUSTER_NAME)","apps.kubeblocks.io/component-name":"$(KB_COMP_NAME)"}' + value: '{"app.kubernetes.io/instance":"$(CLUSTER_NAME)","apps.kubeblocks.io/component-name":"$(POSTGRES_COMPONENT_NAME)","apps.kubeblocks.postgres.patroni/scope":"$(CLUSTER_NAME)-$(POSTGRES_COMPONENT_NAME)"}' - name: RESTORE_DATA_DIR value: /home/postgres/pgdata/kb_restore - name: KB_PG_CONFIG_PATH @@ -279,16 +304,6 @@ spec: value: "true" - name: PGROOT value: /home/postgres/pgdata/pgroot - - name: POD_IP - valueFrom: - fieldRef: - apiVersion: v1 - fieldPath: status.podIP - - name: POD_NAMESPACE - valueFrom: - fieldRef: - apiVersion: v1 - fieldPath: metadata.namespace - name: PGUSER_SUPERUSER value: $(POSTGRES_USER) - name: PGPASSWORD_SUPERUSER @@ -305,6 +320,25 @@ spec: value: $(POSTGRES_USER) - name: PGPASSWORD value: $(POSTGRES_PASSWORD) + - name: CURRENT_POD_NAME + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: metadata.name + - name: CURRENT_POD_IP + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: status.podIP + - name: CURRENT_POD_HOST_IP + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: status.hostIP + - name: POD_IP + value: $(CURRENT_POD_IP) + - name: POD_NAMESPACE + value: $(CLUSTER_NAMESPACE) - name: pgbouncer image: {{ include "pgbouncer.image" . }} imagePullPolicy: {{ .Values.pgbouncer.image.pullPolicy | quote }} @@ -345,15 +379,27 @@ spec: value: $(POSTGRES_PASSWORD) - name: POSTGRESQL_PORT value: "5432" - - name: POSTGRESQL_HOST - valueFrom: - fieldRef: - apiVersion: v1 - fieldPath: status.podIP - name: PGBOUNCER_PORT value: "6432" - name: PGBOUNCER_BIND_ADDRESS value: "0.0.0.0" + - name: CURRENT_POD_NAME + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: metadata.name + - name: CURRENT_POD_IP + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: status.podIP + - name: CURRENT_POD_HOST_IP + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: status.hostIP + - name: POSTGRESQL_HOST + value: $(CURRENT_POD_IP) - name: exporter ports: - name: http-metrics @@ -381,13 +427,3 @@ spec: {{- with .Values.shmVolume.sizeLimit }} sizeLimit: {{ . }} {{- end }} - ## deprecated since v0.8.3, do not use it - - name: pod-info - downwardAPI: - items: - - path: "primary-pod" - fieldRef: - fieldPath: metadata.annotations['rs.apps.kubeblocks.io/primary'] - - path: "component-replicas" - fieldRef: - fieldPath: metadata.annotations['apps.kubeblocks.io/component-replicas'] diff --git a/addons/postgresql/templates/componentdefinition-14.yaml b/addons/postgresql/templates/componentdefinition-14.yaml index 8273a89d0..4398bf292 100644 --- a/addons/postgresql/templates/componentdefinition-14.yaml +++ b/addons/postgresql/templates/componentdefinition-14.yaml @@ -14,6 +14,7 @@ spec: serviceRefDeclarationSpecs: - serviceKind: etcd serviceVersion: "^*" + optional: true services: - name: postgresql serviceName: postgresql @@ -37,8 +38,6 @@ spec: - name: secondary serviceable: false writable: false - labels: - apps.kubeblocks.postgres.patroni/scope: "$(KB_CLUSTER_NAME)-$(KB_COMP_NAME)-patroni$(KB_CLUSTER_UID_POSTFIX_8)" logConfigs: {{- range $name,$pattern := .Values.logConfigs }} - name: {{ $name }} @@ -49,13 +48,13 @@ spec: scrapePath: /metrics configs: - name: postgresql-configuration - templateRef: postgresql-configuration + templateRef: postgresql14-configuration constraintRef: postgresql14-cc keys: - postgresql.conf namespace: {{ .Release.Namespace }} volumeName: postgresql-config - defaultMode: 0444 + defaultMode: 0777 reRenderResourceTypes: - vscale - name: pgbouncer-configuration @@ -64,7 +63,7 @@ spec: - pgbouncer.ini namespace: {{ .Release.Namespace }} volumeName: pgbouncer-config - defaultMode: 0444 + defaultMode: 0777 - name: postgresql-custom-metrics templateRef: postgresql14-custom-metrics namespace: {{ .Release.Namespace }} @@ -77,13 +76,30 @@ spec: volumeName: scripts defaultMode: 0555 vars: - - name: ETCD_API - value: "3" - - name: ETCD_SERVER + - name: CLUSTER_NAMESPACE + valueFrom: + clusterVarRef: + namespace: Required + - name: CLUSTER_NAME + valueFrom: + clusterVarRef: + clusterName: Required + - name: POSTGRES_COMPONENT_NAME + valueFrom: + componentVarRef: + compDef: {{ include "postgresql.compDefPostgresql14" . }} + optional: false + componentName: Required + ## the etcd version if postgres patroni use etcd as DCS + - name: PATRONI_DCS_ETCD_VERSION + value: "3" + ## the etcd service reference endpoint if postgres patroni use etcd as DCS + - name: PATRONI_DCS_ETCD_SERVICE_ENDPOINT valueFrom: serviceRefVarRef: name: etcd endpoint: Required + optional: true - name: POSTGRES_USER valueFrom: credentialVarRef: @@ -98,6 +114,29 @@ spec: name: postgres optional: false password: Required + ## the postgres pod name list for each pod, the value format is "pod1,pod2,..." + - name: POSTGRES_POD_NAME_LIST + valueFrom: + componentVarRef: + compDef: {{ include "postgresql.compDefPostgresql14" . }} + optional: false + podNames: Required + ## the postgres pod fqdn list for each pod, the value format is "pod1FQDN,pod2FQDN,..." + - name: POSTGRES_POD_FQDN_LIST + valueFrom: + componentVarRef: + compDef: {{ include "postgresql.compDefPostgresql14" . }} + optional: false + podFQDNs: Required + ## the postgres primary pod name which is dynamically selected, caution to use it + - name: POSTGRES_PRIMARY_POD_NAME + valueFrom: + componentVarRef: + compDef: {{ include "postgresql.compDefPostgresql14" . }} + optional: true + podNamesForRole: + role: primary + option: Optional systemAccounts: - name: postgres initAccount: true @@ -138,22 +177,16 @@ spec: - postgresql - getrole switchover: - withCandidate: - exec: - image: {{ include "postgresql.image.major14.minor080" . }} - command: - - /bin/bash - - -c - args: - - curl -s http://$(KB_REPLICATION_PRIMARY_POD_FQDN):8008/switchover -XPOST -d '{"leader":"$(KB_REPLICATION_PRIMARY_POD_NAME)","candidate":"$(KB_SWITCHOVER_CANDIDATE_NAME)"}' - withoutCandidate: - exec: - image: {{ include "postgresql.image.major14.minor080" . }} - command: - - /bin/bash - - -c - args: - - curl -s http://$(KB_REPLICATION_PRIMARY_POD_FQDN):8008/switchover -XPOST -d '{"leader":"$(KB_REPLICATION_PRIMARY_POD_NAME)"}' + exec: + container: postgresql + command: + - ["/kb-scripts/switchover.sh"] + env: + - name: CURRENT_POD_NAME + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: metadata.name accountProvision: builtinHandler: postgresql {{/* @@ -173,7 +206,7 @@ spec: */}} preTerminate: exec: - image: {{ include "postgresql.image.major14.minor080" . }} + container: postgresql command: ["/kb-scripts/etcd-clean.sh"] runtime: securityContext: @@ -194,8 +227,6 @@ spec: mountPath: /home/postgres/conf - name: scripts mountPath: /kb-scripts - - name: pod-info - mountPath: /kb-podinfo - command: - cp - -r @@ -216,10 +247,6 @@ spec: runAsUser: 0 command: - /kb-scripts/setup.sh - lifecycle: - preStop: - exec: - command: ["/kb-scripts/etcd-clean.sh"] readinessProbe: failureThreshold: 3 initialDelaySeconds: 10 @@ -243,8 +270,6 @@ spec: mountPath: /home/postgres/conf - name: scripts mountPath: /kb-scripts - - name: pod-info - mountPath: /kb-podinfo - mountPath: /tools name: tools ports: @@ -258,13 +283,13 @@ spec: - name: KUBERNETES_USE_CONFIGMAPS value: "true" - name: SCOPE - value: "$(KB_CLUSTER_NAME)-$(KB_COMP_NAME)-patroni$(KB_CLUSTER_UID_POSTFIX_8)" + value: "$(CLUSTER_NAME)-$(POSTGRES_COMPONENT_NAME)" - name: KUBERNETES_SCOPE_LABEL value: "apps.kubeblocks.postgres.patroni/scope" - name: KUBERNETES_ROLE_LABEL value: "apps.kubeblocks.postgres.patroni/role" - name: KUBERNETES_LABELS - value: '{"app.kubernetes.io/instance":"$(KB_CLUSTER_NAME)","apps.kubeblocks.io/component-name":"$(KB_COMP_NAME)"}' + value: '{"app.kubernetes.io/instance":"$(CLUSTER_NAME)","apps.kubeblocks.io/component-name":"$(POSTGRES_COMPONENT_NAME)","apps.kubeblocks.postgres.patroni/scope":"$(CLUSTER_NAME)-$(POSTGRES_COMPONENT_NAME)"}' - name: RESTORE_DATA_DIR value: /home/postgres/pgdata/kb_restore - name: KB_PG_CONFIG_PATH @@ -279,16 +304,6 @@ spec: value: "true" - name: PGROOT value: /home/postgres/pgdata/pgroot - - name: POD_IP - valueFrom: - fieldRef: - apiVersion: v1 - fieldPath: status.podIP - - name: POD_NAMESPACE - valueFrom: - fieldRef: - apiVersion: v1 - fieldPath: metadata.namespace - name: PGUSER_SUPERUSER value: $(POSTGRES_USER) - name: PGPASSWORD_SUPERUSER @@ -305,6 +320,25 @@ spec: value: $(POSTGRES_USER) - name: PGPASSWORD value: $(POSTGRES_PASSWORD) + - name: CURRENT_POD_NAME + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: metadata.name + - name: CURRENT_POD_IP + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: status.podIP + - name: CURRENT_POD_HOST_IP + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: status.hostIP + - name: POD_IP + value: $(CURRENT_POD_IP) + - name: POD_NAMESPACE + value: $(CLUSTER_NAMESPACE) - name: pgbouncer image: {{ include "pgbouncer.image" . }} imagePullPolicy: {{ .Values.pgbouncer.image.pullPolicy | quote }} @@ -345,15 +379,27 @@ spec: value: $(POSTGRES_PASSWORD) - name: POSTGRESQL_PORT value: "5432" - - name: POSTGRESQL_HOST - valueFrom: - fieldRef: - apiVersion: v1 - fieldPath: status.podIP - name: PGBOUNCER_PORT value: "6432" - name: PGBOUNCER_BIND_ADDRESS value: "0.0.0.0" + - name: CURRENT_POD_NAME + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: metadata.name + - name: CURRENT_POD_IP + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: status.podIP + - name: CURRENT_POD_HOST_IP + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: status.hostIP + - name: POSTGRESQL_HOST + value: $(CURRENT_POD_IP) - name: exporter ports: - name: http-metrics @@ -381,13 +427,3 @@ spec: {{- with .Values.shmVolume.sizeLimit }} sizeLimit: {{ . }} {{- end }} - ## deprecated since v0.8.3, do not use it - - name: pod-info - downwardAPI: - items: - - path: "primary-pod" - fieldRef: - fieldPath: metadata.annotations['rs.apps.kubeblocks.io/primary'] - - path: "component-replicas" - fieldRef: - fieldPath: metadata.annotations['apps.kubeblocks.io/component-replicas'] diff --git a/addons/postgresql/templates/componentdefinition-15.yaml b/addons/postgresql/templates/componentdefinition-15.yaml index 7858a74e6..c388f99d0 100644 --- a/addons/postgresql/templates/componentdefinition-15.yaml +++ b/addons/postgresql/templates/componentdefinition-15.yaml @@ -38,8 +38,6 @@ spec: - name: secondary serviceable: false writable: false - labels: - apps.kubeblocks.postgres.patroni/scope: "$(KB_CLUSTER_NAME)-$(KB_COMP_NAME)-patroni$(KB_CLUSTER_UID_POSTFIX_8)" logConfigs: {{- range $name,$pattern := .Values.logConfigs }} - name: {{ $name }} @@ -56,7 +54,7 @@ spec: - postgresql.conf namespace: {{ .Release.Namespace }} volumeName: postgresql-config - defaultMode: 0444 + defaultMode: 0777 reRenderResourceTypes: - vscale - name: pgbouncer-configuration @@ -65,7 +63,7 @@ spec: - pgbouncer.ini namespace: {{ .Release.Namespace }} volumeName: pgbouncer-config - defaultMode: 0444 + defaultMode: 0777 - name: postgresql-custom-metrics templateRef: postgresql15-custom-metrics namespace: {{ .Release.Namespace }} @@ -78,9 +76,25 @@ spec: volumeName: scripts defaultMode: 0555 vars: - - name: ETCD_API - value: "3" - - name: ETCD_SERVER + - name: CLUSTER_NAMESPACE + valueFrom: + clusterVarRef: + namespace: Required + - name: CLUSTER_NAME + valueFrom: + clusterVarRef: + clusterName: Required + - name: POSTGRES_COMPONENT_NAME + valueFrom: + componentVarRef: + compDef: {{ include "postgresql.compDefPostgresql15" . }} + optional: false + componentName: Required + ## the etcd version if postgres patroni use etcd as DCS + - name: PATRONI_DCS_ETCD_VERSION + value: "3" + ## the etcd service reference endpoint if postgres patroni use etcd as DCS + - name: PATRONI_DCS_ETCD_SERVICE_ENDPOINT valueFrom: serviceRefVarRef: name: etcd @@ -100,6 +114,29 @@ spec: name: postgres optional: false password: Required + ## the postgres pod name list for each pod, the value format is "pod1,pod2,..." + - name: POSTGRES_POD_NAME_LIST + valueFrom: + componentVarRef: + compDef: {{ include "postgresql.compDefPostgresql15" . }} + optional: false + podNames: Required + ## the postgres pod fqdn list for each pod, the value format is "pod1FQDN,pod2FQDN,..." + - name: POSTGRES_POD_FQDN_LIST + valueFrom: + componentVarRef: + compDef: {{ include "postgresql.compDefPostgresql15" . }} + optional: false + podFQDNs: Required + ## the postgres primary pod name which is dynamically selected, caution to use it + - name: POSTGRES_PRIMARY_POD_NAME + valueFrom: + componentVarRef: + compDef: {{ include "postgresql.compDefPostgresql15" . }} + optional: true + podNamesForRole: + role: primary + option: Optional systemAccounts: - name: postgres initAccount: true @@ -140,22 +177,16 @@ spec: - postgresql - getrole switchover: - withCandidate: - exec: - image: {{ include "postgresql.image.major15.minor070" . }} - command: - - /bin/bash - - -c - args: - - curl -s http://$(KB_REPLICATION_PRIMARY_POD_FQDN):8008/switchover -XPOST -d '{"leader":"$(KB_REPLICATION_PRIMARY_POD_NAME)","candidate":"$(KB_SWITCHOVER_CANDIDATE_NAME)"}' - withoutCandidate: - exec: - image: {{ include "postgresql.image.major15.minor070" . }} - command: - - /bin/bash - - -c - args: - - curl -s http://$(KB_REPLICATION_PRIMARY_POD_FQDN):8008/switchover -XPOST -d '{"leader":"$(KB_REPLICATION_PRIMARY_POD_NAME)"}' + exec: + container: postgresql + command: + - ["/kb-scripts/switchover.sh"] + env: + - name: CURRENT_POD_NAME + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: metadata.name accountProvision: builtinHandler: postgresql {{/* @@ -175,7 +206,7 @@ spec: */}} preTerminate: exec: - image: {{ include "postgresql.image.major15.minor070" . }} + container: postgresql command: ["/kb-scripts/etcd-clean.sh"] runtime: securityContext: @@ -196,8 +227,6 @@ spec: mountPath: /home/postgres/conf - name: scripts mountPath: /kb-scripts - - name: pod-info - mountPath: /kb-podinfo - command: - cp - -r @@ -241,8 +270,6 @@ spec: mountPath: /home/postgres/conf - name: scripts mountPath: /kb-scripts - - name: pod-info - mountPath: /kb-podinfo - mountPath: /tools name: tools ports: @@ -256,13 +283,13 @@ spec: - name: KUBERNETES_USE_CONFIGMAPS value: "true" - name: SCOPE - value: "$(KB_CLUSTER_NAME)-$(KB_COMP_NAME)-patroni$(KB_CLUSTER_UID_POSTFIX_8)" + value: "$(CLUSTER_NAME)-$(POSTGRES_COMPONENT_NAME)" - name: KUBERNETES_SCOPE_LABEL value: "apps.kubeblocks.postgres.patroni/scope" - name: KUBERNETES_ROLE_LABEL value: "apps.kubeblocks.postgres.patroni/role" - name: KUBERNETES_LABELS - value: '{"app.kubernetes.io/instance":"$(KB_CLUSTER_NAME)","apps.kubeblocks.io/component-name":"$(KB_COMP_NAME)"}' + value: '{"app.kubernetes.io/instance":"$(CLUSTER_NAME)","apps.kubeblocks.io/component-name":"$(POSTGRES_COMPONENT_NAME)","apps.kubeblocks.postgres.patroni/scope":"$(CLUSTER_NAME)-$(POSTGRES_COMPONENT_NAME)"}' - name: RESTORE_DATA_DIR value: /home/postgres/pgdata/kb_restore - name: KB_PG_CONFIG_PATH @@ -277,16 +304,6 @@ spec: value: "true" - name: PGROOT value: /home/postgres/pgdata/pgroot - - name: POD_IP - valueFrom: - fieldRef: - apiVersion: v1 - fieldPath: status.podIP - - name: POD_NAMESPACE - valueFrom: - fieldRef: - apiVersion: v1 - fieldPath: metadata.namespace - name: PGUSER_SUPERUSER value: $(POSTGRES_USER) - name: PGPASSWORD_SUPERUSER @@ -303,6 +320,25 @@ spec: value: $(POSTGRES_USER) - name: PGPASSWORD value: $(POSTGRES_PASSWORD) + - name: CURRENT_POD_NAME + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: metadata.name + - name: CURRENT_POD_IP + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: status.podIP + - name: CURRENT_POD_HOST_IP + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: status.hostIP + - name: POD_IP + value: $(CURRENT_POD_IP) + - name: POD_NAMESPACE + value: $(CLUSTER_NAMESPACE) - name: pgbouncer image: {{ include "pgbouncer.image" . }} imagePullPolicy: {{ .Values.pgbouncer.image.pullPolicy | quote }} @@ -343,15 +379,27 @@ spec: value: $(POSTGRES_PASSWORD) - name: POSTGRESQL_PORT value: "5432" - - name: POSTGRESQL_HOST - valueFrom: - fieldRef: - apiVersion: v1 - fieldPath: status.podIP - name: PGBOUNCER_PORT value: "6432" - name: PGBOUNCER_BIND_ADDRESS value: "0.0.0.0" + - name: CURRENT_POD_NAME + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: metadata.name + - name: CURRENT_POD_IP + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: status.podIP + - name: CURRENT_POD_HOST_IP + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: status.hostIP + - name: POSTGRESQL_HOST + value: $(CURRENT_POD_IP) - name: exporter ports: - name: http-metrics @@ -379,13 +427,3 @@ spec: {{- with .Values.shmVolume.sizeLimit }} sizeLimit: {{ . }} {{- end }} - ## deprecated since v0.8.3, do not use it - - name: pod-info - downwardAPI: - items: - - path: "primary-pod" - fieldRef: - fieldPath: metadata.annotations['rs.apps.kubeblocks.io/primary'] - - path: "component-replicas" - fieldRef: - fieldPath: metadata.annotations['apps.kubeblocks.io/component-replicas'] diff --git a/addons/postgresql/templates/configmap-14.yaml b/addons/postgresql/templates/configmap-14.yaml index 412503b74..f538b0fb1 100644 --- a/addons/postgresql/templates/configmap-14.yaml +++ b/addons/postgresql/templates/configmap-14.yaml @@ -1,7 +1,7 @@ apiVersion: v1 kind: ConfigMap metadata: - name: postgresql-configuration + name: postgresql14-configuration labels: {{- include "postgresql.labels" . | nindent 4 }} data: diff --git a/addons/postgresql/templates/scripts.yaml b/addons/postgresql/templates/scripts.yaml index 842b7dc78..35827cfbe 100644 --- a/addons/postgresql/templates/scripts.yaml +++ b/addons/postgresql/templates/scripts.yaml @@ -5,4 +5,13 @@ metadata: labels: {{- include "postgresql.labels" . | nindent 4 }} data: - {{- include "postgresql.extend.scripts" . | nindent 2 }} + common.sh: |- + #!/bin/bash + {{- include "kblib.commons.call_func_with_retry" $ | nindent 4 }} + {{- include "kblib.compvars.get_target_pod_fqdn_from_pod_fqdn_vars" $ | nindent 4 }} + {{- include "kblib.pods.min_lexicographical_order_pod" $ | nindent 4 }} + {{- include "kblib.strings.is_empty" $ | nindent 4 }} + {{- include "kblib.strings.split" $ | nindent 4 }} + {{- with include "postgresql.extend.scripts" . }} + {{- . | nindent 2 }} + {{- end }} \ No newline at end of file diff --git a/addons/postgresql/values.yaml b/addons/postgresql/values.yaml index 8c651c9e8..edf6e846d 100644 --- a/addons/postgresql/values.yaml +++ b/addons/postgresql/values.yaml @@ -14,7 +14,7 @@ fullnameOverride: "" image: # refer: https://github.com/apecloud-inc/spilo/blob/main/postgres-appliance/Dockerfile # this is the image of postgresql, which is built from Dockerfile of apecloud-inc/spilo forked from zalando/spilo. - registry: docker.io + registry: apecloud-registry.cn-zhangjiakou.cr.aliyuncs.com repository: apecloud/spilo digest: "" tags: @@ -31,7 +31,8 @@ image: # refer: https://github.com/apecloud/dbctl/blob/main/docker/Dockerfile dbctl: repository: apecloud/dbctl - tag: "0.1.2" + tag: "0.1.3" + ## Specify a imagePullPolicy ## Defaults to 'Always' if image tag is 'latest', else set to 'IfNotPresent' ## ref: https://kubernetes.io/docs/user-guide/images/#pre-pulling-images @@ -92,7 +93,7 @@ metrics: image: # if the value of metrics.image.registry is not specified using `--set`, it will be set to the value of 'image.registry' by default registry: "" - repository: prometheuscommunity/postgres-exporter + repository: apecloud/postgres-exporter tag: v0.15.0 pullPolicy: IfNotPresent service: @@ -104,7 +105,7 @@ pgbouncer: # refer: https://hub.docker.com/r/bitnami/pgbouncer # if the value of pgbouncer.image.registry is not specified using `--set`, it will be set to the value of 'image.registry' by default registry: "" - repository: bitnami/pgbouncer + repository: apecloud/pgbouncer tag: 1.19.0 pullPolicy: IfNotPresent diff --git a/addons/redis/templates/scripts.yaml b/addons/redis/templates/scripts.yaml index 8bcc11cdf..139648bdd 100644 --- a/addons/redis/templates/scripts.yaml +++ b/addons/redis/templates/scripts.yaml @@ -21,4 +21,4 @@ data: {{- include "kblib.envs.env_exists" $ | nindent 4 }} {{- with include "redis.extend.scripts" . }} {{- . | nindent 2 }} - {{- end }} \ No newline at end of file + {{- end }} \ No newline at end of file From 7cbee8f18f7716349d1dea32c52d75d47e354777 Mon Sep 17 00:00:00 2001 From: Y-Rookie Date: Tue, 27 Aug 2024 16:33:56 +0800 Subject: [PATCH 02/12] add pg component short name --- .../templates/componentdefinition-12.yaml | 15 +++++++++++---- .../templates/componentdefinition-14.yaml | 15 +++++++++++---- .../templates/componentdefinition-15.yaml | 15 +++++++++++---- addons/postgresql/templates/scripts.yaml | 2 -- 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/addons/postgresql/templates/componentdefinition-12.yaml b/addons/postgresql/templates/componentdefinition-12.yaml index b18ac9b1f..f41dcd757 100644 --- a/addons/postgresql/templates/componentdefinition-12.yaml +++ b/addons/postgresql/templates/componentdefinition-12.yaml @@ -84,12 +84,20 @@ spec: valueFrom: clusterVarRef: clusterName: Required + ## the component name of postgres, it's the fullname of postgres component - name: POSTGRES_COMPONENT_NAME valueFrom: componentVarRef: compDef: {{ include "postgresql.compDefPostgresql12" . }} optional: false componentName: Required + ## the component short name of postgres, it's the short name of postgres component without clusterName prefix + - name: POSTGRES_COMPONENT_SHORT_NAME + valueFrom: + componentVarRef: + compDef: {{ include "postgresql.compDefPostgresql12" . }} + optional: false + componentShortName: Required ## the etcd version if postgres patroni use etcd as DCS - name: PATRONI_DCS_ETCD_VERSION value: "3" @@ -179,8 +187,7 @@ spec: switchover: exec: container: postgresql - command: - - ["/kb-scripts/switchover.sh"] + command: ["/kb-scripts/switchover.sh"] env: - name: CURRENT_POD_NAME valueFrom: @@ -283,13 +290,13 @@ spec: - name: KUBERNETES_USE_CONFIGMAPS value: "true" - name: SCOPE - value: "$(CLUSTER_NAME)-$(POSTGRES_COMPONENT_NAME)" + value: $(POSTGRES_COMPONENT_NAME) - name: KUBERNETES_SCOPE_LABEL value: "apps.kubeblocks.postgres.patroni/scope" - name: KUBERNETES_ROLE_LABEL value: "apps.kubeblocks.postgres.patroni/role" - name: KUBERNETES_LABELS - value: '{"app.kubernetes.io/instance":"$(CLUSTER_NAME)","apps.kubeblocks.io/component-name":"$(POSTGRES_COMPONENT_NAME)","apps.kubeblocks.postgres.patroni/scope":"$(CLUSTER_NAME)-$(POSTGRES_COMPONENT_NAME)"}' + value: '{"app.kubernetes.io/instance":"$(CLUSTER_NAME)","apps.kubeblocks.io/component-name":"$(POSTGRES_COMPONENT_SHORT_NAME)","apps.kubeblocks.postgres.patroni/scope":"$(POSTGRES_COMPONENT_NAME)"}' - name: RESTORE_DATA_DIR value: /home/postgres/pgdata/kb_restore - name: KB_PG_CONFIG_PATH diff --git a/addons/postgresql/templates/componentdefinition-14.yaml b/addons/postgresql/templates/componentdefinition-14.yaml index 4398bf292..9a710af9c 100644 --- a/addons/postgresql/templates/componentdefinition-14.yaml +++ b/addons/postgresql/templates/componentdefinition-14.yaml @@ -84,12 +84,20 @@ spec: valueFrom: clusterVarRef: clusterName: Required + ## the component name of postgres, it's the fullname of postgres component - name: POSTGRES_COMPONENT_NAME valueFrom: componentVarRef: compDef: {{ include "postgresql.compDefPostgresql14" . }} optional: false componentName: Required + ## the component short name of postgres, it's the short name of postgres component without clusterName prefix + - name: POSTGRES_COMPONENT_SHORT_NAME + valueFrom: + componentVarRef: + compDef: {{ include "postgresql.compDefPostgresql14" . }} + optional: false + componentShortName: Required ## the etcd version if postgres patroni use etcd as DCS - name: PATRONI_DCS_ETCD_VERSION value: "3" @@ -179,8 +187,7 @@ spec: switchover: exec: container: postgresql - command: - - ["/kb-scripts/switchover.sh"] + command: ["/kb-scripts/switchover.sh"] env: - name: CURRENT_POD_NAME valueFrom: @@ -283,13 +290,13 @@ spec: - name: KUBERNETES_USE_CONFIGMAPS value: "true" - name: SCOPE - value: "$(CLUSTER_NAME)-$(POSTGRES_COMPONENT_NAME)" + value: $(POSTGRES_COMPONENT_NAME) - name: KUBERNETES_SCOPE_LABEL value: "apps.kubeblocks.postgres.patroni/scope" - name: KUBERNETES_ROLE_LABEL value: "apps.kubeblocks.postgres.patroni/role" - name: KUBERNETES_LABELS - value: '{"app.kubernetes.io/instance":"$(CLUSTER_NAME)","apps.kubeblocks.io/component-name":"$(POSTGRES_COMPONENT_NAME)","apps.kubeblocks.postgres.patroni/scope":"$(CLUSTER_NAME)-$(POSTGRES_COMPONENT_NAME)"}' + value: '{"app.kubernetes.io/instance":"$(CLUSTER_NAME)","apps.kubeblocks.io/component-name":"$(POSTGRES_COMPONENT_SHORT_NAME)","apps.kubeblocks.postgres.patroni/scope":"$(POSTGRES_COMPONENT_NAME)"}' - name: RESTORE_DATA_DIR value: /home/postgres/pgdata/kb_restore - name: KB_PG_CONFIG_PATH diff --git a/addons/postgresql/templates/componentdefinition-15.yaml b/addons/postgresql/templates/componentdefinition-15.yaml index c388f99d0..0f6929ef6 100644 --- a/addons/postgresql/templates/componentdefinition-15.yaml +++ b/addons/postgresql/templates/componentdefinition-15.yaml @@ -84,12 +84,20 @@ spec: valueFrom: clusterVarRef: clusterName: Required + ## the component name of postgres, it's the fullname of postgres component - name: POSTGRES_COMPONENT_NAME valueFrom: componentVarRef: compDef: {{ include "postgresql.compDefPostgresql15" . }} optional: false componentName: Required + ## the component short name of postgres, it's the short name of postgres component without clusterName prefix + - name: POSTGRES_COMPONENT_SHORT_NAME + valueFrom: + componentVarRef: + compDef: {{ include "postgresql.compDefPostgresql15" . }} + optional: false + componentShortName: Required ## the etcd version if postgres patroni use etcd as DCS - name: PATRONI_DCS_ETCD_VERSION value: "3" @@ -179,8 +187,7 @@ spec: switchover: exec: container: postgresql - command: - - ["/kb-scripts/switchover.sh"] + command: ["/kb-scripts/switchover.sh"] env: - name: CURRENT_POD_NAME valueFrom: @@ -283,13 +290,13 @@ spec: - name: KUBERNETES_USE_CONFIGMAPS value: "true" - name: SCOPE - value: "$(CLUSTER_NAME)-$(POSTGRES_COMPONENT_NAME)" + value: $(POSTGRES_COMPONENT_NAME) - name: KUBERNETES_SCOPE_LABEL value: "apps.kubeblocks.postgres.patroni/scope" - name: KUBERNETES_ROLE_LABEL value: "apps.kubeblocks.postgres.patroni/role" - name: KUBERNETES_LABELS - value: '{"app.kubernetes.io/instance":"$(CLUSTER_NAME)","apps.kubeblocks.io/component-name":"$(POSTGRES_COMPONENT_NAME)","apps.kubeblocks.postgres.patroni/scope":"$(CLUSTER_NAME)-$(POSTGRES_COMPONENT_NAME)"}' + value: '{"app.kubernetes.io/instance":"$(CLUSTER_NAME)","apps.kubeblocks.io/component-name":"$(POSTGRES_COMPONENT_SHORT_NAME)","apps.kubeblocks.postgres.patroni/scope":"$(POSTGRES_COMPONENT_NAME)"}' - name: RESTORE_DATA_DIR value: /home/postgres/pgdata/kb_restore - name: KB_PG_CONFIG_PATH diff --git a/addons/postgresql/templates/scripts.yaml b/addons/postgresql/templates/scripts.yaml index 35827cfbe..2cf726dcc 100644 --- a/addons/postgresql/templates/scripts.yaml +++ b/addons/postgresql/templates/scripts.yaml @@ -7,9 +7,7 @@ metadata: data: common.sh: |- #!/bin/bash - {{- include "kblib.commons.call_func_with_retry" $ | nindent 4 }} {{- include "kblib.compvars.get_target_pod_fqdn_from_pod_fqdn_vars" $ | nindent 4 }} - {{- include "kblib.pods.min_lexicographical_order_pod" $ | nindent 4 }} {{- include "kblib.strings.is_empty" $ | nindent 4 }} {{- include "kblib.strings.split" $ | nindent 4 }} {{- with include "postgresql.extend.scripts" . }} From 5d1d03250232e251fd71a3145312a78773dcec4f Mon Sep 17 00:00:00 2001 From: Y-Rookie Date: Tue, 27 Aug 2024 16:57:17 +0800 Subject: [PATCH 03/12] rename component shortname --- addons/postgresql/templates/componentdefinition-12.yaml | 2 +- addons/postgresql/templates/componentdefinition-14.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/addons/postgresql/templates/componentdefinition-12.yaml b/addons/postgresql/templates/componentdefinition-12.yaml index f41dcd757..3ef8251e2 100644 --- a/addons/postgresql/templates/componentdefinition-12.yaml +++ b/addons/postgresql/templates/componentdefinition-12.yaml @@ -97,7 +97,7 @@ spec: componentVarRef: compDef: {{ include "postgresql.compDefPostgresql12" . }} optional: false - componentShortName: Required + shortName: Required ## the etcd version if postgres patroni use etcd as DCS - name: PATRONI_DCS_ETCD_VERSION value: "3" diff --git a/addons/postgresql/templates/componentdefinition-14.yaml b/addons/postgresql/templates/componentdefinition-14.yaml index 9a710af9c..6c869b17b 100644 --- a/addons/postgresql/templates/componentdefinition-14.yaml +++ b/addons/postgresql/templates/componentdefinition-14.yaml @@ -97,7 +97,7 @@ spec: componentVarRef: compDef: {{ include "postgresql.compDefPostgresql14" . }} optional: false - componentShortName: Required + shortName: Required ## the etcd version if postgres patroni use etcd as DCS - name: PATRONI_DCS_ETCD_VERSION value: "3" From e5a09fe2f58f6ccb697fe67ea9720e05e5771072 Mon Sep 17 00:00:00 2001 From: Y-Rookie Date: Tue, 27 Aug 2024 17:11:12 +0800 Subject: [PATCH 04/12] add switchover todo --- addons/postgresql/scripts/switchover.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/addons/postgresql/scripts/switchover.sh b/addons/postgresql/scripts/switchover.sh index f718be26a..f2b415ad3 100644 --- a/addons/postgresql/scripts/switchover.sh +++ b/addons/postgresql/scripts/switchover.sh @@ -28,15 +28,19 @@ switchoverWithCandidate() { local current_pod_fqdn=$1 local current_primary_pod_fqdn=$2 local candidate_pod_fqdn=$3 + # TODO: check the role in kernel before switchover # shellcheck disable=SC2016 curl -s http://$(current_pod_fqdn):8008/switchover -XPOST -d '{"leader":"$(current_primary_pod_fqdn)","candidate":"$(candidate_pod_fqdn)"}' + # TODO: check switchover result } switchoverWithoutCandidate() { local current_pod_fqdn=$1 local current_primary_pod_fqdn=$2 + # TODO: check the role in kernel before switchover # shellcheck disable=SC2016 curl -s http://$(current_pod_fqdn):8008/switchover -XPOST -d '{"leader":"$(current_primary_pod_fqdn)"}' + # TODO: check switchover result } switchover() { From b7a1b846ea3a20c6fd62f291950e122047d9c7fc Mon Sep 17 00:00:00 2001 From: Y-Rookie Date: Tue, 27 Aug 2024 17:29:04 +0800 Subject: [PATCH 05/12] modify switchover.sh --- addons/postgresql/scripts/switchover.sh | 28 ++++++++++--------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/addons/postgresql/scripts/switchover.sh b/addons/postgresql/scripts/switchover.sh index f2b415ad3..fe54b3c3f 100644 --- a/addons/postgresql/scripts/switchover.sh +++ b/addons/postgresql/scripts/switchover.sh @@ -24,22 +24,22 @@ load_common_library() { source "${common_library_file}" } -switchoverWithCandidate() { +switchover_with_candidate() { local current_pod_fqdn=$1 - local current_primary_pod_fqdn=$2 - local candidate_pod_fqdn=$3 + local current_primary_pod_name=$2 + local candidate_pod_name=$3 # TODO: check the role in kernel before switchover # shellcheck disable=SC2016 - curl -s http://$(current_pod_fqdn):8008/switchover -XPOST -d '{"leader":"$(current_primary_pod_fqdn)","candidate":"$(candidate_pod_fqdn)"}' + curl -s http://$(current_pod_fqdn):8008/switchover -XPOST -d '{"leader":"$(current_primary_pod_name)","candidate":"$(candidate_pod_name)"}' # TODO: check switchover result } -switchoverWithoutCandidate() { +switchover_without_candidate() { local current_pod_fqdn=$1 - local current_primary_pod_fqdn=$2 + local current_primary_pod_name=$2 # TODO: check the role in kernel before switchover # shellcheck disable=SC2016 - curl -s http://$(current_pod_fqdn):8008/switchover -XPOST -d '{"leader":"$(current_primary_pod_fqdn)"}' + curl -s http://$(current_pod_fqdn):8008/switchover -XPOST -d '{"leader":"$(current_primary_pod_name)"}' # TODO: check switchover result } @@ -70,17 +70,11 @@ switchover() { exit 1 fi - current_primary_pod_fqdn=$(get_target_pod_fqdn_from_pod_fqdn_vars "$POSTGRES_POD_FQDN_LIST" "$CURRENT_PRIMARY_POD_NAME") - if is_empty "$current_primary_pod_fqdn"; then - echo "Error: Failed to get current primary pod fqdn: $CURRENT_PRIMARY_POD_NAME from postgres pod fqdn list: $POSTGRES_POD_FQDN_LIST. Exiting." - exit 1 - fi - - # KB_SWITCHOVER_CANDIDATE_FQDN is built-in env in the switchover action injected by the KubeBlocks controller - if ! is_empty "$KB_SWITCHOVER_CANDIDATE_FQDN"; then - switchoverWithCandidate "$current_pod_fqdn" "$current_primary_pod_fqdn" "$KB_SWITCHOVER_CANDIDATE_FQDN" + # KB_SWITCHOVER_CANDIDATE_NAME is built-in env in the switchover action injected by the KubeBlocks controller + if ! is_empty "$KB_SWITCHOVER_CANDIDATE_NAME"; then + switchover_with_candidate "$current_pod_fqdn" "$POSTGRES_PRIMARY_POD_NAME" "$KB_SWITCHOVER_CANDIDATE_NAME" else - switchoverWithoutCandidate "$current_pod_fqdn" "$current_primary_pod_fqdn" + switchover_without_candidate "$current_pod_fqdn" "$POSTGRES_PRIMARY_POD_NAME" fi } From 48946022455df2d919f55bb60a83c4ac05a9645d Mon Sep 17 00:00:00 2001 From: Y-Rookie Date: Tue, 27 Aug 2024 17:50:47 +0800 Subject: [PATCH 06/12] rename pg shell file name --- addons/postgresql/config/pgbouncer-ini.tpl | 3 +-- .../scripts/{pgbouncer_setup.sh => pgbouncer-setup.sh} | 0 .../scripts/{init_container.sh => postgres-pre-setup.sh} | 0 addons/postgresql/scripts/{setup.sh => postgres-setup.sh} | 0 addons/postgresql/templates/componentdefinition-12.yaml | 6 +++--- addons/postgresql/templates/componentdefinition-14.yaml | 6 +++--- addons/postgresql/templates/componentdefinition-15.yaml | 6 +++--- 7 files changed, 10 insertions(+), 11 deletions(-) rename addons/postgresql/scripts/{pgbouncer_setup.sh => pgbouncer-setup.sh} (100%) rename addons/postgresql/scripts/{init_container.sh => postgres-pre-setup.sh} (100%) rename addons/postgresql/scripts/{setup.sh => postgres-setup.sh} (100%) diff --git a/addons/postgresql/config/pgbouncer-ini.tpl b/addons/postgresql/config/pgbouncer-ini.tpl index 987a196e4..3e5e9a2e0 100644 --- a/addons/postgresql/config/pgbouncer-ini.tpl +++ b/addons/postgresql/config/pgbouncer-ini.tpl @@ -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 \ No newline at end of file +;;; [database] \ No newline at end of file diff --git a/addons/postgresql/scripts/pgbouncer_setup.sh b/addons/postgresql/scripts/pgbouncer-setup.sh similarity index 100% rename from addons/postgresql/scripts/pgbouncer_setup.sh rename to addons/postgresql/scripts/pgbouncer-setup.sh diff --git a/addons/postgresql/scripts/init_container.sh b/addons/postgresql/scripts/postgres-pre-setup.sh similarity index 100% rename from addons/postgresql/scripts/init_container.sh rename to addons/postgresql/scripts/postgres-pre-setup.sh diff --git a/addons/postgresql/scripts/setup.sh b/addons/postgresql/scripts/postgres-setup.sh similarity index 100% rename from addons/postgresql/scripts/setup.sh rename to addons/postgresql/scripts/postgres-setup.sh diff --git a/addons/postgresql/templates/componentdefinition-12.yaml b/addons/postgresql/templates/componentdefinition-12.yaml index 3ef8251e2..26456e084 100644 --- a/addons/postgresql/templates/componentdefinition-12.yaml +++ b/addons/postgresql/templates/componentdefinition-12.yaml @@ -226,7 +226,7 @@ spec: image: {{ include "postgresql.image.major12.minor150" . }} imagePullPolicy: {{ default "IfNotPresent" .Values.image.pullPolicy }} command: - - /kb-scripts/init_container.sh + - /kb-scripts/postgres-pre-setup.sh volumeMounts: - name: data mountPath: /home/postgres/pgdata @@ -253,7 +253,7 @@ spec: securityContext: runAsUser: 0 command: - - /kb-scripts/setup.sh + - /kb-scripts/postgres-setup.sh readinessProbe: failureThreshold: 3 initialDelaySeconds: 10 @@ -360,7 +360,7 @@ spec: - name: scripts mountPath: /kb-scripts command: - - /kb-scripts/pgbouncer_setup.sh + - /kb-scripts/pgbouncer-setup.sh livenessProbe: failureThreshold: 3 initialDelaySeconds: 15 diff --git a/addons/postgresql/templates/componentdefinition-14.yaml b/addons/postgresql/templates/componentdefinition-14.yaml index 6c869b17b..43dde07f3 100644 --- a/addons/postgresql/templates/componentdefinition-14.yaml +++ b/addons/postgresql/templates/componentdefinition-14.yaml @@ -226,7 +226,7 @@ spec: image: {{ include "postgresql.image.major14.minor080" . }} imagePullPolicy: {{ default "IfNotPresent" .Values.image.pullPolicy }} command: - - /kb-scripts/init_container.sh + - /kb-scripts/postgres-pre-setup.sh volumeMounts: - name: data mountPath: /home/postgres/pgdata @@ -253,7 +253,7 @@ spec: securityContext: runAsUser: 0 command: - - /kb-scripts/setup.sh + - /kb-scripts/postgres-setup.sh readinessProbe: failureThreshold: 3 initialDelaySeconds: 10 @@ -360,7 +360,7 @@ spec: - name: scripts mountPath: /kb-scripts command: - - /kb-scripts/pgbouncer_setup.sh + - /kb-scripts/pgbouncer-setup.sh livenessProbe: failureThreshold: 3 initialDelaySeconds: 15 diff --git a/addons/postgresql/templates/componentdefinition-15.yaml b/addons/postgresql/templates/componentdefinition-15.yaml index 0f6929ef6..9d1854957 100644 --- a/addons/postgresql/templates/componentdefinition-15.yaml +++ b/addons/postgresql/templates/componentdefinition-15.yaml @@ -226,7 +226,7 @@ spec: image: {{ include "postgresql.image.major15.minor070" . }} imagePullPolicy: {{ default "IfNotPresent" .Values.image.pullPolicy }} command: - - /kb-scripts/init_container.sh + - /kb-scripts/postgres-pre-setup.sh volumeMounts: - name: data mountPath: /home/postgres/pgdata @@ -253,7 +253,7 @@ spec: securityContext: runAsUser: 0 command: - - /kb-scripts/setup.sh + - /kb-scripts/postgres-setup.sh readinessProbe: failureThreshold: 3 initialDelaySeconds: 10 @@ -360,7 +360,7 @@ spec: - name: scripts mountPath: /kb-scripts command: - - /kb-scripts/pgbouncer_setup.sh + - /kb-scripts/pgbouncer-setup.sh livenessProbe: failureThreshold: 3 initialDelaySeconds: 15 From f176cdb2ad8c261af23b7aa6eb202219ed429315 Mon Sep 17 00:00:00 2001 From: Y-Rookie Date: Wed, 28 Aug 2024 14:14:02 +0800 Subject: [PATCH 07/12] remove etcd-clean.sh --- addons/postgresql/scripts/etcd-clean.sh | 57 +------------------ .../templates/componentdefinition-12.yaml | 4 -- .../templates/componentdefinition-14.yaml | 4 -- .../templates/componentdefinition-15.yaml | 4 -- 4 files changed, 1 insertion(+), 68 deletions(-) diff --git a/addons/postgresql/scripts/etcd-clean.sh b/addons/postgresql/scripts/etcd-clean.sh index 64f02a0bb..39ea72f6c 100644 --- a/addons/postgresql/scripts/etcd-clean.sh +++ b/addons/postgresql/scripts/etcd-clean.sh @@ -1,56 +1 @@ -#!/bin/bash - -if [ -z "$ETCD_SERVER" ]; then - exit 0 -fi - -IFS=',' read -ra ADDR <<< "$KB_CLUSTER_COMPONENT_POD_IP_LIST" -FIRST_IP=${ADDR[0]} - -PATRONI_API_URL="http://$FIRST_IP:8008/patroni" -PATRONI_SHUTDOWN_URL="http://$FIRST_IP:8008/shutdown" - -status_code=$(curl -s -o /dev/null -w "%{http_code}" $PATRONI_API_URL) - -if [ "$status_code" -ne 200 ]; then - echo "Failed to access Patroni API at $PATRONI_API_URL. HTTP response code: $status_code." - exit 0 -fi - -response=$(curl -s -o /dev/null -w "%{http_code}" -XPOST $PATRONI_SHUTDOWN_URL) - -if [ "$response" -eq 200 ]; then - echo "Successfully terminated Patroni service at $FIRST_IP." -else - echo "Failed to terminate Patroni service at $FIRST_IP. HTTP response code: $response." - sleep 10 - exit 0 -fi -sleep 10 - -export ETCDCTL_API=${PATRONI_DCS_ETCD_VERSION:-'2'} - -endpoints=${ETCD_SERVER:-'127.0.0.1:2379'} - -servers="" -IFS=',' read -ra ADDR <<< "$endpoints" -for addr in "${ADDR[@]}"; do - if [[ $addr != http* ]]; then - addr="http://$addr" - fi - servers="${servers},${addr}" -done - -servers=${servers:1} - -echo $servers - -echo "Deleting all keys with prefix /service/${KB_CLUSTER_NAME}-${KB_COMP_NAME}-patroni${KB_CLUSTER_UID_POSTFIX_8} from Etcd server at ${endpoints}..." -etcdctl --endpoints $servers del /service/${KB_CLUSTER_NAME}-${KB_COMP_NAME}-patroni${KB_CLUSTER_UID_POSTFIX_8} --prefix - -if [ $? -eq 0 ]; then - echo "Successfully deleted all keys with prefix /service/${KB_CLUSTER_NAME}-${KB_COMP_NAME}-patroni${KB_CLUSTER_UID_POSTFIX_8}." -else - echo "Failed to delete keys. Please check your Etcd server and try again." - exit 0 -fi +# TODO: implement etcd-clean.sh \ No newline at end of file diff --git a/addons/postgresql/templates/componentdefinition-12.yaml b/addons/postgresql/templates/componentdefinition-12.yaml index 26456e084..679e60df5 100644 --- a/addons/postgresql/templates/componentdefinition-12.yaml +++ b/addons/postgresql/templates/componentdefinition-12.yaml @@ -211,10 +211,6 @@ spec: - name: PGPASSWORD value: $(POSTGRES_PASSWORD) */}} - preTerminate: - exec: - container: postgresql - command: ["/kb-scripts/etcd-clean.sh"] runtime: securityContext: runAsUser: 0 diff --git a/addons/postgresql/templates/componentdefinition-14.yaml b/addons/postgresql/templates/componentdefinition-14.yaml index 43dde07f3..c93463da3 100644 --- a/addons/postgresql/templates/componentdefinition-14.yaml +++ b/addons/postgresql/templates/componentdefinition-14.yaml @@ -211,10 +211,6 @@ spec: - name: PGPASSWORD value: $(POSTGRES_PASSWORD) */}} - preTerminate: - exec: - container: postgresql - command: ["/kb-scripts/etcd-clean.sh"] runtime: securityContext: runAsUser: 0 diff --git a/addons/postgresql/templates/componentdefinition-15.yaml b/addons/postgresql/templates/componentdefinition-15.yaml index 9d1854957..d305667d7 100644 --- a/addons/postgresql/templates/componentdefinition-15.yaml +++ b/addons/postgresql/templates/componentdefinition-15.yaml @@ -211,10 +211,6 @@ spec: - name: PGPASSWORD value: $(POSTGRES_PASSWORD) */}} - preTerminate: - exec: - container: postgresql - command: ["/kb-scripts/etcd-clean.sh"] runtime: securityContext: runAsUser: 0 From 27364ed427355b772ee1b8f211bdfd6753f7a644 Mon Sep 17 00:00:00 2001 From: Y-Rookie Date: Wed, 28 Aug 2024 17:38:48 +0800 Subject: [PATCH 08/12] add ut for postgresql scripts --- .../scripts-ut-spec/pgbouncer_setup_spec.sh | 87 ++++++++++++ .../postgres_pre_setup_spec.sh | 44 ++++++ .../scripts-ut-spec/postgres_setup_spec.sh | 117 ++++++++++++++++ .../scripts-ut-spec/switchover_spec.sh | 126 ++++++++++++++++++ addons/postgresql/scripts-ut-spec/utils.sh | 32 +++++ addons/postgresql/scripts/pgbouncer-setup.sh | 40 +++--- .../postgresql/scripts/postgres-pre-setup.sh | 28 +--- addons/postgresql/scripts/postgres-setup.sh | 21 +-- addons/postgresql/scripts/switchover.sh | 19 +-- 9 files changed, 433 insertions(+), 81 deletions(-) create mode 100644 addons/postgresql/scripts-ut-spec/pgbouncer_setup_spec.sh create mode 100644 addons/postgresql/scripts-ut-spec/postgres_pre_setup_spec.sh create mode 100644 addons/postgresql/scripts-ut-spec/postgres_setup_spec.sh create mode 100644 addons/postgresql/scripts-ut-spec/switchover_spec.sh create mode 100644 addons/postgresql/scripts-ut-spec/utils.sh diff --git a/addons/postgresql/scripts-ut-spec/pgbouncer_setup_spec.sh b/addons/postgresql/scripts-ut-spec/pgbouncer_setup_spec.sh new file mode 100644 index 000000000..1ffcf6d59 --- /dev/null +++ b/addons/postgresql/scripts-ut-spec/pgbouncer_setup_spec.sh @@ -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 \ No newline at end of file diff --git a/addons/postgresql/scripts-ut-spec/postgres_pre_setup_spec.sh b/addons/postgresql/scripts-ut-spec/postgres_pre_setup_spec.sh new file mode 100644 index 000000000..71785df6d --- /dev/null +++ b/addons/postgresql/scripts-ut-spec/postgres_pre_setup_spec.sh @@ -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 \ No newline at end of file diff --git a/addons/postgresql/scripts-ut-spec/postgres_setup_spec.sh b/addons/postgresql/scripts-ut-spec/postgres_setup_spec.sh new file mode 100644 index 000000000..9bb663299 --- /dev/null +++ b/addons/postgresql/scripts-ut-spec/postgres_setup_spec.sh @@ -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 \ No newline at end of file diff --git a/addons/postgresql/scripts-ut-spec/switchover_spec.sh b/addons/postgresql/scripts-ut-spec/switchover_spec.sh new file mode 100644 index 000000000..1842bf2c6 --- /dev/null +++ b/addons/postgresql/scripts-ut-spec/switchover_spec.sh @@ -0,0 +1,126 @@ +# 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 "switchover_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 Switchover Script Tests" + + Include ../scripts/switchover.sh + Include $common_library_file + + cleanup() { + rm -f $common_library_file + } + AfterAll 'cleanup' + + Describe "switchover()" + Context "when CURRENT_POD_NAME or POSTGRES_PRIMARY_POD_NAME is not set" + setup() { + unset CURRENT_POD_NAME + unset POSTGRES_PRIMARY_POD_NAME + } + Before 'setup' + + It "exits with an error" + When run switchover + The output should include "CURRENT_POD_NAME or POSTGRES_PRIMARY_POD_NAME is not set. Exiting..." + The status should be failure + End + End + + Context "when POSTGRES_PRIMARY_POD_NAME is not unique" + setup() { + CURRENT_POD_NAME="pod1" + POSTGRES_PRIMARY_POD_NAME="pod1,pod2" + } + Before 'setup' + + It "exits with an error" + When run switchover + The output should include "Error: POSTGRES_PRIMARY_POD_NAME should be a unique pod name. Exiting." + The status should be failure + End + End + + Context "when POSTGRES_POD_NAME_LIST or POSTGRES_POD_FQDN_LIST is not set" + setup() { + CURRENT_POD_NAME="pod1" + POSTGRES_PRIMARY_POD_NAME="pod1" + unset POSTGRES_POD_NAME_LIST + unset POSTGRES_POD_FQDN_LIST + } + Before 'setup' + + It "exits with an error" + When run switchover + The output should include "POSTGRES_POD_NAME_LIST or POSTGRES_POD_FQDN_LIST is not set. Exiting..." + The status should be failure + End + End + + Context "when the current pod FQDN is not found" + setup() { + CURRENT_POD_NAME="pod1" + POSTGRES_PRIMARY_POD_NAME="pod1" + POSTGRES_POD_NAME_LIST="pod2,pod3" + POSTGRES_POD_FQDN_LIST="pod2.example.com,pod3.example.com" + } + Before 'setup' + + It "exits with an error" + When run switchover + The output should include "Error: Failed to get current pod: pod1 fqdn from postgres pod fqdn list: pod2.example.com,pod3.example.com. Exiting." + The status should be failure + End + End + + Context "when KB_SWITCHOVER_CANDIDATE_NAME is set" + setup() { + CURRENT_POD_NAME="pod1" + POSTGRES_PRIMARY_POD_NAME="pod1" + POSTGRES_POD_NAME_LIST="pod1,pod2" + POSTGRES_POD_FQDN_LIST="pod1.example.com,pod2.example.com" + KB_SWITCHOVER_CANDIDATE_NAME="pod2" + } + Before 'setup' + + It "calls switchover_with_candidate" + switchover_with_candidate() { + echo "Calling switchover_with_candidate with arguments: $1 $2 $3" + } + When run switchover + The output should include "Calling switchover_with_candidate with arguments: pod1.example.com pod1 pod2" + End + End + + Context "when KB_SWITCHOVER_CANDIDATE_NAME is not set" + setup() { + CURRENT_POD_NAME="pod1" + POSTGRES_PRIMARY_POD_NAME="pod1" + POSTGRES_POD_NAME_LIST="pod1,pod2" + POSTGRES_POD_FQDN_LIST="pod1.example.com,pod2.example.com" + unset KB_SWITCHOVER_CANDIDATE_NAME + } + Before 'setup' + + It "calls switchover_without_candidate" + switchover_without_candidate() { + echo "Calling switchover_without_candidate with arguments: $1 $2" + } + When run switchover + The output should include "Calling switchover_without_candidate with arguments: pod1.example.com pod1" + End + End + End +End \ No newline at end of file diff --git a/addons/postgresql/scripts-ut-spec/utils.sh b/addons/postgresql/scripts-ut-spec/utils.sh new file mode 100644 index 000000000..5b2506969 --- /dev/null +++ b/addons/postgresql/scripts-ut-spec/utils.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +# utils functions for shellspec unit tests + +convert_tpl_to_bash() { + local input_file="$1" + local output_file="$2" + + sed -e '/^{{\/\*$/,/^\*\/}}$/d' \ + -e '/^{{-.*}}/d' \ + -e 's/{{- define ".*" }}//' \ + -e 's/{{- end }}//' \ + "$input_file" >> "$output_file" +} + +generate_common_library() { + local library_file="$1" + + libcommons_tpl_file="../../kblib/templates/_libcommons.tpl" + libpods_tpl_file="../../kblib/templates/_libpods.tpl" + libstrings_tpl_file="../../kblib/templates/_libstrings.tpl" + libenvs_tpl_file="../../kblib/templates/_libenvs.tpl" + libcompvars_tpl_file="../../kblib/templates/_libcompvars.tpl" + libututils_tpl_file="../../kblib/templates/_libututils.tpl" + + convert_tpl_to_bash $libcommons_tpl_file "$library_file" + convert_tpl_to_bash $libpods_tpl_file "$library_file" + convert_tpl_to_bash $libstrings_tpl_file "$library_file" + convert_tpl_to_bash $libenvs_tpl_file "$library_file" + convert_tpl_to_bash $libcompvars_tpl_file "$library_file" + convert_tpl_to_bash $libututils_tpl_file "$library_file" +} \ No newline at end of file diff --git a/addons/postgresql/scripts/pgbouncer-setup.sh b/addons/postgresql/scripts/pgbouncer-setup.sh index 08c727702..00e66bdb8 100755 --- a/addons/postgresql/scripts/pgbouncer-setup.sh +++ b/addons/postgresql/scripts/pgbouncer-setup.sh @@ -1,21 +1,11 @@ #!/bin/bash -# This is magic for shellspec ut framework. "test" is a `test [expression]` well known as a shell command. -# Normally test without [expression] returns false. It means that __() { :; } -# function is defined if this script runs directly. -# -# shellspec overrides the test command and returns true *once*. It means that -# __() function defined internally by shellspec is called. -# -# In other words. If not in test mode, __ is just a comment. If test mode, __ -# is a interception point. -# you should set ut_mode="true" when you want to run the script in shellspec file. -# shellcheck disable=SC2034 -ut_mode="false" -test || __() { - # when running in non-unit test mode, set the options "set -ex". - set -ex; -} +pgbouncer_template_conf_file="/home/pgbouncer/conf/pgbouncer.ini" +pgbouncer_conf_dir="/opt/bitnami/pgbouncer/conf/" +pgbouncer_log_dir="/opt/bitnami/pgbouncer/logs/" +pgbouncer_tmp_dir="/opt/bitnami/pgbouncer/tmp/" +pgbouncer_conf_file="/opt/bitnami/pgbouncer/conf/pgbouncer.ini" +pgbouncer_user_list_file="/opt/bitnami/pgbouncer/conf/userlist.txt" load_common_library() { # the common.sh scripts is mounted to the same path which is defined in the cmpd.spec.scripts @@ -30,17 +20,17 @@ build_pgbouncer_conf() { exit 1 fi - mkdir -p /opt/bitnami/pgbouncer/conf/ /opt/bitnami/pgbouncer/logs/ /opt/bitnami/pgbouncer/tmp/ - cp /home/pgbouncer/conf/pgbouncer.ini /opt/bitnami/pgbouncer/conf/ - echo "\"$POSTGRESQL_USERNAME\" \"$POSTGRESQL_PASSWORD\"" > /opt/bitnami/pgbouncer/conf/userlist.txt + mkdir -p $pgbouncer_conf_dir $pgbouncer_log_dir $pgbouncer_tmp_dir + cp $pgbouncer_template_conf_file $pgbouncer_conf_dir + echo "\"$POSTGRESQL_USERNAME\" \"$POSTGRESQL_PASSWORD\"" > $pgbouncer_user_list_file # shellcheck disable=SC2129 - echo -e "\\n[databases]" >> /opt/bitnami/pgbouncer/conf/pgbouncer.ini - echo "postgres=host=$CURRENT_POD_IP port=5432 dbname=postgres" >> /opt/bitnami/pgbouncer/conf/pgbouncer.ini - echo "*=host=$CURRENT_POD_IP port=5432" >> /opt/bitnami/pgbouncer/conf/pgbouncer.ini - chmod +777 /opt/bitnami/pgbouncer/conf/pgbouncer.ini - chmod +777 /opt/bitnami/pgbouncer/conf/userlist.txt + echo -e "\\n[databases]" >> $pgbouncer_conf_file + echo "postgres=host=$CURRENT_POD_IP port=5432 dbname=postgres" >> $pgbouncer_conf_file + echo "*=host=$CURRENT_POD_IP port=5432" >> $pgbouncer_conf_file + chmod 777 $pgbouncer_conf_file + chmod 777 $pgbouncer_user_list_file useradd pgbouncer - chown -R pgbouncer:pgbouncer /opt/bitnami/pgbouncer/conf/ /opt/bitnami/pgbouncer/logs/ /opt/bitnami/pgbouncer/tmp/ + chown -R pgbouncer:pgbouncer $pgbouncer_conf_dir $pgbouncer_log_dir $pgbouncer_tmp_dir } start_pgbouncer() { diff --git a/addons/postgresql/scripts/postgres-pre-setup.sh b/addons/postgresql/scripts/postgres-pre-setup.sh index 449cc9710..b78e63450 100755 --- a/addons/postgresql/scripts/postgres-pre-setup.sh +++ b/addons/postgresql/scripts/postgres-pre-setup.sh @@ -1,28 +1,14 @@ #!/bin/bash -# This is magic for shellspec ut framework. "test" is a `test [expression]` well known as a shell command. -# Normally test without [expression] returns false. It means that __() { :; } -# function is defined if this script runs directly. -# -# shellspec overrides the test command and returns true *once*. It means that -# __() function defined internally by shellspec is called. -# -# In other words. If not in test mode, __ is just a comment. If test mode, __ -# is a interception point. -# you should set ut_mode="true" when you want to run the script in shellspec file. -# shellcheck disable=SC2034 -ut_mode="false" -test || __() { - # when running in non-unit test mode, set the options "set -ex". - set -ex; -} +postgres_template_conf_file="/home/postgres/conf/postgresql.conf" +postgres_conf_dir="/home/postgres/pgdata/conf/" +postgres_conf_file="/home/postgres/pgdata/conf/postgresql.conf" build_real_postgres_conf() { - # /home/postgres/conf is the mounted path which postgres config template defined in the cmpd.spec.scripts - mkdir -p /home/postgres/pgdata/conf - chmod +777 -R /home/postgres/pgdata/conf - cp /home/postgres/conf/postgresql.conf /home/postgres/pgdata/conf - chmod +777 /home/postgres/pgdata/conf/postgresql.conf + mkdir -p $postgres_conf_dir + chmod -R 777 $postgres_conf_dir + cp $postgres_template_conf_file $postgres_conf_dir + chmod 777 $postgres_conf_file } # This is magic for shellspec ut framework. diff --git a/addons/postgresql/scripts/postgres-setup.sh b/addons/postgresql/scripts/postgres-setup.sh index 0dfd5883b..d906e1d93 100755 --- a/addons/postgresql/scripts/postgres-setup.sh +++ b/addons/postgresql/scripts/postgres-setup.sh @@ -1,21 +1,6 @@ #!/bin/bash -# This is magic for shellspec ut framework. "test" is a `test [expression]` well known as a shell command. -# Normally test without [expression] returns false. It means that __() { :; } -# function is defined if this script runs directly. -# -# shellspec overrides the test command and returns true *once*. It means that -# __() function defined internally by shellspec is called. -# -# In other words. If not in test mode, __ is just a comment. If test mode, __ -# is a interception point. -# you should set ut_mode="true" when you want to run the script in shellspec file. -# shellcheck disable=SC2034 -ut_mode="false" -test || __() { - # when running in non-unit test mode, set the options "set -ex". - set -ex; -} +tmp_patroni_yaml="tmp_patroni.yaml" load_common_library() { # the common.sh scripts is mounted to the same path which is defined in the cmpd.spec.scripts @@ -41,9 +26,9 @@ regenerate_spilo_configuration_and_start_postgres() { if [ -f "${RESTORE_DATA_DIR}"/kb_restore.signal ]; then chown -R postgres "${RESTORE_DATA_DIR}" fi - python3 /kb-scripts/generate_patroni_yaml.py tmp_patroni.yaml + python3 /kb-scripts/generate_patroni_yaml.py $tmp_patroni_yaml # SPILO_CONFIGURATION is defined by spilo image - SPILO_CONFIGURATION=$(cat tmp_patroni.yaml) + SPILO_CONFIGURATION=$(cat $tmp_patroni_yaml) export SPILO_CONFIGURATION exec /launch.sh init } diff --git a/addons/postgresql/scripts/switchover.sh b/addons/postgresql/scripts/switchover.sh index fe54b3c3f..017ee2a8d 100644 --- a/addons/postgresql/scripts/switchover.sh +++ b/addons/postgresql/scripts/switchover.sh @@ -1,22 +1,5 @@ #!/bin/bash -# This is magic for shellspec ut framework. "test" is a `test [expression]` well known as a shell command. -# Normally test without [expression] returns false. It means that __() { :; } -# function is defined if this script runs directly. -# -# shellspec overrides the test command and returns true *once*. It means that -# __() function defined internally by shellspec is called. -# -# In other words. If not in test mode, __ is just a comment. If test mode, __ -# is a interception point. -# you should set ut_mode="true" when you want to run the script in shellspec file. -# shellcheck disable=SC2034 -ut_mode="false" -test || __() { - # when running in non-unit test mode, set the options "set -ex". - set -ex; -} - load_common_library() { # the common.sh scripts is mounted to the same path which is defined in the cmpd.spec.scripts common_library_file="/kb-scripts/common.sh" @@ -27,6 +10,7 @@ load_common_library() { switchover_with_candidate() { local current_pod_fqdn=$1 local current_primary_pod_name=$2 + # shellcheck disable=SC2034 local candidate_pod_name=$3 # TODO: check the role in kernel before switchover # shellcheck disable=SC2016 @@ -36,6 +20,7 @@ switchover_with_candidate() { switchover_without_candidate() { local current_pod_fqdn=$1 + # shellcheck disable=SC2034 local current_primary_pod_name=$2 # TODO: check the role in kernel before switchover # shellcheck disable=SC2016 From 68572bcb1e0b41a751a8d3ca0bebee54f4e23868 Mon Sep 17 00:00:00 2001 From: Y-Rookie Date: Wed, 28 Aug 2024 17:40:26 +0800 Subject: [PATCH 09/12] remove etcd-clean --- addons/postgresql/scripts/etcd-clean.sh | 1 - 1 file changed, 1 deletion(-) delete mode 100644 addons/postgresql/scripts/etcd-clean.sh diff --git a/addons/postgresql/scripts/etcd-clean.sh b/addons/postgresql/scripts/etcd-clean.sh deleted file mode 100644 index 39ea72f6c..000000000 --- a/addons/postgresql/scripts/etcd-clean.sh +++ /dev/null @@ -1 +0,0 @@ -# TODO: implement etcd-clean.sh \ No newline at end of file From 1549b2e9f9baba300a8812651ef6a23fc2efd88a Mon Sep 17 00:00:00 2001 From: Y-Rookie Date: Wed, 28 Aug 2024 17:59:15 +0800 Subject: [PATCH 10/12] update repo --- addons/postgresql/values.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/addons/postgresql/values.yaml b/addons/postgresql/values.yaml index edf6e846d..1b529f090 100644 --- a/addons/postgresql/values.yaml +++ b/addons/postgresql/values.yaml @@ -14,7 +14,7 @@ fullnameOverride: "" image: # refer: https://github.com/apecloud-inc/spilo/blob/main/postgres-appliance/Dockerfile # this is the image of postgresql, which is built from Dockerfile of apecloud-inc/spilo forked from zalando/spilo. - registry: apecloud-registry.cn-zhangjiakou.cr.aliyuncs.com + registry: docker.io repository: apecloud/spilo digest: "" tags: @@ -93,7 +93,7 @@ metrics: image: # if the value of metrics.image.registry is not specified using `--set`, it will be set to the value of 'image.registry' by default registry: "" - repository: apecloud/postgres-exporter + repository: prometheuscommunity/postgres-exporter tag: v0.15.0 pullPolicy: IfNotPresent service: @@ -105,7 +105,7 @@ pgbouncer: # refer: https://hub.docker.com/r/bitnami/pgbouncer # if the value of pgbouncer.image.registry is not specified using `--set`, it will be set to the value of 'image.registry' by default registry: "" - repository: apecloud/pgbouncer + repository: bitnami/pgbouncer tag: 1.19.0 pullPolicy: IfNotPresent From 4932186171ecd612371c83cc5c30a5d7ea87339a Mon Sep 17 00:00:00 2001 From: Y-Rookie Date: Thu, 29 Aug 2024 10:55:59 +0800 Subject: [PATCH 11/12] update shortname --- addons/postgresql/templates/componentdefinition-15.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/postgresql/templates/componentdefinition-15.yaml b/addons/postgresql/templates/componentdefinition-15.yaml index d305667d7..ede59bfd4 100644 --- a/addons/postgresql/templates/componentdefinition-15.yaml +++ b/addons/postgresql/templates/componentdefinition-15.yaml @@ -97,7 +97,7 @@ spec: componentVarRef: compDef: {{ include "postgresql.compDefPostgresql15" . }} optional: false - componentShortName: Required + shortName: Required ## the etcd version if postgres patroni use etcd as DCS - name: PATRONI_DCS_ETCD_VERSION value: "3" From e22e68a6a9818c0b0666c0a1285a43cc7454b781 Mon Sep 17 00:00:00 2001 From: Y-Rookie Date: Thu, 29 Aug 2024 17:37:30 +0800 Subject: [PATCH 12/12] modify pg switchover --- addons/postgresql/scripts/switchover.sh | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/addons/postgresql/scripts/switchover.sh b/addons/postgresql/scripts/switchover.sh index 017ee2a8d..2e9dea96a 100644 --- a/addons/postgresql/scripts/switchover.sh +++ b/addons/postgresql/scripts/switchover.sh @@ -1,5 +1,22 @@ #!/bin/bash +# This is magic for shellspec ut framework. "test" is a `test [expression]` well known as a shell command. +# Normally test without [expression] returns false. It means that __() { :; } +# function is defined if this script runs directly. +# +# shellspec overrides the test command and returns true *once*. It means that +# __() function defined internally by shellspec is called. +# +# In other words. If not in test mode, __ is just a comment. If test mode, __ +# is a interception point. +# you should set ut_mode="true" when you want to run the script in shellspec file. +# shellcheck disable=SC2034 +ut_mode="false" +test || __() { + # when running in non-unit test mode, set the options "set -ex". + set -ex; +} + load_common_library() { # the common.sh scripts is mounted to the same path which is defined in the cmpd.spec.scripts common_library_file="/kb-scripts/common.sh" @@ -13,8 +30,7 @@ switchover_with_candidate() { # shellcheck disable=SC2034 local candidate_pod_name=$3 # TODO: check the role in kernel before switchover - # shellcheck disable=SC2016 - curl -s http://$(current_pod_fqdn):8008/switchover -XPOST -d '{"leader":"$(current_primary_pod_name)","candidate":"$(candidate_pod_name)"}' + curl -s "http://${current_pod_fqdn}:8008/switchover" -XPOST -d "{\"leader\":\"${current_primary_pod_name}\",\"candidate\":\"${candidate_pod_name}\"}" # TODO: check switchover result } @@ -23,8 +39,7 @@ switchover_without_candidate() { # shellcheck disable=SC2034 local current_primary_pod_name=$2 # TODO: check the role in kernel before switchover - # shellcheck disable=SC2016 - curl -s http://$(current_pod_fqdn):8008/switchover -XPOST -d '{"leader":"$(current_primary_pod_name)"}' + curl -s "http://${current_pod_fqdn}:8008/switchover" -XPOST -d "{\"leader\":\"${current_primary_pod_name}\"}" # TODO: check switchover result }