Skip to content

fix: stop retrying instance-status HTTP errors - #1

Closed
kabbydriver wants to merge 81 commits into
mainfrom
dev/11628
Closed

kabbydriver wants to merge 81 commits into
mainfrom
dev/11628

Conversation

@kabbydriver

@kabbydriver kabbydriver commented Oct 14, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fix the errors.As destination from StatusError to *StatusError, matching the error returned by rawInstanceStatusRequest. Non-200 responses now stop after one request and preserve the status code/body; transient transport errors still retry.

Fixes cloudnative-pg#11628

AI disclosure: OpenAI Codex assisted with investigation, code, tests, validation, and this description.

Background

A three-instance CNPG 1.30.1 rolling restart showed a 3.748-second gap between PostgreSQL readiness on the new primary and its primary role label, delaying RW routing. Meanwhile, the old primary returned HTTP 500 because its PostgreSQL socket was absent. Unnecessary retries plausibly contributed; no end-to-end improvement has been measured.

Upstream #1708 proposed broader retry/timeout changes and was closed as resolved. Current comments and the original timeout change (ff6dd54d) explicitly exclude HTTP errors from retries. This patch leaves timeout/backoff, failover safety, and service reconciliation unchanged.

Validation

Using Go 1.27.2 and mocked RoundTrippers through the actual instance-status request path:

  • Before fix: HTTP 500 regression failed with 5 requests instead of 1.
  • After fix: all 5 remote specs passed. HTTP 500 makes 1 request and preserves code/body; transport EOF followed by HTTP 200 succeeds in 2 requests with decoded status and no final error. Assertions use request counts, not timing.
  • Passed: main-module unit tests; formatting, vet, and golangci-lint 2.14.0 in both modules.
  • Rerun passed: make test, plus uncached remote (5 specs) and PostgreSQL helper (3 specs) suites, including the previously blocked Docker image pull.
  • E2E passed: BUILD_IMAGE=true OPERATOR=local FEATURE_TYPE=smoke,basic make e2e-test-kind on local Kubernetes v1.37.0: 50 passed, 0 failed, 207 skipped (205 outside the label filter; architecture and pod-patch tests skipped by default test depth). The deployed operator reported build commit c6c7b0ae. The disposable cluster was removed afterward.
  • Limits: full suite, higher-depth tests, upgrades, and the complete version matrix were not validated. No end-to-end routing improvement was measured.

mnencia and others added 5 commits October 14, 2026 11:24
A second reconciliation working from a stale cached Backup could ask the
instance manager to run it again, start a volume snapshot backup again,
or move a started backup back to pending. Make the status patches that
mark the backup as started or pending conditional on the resourceVersion
that was read, and requeue on conflict. The major version is now
persisted by the start patches, and the Starting event is emitted only
once that patch succeeds.

Closes cloudnative-pg#11616

---------

Signed-off-by: Marco Nenciarini <[email protected]>
Signed-off-by: Armando Ruocco <[email protected]>
Co-authored-by: Armando Ruocco <[email protected]>
…loudnative-pg#11645)

The status probe read the system ID with SQL. A fenced or restarting
instance can't run SQL, so it reported none. After a demotion all
instances are in that state, so the operator set ConsistentSystemID to
False (NotFound) and did not update it until another event touched the
Cluster.

Read the ID from pg_control instead, which doesn't need a running
PostgreSQL. A cluster fenced on purpose now reports ConsistentSystemID
as True.

Closes cloudnative-pg#11602

Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Co-authored-by: Leonardo Cecchi <[email protected]>
Co-authored-by: Marco Nenciarini <[email protected]>
Update the versions used to test the operator on public cloud providers

Signed-off-by: public-cloud-k8s-versions-check <[email protected]>
Co-authored-by: public-cloud-k8s-versions-check <[email protected]>
…1659)

The recovery test resizes the WAL volume of the old primary and expects
that same pod to come back as primary. If the instance manager restarts
before the resize is visible, it exits and kubelet backoff keeps the pod
down for about 20 seconds. The operator can fail over inside that
window. The test then runs `pg_switch_wal()` on a standby and fails with
`recovery is in progress`.

