feat: support automatic MongoDB database cleanup on stack destroy#178
Open
feat: support automatic MongoDB database cleanup on stack destroy#178
Conversation
When `mongoDbDestroyDatabase: true` is set in `cloudExtras` of a stack client config, the provisioner will automatically drop the corresponding MongoDB database before destroying the Pulumi stack. - Add `pkg/clouds/pulumi/mongodb/drop_db.go` with `DropDatabase` helper that connects to MongoDB and drops the target database - Extend `destroyStack` to accept optional `preDestroyHooks` so callers can inject cleanup logic before the actual destroy runs - In `DestroyChildStack`, read stack outputs to discover MongoDB service user credentials (keys ending in `-service-user`), then call `DropDatabase` when the feature flag is enabled - Add `go.mongodb.org/mongo-driver` and related transitive dependencies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
When
mongoDbDestroyDatabase: trueis set incloudExtrasof a stack client config, the provisioner will automatically drop the corresponding MongoDB database before the Pulumi stack is destroyed.Changes
pkg/clouds/pulumi/mongodb/drop_db.go(new file)DropDatabase(ctx, mongoUri, dbName)helper that connects to MongoDB (with a 30s timeout) and drops the target databasedbAdminprivileges on the target databasepkg/clouds/pulumi/destroy.godestroyStacksignature to accept variadicpreDestroyHooks ...func(auto.Stack)pkg/clouds/pulumi/provisioner.godropMongoDbIfEnabledmethod called as apreDestroyHookinDestroyChildStackcloudExtras.mongoDbDestroyDatabasefrom the stack's client config-service-user)DbUserOutputJSON to extractdbUri,userName, andpasswordDropDatabase; failures are logged as warnings (non-fatal) so the destroy proceeds regardlessgo.mod/go.sumgo.mongodb.org/mongo-driverand its transitive dependenciesConfiguration example
Behavior notes
mongoDbDestroyDatabaseis absent orfalse, no MongoDB operation is performed-service-userare used to discover MongoDB databases to drop