jerryshao commented on code in PR #14941:
URL: https://github.com/apache/gravitino/pull/14941#discussion_r4562787009


##########
dev/charts/gravitino/templates/deployment.yaml:
##########
@@ -125,18 +125,52 @@ spec:
             - mountPath: /scripts
               name: scripts-emptydir
       {{- end }}
-      {{- if .Values.postgresql.enabled }}
+      {{- if eq (include "gravitino.postgresqlSchemaInitEnabled" . | trim) 
"true" }}
+        {{- $_ := include "gravitino.postgresqlInitSslValidate" . }}
+        {{- $pgInitHost := include "gravitino.postgresqlInitHost" . | trim }}
+        {{- $pgInitSecret := include "gravitino.postgresqlInitSecretName" . | 
trim }}
+        {{- $pgInitSecretKey := 
.Values.postgresql.init.existingSecretPasswordKey | default "password" }}
+        {{- $pgInitPort := include "gravitino.postgresqlInitPort" . | trim }}
+        {{- $pgInitSslMode := include "gravitino.postgresqlInitSslMode" . | 
trim }}
+        {{- $pgInitCurrentSchema := include 
"gravitino.postgresqlInitCurrentSchema" . | trim }}
+        {{- $pgInitSslCaSecret := include 
"gravitino.postgresqlInitSslCaSecretName" . | trim }}
+        {{- $pgInitWaitTimeout := .Values.postgresql.init.waitTimeoutSeconds | 
default 300 }}
         - name: init-postgresql
           image: {{ include "init-postgresql.image" . }}
           imagePullPolicy: "{{ .Values.image.pullPolicy }}"
           command:
             - /bin/bash
             - -c
             - |
-              while ! PGPASSWORD=$POSTGRES_PASSWORD psql -h {{ .Release.Name 
}}-postgresql -U $POSTGRES_USER -c "SELECT 1"; do
+              set -e
+              timeout={{ $pgInitWaitTimeout }}
+              case "$timeout" in
+                ''|*[!0-9]*) echo "ERROR: waitTimeoutSeconds must be a 
positive integer" >&2; exit 1 ;;
+              esac
+              if [ "$timeout" -lt 1 ]; then
+                echo "ERROR: waitTimeoutSeconds must be a positive integer" >&2
+                exit 1
+              fi
+              deadline=$((SECONDS + timeout))
+              while true; do
+                set +e
+                err=$(PGPASSWORD=$POSTGRES_PASSWORD psql -h {{ $pgInitHost }} 
-U $POSTGRES_USER -d $GRAVITINO_DB -c "SELECT 1" 2>&1)
+                rc=$?
+                set -e
+                if [ "$rc" -eq 0 ]; then
+                  break
+                fi
+                if [ "$SECONDS" -ge "$deadline" ]; then
+                  echo "ERROR: timed out after ${timeout}s waiting for 
PostgreSQL at {{ $pgInitHost }}:$PGPORT/$GRAVITINO_DB" >&2
+                  echo "$err" >&2
+                  exit 1
+                fi
                 echo "Waiting for PostgreSQL to be ready..."
                 sleep 2
               done
+              if [ -n "${PG_CURRENT_SCHEMA:-}" ]; then
+                PGPASSWORD=$POSTGRES_PASSWORD psql -h {{ $pgInitHost }} -U 
$POSTGRES_USER -d $GRAVITINO_DB -v ON_ERROR_STOP=1 -c "CREATE SCHEMA IF NOT 
EXISTS \"${PG_CURRENT_SCHEMA}\""

Review Comment:
   PostgreSQL checks CREATE privilege on the database before it evaluates IF 
NOT EXISTS, so with ON_ERROR_STOP this fails the init container when the app 
user lacks CREATE on the database, even if the schema already exists. Check 
pg_namespace first and only run CREATE SCHEMA when the schema is missing, or 
make schema creation a separate opt-in flag.



##########
dev/charts/gravitino/templates/deployment.yaml:
##########
@@ -125,18 +125,52 @@ spec:
             - mountPath: /scripts
               name: scripts-emptydir
       {{- end }}
-      {{- if .Values.postgresql.enabled }}
+      {{- if eq (include "gravitino.postgresqlSchemaInitEnabled" . | trim) 
"true" }}
+        {{- $_ := include "gravitino.postgresqlInitSslValidate" . }}
+        {{- $pgInitHost := include "gravitino.postgresqlInitHost" . | trim }}
+        {{- $pgInitSecret := include "gravitino.postgresqlInitSecretName" . | 
trim }}
+        {{- $pgInitSecretKey := 
.Values.postgresql.init.existingSecretPasswordKey | default "password" }}
+        {{- $pgInitPort := include "gravitino.postgresqlInitPort" . | trim }}
+        {{- $pgInitSslMode := include "gravitino.postgresqlInitSslMode" . | 
trim }}
+        {{- $pgInitCurrentSchema := include 
"gravitino.postgresqlInitCurrentSchema" . | trim }}
+        {{- $pgInitSslCaSecret := include 
"gravitino.postgresqlInitSslCaSecretName" . | trim }}
+        {{- $pgInitWaitTimeout := .Values.postgresql.init.waitTimeoutSeconds | 
default 300 }}
         - name: init-postgresql
           image: {{ include "init-postgresql.image" . }}
           imagePullPolicy: "{{ .Values.image.pullPolicy }}"
           command:
             - /bin/bash
             - -c
             - |
-              while ! PGPASSWORD=$POSTGRES_PASSWORD psql -h {{ .Release.Name 
}}-postgresql -U $POSTGRES_USER -c "SELECT 1"; do
+              set -e
+              timeout={{ $pgInitWaitTimeout }}
+              case "$timeout" in
+                ''|*[!0-9]*) echo "ERROR: waitTimeoutSeconds must be a 
positive integer" >&2; exit 1 ;;
+              esac
+              if [ "$timeout" -lt 1 ]; then
+                echo "ERROR: waitTimeoutSeconds must be a positive integer" >&2
+                exit 1
+              fi
+              deadline=$((SECONDS + timeout))
+              while true; do
+                set +e
+                err=$(PGPASSWORD=$POSTGRES_PASSWORD psql -h {{ $pgInitHost }} 
-U $POSTGRES_USER -d $GRAVITINO_DB -c "SELECT 1" 2>&1)

Review Comment:
   psql has no connect timeout by default, so one attempt against a firewalled 
host can block for minutes and overshoot waitTimeoutSeconds. Set 
PGCONNECT_TIMEOUT (for example 5) in the init container env so the deadline is 
enforced.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to