Skip to content

fead: add CNPG - #262

Merged
MykolaShev merged 3 commits into
mainfrom
feat/add_cnpg_k8s
Jun 20, 2026
Merged

fead: add CNPG#262
MykolaShev merged 3 commits into
mainfrom
feat/add_cnpg_k8s

Conversation

@MykolaShev

@MykolaShev MykolaShev commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator

Closes #260

Summary by CodeRabbit

  • Infrastructure
    • Added Redis connection settings to the business service configuration.
    • Standardized PostgreSQL connection environment variables across API components and the migration job.
    • Switched infrastructure manifests to use CloudNativePG configuration.
  • Database & Migrations
    • Deployed a CloudNativePG-managed PostgreSQL cluster.
    • Added a migration to grant business-schema permissions and set default privileges.
  • Security
    • Hardened workloads by running as non-root and enabling read-only root filesystems where applicable.
  • Service Reliability
    • Improved the readability of readiness-failure responses.
  • Kubernetes
    • Added operator Kustomize manifests and updated the business operator example and deployment configuration.

@coderabbitai

coderabbitai Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@MykolaShev, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 55 minutes and 41 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan refill rate.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, the refill rate gradually slows as usage increases. The highest same-day bursts are limited more strictly.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f5ab5dc0-e693-4f6a-9704-7c5233817849

📥 Commits

Reviewing files that changed from the base of the PR and between 19a0def and 84aa327.

📒 Files selected for processing (4)
  • build/Dockerfile.business-operator
  • deploy/k8s/operators/business/example.yaml
  • deploy/k8s/operators/business/kustomization.yaml
  • deploy/k8s/operators/business/operator-deployment.yaml
📝 Walkthrough

Walkthrough

Integrates CloudNativePG as the PostgreSQL provider: adds a CNPG Cluster manifest, operator Kustomize overlays, and switches the infra kustomization to cnpg.yaml. The migrator job and business/guest service deployments are updated to source credentials from CNPG-generated secrets. A SQL migration grants the share role permissions on the business schema, and the business operator runs as non-root with updated example configuration.

Changes

CloudNativePG Integration

Layer / File(s) Summary
CNPG Cluster manifest and operator overlays
deploy/k8s/infra/cnpg.yaml, deploy/k8s/infra/kustomization.yaml, deploy/k8s/operators/kustomization.yaml, deploy/k8s/operators/business/kustomization.yaml, deploy/k8s/operators/business/operator-deployment.yaml, deploy/k8s/operators/guest/kustomization.yaml
Introduces the share-bite-cnpg CloudNativePG Cluster resource (2 instances, share_bite DB, 2Gi storage), switches the infra kustomization from postgres.yaml to cnpg.yaml, adds Kustomize overlays for the operators root, business operator (CRD/deployment/RBAC), and guest operator (CRD), and removes explicit namespace pinning from the business-operator Deployment manifest.
Migrator job environment and security context
deploy/k8s/infra/migrator-job.yaml
Both the wait-for-postgres init container and the migrator container now set readOnlyRootFilesystem: true and declare explicit POSTGRES_HOST, POSTGRES_PORT, and POSTGRES_DB env vars with user and password sourced from the share-bite-cnpg-superuser secret via secretKeyRef.
Service deployments environment configuration
deploy/k8s/business/business-configmap.yaml, deploy/k8s/business/business-deploy.yaml, deploy/k8s/guest/deployment.yaml
Adds REDIS_HOST and REDIS_PORT to the business ConfigMap. Both business-api and guest-api containers receive explicit POSTGRES_* env blocks with credentials from the share-bite-cnpg-app secret. The guest deployment adds securityContext.runAsUser: 1000.
Business schema permissions migration, business operator hardening, and handler readiness
migrations/20260617205326_grant_business_permissions.sql, build/Dockerfile.business-operator, deploy/k8s/operators/business/example.yaml, internal/business/handler/business/handler.go
Adds a Goose migration granting the share role USAGE, table CRUD, and sequence privileges on the business schema with matching default privileges and rollback. The business-operator container image now runs as non-root user UID 1000 with an explicit WORKDIR. The BusinessAppProfile example updates the namespace to share-bite-local and scales replicas to 3. The ready handler's 503 JSON response is reformatted into multi-line format while preserving the error field.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • ua-academy-projects/share-bite#228: Both PRs touch the Kubernetes local setup, specifically deploy/k8s/infra/migrator-job.yaml, where the main PR changes how POSTGRES_* env vars are provided (from envFrom-based config/secret loading toward explicit env/secretKeyRefs) compared to the retrieved PR's initial envFrom-based job.
  • ua-academy-projects/share-bite#232: Main PR updates business-operator Kubernetes manifests (e.g., deploy/k8s/operators/business/* kustomization/example/operator-deployment) to wire the operator/CRD into the target namespace and desired scaling, which directly aligns with the business-operator/CRD implementation added in retrieved PR #232.
  • ua-academy-projects/share-bite#235: Both PRs touch the business readiness handling in internal/business/handler/business/handler.go (main PR reformats the /ready 503 JSON error payload in handler.ready, while the retrieved PR implements/updates the /ready readiness logic), so the changes are directly related.

Suggested reviewers

  • DmyMi
  • viktorzhabskyi
  • mblinovv

