Skip to content

Add DocumentDB upgrade support with configurable PostgresImage and ImageVolume extensions - #208

Merged
xgerman merged 17 commits into
documentdb:mainfrom
WentingWu666666:users/wentingwu/upgrade-documentdb
Feb 26, 2026
Merged

xgerman merged 17 commits into
documentdb:mainfrom
WentingWu666666:users/wentingwu/upgrade-documentdb

Conversation

@WentingWu666666

@WentingWu666666 WentingWu666666 commented Jan 13, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

This PR introduces DocumentDB upgrade capabilities by adding a configurable PostgresImage field to the DocumentDB CRD and enabling ImageVolume-based extension loading through CNPG.

Key Changes

CRD Updates

  • Added postgresImage field to DocumentDB spec for configurable postgres image selection
  • Added schemaVersion status field to track the current version

CNPG Integration

  • Upgraded CNPG to support ImageVolume extensions
  • Configure LD_LIBRARY_PATH for extension loading
  • Removed hardcoded PostgresUID/GID (105/108) to allow standard postgres images to use their default UID/GID values and avoid initdb: could not look up effective user ID 105: user does not exist errors

CI/CD Updates

  • Upgraded kind to v0.31.0 with Kubernetes v1.35.0 for E2E tests
  • Enabled ImageVolume feature gate in kind cluster configuration
  • Added E2E tests for upgrade scenarios

Testing

  • Unit tests: Added/updated in documentdb_controller_test.go
  • E2E tests: New upgrade test workflow added

@WentingWu666666
WentingWu666666 force-pushed the users/wentingwu/upgrade-documentdb branch from 433e80d to 1e6c870 Compare January 13, 2026 02:04
@WentingWu666666
WentingWu666666 marked this pull request as ready for review January 13, 2026 02:04
@WentingWu666666
WentingWu666666 marked this pull request as draft January 13, 2026 02:04
@WentingWu666666
WentingWu666666 force-pushed the users/wentingwu/upgrade-documentdb branch from 1e6c870 to ed68947 Compare January 13, 2026 02:16
@WentingWu666666
WentingWu666666 force-pushed the users/wentingwu/upgrade-documentdb branch 3 times, most recently from 006724a to a61d520 Compare January 22, 2026 14:29
@WentingWu666666
WentingWu666666 force-pushed the users/wentingwu/upgrade-documentdb branch 8 times, most recently from 5144126 to d6db610 Compare January 29, 2026 15:20
@WentingWu666666 WentingWu666666 changed the title upgrade documentdb extension Add DocumentDB upgrade support with configurable PostgresImage and ImageVolume extensions Jan 29, 2026
@WentingWu666666
WentingWu666666 marked this pull request as ready for review January 29, 2026 17:21
Copilot AI review requested due to automatic review settings January 29, 2026 17:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces DocumentDB upgrade capabilities by adding support for configurable PostgreSQL and DocumentDB extension images, along with automated extension upgrade logic. The changes enable rolling upgrades of DocumentDB clusters with minimal downtime.

Changes:

  • Adds postgresImage field to DocumentDB CRD spec for configurable Postgres base image selection
  • Adds documentDBVersion field to DocumentDB status to track the currently installed extension version
  • Implements automated extension upgrade logic that updates the extension image and runs ALTER EXTENSION when needed
  • Upgrades CNPG dependency from 0.26.1 to 0.27.0 to support ImageVolume-based extension loading
  • Adds E2E upgrade tests that verify version transitions
  • Removes DocumentDB image build from CI pipeline (now using external registry)
  • Migrates certificate tests from testify to Ginkgo/Gomega for consistency

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
operator/src/api/preview/documentdb_types.go Adds PostgresImage spec field and DocumentDBVersion status field to DocumentDB CRD
operator/src/config/crd/bases/documentdb.io_dbs.yaml Updates CRD schema with new postgresImage and documentDBVersion fields
operator/documentdb-helm-chart/crds/documentdb.io_dbs.yaml Mirrors CRD schema changes in Helm chart
operator/src/internal/cnpg/cnpg_cluster.go Configures CNPG cluster with ImageVolume extensions and uses postgresImage from spec
operator/src/internal/controller/documentdb_controller.go Adds upgrade logic with upgradeDocumentDBExtensionIfNeeded and updateExtensionImageIfNeeded functions, plus version parsing
operator/src/internal/controller/documentdb_controller_test.go Comprehensive unit tests for upgrade logic using Ginkgo/Gomega
operator/src/internal/controller/certificate_controller_test.go Extracts certificate tests into separate file, maintaining testify framework
operator/documentdb-helm-chart/Chart.yaml Bumps CNPG dependency from 0.26.1 to 0.27.0
.github/workflows/test-E2E.yml Adds upgrade test scenario that patches image and verifies version transition
.github/workflows/test-integration.yml Updates to use hardcoded DocumentDB image instead of version parameter
.github/workflows/test-backup-and-restore.yml Removes unused documentdb_version input parameter
.github/workflows/test-build-and-package.yml Removes DocumentDB image build step (now using external images)
.github/actions/setup-test-environment/action.yml Enables ImageVolume feature gate in kind cluster config, upgrades to kind v0.31.0 with K8s v1.35.0

