Skip to content

PostgresCluster and PostgresDatabase events emitting#1798

Draft
limak9182 wants to merge 8 commits intofeature/database-controllersfrom
feature/postgrescluster-and-postgresdatabase-events
Draft

PostgresCluster and PostgresDatabase events emitting#1798
limak9182 wants to merge 8 commits intofeature/database-controllersfrom
feature/postgrescluster-and-postgresdatabase-events

Conversation

@limak9182
Copy link
Copy Markdown

@limak9182 limak9182 commented Mar 27, 2026

Description

Add Kubernetes event emission to PostgresCluster and PostgresDatabase controllers for improved observability. Events are emitted on key lifecycle transitions (create, update, delete, ready) and on failures, making it easier to debug issues via kubectl describe or kubectl get events.

Key Changes

  • Introduced ReconcileContext struct in both cluster and database core packages to bundle Client, Scheme, and EventRecorder
  • Added events.go with event reason constants and helper methods (emitNormal, emitWarning, transition-aware emitters) for each controller
  • Wired EventRecorder from controller-runtime manager into both reconcilers
  • Added events RBAC permission (create, patch) to both controllers
  • Events are deduplicated on phase/condition transitions to avoid noise on steady-state reconciles

Testing and Verification

I tested changes manually with following scenerios:

  1. Role conflict on the postgresdatabase
❯ k describe postgresdatabase db-owner-two
Name:         db-owner-two
Namespace:    test1
Labels:       <none>
Annotations:  <none>
API Version:  enterprise.splunk.com/v4
Kind:         PostgresDatabase
Metadata:
  Creation Timestamp:  2026-03-27T11:43:46Z
  Finalizers:
    postgresdatabases.enterprise.splunk.com/finalizer
  Generation:        1
  Resource Version:  2079126
  UID:               5ab55286-2a4d-450a-9bb5-d59623af68d9
Spec:
  Cluster Ref:
    Name:  conflict-test
  Databases:
    Deletion Policy:  Delete
    Name:             shareddb
Status:
  Conditions:
    Last Transition Time:  2026-03-27T11:43:46Z
    Message:               Cluster is operational
    Observed Generation:   1
    Reason:                ClusterAvailable
    Status:                True
    Type:                  ClusterReady
    Last Transition Time:  2026-03-27T11:43:46Z
    Message:               Role conflict: shareddb_admin (owned by postgresdatabase-db-owner-one), shareddb_rw (owned by postgresdatabase-db-owner-one). If you deleted a previous PostgresDatabase, recreate it with the original name to re-adopt the orphaned resources.
    Observed Generation:   1
    Reason:                RoleConflict
    Status:                False
    Type:                  RolesReady
  Phase:                   Failed
Events:
  Type     Reason            Age    From                         Message
  ----     ------            ----   ----                         -------
  Normal   ClusterValidated  2m37s  postgresdatabase-controller  Referenced PostgresCluster is ready
  Warning  RoleConflict      2m37s  postgresdatabase-controller  Role conflict: shareddb_admin (owned by postgresdatabase-db-owner-one), shareddb_rw (owned by postgresdatabase-db-owner-one). If you deleted a previous PostgresDatabase, recreate it with the original name to re-adopt the orphaned resources.
  1. PostgresClass not found
❯ k describe postgrescluster warn-repeat-test
Name:         warn-repeat-test
Namespace:    test1
Labels:       <none>
Annotations:  <none>
API Version:  enterprise.splunk.com/v4
Kind:         PostgresCluster
Metadata:
  Creation Timestamp:  2026-03-27T11:48:53Z
  Finalizers:
    postgresclusters.enterprise.splunk.com/finalizer
  Generation:        1
  Resource Version:  2079841
  UID:               247fad74-fd37-4559-9656-3edb7188bb52
Spec:
  Class:                      does-not-exist
  Cluster Deletion Policy:    Delete
  Connection Pooler Enabled:  false
  Pg HBA:
  Postgresql Config:
Status:
  Conditions:
    Last Transition Time:  2026-03-27T11:48:53Z
    Message:               ClusterClass does-not-exist not found: PostgresClusterClass.enterprise.splunk.com "does-not-exist" not found
    Observed Generation:   1
    Reason:                ClusterClassNotFound
    Status:                False
    Type:                  ClusterReady
  Phase:                   Failed
  Resources:
Events:
  Type     Reason                Age                From                        Message
  ----     ------                ----               ----                        -------
  Warning  ClusterClassNotFound  9s (x14 over 50s)  postgrescluster-controller  ClusterClass does-not-exist not found
  1. Postgrescluster not found in the database CR
❯ k describe postgresdatabase orphan-db
Name:         orphan-db
Namespace:    test1
Labels:       <none>
Annotations:  <none>
API Version:  enterprise.splunk.com/v4
Kind:         PostgresDatabase
Metadata:
  Creation Timestamp:  2026-03-27T11:50:36Z
  Finalizers:
    postgresdatabases.enterprise.splunk.com/finalizer
  Generation:        1
  Resource Version:  2080097
  UID:               58f85fd1-dd73-4d41-8e60-0239c5150173
Spec:
  Cluster Ref:
    Name:  nonexistent-cluster
  Databases:
    Deletion Policy:  Delete
    Name:             mydb
Status:
  Conditions:
    Last Transition Time:  2026-03-27T11:50:36Z
    Message:               Cluster CR not found
    Observed Generation:   1
    Reason:                ClusterNotFound
    Status:                False
    Type:                  ClusterReady
  Phase:                   Pending