Set `failoverDelay` in both disk-space fixtures so the operator waits
instead of promoting a replica.

Signed-off-by: Marco Nenciarini <[email protected]>
A single timeout covered all retry attempts. A transient network error
such as a handshake or DNS timeout used the whole budget, so the retry
never ran. The timeout now applies to each attempt.

An attempt that hits its own deadline is not retried, because the
command may already have started.

Signed-off-by: Marco Nenciarini <[email protected]>
leonardoce and others added 22 commits October 15, 2026 12:47
…udnative-pg#11649)

Sources that do not follow the CloudNativePG tablespace layout could not
be cloned with `pg_basebackup`. Add
`spec.bootstrap.pg_basebackup.tablespaceMapping` to map each tablespace
declared in `spec.tablespaces` to its location in the source instance.
The operator passes one `--tablespace-mapping` per entry to
`pg_basebackup` for the initial clone. Replica joins do not use it.

Closes cloudnative-pg#11621

Signed-off-by: Leonardo Cecchi <[email protected]>
Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Co-authored-by: Armando Ruocco <[email protected]>
Co-authored-by: Gabriele Bartolini <[email protected]>
Co-authored-by: Marco Nenciarini <[email protected]>
…loudnative-pg#11652)

Image extensions contributed their directories to
`extension_control_path` and `dynamic_library_path` unconditionally, so
an image shipping only `lib` still added a `share` entry pointing to a
non-existent directory. This made the effective configuration misleading
and relied on PostgreSQL silently ignoring missing entries.

The instance manager now adds a directory only when it exists in the
mounted image volume. This applies to both the default `share` and `lib`
directories and to custom paths. Image volumes cannot change during the
life of a Pod, so the generated configuration stays stable.

Closes cloudnative-pg#11650

Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
Co-authored-by: Gabriele Bartolini <[email protected]>
Co-authored-by: Leonardo Cecchi <[email protected]>
…tive-pg#11683)

Since cloudnative-pg#11344 replaced the deprecated `Requeue` field, a reconciler hook
returning `BEHAVIOR_REQUEUE` without `requeue_after` maps to an empty
`ctrl.Result`, so the Cluster is not requeued at all. `requeue_after` is
optional in CNPG-I, and the barman-cloud plugin returns no delay when
the referenced ObjectStore doesn't exist yet: a Cluster created before
its ObjectStore then stays in `BootstrapPending` until something else
triggers a reconciliation.

Requeue after one second when no delay is requested.

Closes cloudnative-pg#11618

Assisted-by: Claude