Comment thread .github/workflows/test-E2E.yml Resolved
@WentingWu666666
WentingWu666666 force-pushed the users/wentingwu/upgrade-documentdb branch from 13fad47 to d6db610 Compare January 29, 2026 17:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.

@WentingWu666666 WentingWu666666 self-assigned this Feb 13, 2026
@WentingWu666666
WentingWu666666 force-pushed the users/wentingwu/upgrade-documentdb branch from 3ff7c9f to 7f944e6 Compare February 20, 2026 03:07
…QLExecutor

- Add SQLExecutor function field to DocumentDBReconciler for testability
- Default to executeSQLCommand in SetupWithManager (zero production changes)
- Replace 4 executeSQLCommand call sites with SQLExecutor
- Add 8 new upgradeDocumentDBIfNeeded tests covering Steps 4-7:
  SQL error, unparseable output, empty installed version, versions match,
  rollback detection with warning event, successful ALTER EXTENSION,
  ALTER EXTENSION failure, invalid version comparison
- Add CompareExtensionVersions error-path tests for invalid inputs

Signed-off-by: Wenting Wu <[email protected]>
Add 11 new targeted tests for uncovered error paths:
- detectImageVolumeSupport: ServerVersion error, unparseable minor
- upgradeDocumentDBIfNeeded: CNPG patch error, gateway restart annotation
  patch error, updateImageStatus failures, status update failures
- updateImageStatus: status update error
- SetupWithManager: initialization with nil manager, SQLExecutor preservation

Patch coverage improved from 82.2% to 91.5% (195/213 statements).

Signed-off-by: Wenting Wu <[email protected]>
…for extension image check

With ImageVolume (K8s >= 1.35), the extension image is mounted as a
Kubernetes volume, not as an init container. Updated the pod image
verification in upgrade and rollback steps to query the correct path.

Signed-off-by: Wenting Wu <[email protected]>
@WentingWu666666
WentingWu666666 force-pushed the users/wentingwu/upgrade-documentdb branch 2 times, most recently from 0d137dc to a900223 Compare February 25, 2026 13:11
… relation error

The find() and findOne() operations cause 'pruned relation' errors after
extension upgrade due to gateway/extension version mismatch. Use
countDocuments() which works correctly for verifying data persistence.

Changes:
- Replace find().toArray() with countDocuments() in all verification steps
- Simplified assertions to check document count only
- Affected steps: after upgrade, after extension rollback, after gateway rollback

Signed-off-by: Wenting Wu <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 20 out of 20 changed files in this pull request and generated 9 comments.

Comment thread operator/src/internal/utils/util.go Resolved
Comment thread .github/workflows/test-E2E.yml Resolved
@xgerman

xgerman commented Feb 25, 2026

Copy link
Copy Markdown
Collaborator

My Copiot says:

  1. CRD description semantic change: "adding" → "subtracting" weight in anti-affinity

In documentdb.io_dbs.yaml (line ~422):

  • compute a sum by iterating through the elements of this field and adding
  • "weight" to the sum if the node has pods which matches the corresponding podAffinityTerm
  • compute a sum by iterating through the elements of this field and subtracting
  • "weight" from the sum if the node has pods which matches the corresponding podAffinityTerm

This appears in the preferredDuringSchedulingIgnoredDuringExecution section of podAntiAffinity. This is a description
change from the upstream controller-gen / K8s API regeneration and is technically correct for anti-affinity (you
subtract weight to make nodes less preferred). However:

[Question] Was this generated via make manifests or manually edited? If generated, this is fine — it's upstream
catching up. If manual, please revert and let the generators handle it.


🟠 Major Issues

  1. Race condition: multiple Status().Update() calls in upgradeDocumentDBIfNeeded

The function does up to 3 separate Status().Update() calls within a single reconcile pass:

  • updateImageStatus() (line ~1646)
  • Update DocumentDBVersion after reading installed version (line ~1700)
  • Update DocumentDBVersion after ALTER EXTENSION (line ~1812)

Each call refetches via r.Get() at the top, but between the refetch and the later status updates, another reconcile or
controller could modify the status, causing a conflict error. The first update succeeding would make the second update
use a stale resourceVersion.

