mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-06 07:03:45 +09:00
An operand image built on another Debian release ships another glibc and
ICU. An index ordered by a collation of either library is then ordered by
rules the server no longer applies: lookups through it miss rows, and a
unique index stops seeing duplicates. PostgreSQL only logs a warning.
The init Job compares the library version each collation was created with
against the one the server reports, rebuilds the indexes that depend on a
collation whose version changed, and refreshes the recorded versions once
every rebuild in the database succeeded. A database on the C or POSIX
collation with no explicitly collated index, which is what the chart
creates by default, has nothing to rebuild and is not delayed.
The pass has to hold on a hook that starts while the instances are still
rolling, runs as the superuser in databases tenants own, and is retried
after a failure:
- It runs after the passes that manage roles and databases, so that a
rebuild that fails costs none of them, and it waits for the server to
report the PostgreSQL version and Debian release of its own image when
an index depends on a versioned collation. The rebuild counter of the
package is left out of that comparison, because a tag is rebuilt in
place.
- Every session pins search_path to pg_catalog, so that a function or an
operator a tenant defined in public cannot stand in for a built-in.
- A retry continues where the last run stopped. REINDEX CONCURRENTLY gives
an index a new OID, so the highest OID seen when a repair begins marks
what is left; an index rebuilt in place keeps its OID and is told apart
by its storage file. Both live in a setting of the database.
- A rebuild that cannot finish is not started for ever: after two
attempts the index is left to an operator, the pass says how to clear
it, and the release stays failed until then. Lock waits and in-place
rebuilds are bounded by what is left of the Job.
- A collation that a range partition key depends on keeps its recorded
version, because rebuilding an index moves no row between partitions.
PostgreSQL records a collation for an index only where it orders a key,
so the collations of the columns an index reads in its expressions and
predicate count as well. Before PostgreSQL 15 the server records no
version for a database's default collation, and the libc collation of the
same locale stands in for it.
The e2e suite creates a collation with a stale version and checks that an
upgrade rebuilds a unique index and an exclusion constraint's index on it,
refreshes the version and removes the repair setting.
On release-1.6 the init Job had no deadline. It gets activeDeadlineSeconds,
backoffLimit 0 and a JOB_DEADLINE env that the pass bounds its waits by,
and the e2e lib gains HELMRELEASE_HISTORY_JSONPATH, which the new step
reads and main defines there.
(cherry picked from commit a10e449a56)
Assisted-By: LLM
Signed-off-by: Andrey Kolkov <androndo@gmail.com>
334 lines
13 KiB
YAML
334 lines
13 KiB
YAML
# Chainsaw port of hack/e2e-apps/postgres.bats ("Create DB PostgreSQL").
|
|
#
|
|
# Every `timeout N sh -ec "until kubectl get ..."` + `kubectl wait` pair from
|
|
# the BATS test collapses into a single assert: chainsaw polls until the
|
|
# resource exists AND matches, or the timeout expires. Failure output is a
|
|
# structured diff of the assertion tree against the live object.
|
|
apiVersion: chainsaw.kyverno.io/v1alpha1
|
|
kind: Test
|
|
metadata:
|
|
name: postgres
|
|
spec:
|
|
# Diagnostics collected automatically when any step fails. The BATS suite
|
|
# only does this in harbor.bats, by hand.
|
|
catch:
|
|
- events: {}
|
|
- describe:
|
|
apiVersion: helm.toolkit.fluxcd.io/v2
|
|
kind: HelmRelease
|
|
name: postgres-test
|
|
- podLogs:
|
|
selector: cnpg.io/cluster=postgres-test
|
|
tail: 50
|
|
steps:
|
|
- name: create-postgres
|
|
try:
|
|
- apply:
|
|
file: postgres.yaml
|
|
|
|
- name: wait-helmrelease-ready
|
|
try:
|
|
- assert:
|
|
timeout: 6m
|
|
resource:
|
|
apiVersion: helm.toolkit.fluxcd.io/v2
|
|
kind: HelmRelease
|
|
metadata:
|
|
name: postgres-test
|
|
status:
|
|
# JMESPath filter: condition order in the array does not matter
|
|
(conditions[?type == 'Ready']):
|
|
- status: "True"
|
|
|
|
- name: wait-init-job-complete
|
|
try:
|
|
- assert:
|
|
timeout: 2m
|
|
resource:
|
|
apiVersion: batch/v1
|
|
kind: Job
|
|
metadata:
|
|
name: postgres-test-init-job
|
|
status:
|
|
(conditions[?type == 'Complete']):
|
|
- status: "True"
|
|
|
|
- name: verify-services-and-endpoints
|
|
try:
|
|
# Replaces three `until kubectl get svc ... | grep -q 5432` loops
|
|
# TODO(chainsaw>0.2.15): the (ports[0].port) / (ports[*].port) projection form in
|
|
# these Service asserts is a v0.2.15 workaround for mis-evaluated number-literal
|
|
# filters (ports[?port==`5432`]); (ports[0]) also checks only the first port.
|
|
# Restore the filter form on a chainsaw bump. Same workaround lives in
|
|
# kafka/mariadb/harbor — see "Chainsaw v0.2.15 gotchas" in docs/agents/e2e-testing.md.
|
|
- assert:
|
|
resource:
|
|
apiVersion: v1
|
|
kind: Service
|
|
metadata:
|
|
name: postgres-test-r
|
|
spec:
|
|
(ports[0].port): 5432
|
|
- assert:
|
|
resource:
|
|
apiVersion: v1
|
|
kind: Service
|
|
metadata:
|
|
name: postgres-test-ro
|
|
spec:
|
|
(ports[0].port): 5432
|
|
- assert:
|
|
resource:
|
|
apiVersion: v1
|
|
kind: Service
|
|
metadata:
|
|
name: postgres-test-rw
|
|
spec:
|
|
(ports[0].port): 5432
|
|
# Replaces `until kubectl get endpoints ... | grep -q '[0-9]'` loops.
|
|
# The read-only endpoint is skipped, same as in the BATS test.
|
|
- assert:
|
|
timeout: 3m
|
|
resource:
|
|
apiVersion: v1
|
|
kind: Endpoints
|
|
metadata:
|
|
name: postgres-test-r
|
|
(subsets[0].addresses[0].ip != null): true
|
|
- assert:
|
|
timeout: 3m
|
|
resource:
|
|
apiVersion: v1
|
|
kind: Endpoints
|
|
metadata:
|
|
name: postgres-test-rw
|
|
(subsets[0].addresses[0].ip != null): true
|
|
# Functional probe — the BATS test never actually talks to the database.
|
|
- script:
|
|
timeout: 2m
|
|
content: |
|
|
set -eu
|
|
pod=$(kubectl -n "$NAMESPACE" get pod \
|
|
-l cnpg.io/cluster=postgres-test,cnpg.io/instanceRole=primary \
|
|
-o name | head -n1)
|
|
kubectl -n "$NAMESPACE" exec "$pod" -c postgres -- \
|
|
psql -U postgres -tAc 'SELECT 1'
|
|
check:
|
|
($error == null): true
|
|
(contains($stdout, '1')): true
|
|
# The init-job is operator-created (not applied by chainsaw), so it is
|
|
# not part of automatic cleanup — delete it explicitly, as BATS does.
|
|
finally:
|
|
- delete:
|
|
ref:
|
|
apiVersion: batch/v1
|
|
kind: Job
|
|
name: postgres-test-init-job
|
|
# The Postgres CR itself was applied by chainsaw and is deleted (and waited
|
|
# on) automatically during cleanup — no explicit teardown step needed.
|
|
|
|
# The init Job rebuilds an index whose collation changed library version.
|
|
# The pass is SQL run by a Helm hook, so only a real server can say whether
|
|
# it works. A collation created with an explicit version reads as stale
|
|
# without swapping the operand image, which makes one more upgrade of the
|
|
# release enough to run the pass over it.
|
|
- name: rebuild-index-after-collation-version-change
|
|
try:
|
|
- script:
|
|
timeout: 2m
|
|
content: |
|
|
set -eu
|
|
pod=$(kubectl -n "$NAMESPACE" get pod \
|
|
-l cnpg.io/cluster=postgres-test,cnpg.io/instanceRole=primary \
|
|
-o name | head -n1)
|
|
kubectl -n "$NAMESPACE" exec -i "$pod" -c postgres -- \
|
|
psql -U postgres -d testdb -v ON_ERROR_STOP=1 <<'EOF'
|
|
CREATE COLLATION stale_icu (provider = icu, locale = 'en', version = '0');
|
|
CREATE TABLE collation_probe (s text COLLATE stale_icu);
|
|
INSERT INTO collation_probe SELECT 'v' || g FROM generate_series(1, 1000) g;
|
|
CREATE UNIQUE INDEX collation_probe_s ON collation_probe (s);
|
|
CREATE EXTENSION IF NOT EXISTS btree_gist;
|
|
CREATE TABLE collation_probe_excl (s text COLLATE stale_icu, EXCLUDE USING gist (s WITH =));
|
|
INSERT INTO collation_probe_excl SELECT 'k' || g FROM generate_series(1, 200) g;
|
|
CREATE TABLE collation_probe_before AS
|
|
SELECT 'collation_probe_s'::regclass::oid AS index,
|
|
(SELECT relfilenode FROM pg_class WHERE oid = 'collation_probe_excl_s_excl'::regclass) AS in_place_file;
|
|
EOF
|
|
check:
|
|
($error == null): true
|
|
# A declared user is the smallest change that makes Helm upgrade the
|
|
# release, and so run the hook, without restarting PostgreSQL.
|
|
- patch:
|
|
resource:
|
|
apiVersion: apps.cozystack.io/v1alpha1
|
|
kind: Postgres
|
|
metadata:
|
|
name: test
|
|
spec:
|
|
users:
|
|
collationprobe: {}
|
|
# 11m: above the 600s upgrade timeout cozystack-api gives this release.
|
|
# The step above that waited for this Job deleted it, so the Job found
|
|
# here is the one the upgrade created.
|
|
- assert:
|
|
timeout: 11m
|
|
resource:
|
|
apiVersion: batch/v1
|
|
kind: Job
|
|
metadata:
|
|
name: postgres-test-init-job
|
|
status:
|
|
(conditions[?type == 'Complete']):
|
|
- status: "True"
|
|
- script:
|
|
timeout: 2m
|
|
content: |
|
|
set -eu
|
|
pod=$(kubectl -n "$NAMESPACE" get pod \
|
|
-l cnpg.io/cluster=postgres-test,cnpg.io/instanceRole=primary \
|
|
-o name | head -n1)
|
|
q() {
|
|
kubectl -n "$NAMESPACE" exec "$pod" -c postgres -- \
|
|
psql -U postgres -d testdb -v ON_ERROR_STOP=1 -tAc "$1"
|
|
}
|
|
refreshed=$(q "SELECT collversion = pg_collation_actual_version(oid) FROM pg_collation WHERE collname = 'stale_icu'")
|
|
if [ "$refreshed" != t ]; then
|
|
echo "the init Job left the collation version stale" >&2
|
|
exit 1
|
|
fi
|
|
# REINDEX CONCURRENTLY gives the index a new OID, so an unchanged
|
|
# one means the version was refreshed over an index nobody rebuilt.
|
|
rebuilt=$(q "SELECT 'collation_probe_s'::regclass::oid <> index FROM collation_probe_before")
|
|
if [ "$rebuilt" != t ]; then
|
|
echo "the init Job refreshed the collation version without rebuilding the index" >&2
|
|
exit 1
|
|
fi
|
|
q "CREATE EXTENSION IF NOT EXISTS amcheck"
|
|
q "SELECT bt_index_check('collation_probe_s', true)"
|
|
# An exclusion constraint's index is rebuilt in place: it keeps its
|
|
# OID and gets a new storage file.
|
|
rebuilt_in_place=$(q "SELECT c.relfilenode <> b.in_place_file FROM pg_class c, collation_probe_before b WHERE c.oid = 'collation_probe_excl_s_excl'::regclass")
|
|
if [ "$rebuilt_in_place" != t ]; then
|
|
echo "the init Job did not rebuild the exclusion constraint's index" >&2
|
|
exit 1
|
|
fi
|
|
# The pass keeps its progress in a setting of the database and
|
|
# removes it once nothing is left to repair.
|
|
progress=$(q "SELECT count(*) FROM pg_db_role_setting s JOIN pg_database d ON d.oid = s.setdatabase, unnest(s.setconfig) AS c WHERE d.datname = current_database() AND c LIKE 'cozystack.collation_repair=%'")
|
|
if [ "$progress" != 0 ]; then
|
|
echo "the init Job left its repair setting on a database it finished repairing" >&2
|
|
exit 1
|
|
fi
|
|
leftovers=$(q "SELECT count(*) FROM pg_index WHERE NOT indisvalid")
|
|
if [ "$leftovers" != 0 ]; then
|
|
echo "the rebuild left $leftovers invalid index(es) behind" >&2
|
|
exit 1
|
|
fi
|
|
# The Job above also completes on a retry, so a hook that failed
|
|
# first would pass unseen without this.
|
|
cd ../../..
|
|
. hack/e2e-chainsaw/_lib/remediation-guard.sh
|
|
statuses=$(kubectl -n "$NAMESPACE" get helmrelease postgres-test \
|
|
-o jsonpath="$HELMRELEASE_HISTORY_JSONPATH")
|
|
printf 'HelmRelease postgres-test history statuses:\n%s\n' "${statuses:-<empty>}"
|
|
if helmrelease_has_remediation_cycle "$statuses"; then
|
|
echo "HelmRelease postgres-test failed an action while the index was rebuilt" >&2
|
|
exit 1
|
|
fi
|
|
check:
|
|
($error == null): true
|
|
finally:
|
|
- delete:
|
|
ref:
|
|
apiVersion: batch/v1
|
|
kind: Job
|
|
name: postgres-test-init-job
|
|
---
|
|
# Full barman-cloud backup -> to-copy RestoreJob round-trip, driving the
|
|
# validated example scripts (examples/backups/postgres) as the harness so the
|
|
# test and the documented flow can never drift. run-all.sh runs Bucket ->
|
|
# source Postgres + sentinel row -> CNPG strategy + BackupClass -> ad-hoc
|
|
# BackupJob (waits Succeeded) -> empty target Postgres + to-copy RestoreJob
|
|
# (waits Succeeded) and asserts the sentinel round-tripped through S3 into the
|
|
# restored copy — the in-cluster witness that a barman-cloud plugin backup is
|
|
# both taken and restorable end to end.
|
|
#
|
|
# Runs in tenant-root, not the default tenant-test: the S3 client here is the
|
|
# barman-cloud sidecar INSIDE the Postgres Pod, and the e2e tenant is created
|
|
# isolated (hack/e2e-install-cozystack.bats) — its Cilium egress allowlist
|
|
# blocks a tenant-test Pod from reaching tenant-root's seaweedfs, while a
|
|
# tenant-root Pod reaches it over same-tenant egress. The harness targets the
|
|
# in-cluster endpoint (S3_ENDPOINT below) instead of the BucketInfo external
|
|
# ingress URL, which in CI is an unroutable placeholder, and trusts its
|
|
# self-signed cert by copying the seaweedfs CA secret (seaweedfs-ca-cert,
|
|
# auto-discovered from the cert-manager Certificate) into the per-app
|
|
# endpointCA Secret. (The bucket suite's mc checks lean on the same
|
|
# tenant-root seaweedfs, just from the test runner via port-forward.)
|
|
apiVersion: chainsaw.kyverno.io/v1alpha1
|
|
kind: Test
|
|
metadata:
|
|
name: postgres-2-backup-roundtrip
|
|
spec:
|
|
namespace: tenant-root
|
|
skipDelete: true
|
|
catch:
|
|
- events: {}
|
|
- describe:
|
|
apiVersion: helm.toolkit.fluxcd.io/v2
|
|
kind: HelmRelease
|
|
name: postgres-pg-src
|
|
- describe:
|
|
apiVersion: postgresql.cnpg.io/v1
|
|
kind: Cluster
|
|
name: postgres-pg-src
|
|
- describe:
|
|
apiVersion: barmancloud.cnpg.io/v1
|
|
kind: ObjectStore
|
|
- describe:
|
|
apiVersion: backups.cozystack.io/v1alpha1
|
|
kind: BackupJob
|
|
- describe:
|
|
apiVersion: backups.cozystack.io/v1alpha1
|
|
kind: RestoreJob
|
|
# The barman-cloud backup/WAL work runs in the cnpg Cluster Pods' sidecar; a
|
|
# failed upload or TLS handshake leaves its error only in that Pod's log.
|
|
- podLogs:
|
|
selector: cnpg.io/cluster=postgres-pg-src
|
|
tail: 200
|
|
steps:
|
|
- name: run
|
|
try:
|
|
# The 50m budget is the whole documented flow: bucket provisioning, a
|
|
# 2-replica source Postgres, the plugin base backup to S3, an empty target
|
|
# app, and TWO sequential purge + re-bootstrap restores - the latest-point
|
|
# to-copy restore (step 40) and the in-place restore (step 50, the
|
|
# regression guard for the archive-vs-recovery serverName collision)
|
|
# (convention 6: justified in-line). The old 25m budget covered a single
|
|
# restore; the in-place restore re-bootstraps the source on top of it, and
|
|
# its own fail-fast waits allow 20m for the RestoreJob plus 10m for the
|
|
# cluster to return healthy. run-all.sh fails fast on terminal
|
|
# BackupJob/RestoreJob phases and stalled HelmReleases rather than polling
|
|
# out the budget.
|
|
- script:
|
|
timeout: 50m
|
|
content: |
|
|
set -eu
|
|
cd ../../..
|
|
[ -x examples/backups/postgres/run-all.sh ] || { echo "postgres backup example scripts not found at examples/backups/postgres" >&2; exit 1; }
|
|
NAMESPACE="$NAMESPACE" S3_ENDPOINT="https://seaweedfs-s3.tenant-root:8333" \
|
|
examples/backups/postgres/run-all.sh
|
|
check:
|
|
($error == null): true
|
|
finally:
|
|
# Resumes the driver-suspended HelmReleases before deleting the RestoreJob
|
|
# so the apps are not stranded, then removes every resource the harness
|
|
# created (chainsaw did not apply them, so they are not part of automatic
|
|
# cleanup).
|
|
- description: backup-flow cleanup
|
|
script:
|
|
timeout: 8m
|
|
content: |
|
|
set -eu
|
|
cd ../../..
|
|
NAMESPACE="$NAMESPACE" examples/backups/postgres/cleanup.sh
|