Skip to content

Commit 38e77d9

Browse files
committed
refactor: resolution of PostgreSQL image to avoid permission issues
When using the image resolution within template, it seems that it needs to create some temporary objects in the openshift namespace. To avoid the need for setting up writing permissions, the image is now resolved just by reading the image from the API and passing it down to the template. I have also simplified the template a bit and fixed database migration job behavior - indentation issues, time to live, removal of finished jobs.
1 parent 96b4534 commit 38e77d9

File tree

3 files changed

+39
-63
lines changed

3 files changed

+39
-63
lines changed

ansible/roles/project_setup/tasks/main.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,20 @@
6161
tls.crt: "{{ pullsar_sa_cert_crt | b64encode }}"
6262
tls.key: "{{ pullsar_sa_cert_key | b64encode }}"
6363

64+
- name: "Find the full image name for the specified PostgreSQL version"
65+
kubernetes.core.k8s_info:
66+
api_version: image.openshift.io/v1
67+
kind: ImageStreamTag
68+
name: "postgresql:{{ postgres_version }}"
69+
namespace: openshift
70+
register: postgres_imagestream_tag
71+
6472
- name: "Process the official OpenShift PostgreSQL template"
6573
community.okd.openshift_process:
6674
src: "../templates/postgresql-persistent.yml"
6775
namespace_target: "{{ project_namespace }}"
6876
parameters:
77+
POSTGRESQL_IMAGE: "{{ postgres_imagestream_tag.resources[0].image.dockerImageReference }}"
6978
POSTGRESQL_USER: "{{ postgres_credentials.DB_USER }}"
7079
POSTGRESQL_PASSWORD: "{{ postgres_credentials.DB_PASSWORD }}"
7180
POSTGRESQL_DATABASE: "{{ postgres_credentials.DB_NAME }}"

ansible/templates/postgresql-migration-job.yml

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ metadata:
44
generateName: "postgresql-migration-job-"
55
namespace: "{{ project_namespace }}"
66
spec:
7+
backoffLimit: 4
8+
activeDeadlineSeconds: 900
9+
ttlSecondsAfterFinished: 3600
710
template:
811
spec:
912
containers:
@@ -14,36 +17,35 @@ spec:
1417
- "-c"
1518
- |
1619
echo "Waiting for database to be available..."
17-
until psql -h {{ postgres_credentials.DB_HOST }} -c '\q'; do
20+
until psql -h {{ postgres_credentials.DB_HOST }} -U {{ postgres_credentials.DB_USER }} -c '\q'; do
1821
>&2 echo "Postgres is unavailable - sleeping"
1922
sleep 2
2023
done
2124
2225
echo "Database is available. Running migration script..."
23-
psql -h {{ postgres_credentials.DB_HOST }} -f /migrations/V1__initial_setup.sql
26+
psql -h {{ postgres_credentials.DB_HOST }} -U {{ postgres_credentials.DB_USER }} -f /migrations/V1__initial_setup.sql
2427
echo "Migration complete."
25-
env:
26-
- name: PGPASSWORD
27-
valueFrom:
28-
secretKeyRef:
29-
name: postgres-credentials
30-
key: DB_PASSWORD
31-
- name: PGUSER
32-
valueFrom:
33-
secretKeyRef:
34-
name: postgres-credentials
35-
key: DB_USER
36-
- name: PGDATABASE
37-
valueFrom:
38-
secretKeyRef:
39-
name: postgres-credentials
40-
key: DB_NAME
41-
volumeMounts:
42-
- name: migration-script
43-
mountPath: /migrations
28+
env:
29+
- name: PGPASSWORD
30+
valueFrom:
31+
secretKeyRef:
32+
name: postgres-credentials
33+
key: DB_PASSWORD
34+
- name: PGUSER
35+
valueFrom:
36+
secretKeyRef:
37+
name: postgres-credentials
38+
key: DB_USER
39+
- name: PGDATABASE
40+
valueFrom:
41+
secretKeyRef:
42+
name: postgres-credentials
43+
key: DB_NAME
44+
volumeMounts:
45+
- name: migration-script
46+
mountPath: /migrations
4447
volumes:
4548
- name: migration-script
4649
configMap:
4750
name: postgresql-migration-script
4851
restartPolicy: Never
49-
backoffLimit: 4