[Required] Consolidate into a single status update at the end of the function, or re-fetch the resource before each
Status().Update(). The pattern should be:

// Re-fetch before status update to get latest resourceVersion
if err := r.Get(ctx, types.NamespacedName{...}, documentdb); err != nil { ... }
documentdb.Status.DocumentDBVersion = ...
documentdb.Status.DocumentDBImage = ...
documentdb.Status.GatewayImage = ...
if err := r.Status().Update(ctx, documentdb); err != nil { ... }

  1. GetCnpgClusterSpec parameter documentdbImage is now overloaded

In ImageVolume mode, documentdbImage is the extension image reference. In combined mode, it's used as spec.ImageName
(the Postgres container image). The same parameter name carries different semantics depending on the mode, which is
confusing. The function signature now has 8 parameters — this is getting unwieldy.

[Suggestion] Consider a config struct to bundle related parameters:

type CnpgClusterConfig struct {
DocumentDBImage string
ServiceAccount string
StorageClass string
IsPrimary bool
UseImageVolume bool
}

  1. detectImageVolumeSupport runs once at startup in SetupWithManager

If the operator is running during a K8s version upgrade (unlikely but possible in-place upgrades), or in tests where
you want to override the detection, the one-time detection could be limiting.

[Suggestion] This is fine for now, but document this as a known limitation: the operator must be restarted after a K8s
cluster upgrade to pick up ImageVolume support.


🟡 Minor Issues

  1. parseExtensionVersionsFromOutput — fragile SQL output parsing

The function parses the psql tabular output by splitting on \n and |. This is brittle if:

  • PostgreSQL locale changes column alignment
  • Extra whitespace or header formatting changes between PG versions

[Suggestion] Consider using -t (tuples only) and -A (unaligned) flags in the SQL executor to get a simpler format, or
use to_json() to return structured output.

  1. Helm chart CRD not synced with generated CRD

The diff shows changes to both operator/documentdb-helm-chart/crds/documentdb.io_dbs.yaml and
operator/src/config/crd/bases/documentdb.io_dbs.yaml, but the helm chart CRD doesn't include the anti-affinity
description changes that the base CRD has.

[Question] Is the helm chart CRD manually maintained or copied from the generated one? If copied, it looks out of sync.

  1. No postgresImage validation

The new postgresImage field accepts any string. There's no validation that it's a valid container image reference, or
that it's compatible with the DocumentDB extension.

[Suggestion] Add a webhook or at minimum a reconcile-time validation that warns if the image doesn't match expected
patterns (e.g., postgresql:*-bookworm).

  1. CHANGELOG version section is [Unreleased]

[Nitpick] This is fine for now, but ensure it gets a version number before the release cut.

  1. ExtensionVersionToSemver only handles the last hyphen

strings.LastIndex(v, "-") means a version like "0.110-beta-0" would produce "0.110-beta.0" which is incorrect. The
current DocumentDB versions don't have this format, but it's worth noting.

[Nitpick] Add a comment noting the expected input format.

@WentingWu666666
WentingWu666666 force-pushed the users/wentingwu/upgrade-documentdb branch from 6e5674a to 6515c6c Compare February 26, 2026 01:44
1. Rename status.documentDBVersion to status.schemaVersion to clarify that
   this field represents the installed extension schema version (after
   ALTER EXTENSION UPDATE), not the image version.
2. Update postgresImage comment to be more generic: 'defaults to the last
   stable PostgreSQL version compatible with DocumentDB'.

Signed-off-by: Wenting Wu <[email protected]>
@WentingWu666666
WentingWu666666 force-pushed the users/wentingwu/upgrade-documentdb branch from 665c4bc to e1dcd11 Compare February 26, 2026 02:03
@WentingWu666666

WentingWu666666 commented Feb 26, 2026 •

Copy link
Copy Markdown
Collaborator Author

My Copiot says:

  1. CRD description semantic change: "adding" → "subtracting" weight in anti-affinity

In documentdb.io_dbs.yaml (line ~422):

  • compute a sum by iterating through the elements of this field and adding

  • "weight" to the sum if the node has pods which matches the corresponding podAffinityTerm

  • compute a sum by iterating through the elements of this field and subtracting

  • "weight" from the sum if the node has pods which matches the corresponding podAffinityTerm

This appears in the preferredDuringSchedulingIgnoredDuringExecution section of podAntiAffinity. This is a description change from the upstream controller-gen / K8s API regeneration and is technically correct for anti-affinity (you subtract weight to make nodes less preferred). However:

[Question] Was this generated via make manifests or manually edited? If generated, this is fine — it's upstream catching up. If manual, please revert and let the generators handle it.

This was generated via make manifests.

