Skip to content

Commit

Permalink
Add initContainer to wait for Postgres to be ready (#95)
Browse files Browse the repository at this point in the history
* Add initContainer to wait for Postgres to be ready

- Adds an initContainer definition that waits until a given Postgres
  server is ready to accept connections using `pg_isready`
- Uses this initContainer in the Prefect migrations Job and the Prefect
  Server Deployment

This ensures that workloads depending on the database first check to see
if the database is ready. This should help avoid crash loop backoffs and
can, in certain cases, improve overall spin-up time.

Related to https://linear.app/prefect/issue/PLA-358/optimize-the-time-it-takes-for-the-prefect-operator-to-create-a-new

* Use smaller alpine flavor of postgresql image
  • Loading branch information
mitchnielsen authored Sep 26, 2024
1 parent 5f3d8b6 commit 46cd7e3
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions internal/controller/prefectserver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,9 @@ func (r *PrefectServerReconciler) postgresDeploymentSpec(server *prefectiov1.Pre
Labels: server.ServerLabels(),
},
Spec: corev1.PodSpec{
InitContainers: []corev1.Container{
r.initContainerWaitForPostgres(server),
},
Containers: []corev1.Container{
{
Name: "prefect-server",
Expand Down Expand Up @@ -526,6 +529,9 @@ func (r *PrefectServerReconciler) postgresMigrationJob(server *prefectiov1.Prefe
Labels: server.ServerLabels(),
},
Spec: corev1.PodSpec{
InitContainers: []corev1.Container{
r.initContainerWaitForPostgres(server),
},
Containers: []corev1.Container{
{
Name: "prefect-server-migration",
Expand Down Expand Up @@ -581,6 +587,23 @@ func (r *PrefectServerReconciler) prefectServerService(server *prefectiov1.Prefe
return service
}

func (r *PrefectServerReconciler) initContainerWaitForPostgres(server *prefectiov1.PrefectServer) corev1.Container {
return corev1.Container{
Name: "wait-for-database",
Image: "postgres:16-alpine",
ImagePullPolicy: corev1.PullIfNotPresent,
Command: []string{
"/bin/sh",
"-c",
"until pg_isready -h $PREFECT_API_DATABASE_HOST -U $PREFECT_API_DATABASE_USER; do echo 'Waiting for PostgreSQL...'; sleep 2; done;",
},
Env: []corev1.EnvVar{
server.Spec.Postgres.HostEnvVar(),
server.Spec.Postgres.UserEnvVar(),
},
}
}

// SetupWithManager sets up the controller with the Manager.
func (r *PrefectServerReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
Expand Down

0 comments on commit 46cd7e3

Please sign in to comment.