forked from opendatahub-io/models-as-a-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-database.sh
More file actions
executable file
Β·178 lines (165 loc) Β· 5.49 KB
/
setup-database.sh
File metadata and controls
executable file
Β·178 lines (165 loc) Β· 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash
#
# Deploy PostgreSQL for MaaS API key storage.
#
# Creates a PostgreSQL Deployment, Service, and the maas-db-config Secret
# containing DB_CONNECTION_URL. This is a POC-grade setup with ephemeral
# storage; for production use AWS RDS, Crunchy Operator, or Azure Database.
#
# Namespace selection:
# - Use NAMESPACE environment variable if set
# - Default: opendatahub (ODH) or redhat-ods-applications (RHOAI)
#
# Environment variables:
# NAMESPACE Target namespace (default: opendatahub)
# POSTGRES_USER Database user (default: maas)
# POSTGRES_DB Database name (default: maas)
# POSTGRES_PASSWORD Database password (default: auto-generated)
#
# Usage:
# ./scripts/setup-database.sh
# NAMESPACE=redhat-ods-applications ./scripts/setup-database.sh
#
# Docker alternative: Replace 'kubectl' with 'oc' if using OpenShift.
#
set -euo pipefail
# Source helpers
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=deployment-helpers.sh
source "${SCRIPT_DIR}/deployment-helpers.sh"
# Default namespace for ODH; use redhat-ods-applications for RHOAI
: "${NAMESPACE:=opendatahub}"
# Ensure namespace exists
if ! kubectl get namespace "$NAMESPACE" >/dev/null 2>&1; then
echo "π¦ Creating namespace '$NAMESPACE'..."
kubectl create namespace "$NAMESPACE"
fi
echo ""
echo "βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
echo "β β οΈ NOT INTENDED FOR PRODUCTION USE β"
echo "β This deploys PostgreSQL with ephemeral storage (emptyDir). β"
echo "β Data WILL be lost on pod restart. β"
echo "β For production, use an external database: β"
echo "β deploy.sh --postgres-connection postgresql://... β"
echo "βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
echo ""
echo "π§ Deploying PostgreSQL for API key storage in namespace '$NAMESPACE'..."
# Check if PostgreSQL already exists
if kubectl get deployment postgres -n "$NAMESPACE" &>/dev/null; then
echo " PostgreSQL already deployed in namespace $NAMESPACE"
echo " Service: postgres:5432"
echo " Secret: maas-db-config (contains DB_CONNECTION_URL)"
exit 0
fi
# PostgreSQL configuration (POC-grade, not for production)
POSTGRES_USER="${POSTGRES_USER:-maas}"
POSTGRES_DB="${POSTGRES_DB:-maas}"
# Generate random password if not provided
if [[ -z "${POSTGRES_PASSWORD:-}" ]]; then
POSTGRES_PASSWORD="$(openssl rand -base64 32 | tr -d '/+=' | cut -c1-32)"
echo " Generated random PostgreSQL password (stored in secret postgres-creds)"
fi
echo " Creating PostgreSQL deployment..."
echo " β οΈ Using POC configuration (ephemeral storage)"
echo ""
# Deploy PostgreSQL resources
kubectl apply -n "$NAMESPACE" -f - <<EOF
apiVersion: v1
kind: Secret
metadata:
name: postgres-creds
labels:
app: postgres
purpose: poc
stringData:
POSTGRES_USER: "${POSTGRES_USER}"
POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}"
POSTGRES_DB: "${POSTGRES_DB}"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres
labels:
app: postgres
purpose: poc
spec:
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: registry.redhat.io/rhel9/postgresql-15:latest
env:
- name: POSTGRESQL_USER
valueFrom:
secretKeyRef:
name: postgres-creds
key: POSTGRES_USER
- name: POSTGRESQL_PASSWORD
valueFrom:
secretKeyRef:
name: postgres-creds
key: POSTGRES_PASSWORD
- name: POSTGRESQL_DATABASE
valueFrom:
secretKeyRef:
name: postgres-creds
key: POSTGRES_DB
ports:
- containerPort: 5432
volumeMounts:
- name: data
mountPath: /var/lib/pgsql/data
resources:
requests:
memory: "256Mi"
cpu: "100m"
limits:
memory: "512Mi"
cpu: "500m"
readinessProbe:
exec:
command: ["/usr/libexec/check-container"]
initialDelaySeconds: 5
periodSeconds: 5
volumes:
- name: data
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
name: postgres
labels:
app: postgres
purpose: poc
spec:
selector:
app: postgres
ports:
- port: 5432
targetPort: 5432
EOF
# Create the maas-db-config secret used by maas-api
DB_CONNECTION_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}?sslmode=disable"
create_maas_db_config_secret "$NAMESPACE" "$DB_CONNECTION_URL"
echo " Waiting for PostgreSQL to be ready..."
if ! kubectl wait -n "$NAMESPACE" --for=condition=available deployment/postgres --timeout=120s; then
echo "β PostgreSQL deployment failed to become ready" >&2
exit 1
fi
echo ""
echo "β
PostgreSQL deployed successfully"
echo " Database: $POSTGRES_DB"
echo " User: $POSTGRES_USER"
echo " Secret: maas-db-config (contains DB_CONNECTION_URL)"
echo ""
echo " β οΈ For production, use AWS RDS, Crunchy Operator, or Azure Database"
echo " Note: Schema migrations run automatically when maas-api starts"