ansible/templates/postgresql-persistent.yml

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,11 @@ metadata:
2828
openshift.io/support-url: https://access.redhat.com
2929
samples.operator.openshift.io/version: 4.18.21
3030
tags: database,postgresql
31-
creationTimestamp: "2021-07-27T18:39:32Z"
32-
labels:
33-
samples.operator.openshift.io/managed: "true"
3431
name: postgresql-persistent
35-
namespace: openshift
36-
resourceVersion: "2673535493"
37-
uid: 8dc9aa48-0b02-43a1-bd14-dd09b71f3ab6
3832
objects:
3933
- apiVersion: v1
4034
kind: Secret
4135
metadata:
42-
annotations:
43-
template.openshift.io/expose-database_name: '{.data[''database-name'']}'
44-
template.openshift.io/expose-password: '{.data[''database-password'']}'
45-
template.openshift.io/expose-username: '{.data[''database-user'']}'
4636
name: ${DATABASE_SERVICE_NAME}
4737
stringData:
4838
database-name: ${POSTGRESQL_DATABASE}
@@ -51,22 +41,14 @@ objects:
5141
- apiVersion: v1
5242
kind: Service
5343
metadata:
54-
annotations:
55-
template.openshift.io/expose-uri: postgres://{.spec.clusterIP}:{.spec.ports[?(.name=="postgresql")].port}
5644
name: ${DATABASE_SERVICE_NAME}
5745
spec:
5846
ports:
5947
- name: postgresql
60-
nodePort: 0
6148
port: 5432
62-
protocol: TCP
6349
targetPort: 5432
6450
selector:
6551
name: ${DATABASE_SERVICE_NAME}
66-
sessionAffinity: None
67-
type: ClusterIP
68-
status:
69-
loadBalancer: {}
7052
- apiVersion: v1
7153
kind: PersistentVolumeClaim
7254
metadata:
@@ -80,8 +62,6 @@ objects:
8062
- apiVersion: apps.openshift.io/v1
8163
kind: DeploymentConfig
8264
metadata:
83-
annotations:
84-
template.alpha.openshift.io/wait-for-ready: "true"
8565
name: ${DATABASE_SERVICE_NAME}
8666
spec:
8767
replicas: 1
@@ -95,8 +75,7 @@ objects:
9575
name: ${DATABASE_SERVICE_NAME}
9676
spec:
9777
containers:
98-
- capabilities: {}
99-
env:
78+
- env:
10079
- name: POSTGRESQL_USER
10180
valueFrom:
10281
secretKeyRef:
@@ -112,8 +91,7 @@ objects:
11291
secretKeyRef:
11392
key: database-name
11493
name: ${DATABASE_SERVICE_NAME}
115-
image: ' '
116-
imagePullPolicy: IfNotPresent
94+
image: '${POSTGRESQL_IMAGE}'
11795
livenessProbe:
11896
exec:
11997
command:
@@ -134,33 +112,20 @@ objects:
134112
resources:
135113
limits:
136114
memory: ${MEMORY_LIMIT}
137-
securityContext:
138-
capabilities: {}
139-
privileged: false
140-
terminationMessagePath: /dev/termination-log
141115
volumeMounts:
142116
- mountPath: /var/lib/pgsql/data
143117
name: ${DATABASE_SERVICE_NAME}-data
144-
dnsPolicy: ClusterFirst
145-
restartPolicy: Always
146118
volumes:
147119
- name: ${DATABASE_SERVICE_NAME}-data
148120
persistentVolumeClaim:
149121
claimName: ${DATABASE_SERVICE_NAME}
150122
triggers:
151-
- imageChangeParams:
152-
automatic: true
153-
containerNames:
154-
- postgresql
155-
from:
156-
kind: ImageStreamTag
157-
name: postgresql:${POSTGRESQL_VERSION}
158-
namespace: ${NAMESPACE}
159-
lastTriggeredImage: ""
160-
type: ImageChange
161123
- type: ConfigChange
162-
status: {}
163124
parameters:
125+
- description: The full pull spec for the PostgreSQL image, resolved from the ImageStream.
126+
displayName: PostgreSQL Image Name
127+
name: POSTGRESQL_IMAGE
128+
required: true
164129
- description: Maximum amount of memory the container can use.
165130
displayName: Memory Limit
166131
name: MEMORY_LIMIT

0 commit comments

Comments
 (0)