Events:
  Type     Reason           Age                From                         Message
  ----     ------           ----               ----                         -------
  Warning  ClusterNotFound  14s (x2 over 44s)  postgresdatabase-controller  PostgresCluster nonexistent-cluster not found
  1. Correct events for both
    4.1. Cluster
  ⎿  LAST SEEN   TYPE     REASON                        OBJECT                MESSAGE
     29s         Normal   SecretReady                   postgrescluster/ev4   Superuser secret ev4-secret created
     29s         Normal   ClusterCreationStarted        postgrescluster/ev4   CNPG cluster created, waiting for healthy state
     10s         Normal   ConfigMapReady                postgrescluster/ev4   ConfigMap ev4-configmap created
     10s         Normal   ClusterReady                  postgrescluster/ev4   Cluster is up and running

4.2 Postgresdatabase

  ⎿  LAST SEEN   TYPE     REASON                          OBJECT                    MESSAGE
     24s         Normal   ClusterValidated                postgresdatabase/ev4-db   Referenced PostgresCluster is ready
     24s         Normal   SecretsReady                    postgresdatabase/ev4-db   All secrets provisioned for 1 databases
     24s         Normal   ConfigMapsReady                 postgresdatabase/ev4-db   All ConfigMaps provisioned for 1 databases
     24s         Normal   RoleReconciliationStarted       postgresdatabase/ev4-db   Patched managed roles, waiting for 2 roles to reconcile
     9s          Normal   RolesReady                      postgresdatabase/ev4-db   All 2 roles reconciled
     9s          Normal   DatabaseReconciliationStarted   postgresdatabase/ev4-db   Reconciling 1 databases, waiting for readiness
     9s          Normal   DatabasesReady                  postgresdatabase/ev4-db   All 1 databases ready
     8s          Normal   PrivilegesReady                 postgresdatabase/ev4-db   RW role privileges granted for all 1 databases
     8s          Normal   PostgresDatabaseReady           postgresdatabase/ev4-db   PostgresDatabase ev4-db is ready
  1. Deletion
    5.1. Cluster
     24s         Normal   ClusterReady                  postgrescluster/ev4   Cluster is up and running
     5s          Normal   CleanupComplete               postgrescluster/ev4   Cleanup complete (policy: Retain)

5.2. Database

     35s         Normal   DatabasesReady                  postgresdatabase/ev4-db   All 2 databases ready
     6s          Normal   CleanupComplete                 postgresdatabase/ev4-db   Cleanup complete (0 retained, 2 deleted)
  1. Events on recreation:
    6.1. Cluster
  ⎿  LAST SEEN   TYPE     REASON                        OBJECT                       MESSAGE
     109s        Normal   ClusterAdopted                postgrescluster/adopt-test   Adopted existing CNPG cluster and secret adopt-test-secret
     109s        Normal   ClusterCreationStarted        postgrescluster/adopt-test   CNPG cluster created, waiting for healthy state
     90s         Normal   ConfigMapReady                postgrescluster/adopt-test   ConfigMap adopt-test-configmap created
     90s         Normal   ClusterReady                  postgrescluster/adopt-test   Cluster is up and running
     55s         Normal   CleanupComplete               postgrescluster/adopt-test   Cleanup complete (policy: Retain)
     16s         Normal   ClusterAdopted                postgrescluster/adopt-test   Adopted existing CNPG cluster and secret adopt-test-secret
     16s         Normal   ConfigMapReady                postgrescluster/adopt-test   ConfigMap adopt-test-configmap created
     16s         Normal   ClusterReady                  postgrescluster/adopt-test   Cluster is up and running

6.2. Database

     18s         Normal   ClusterValidated                postgresdatabase/adopt-db   Referenced PostgresCluster is ready
     18s         Normal   SecretsReady                    postgresdatabase/adopt-db   All secrets provisioned for 1 databases
     18s         Normal   ConfigMapsReady                 postgresdatabase/adopt-db   All ConfigMaps provisioned for 1 databases
     18s         Normal   RoleReconciliationStarted       postgresdatabase/adopt-db   Patched managed roles, waiting for 2 roles to reconcile
     18s         Normal   RolesReady                      postgresdatabase/adopt-db   All 2 roles reconciled
     18s         Normal   ResourcesAdopted                postgresdatabase/adopt-db   Adopted retained databases: [adoptdb1]
     18s         Normal   DatabasesReady                  postgresdatabase/adopt-db   All 1 databases ready
     18s         Normal   PrivilegesReady                 postgresdatabase/adopt-db   RW role privileges granted for all 1 databases
     18s         Normal   PostgresDatabaseReady           postgresdatabase/adopt-db   PostgresDatabase adopt-db is ready

Related Issues

CPI-1852

PR Checklist

  • Code changes adhere to the project's coding standards.
  • Relevant unit and integration tests are included.
  • Documentation has been updated accordingly.
  • All tests pass locally.
  • The PR description follows the project's guidelines.

@github-actions
Copy link
Copy Markdown
Contributor

CLA Assistant Lite bot:
Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contribution License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment with the exact sentence copied from below.


I have read the CLA Document and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request

@limak9182 limak9182 requested review from DmytroPI-dev, M4KIF and mploski and removed request for DmytroPI-dev March 27, 2026 15:28
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.

1 participant