Skip to content

ERA-66338: Add MongoDB HA (ReplicaSet) instance support - #246

Draft
sasikanthmasini wants to merge 1 commit into
mainfrom
cursor/mongo-ha-replicaset-support-cb67
Draft

ERA-66338: Add MongoDB HA (ReplicaSet) instance support#246
sasikanthmasini wants to merge 1 commit into
mainfrom
cursor/mongo-ha-replicaset-support-cb67

Conversation

@sasikanthmasini

Copy link
Copy Markdown
Contributor

What this PR does / why we need it

This PR implements PR #244 — adds support for MongoDB HA (ReplicaSet) via the NDB Operator.

Changes

API (api/v1alpha1)

  • database_types.go: Added MongoHAConfig struct with ReplicaSetName, ReplicaSetDescription, DeployArbiter, ArbiterComputeProfileId, and ListenerPort fields. Added MongoDB *MongoHAConfig to InstanceHAConfig. Extended nodeType enum with "arbiter".
  • ha_validators.go: Added MongoHAParamsValidator for webhook validation of MongoDB HA constraints (required mongodb config block, exactly one primary, arbiter consistency with deployArbiter, node type restrictions).
  • zz_generated.deepcopy.go: Added generated DeepCopy/DeepCopyInto for MongoHAConfig.

Common

  • constants.go: Added HA_MONGO_DEFAULT_LISTENER_PORT, HA_NODE_TYPE_ARBITER, HA_NODE_ROLE_MONGO_PRIMARY/SECONDARY/ARBITER constants.
  • common/util/additionalArguments.go: Allowed cluster_name and cluster_description as overridable MongoDB HA action arguments.

NDB API (ndb_api)

  • interfaces.go: Added IsMongoHA() to DatabaseInterface and MongoDB-specific fields (ReplicaSetName, DeployArbiter, ArbiterComputeProfileId, MongoListenerPort) to HAConfig.
  • db_helpers.go: Added MongoDB HA provisioning request block (clustered=true, per-node properties with role/votes/priority, SLA cluster IDs). Updated MongoDbRequestAppender with HA-specific action arguments and changed default db_user from admin to mongod.

Controller Adapters

  • controller_adapters/database.go: Added IsMongoHA() method and wired MongoDB fields in GetInstanceHAConfig().

Controllers

  • controllers/ha_connectivity.go: Added MongoHAIPResolver (returns IPs of all data nodes, excludes arbiters) and MongoHAConnectivityManager (returns nil additional services — MongoDB HA uses a URI secret instead of K8s Services).
  • controllers/database_reconciler_helpers.go: Added setupMongoHAConnectivity (creates a headless Service + Endpoints per data node for CoreDNS resolution, then creates/updates a <db>-db-uri Secret with the MongoDB connection URI) and setupMongoHeadlessService.
  • controllers/database_controller.go: Updated RBAC annotation for secrets to include create;update;patch.

Config

  • config/crd/bases/ndb.nutanix.com_databases.yaml: Added mongodb block and arbiter to nodeType enum.
  • config/rbac/role.yaml: Added create, patch, update verbs for secrets.

Dependencies

  • Updated go.mod/go.sum with minor dependency bumps.

How it works

MongoDB HA uses a Replica Set topology where:

  • Primary node receives write/read traffic
  • Secondary nodes handle read traffic and async replication
  • Arbiter (optional) participates only in elections, holds no data

The operator creates:

  1. A headless Kubernetes Service + Endpoints per data node so CoreDNS resolves each VM hostname to its IP (required by MongoDB SDAM for topology discovery and automatic failover)
  2. A <database-name>-db-uri Secret containing the full MongoDB connection URI: mongodb://user:pass@vm1:27017,vm2:27017/dbname?replicaSet=rs0&authSource=admin

Applications use this URI with a MongoDB driver that handles routing and automatic failover via SDAM.

Tests

  • TestMongoHAParamsValidator_Validate — 11 subtests covering all validation scenarios
  • TestMongoHAConnectivityManager_* — registry, PrimaryPort, AdditionalServices
  • TestMongoHAIPResolver_* — registry, ResolveIPs (excludes arbiters, handles empty nodes)
  • Updated existing MongoDB provisioning tests with IsMongoHA mock and corrected db_user value
Open in Web Open in Cursor 

- Add MongoHAConfig type with ReplicaSetName, DeployArbiter, ArbiterComputeProfileId, ListenerPort fields
- Add MongoDB field to InstanceHAConfig
- Add arbiter to HA node type enum in CRD
- Add MongoHAParamsValidator for webhook validation
- Add IsMongoHA() method to Database adapter and interface
- Add MongoHAConnectivityManager and MongoHAIPResolver
- Add setupMongoHAConnectivity: creates headless Services/Endpoints per data node and a <db>-db-uri Secret with the full MongoDB connection URI
- Update MongoDB provisioning request appender with HA-specific action arguments (cluster_name, cluster_description, listener_port)
- Change db_user default from 'admin' to 'mongod'
- Allow cluster_name and cluster_description in MongoDB additional arguments
- Update RBAC to allow create/patch/update on secrets
- Update go.mod/go.sum dependencies
- Add tests for all new functionality

Co-authored-by: sasikanthmasini <sasikanth.masini@nutanix.com>
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.

2 participants