Skip to content

Conversation

@lucian-tosa
Copy link
Contributor

@lucian-tosa lucian-tosa commented Dec 4, 2025

Summary

This PR fixes an issue where roles added to an OM project outside the operator (through UI or the API) are overwritten by the operator every reconciliation. This is now consistent with the user behaviour, where users can be defined in the UI and will not be removed.

To accomplish this, we are now tracking the roles we configure with the operator in an annotation, and state configmap (where applicable). This was necessary due to possible usage of the ClusterMongoDBRole resource. If the reference to a role resource is removed at the same time as removing the resource itself, then it is not possible to determine which role needs to be removed from the automation config without knowing the previous set of roles.

Proof of Work

Unit tests have been added to verify that the ensureRoles method which is reused across all controllers behaves correctly. Additionally, the unit tests verify that each controller keeps track of the previously configured roles in an annotation.

Checklist

  • Have you linked a jira ticket and/or is the ticket in the title?
  • Have you checked whether your jira ticket required DOCSP changes?
  • Have you added changelog file?

@github-actions
Copy link

github-actions bot commented Dec 4, 2025

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

MCK 1.6.1 Release Notes

Bug Fixes

  • Backed up the agent password in a secret for SCRAM authentication to prevent unnecessary password rotations.
  • MongoDB Adding missing ownerrefs to ensure proper resource deletion by kubernetes.
  • Single Cluster Deleting resources created by CRD now only happens on multi-cluster deployments. Single Cluster will solely rely on ownerrefs.
  • MongoDB, MongoDBOpsManager: Improve validation for featureCompatibilityVersion field in MongoDB and MongoDBOpsManager spec.
    The field now enforces proper semantic versioning. Previously, invalid semver values could be accepted,
    potentially resulting in incorrect configurations.
  • Roles configured via Ops Manager UI or API will no longer be removed by the operator

@lucian-tosa lucian-tosa marked this pull request as ready for review December 4, 2025 16:49
@lucian-tosa lucian-tosa requested review from a team and vinilage as code owners December 4, 2025 16:49
@lucian-tosa lucian-tosa requested review from lsierant and nammn December 4, 2025 16:49
}

// Set annotation and state for previously configured roles
roleAnnotation, roleStrings, err := r.commonController.getRoleAnnotation(ctx, r.sc.Spec.DbCommonSpec, r.enableClusterMongoDBRoles, kube.ObjectKeyFromApiObject(r.sc))
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we rely on annotation where we have deployment state available? I understand this is necessary for mongodb and mdbmc as we don't have the state yet, but here?

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, I see that we actually rely on the LastConfiguredRoles in the logic, so the annotation is to align with other controllers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I can remove the annotation from the sharded cluster, I was unsure whether I should add it to both the state and the annotation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the annotation

roles = mockOm.GetRoles()
require.Len(t, roles, 2)

// Delete embedded role, only the external should remain
Copy link
Collaborator

Choose a reason for hiding this comment

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

q: why did the embedded role get removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

re-initialized rs to a replicaset without roles on line 573

Copy link
Collaborator

Choose a reason for hiding this comment

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

ahhhh "deleting" by re-initialisation. Can you update the comment to clarify that ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no, you're right, I'll just set the roles field to an empty array to make it clear

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

// This is achieved by removing currently configured roles from the deployed roles.
// To ensure that roles removed from the spec are also removed from OM, we also remove the previously configured roles.
// Finally, we add back the currently configured roles.
func mergeRoles(deployed []mdbv1.MongoDBRole, current []mdbv1.MongoDBRole, previous []string) []mdbv1.MongoDBRole {
Copy link
Collaborator

Choose a reason for hiding this comment

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

blocking: lets add a dedicated unit test for mergeRoles

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}},
}
mockOm.AddRole(externalRole)

Copy link
Collaborator

Choose a reason for hiding this comment

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

can you - for the sake of clarity - add here:

	roles = mockOm.GetRoles()
	require.Len(t, roles, 2)

as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines +1 to +6
---
kind: fix
date: 2025-12-04
---

* Roles configured via Ops Manager UI or API will no longer be removed by the operator
Copy link
Collaborator

Choose a reason for hiding this comment

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

LGTM!

@lucian-tosa lucian-tosa merged commit 0fef73e into master Dec 10, 2025
34 of 37 checks passed
@lucian-tosa lucian-tosa deleted the lucian/CLOUDP-355710-fix-roles branch December 10, 2025 19:31
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.

5 participants