🟠 Major Issues

  1. Race condition: multiple Status().Update() calls in upgradeDocumentDBIfNeeded

The function does up to 3 separate Status().Update() calls within a single reconcile pass:

  • updateImageStatus() (line ~1646)
  • Update DocumentDBVersion after reading installed version (line ~1700)
  • Update DocumentDBVersion after ALTER EXTENSION (line ~1812)

Each call refetches via r.Get() at the top, but between the refetch and the later status updates, another reconcile or controller could modify the status, causing a conflict error. The first update succeeding would make the second update use a stale resourceVersion.

[Required] Consolidate into a single status update at the end of the function, or re-fetch the resource before each Status().Update(). The pattern should be:

// Re-fetch before status update to get latest resourceVersion if err := r.Get(ctx, types.NamespacedName{...}, documentdb); err != nil { ... } documentdb.Status.DocumentDBVersion = ... documentdb.Status.DocumentDBImage = ... documentdb.Status.GatewayImage = ... if err := r.Status().Update(ctx, documentdb); err != nil { ... }

Will add re-fetch before each Status().Update() call.

  1. GetCnpgClusterSpec parameter documentdbImage is now overloaded

In ImageVolume mode, documentdbImage is the extension image reference. In combined mode, it's used as spec.ImageName (the Postgres container image). The same parameter name carries different semantics depending on the mode, which is confusing. The function signature now has 8 parameters — this is getting unwieldy.

[Suggestion] Consider a config struct to bundle related parameters:

type CnpgClusterConfig struct { DocumentDBImage string ServiceAccount string StorageClass string IsPrimary bool UseImageVolume bool }

The useImageVolume parameter will be removed when combined mode is deprecated (after AKS supports K8s 1.35). This will reduce the parameter count and eliminate the semantic confusion.

  1. detectImageVolumeSupport runs once at startup in SetupWithManager

If the operator is running during a K8s version upgrade (unlikely but possible in-place upgrades), or in tests where you want to override the detection, the one-time detection could be limiting.

[Suggestion] This is fine for now, but document this as a known limitation: the operator must be restarted after a K8s cluster upgrade to pick up ImageVolume support.

When combined mode is deprecated, detectImageVolumeSupport() will be simplified to an enforcement check:

  • Remove combined mode fallback logic
  • Keep version detection
  • Fail fast at startup with clear error if K8s < 1.35:
    FATAL: This operator requires Kubernetes 1.35+ for ImageVolume support.
    Detected version: 1.34. Please upgrade your cluster.
    
  • This provides better UX than letting users create DocumentDB clusters that fail with cryptic CNPG/ImageVolume errors

🟡 Minor Issues

  1. parseExtensionVersionsFromOutput — fragile SQL output parsing

The function parses the psql tabular output by splitting on \n and |. This is brittle if:

  • PostgreSQL locale changes column alignment
  • Extra whitespace or header formatting changes between PG versions

[Suggestion] Consider using -t (tuples only) and -A (unaligned) flags in the SQL executor to get a simpler format, or use to_json() to return structured output.

Added TODO comment to improve it in the future.

  1. Helm chart CRD not synced with generated CRD

The diff shows changes to both operator/documentdb-helm-chart/crds/documentdb.io_dbs.yaml and operator/src/config/crd/bases/documentdb.io_dbs.yaml, but the helm chart CRD doesn't include the anti-affinity description changes that the base CRD has.

[Question] Is the helm chart CRD manually maintained or copied from the generated one? If copied, it looks out of sync.

Already synced via make manifests

  1. No postgresImage validation

The new postgresImage field accepts any string. There's no validation that it's a valid container image reference, or that it's compatible with the DocumentDB extension.

[Suggestion] Add a webhook or at minimum a reconcile-time validation that warns if the image doesn't match expected patterns (e.g., postgresql:*-bookworm).

Currently there's no validation for any image fields:

  • spec.postgresImage - PostgreSQL base image
  • spec.documentDBImage - DocumentDB extension image
  • spec.gatewayImage - Gateway sidecar image

Image validation could be added for all image fields in the future.

  1. CHANGELOG version section is [Unreleased]

[Nitpick] This is fine for now, but ensure it gets a version number before the release cut.

Will be versioned at release time.

  1. ExtensionVersionToSemver only handles the last hyphen

strings.LastIndex(v, "-") means a version like "0.110-beta-0" would produce "0.110-beta.0" which is incorrect. The current DocumentDB versions don't have this format, but it's worth noting.

[Nitpick] Add a comment noting the expected input format.

Will add comment.

Signed-off-by: Wenting Wu <[email protected]>
This was referenced Feb 26, 2026
@xgerman
xgerman merged commit 47acc35 into documentdb:main Feb 26, 2026
65 checks passed
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.

6 participants