Skip to content

Conversation

Julien-Ben
Copy link
Collaborator

@Julien-Ben Julien-Ben commented Oct 1, 2025

CLOUDP-347497 - Single cluster Replica Set Controller Refactoring

Why this refactoring

The single-cluster RS controller was mixing two concerns:

  • Kubernetes stuff (StatefulSets, pods, volumes)
  • Ops Manager/MongoDB stuff (MongoDB processes, replication config)

This worked fine for single-cluster, but it's a problem when you think about multi-cluster:

  • Multi-cluster has multiple StatefulSets (one per cluster) but only one logical ReplicaSet in Ops Manager
  • The OM automation config doesn't care about how many K8s clusters you have or how the pods are deployed

So we need to separate these layers properly.

Main changes

1. Broke down the huge Reconcile() method

Before: ~300 lines of inline logic in Reconcile()

Now:

Reconcile()
  ├── reconcileMemberResources()        // Handles all K8s resource creation
  │   ├── reconcileHostnameOverrideConfigMap()
  │   ├── ensureRoles()
  │   └── reconcileStatefulSet()        // StatefulSet-specific logic isolated here
  │       └── buildStatefulSetOptions() // Builds STS configuration
  └── updateOmDeploymentRs()            // Handles Ops Manager automation config updates

This makes it way easier to understand what's happening and matches the multi-cluster controller structure.

2. Removed StatefulSet dependency from OM operations

Created new helper functions that work directly with MongoDB resources instead of StatefulSets:

  • CreateMongodProcessesFromMongoDB() - was using StatefulSet before
  • BuildFromMongoDBWithReplicas() - same
  • WaitForRsAgentsToRegisterByResource() - same

These mirror the existing ...FromStatefulSet functions but take MongoDB resources instead.

Why it matters: The OM layer now only cares about the MongoDB resource definition, not how it's deployed in K8s. This makes the code work the same way for both single-cluster and multi-cluster.

3. Added publishAutomationConfigFirstRS checks

Dedicated function for RS instead of using the shared one. Does not rely on a statefulset object.

Important for review

The ideal way to review this PR is to compare the new structure to the mongodbmultireplicaset_controller.go. The aim of the refactoring is to get the single cluster controller closer to it.

Look at:

  • reconcileMemberResources() in both controllers - similar structure now
  • updateOmDeploymentRs() - no more StatefulSet dependency
  • New helper functions in om/process and om/replicaset - mirror existing patterns

Bug found along the way

The logic to handle scale up + disable TLS at the same time doesn't actually work properly and should be blocked by validation (see draft PR #490 for more details).

Tests added

Added tests for the new helper functions:

  • TestCreateMongodProcessesFromMongoDB - basic scenarios, scaling, custom domains, TLS, additional config
  • TestBuildFromMongoDBWithReplicas - integration test checking ReplicaSet structure and member options propagation
  • TestPublishAutomationConfigFirstRS - automation config publish logic with various TLS/auth scenarios

Copy link

github-actions bot commented Oct 1, 2025

⚠️ (this preview might not be accurate if the PR is not rebased on current master branch)

MCK 1.5.0 Release Notes

New Features

  • Improve automation agent certificate rotation: the agent now restarts automatically when its certificate is renewed, ensuring smooth operation without manual intervention and allowing seamless certificate updates without requiring manual Pod restarts.

Bug Fixes

  • MongoDBMultiCluster: fix resource stuck in Pending state if any clusterSpecList item has 0 members. After the fix, a value of 0 members is handled correctly, similarly to how it's done in the MongoDB resource.

@Julien-Ben Julien-Ben added the skip-changelog Use this label in Pull Request to not require new changelog entry file label Oct 7, 2025
Builder functions
Test works
Copy link
Contributor

@m1kola m1kola left a comment

Choose a reason for hiding this comment

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

Early review: I had a quick-ish look at the controllers and couldn't spot anything obviously wrong with it. I didn't review the tests at all.

Looks good so far.

}
}

publishAutomationConfigFirst, err := r.publishAutomationConfigFirstMultiCluster(ctx, &mrs, log)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Brought it closer to where it's actually used

log.Debugw("Marked replica set members as non-voting", "replica set with members", rsMembers)
}

// TODO practice shows that automation agents can get stuck on setting db to "disabled" also it seems that this process
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This comment was old (<2022)

@Julien-Ben Julien-Ben marked this pull request as ready for review October 7, 2025 16:27
@Julien-Ben Julien-Ben requested a review from a team as a code owner October 7, 2025 16:27
// Reconcile reads that state of the cluster for a MongoDbReplicaSet object and makes changes based on the state read
// and what is in the MongoDbReplicaSet.Spec
func (r *ReconcileMongoDbReplicaSet) Reconcile(ctx context.Context, request reconcile.Request) (res reconcile.Result, e error) {
// === 1. Initial Checks and setup
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I added these as helpers for myself at the beginning of the refactoring, to keep track of where I was in the hundreds of lines of the reconcile loop

I'm okay to remove them now if they feel like noise more than useful comments

@Julien-Ben Julien-Ben changed the title [DO NOT REVIEW] WIP CLOUDP-347497: Single cluster Replica Set Controller Refactoring Oct 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip-changelog Use this label in Pull Request to not require new changelog entry file
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants