Skip to content

Conversation

@nammn
Copy link
Collaborator

@nammn nammn commented Dec 2, 2025

Summary

fixes: #589

This pull request introduces important improvements to how Kubernetes resources are managed and cleaned up for MongoDB and Ops Manager deployments, especially in multi-cluster versus single-cluster scenarios. The main focus is on correctly setting owner references for resources to enable automatic garbage collection by Kubernetes, and ensuring explicit resource deletion only occurs when necessary (i.e., in multi-cluster setups where owner references cannot span clusters).

Owner Reference Management & Resource Cleanup

  • Added missing owner references (ownerReferences) to all relevant ConfigMaps and resources for MongoDB and Ops Manager, ensuring proper automatic cleanup by Kubernetes in single-cluster deployments.
  • Refactored constructors and initialization logic across controllers (appdbreplicaset_controller.go, mongodbopsmanager_controller.go, mongodbshardedcluster_controller.go, state_store.go) to consistently pass and set ownerReferences.

Multi-Cluster vs Single-Cluster Resource Deletion Logic

  • Updated deletion logic in controllers so that explicit resource deletion is only performed in multi-cluster deployments; single-cluster setups now rely solely on owner references for cleanup.

Proof of Work

  • unit tests
  • relying on kubernetes GC working properly

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 2, 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.

@nammn nammn marked this pull request as ready for review December 2, 2025 12:50
@nammn nammn requested review from a team and vinilage as code owners December 2, 2025 12:50
@nammn nammn self-assigned this Dec 2, 2025
@nammn nammn marked this pull request as draft December 2, 2025 13:42
Copy link
Collaborator

@Julien-Ben Julien-Ben left a comment

Choose a reason for hiding this comment

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

Well tested and implementation LGTM ! Please check if the failing tests are due to flakiness 🙏

@nammn nammn marked this pull request as ready for review December 3, 2025 09:36
@nammn nammn requested a review from fealebenpae December 3, 2025 10:04
Copy link
Collaborator

@vinilage vinilage left a comment

Choose a reason for hiding this comment

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

LGTM!

Comment on lines +1 to +7
---
kind: fix
date: 2025-12-02
---

* **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.
Copy link
Collaborator

Choose a reason for hiding this comment

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

LGTM!

clusterClient := item.Client
clusterName := item.Name
if err := r.commonController.deleteClusterResources(ctx, clusterClient, clusterName, sc, log); err != nil {
errs = multierror.Append(errs, xerrors.Errorf("failed deleting dependant resources in cluster %s: %w", clusterName, err))
Copy link
Contributor

Choose a reason for hiding this comment

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

in om's controller we're only logging the error in this case
here though failing to delete will result in errors returned - would that impact anything?

namespace string
resourceName string
ownerLabels map[string]string
ownerReferences []metav1.OwnerReference
Copy link
Contributor

Choose a reason for hiding this comment

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

how the stored owner refs are actually consumed by the controller's impl?

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.

Cleanup of resources requires deletecollection permission

5 participants