🐇 A cluster of Postgres, now cloud-native and bright,
Two instances humming, a primary in flight!
The share role gets CRUD, the migrator's wired neat,
Redis hops in the config — the setup's complete.
🥕 With secrets from CNPG, the services align,
Every env var explicit, by design!

🚥 Pre-merge checks | ✅ 2 | ❌ 3

❌ Failed checks (1 warning, 2 inconclusive)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR implements most required changes from issue #260 but is missing the documentation file and uses 'cnpg.yaml' instead of 'cnpg-cluster.yaml' as specified in the issue requirements. Create docs/k8s/cloudnativepg.md documenting operator setup and usage, and verify the cluster manifest filename matches issue specifications or update the issue scope accordingly.
Title check ❓ Inconclusive The PR title 'fead: add CNPG' contains a typo ('fead' instead of 'feat') and only briefly references adding CNPG without clearly summarizing the main changes in the changeset. Correct the typo to 'feat: add CNPG' or provide a more descriptive title that clearly indicates CloudNativePG operator integration and related configuration updates.
Out of Scope Changes check ❓ Inconclusive The PR includes several changes not directly mentioned in issue #260 such as security context updates, migration files, and Dockerfile changes that appear to be supporting infrastructure modifications. Clarify whether security context additions (runAsUser, readOnlyRootFilesystem) and database migration/permissions changes are part of the CloudNativePG implementation scope or separate enhancements.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/add_cnpg_k8s

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@deploy/k8s/infra/cnpg.yaml`:
- Line 7: The imageName field in cnpg.yaml is set to PostgreSQL 18, but the CNPG
operator pinned in deploy/k8s/operators/kustomization.yaml is version 1.22.1,
which only supports PostgreSQL versions 12 through 16. To resolve this
compatibility mismatch, either upgrade the CNPG operator version to 1.29.0 or
later in the kustomization.yaml file, or downgrade the PostgreSQL imageName to a
supported major version within the 12-16 range. Choose whichever aligns with
your infrastructure requirements.

In `@deploy/k8s/infra/migrator-job.yaml`:
- Around line 44-60: Add `readOnlyRootFilesystem: true` to the securityContext
of both containers in the Job spec. For the `wait-for-postgres` container and
the `migrator` container, locate their respective securityContext sections
(which already contain runAsNonRoot and other hardening settings) and add the
readOnlyRootFilesystem field set to true at the same indentation level as the
existing security settings. This will restrict both containers from writing to
the root filesystem, completing the security hardening of the Job resource.

In `@internal/business/handler/business/handler.go`:
- Around line 189-192: Remove the "details" field containing err.Error() from
the gin.H response object in the readiness handler to prevent exposing internal
service/database details to clients. Keep the response generic by only including
the "error" field with "service not ready". Instead, log the full error details
internally using the appropriate logger so that developers can debug the issue
while clients receive a safe, generic message.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: cb37c639-8298-4c83-b517-4b8d5fe834fa

📥 Commits

Reviewing files that changed from the base of the PR and between ac86bcd and 3de7dcf.

📒 Files selected for processing (11)
  • deploy/k8s/business/business-configmap.yaml
  • deploy/k8s/business/business-deploy.yaml
  • deploy/k8s/guest/deployment.yaml
  • deploy/k8s/infra/cnpg.yaml
  • deploy/k8s/infra/kustomization.yaml
  • deploy/k8s/infra/migrator-job.yaml
  • deploy/k8s/operators/business/kustomization.yaml
  • deploy/k8s/operators/guest/kustomization.yaml
  • deploy/k8s/operators/kustomization.yaml
  • internal/business/handler/business/handler.go
  • migrations/20260617205326_grant_business_permissions.sql

Comment thread deploy/k8s/infra/cnpg.yaml
Comment thread deploy/k8s/infra/migrator-job.yaml
Comment thread internal/business/handler/business/handler.go
Comment thread build/Dockerfile.business-operator Outdated

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@deploy/k8s/operators/business/example.yaml`:
- Around line 7-8: The example manifest in the business operator configuration
has conflicting settings where replicas is set to 3 but enabled is set to false,
which causes the reconciler to scale the business-api deployment to 0 replicas
despite the replicas field value. To fix this, either change enabled to true to
make the example actually run 3 pod replicas, or change replicas to 0 to
explicitly document that no pods should be running with the enabled false
configuration.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 39e31fd3-dae6-4136-98e8-6cfc85cdde2c

📥 Commits

Reviewing files that changed from the base of the PR and between 468bb57 and 19a0def.

📒 Files selected for processing (4)
  • build/Dockerfile.business-operator
  • deploy/k8s/operators/business/example.yaml
  • deploy/k8s/operators/business/kustomization.yaml
  • deploy/k8s/operators/business/operator-deployment.yaml
💤 Files with no reviewable changes (1)
  • deploy/k8s/operators/business/operator-deployment.yaml
✅ Files skipped from review due to trivial changes (1)
  • deploy/k8s/operators/business/kustomization.yaml
🚧 Files skipped from review as they are similar to previous changes (1)
  • build/Dockerfile.business-operator

Comment thread deploy/k8s/operators/business/example.yaml Outdated
@MykolaShev
MykolaShev merged commit e5b4328 into main Jun 20, 2026
10 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.

Implement CloudNativePG for PostgreSQL cluster management

2 participants