mongodb / mongodb-kubernetes-operator

MongoDB Community Kubernetes Operator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for opt-in cleanup of PVCs

snarlysodboxer opened this issue · comments

What did you do to encounter the bug?
Steps to reproduce the behavior:

  1. Use latest operator
  2. Use CI to apply MongoDBCommunity
  3. Attempt to use CI to cleanup MongoDBCommunity
  4. PVCs get left behind, external hacks required

What did you expect?
A clear and explicit opt-in way to configure a specific MongoDBCommunity to cause full cascading deletion including the PVCs. Many operators that deal with state have this feature for this very reason. For example with the postgres-operator you can set a specific annotation's value to match the object's name, in which case the operator will fully cleanup including the PVCs via ownerReferences. Without that annotation it will orphan the StatefulSet and therefore PVCs, helping to prevent mistakenly deleting stateful data. - Set that annotation only in your ephemeral CI testing envs, and you're pretty safe.

Even when you're migrating a production environment, once you're done, you want the operator to be able to clean up the old environment. The MongoDB Operator seems to be an outlier in not accounting for a full lifecycle.

What happened instead?
One can't just kubectl delete -f my-testing-env/, one also has to discover and manually delete the PVCs created by the MongoDBCommunity. This precludes the use of things like ArgoCD to deliver and cleanup ephemeral envs for CI. Even if CI just uses a bash script that executes kubectl commands, it makes the script suddenly needlessly complicated - just for Mongo.

Operator Information

  • Latest

The behavior you described can be achieved by using Kubernetes StatefulSet Autodeletion mechanism with spec.statefulset.spec override. For a CI-based use case, using whenDeleted policy might be a good fit.

Please close the issue if this explanation solves your case.

@slaskawi Thanks for your response and help! I went ahead and tried that, like this:

---
apiVersion: mongodbcommunity.mongodb.com/v1
kind: MongoDBCommunity
metadata:
  name: my-mongodb
spec:
...
  statefulSet:
    spec:
      persistentVolumeClaimRetentionPolicy:
        whenDeleted: Delete
        whenScaled: Retain
...

But when I look at the resulting StatefulSet, I see no persistentVolumeClaimRetentionPolicy anywhere. (Indeed the operator seems to not copy over most of the stuff from spec.statefulSet.spec, despite accepting it as input.) Nor are the PVCs cleaned up when deleting the MongoDBCommunity.

Additionally, our version of k8s still has this feature in alpha, and therefore not enabled yet, but I supposed that's an us problem. :)

Same to me. I edited like below

apiVersion: mongodbcommunity.mongodb.com/v1
kind: MongoDBCommunity
metadata:
  name: mongodb
  namespace: mongodb
spec:
  members: 3
  type: ReplicaSet
  version: "6.0.6"

  prometheus:
    username: user
    passwordSecretRef:
      name: mongo-prometheus-password

  security:
    authentication:
      modes: ["SCRAM"]

  users:
    - name: admin
      db: admin
      passwordSecretRef:
        name: mongo-admin-password
      roles:
        - name: clusterAdmin
          db: admin
        - name: userAdminAnyDatabase
          db: admin
        - name: readWriteAnyDatabase
          db: admin
      scramCredentialsSecretName: mongodb
  statefulSet:
    spec:
      persistentVolumeClaimRetentionPolicy:
        whenDeleted: Retain
        whenScaled: Delete

But, In the statefulset, persistentVolumeClaimRetentionPolicy.[*] is all 'Retain'

kubectl get statefulsets.apps mongodb -oyaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  creationTimestamp: "2023-10-12T03:34:00Z"
  generation: 1
  labels:
    app: mongodb-svc
  name: mongodb
  namespace: mongodb
  ownerReferences:
  - apiVersion: mongodbcommunity.mongodb.com/v1
    blockOwnerDeletion: true
    controller: true
    kind: MongoDBCommunity
    name: mongodb
    uid: c3c980e5-3db4-4699-afd7-c231ca0a9c6f
  resourceVersion: "11290161"
  uid: 1d78e0aa-8167-4052-b3aa-e3c3ae4dacb3
spec:
  persistentVolumeClaimRetentionPolicy:
    whenDeleted: Retain
    whenScaled: Retain

sts.spec. persistentVolumeClaimRetentionPolicy not in beta until Kubernetes v1.27. Current operator use k8s.io/api v0.25.12, which means operator don't recognize this field.

to use sts.spec. persistentVolumeClaimRetentionPolicy should:

  1. update k8s.io/api v0.25.12
  2. write code in pkg/util/merge/merge_statefulset.go.StatefulSetSpecs

This issue is being marked stale because it has been open for 60 days with no activity. Please comment if this issue is still affecting you. If there is no change, this issue will be closed in 30 days.

This issue was closed because it became stale and did not receive further updates. If the issue is still affecting you, please re-open it, or file a fresh Issue with updated information.

We still really need this! :)