Signed-off-by: Valerio Del Sarto <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Co-authored-by: Marco Nenciarini <[email protected]>
…ease (cloudnative-pg#11357)

Primary self-fencing on network isolation used to live in the liveness
probe: a failed peer check made the kubelet restart the pod about 30s
after the partition began, through a smart shutdown bounded by
`.spec.smartShutdownTimeout`. The reaction time depended on the liveness
probe periods, which have nothing to do with how long a replica waits
before it may promote, and the smart shutdown let open sessions keep
committing on the isolated primary for up to 180s.

The primary lease already measures what matters: a replica may promote
once the lease has gone `leaseDurationSeconds` without a renewal. The
instance manager now records, on a monotonic clock, when its last
successful lease write started, and a goroutine checks that age every
`retryPeriodSeconds`. Once the age passes `renewDeadlineSeconds` it
probes the other instances in parallel and, if one is unreachable or
names a different target primary, requests an immediate PostgreSQL
shutdown, 10 to 13 seconds after the last renewal with the default
timings. The trigger is the age, not an error from the election loop,
because client-go's acquire path has no overall deadline and may never
return while the API server is unreachable.

If the primary has decided to step down and PostgreSQL is still running
on the last tick before the lease reaches `leaseDurationSeconds`,
because the lifecycle manager never took the request or `pg_ctl` failed,
the instance manager sends `SIGQUIT` to the postmaster and exits with
code 5. The monitor lives as long as the lifecycle manager, so it also
fences when the manager stops because another runnable failed and the
smart shutdown that follows outlasts the lease. A renewal that lands
before the lifecycle manager takes the request withdraws the decision,
so a primary whose lease is fresh again is not shut down late. Once the
request is taken, later renewals no longer move the forced stop, so a
restart that waits for PostgreSQL cannot keep the instance manager alive
with a fresh lease.

Taking the lease over is stricter in three ways. A record that already
names this pod, for instance after a restart, is rewritten with a
conditional write before the lease counts as held, so a replica that
claims it in between makes the write fail and PostgreSQL is not started.
Only the pod that the last reconciled Cluster names as target primary
takes the lease over, since the runnable keeps polling for as long as
the process lives. And the observation window starts only once the pod
is the target, so a claim comes at least `leaseDurationSeconds` after
the flip, which gives the old primary time to learn about the
replacement from its peers.

`/failsafe` now returns the responding instance's view of the target
primary, through `pkg/management/url`, so a primary that still reaches
every peer but learns from one that it has been replaced steps down too.
A pre-upgrade peer answering plain `OK` counts as agreeing. The probe
skips our own entry and peers without an IP.

The liveness probe now always reports OK. The
`.spec.probes.liveness.isolationCheck` stanza keeps its fields:
`requestTimeout` and `connectionTimeout` bound each peer probe, while
the probe periods no longer affect fencing and
`.spec.smartShutdownTimeout: 0` is no longer needed for it. The
admission webhook warns when `renewDeadlineSeconds + retryPeriodSeconds
+ isolationCheck.requestTimeout` is not below `leaseDurationSeconds`.

The stop beats a replica only if that sum is below the lease duration.
The signal goes to the postmaster only; the exit of PID 1 stops the
rest. A cluster with `instances: 1`, or with the check disabled, or a
primary that knows no peer to probe never steps down. One stale peer is
enough to stop the primary. No Event is emitted; the step-down is logged
under `primary-lease` and shows as a container restart, and the
last-resort stop exits with code 5.

Signed-off-by: Leonardo Cecchi <[email protected]>
Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Niccolò Fei <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Co-authored-by: Armando Ruocco <[email protected]>
Co-authored-by: Niccolò Fei <[email protected]>
Co-authored-by: Marco Nenciarini <[email protected]>
Co-authored-by: Gabriele Bartolini <[email protected]>
…-pg#11690)

The upgrade specs drew serverName as `cluster1-<0..9999>`. Two specs in
one job could draw the same value and archive the same WAL names to the
shared object store. The WAL assertion searches every bucket, so it
found two files instead of one.

The namespace is unique for each run, so use it for the serverName.

Signed-off-by: Marco Nenciarini <[email protected]>
…loudnative-pg#11583)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
…loudnative-pg#11599)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
…pg#11687)

`kubectl cnpg status` runs commands in the primary Pod through exec:
`du` for the cluster size and, with `-v -v`, `cat` to read `custom.conf`
and `pg_hba.conf`. Without the `pods/exec` permission, it printed raw
exec errors, and the configuration sections made the command fail.

Check permissions first with a `SelfSubjectAccessReview` on the primary
Pod, and show `not permitted: requires the pods/exec permission` in
place of the size and configuration sections. If the review itself
fails, run the command anyway and report the actual error. When no Pod
holds the primary role, as during bootstrap or a failover, skip the
check, because the review would have no namespace and be evaluated at
cluster scope.

Mark `pods/exec` as optional for `status` in the kubectl plugin
documentation.

Assisted-by: Claude

References cloudnative-pg#11519

Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Co-authored-by: Armando Ruocco <[email protected]>
Co-authored-by: Marco Nenciarini <[email protected]>
…reflight to v1.21.2 (cloudnative-pg#11697)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
cloudnative-pg#11634)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
…1674)

When a parallel worker panics, Ginkgo reports only that a process
disappeared. The panic text is lost because the output interceptor holds
stderr while a spec runs, and the job summary says "0 Failure(s)
Found!".

Each worker now copies its crash output to `out/crash-proc<N>-<pid>.log`
and records its running spec in `out/current-proc<N>-<pid>.txt`, so a
worker killed from outside still names the spec. Both files are removed
on a clean exit and are collected with the failure artifacts. The report
script prints them with any kernel OOM messages, and an empty Ginkgo
report is named as such instead of "list index out of range".

Assisted-by: Claude

---------

Signed-off-by: Marco Nenciarini <[email protected]>
…docker digest to 5be877a (cloudnative-pg#11693)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
….26.1 (cloudnative-pg#11704)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
The AKS E2E clusters shipped container and kube-apiserver logs to an
Azure Log Analytics workspace that nobody reads. It was the largest item
on the subscription bill. Create the clusters without the monitoring
addon and diagnostic settings, and drop the cleanup of the
data-collection rules the addon created.

Signed-off-by: Marco Nenciarini <[email protected]>
…#11694)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
…e-pg#11706)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
…udnative-pg#11707)

Signed-off-by: Marco Nenciarini <[email protected]>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Marco Nenciarini <[email protected]>
renovate Bot and others added 13 commits October 27, 2026 13:38
…ve-pg#11772)

Signed-off-by: Marco Nenciarini <[email protected]>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Marco Nenciarini <[email protected]>
…1768)

Signed-off-by: Marco Nenciarini <[email protected]>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Marco Nenciarini <[email protected]>
)

The destroy step can leave behind the disks of the cluster volumes.
Delete the unattached disks that carry the cluster label once the
cluster is gone.

Signed-off-by: Marco Nenciarini <[email protected]>
Signed-off-by: Tao Li <[email protected]>
Co-authored-by: Tao Li <[email protected]>
…standby parameter (cloudnative-pg#11236)

Removing a hot standby sensitive parameter (e.g. max_connections) from
the Cluster spec could leave a replica unable to restart. With
`primaryUpdateStrategy: supervised`, the primary keeps running with the old,
higher value until you act. A replica restarted in that window (eviction,
drain, node restart) came up with the PostgreSQL default and aborted
recovery with:

    FATAL: recovery aborted because of insufficient parameter settings

It then ended up in CrashLoopBackOff.

A replica now keeps the value recorded in pg_controldata for such a
parameter while it is higher than the value PostgreSQL would start with.
Once the primary is restarted, every instance converges to the PostgreSQL
default without a switchover.

A related issue is also fixed. If the configuration files changed while
PostgreSQL was still starting up, the reload was lost and PostgreSQL kept
the values it read at startup. A replica could then miss the decrease
and not be restarted after the primary. The reload now stays pending
until it runs, or until a restart loads the new configuration.

Closes cloudnative-pg#10395

Assisted-by: Claude Fable 5

Signed-off-by: Jihyun Bahn <[email protected]>
Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
Co-authored-by: Armando Ruocco <[email protected]>
Co-authored-by: Leonardo Cecchi <[email protected]>
Update the Postgres versions used in E2E tests

Signed-off-by: postgres-versions-updater <[email protected]>
Co-authored-by: postgres-versions-updater <[email protected]>
The docs image is switching from yarn to npm (cloudnative-pg/docs#180).
Use `npm run build`, which works with both the current and the new
image, and drop the stale yarn.lock entry from docs/.gitignore.

Signed-off-by: Francesco Canovai <[email protected]>
cloudnative-pg#11608)

The `bootstrap-instance` init container runs on a Pod whose PGDATA PVC
is not ready, and Kubernetes runs it again whenever the Pod is recreated
as it was, for example when a backup tool restores the Pod with its PVCs
or the kubelet recreates the Pod sandbox. On a volume that already holds
the instance, `initdb` moves the data directory aside and starts an
empty instance.

The operator now creates the Pod with the `cnpg.io/bootstrapPending`
annotation, which the container reads through the downward API, and
removes it once the container succeeds. Every bootstrap command logs a
warning and exits without touching the volumes when the annotation is
not set. This needs no new permission and no API call, and the variable
is not part of the Pod spec compared for rollouts.

The PVC classifier also took a missing init container status for a Pod
without the container, so it marked a PVC ready before the kubelet
reported on its Pod, and a Pod replaced then lost its bootstrap
container. It now looks at the Pod spec.

Closes cloudnative-pg#11505

Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Co-authored-by: Leonardo Cecchi <[email protected]>
Co-authored-by: Marco Nenciarini <[email protected]>
BRGOVIND and others added 8 commits November 2, 2026 10:58
…udnative-pg#10910)

Adds a `permissions` field to schema entries in the `Database` CRD,
letting users declaratively grant or revoke the `USAGE` and `CREATE`
privileges on a schema, alongside the existing `owner`/`ensure`
management:

```
    schemas:
      - name: app
        owner: app
        permissions:
          usage:
            - name: public
              type: revoke
```

Each entry reuses the existing grant/revoke model used for FDW and
foreign-server usage (`name` + `type: grant|revoke`, default: `grant`),
so public targets the PostgreSQL PUBLIC pseudo-role as it already does
elsewhere.

Closes cloudnative-pg#7872

Signed-off-by: B R GOVIND <[email protected]>
Signed-off-by: BRGOVIND <[email protected]>
Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Co-authored-by: Armando Ruocco <[email protected]>
Co-authored-by: Gabriele Bartolini <[email protected]>
…pg#11790)

EnsureResourceIsAdmitted runs the defaulting webhook on every
reconcile, not just on admission, so these "Defaulting for X" lines
were printed at info level on every reconcile of every Backup,
Cluster, Database and ScheduledBackup, drowning out other operator
logs. Demote them to debug.

Closes cloudnative-pg#11792

Signed-off-by: Leonardo Cecchi <[email protected]>
renovate Bot and others added 5 commits November 2, 2026 13:59
Renovate runs go mod tidy only in the modules it edits. The tests
module lists most root dependencies as indirect, which Renovate does
not update, so root-only bumps left tests/go.mod stale and broke make
generate (cloudnative-pg#11772, cloudnative-pg#11818).

gomodTidyAll also tidies every module that depends on the updated one
through a local replace directive.

Signed-off-by: Marco Nenciarini <[email protected]>
…ative-pg#11342)

The `clientCertificate` stanza of a `DatabaseRole` took its lifetime and
renewal window from `CERTIFICATE_DURATION` and
`EXPIRING_CHECK_THRESHOLD`, which apply to every certificate the
operator signs, so a role could not have a policy of its own.

Add optional `duration` and `renewBefore` to that stanza. A role that
sets neither keeps the operator-wide values. `duration` must be at least
one minute. `renewBefore` needs `duration` and must be between 30
seconds and half of it. The API server enforces this through CEL, since
a `DatabaseRole` has no admission webhook.

Two things had to change underneath. The fixed five-minute backdate of
leaf certificates would put `notAfter` in the past for lifetimes below
five minutes, so the allowance now scales with the lifetime, capped at
five minutes. And renewal is no longer decided by
`RenewLeafCertificate`, which only knows the operator-wide threshold:
the role reconciler compares the stored certificate with the role's own
lifetime and window.

Roles that set neither field see three changes. Renewal generates a new
private key along with the certificate. Changing `CERTIFICATE_DURATION`
re-issues their certificate. The default renewal window is capped at
half the lifetime. For every role, a missing cluster or client CA is now
reported in `status.clientCertificate.message` and as a warning event.
The docs also gain a short walkthrough for using the generated Secret
with `psql`.

Closes cloudnative-pg#11312

Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Tao Li <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Co-authored-by: Tao Li <[email protected]>
Co-authored-by: Leonardo Cecchi <[email protected]>
Co-authored-by: Marco Nenciarini <[email protected]>
Match the *StatusError returned by rawInstanceStatusRequest so HTTP errors stop immediately. Cover HTTP 500 response preservation and transient transport-error recovery through the instance-status request path.

Assisted-by: OpenAI Codex
Signed-off-by: Kabir Gogia <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Instance-status HTTP errors incorrectly consume all retry attempts