Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(modules.mongodb): setup mongodb replicaset #2139

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

trungdlp-wolffun
Copy link

What does this PR do?

Fix issue #2138

Why is it important?

Related issues

@trungdlp-wolffun trungdlp-wolffun requested a review from a team as a code owner January 23, 2024 12:12
Copy link

netlify bot commented Jan 23, 2024

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit 028c5c5
🔍 Latest deploy log https://app.netlify.com/sites/testcontainers-go/deploys/65b214c8ce225b0008c85662
😎 Deploy Preview https://deploy-preview-2139--testcontainers-go.netlify.app/modules/mongodb
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@eddumelendez
Copy link
Member

eddumelendez commented Jan 23, 2024

Thanks for your contribution, @trungdlp-wolffun. Currently, the module support standalone mode by default. Adding ReplicaSet is really nice, however, IMO, this should be an opt-in feature. In the future, sharding can be added as well as a opt-in feature too.

Please, do not forget to add proper test.


func replicaSetLifecycleHooks() testcontainers.ContainerLifecycleHooks {
return testcontainers.ContainerLifecycleHooks{
PostStarts: []testcontainers.ContainerHook{
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it would be clearer if we append the container hook functions directly to the PostStarts hook, instead of appending a complete lifecycle hook struct. In the end, the result is the same, but having them all together in the same lifecycle hook seems better aligned, as they all belong to the same hook's post-start events. Wdyt?

Copy link
Author

Choose a reason for hiding this comment

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

I move replicaSetPostStart code into replicaSetLifecycleHooks, right? @mdelapenya

Copy link
Collaborator

Choose a reason for hiding this comment

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

You could leverage the Options pattern we are seen in other modules, such as Redpanda, where the Options struct holds all the values to be applied and all the functional options to configure the module are setting the values into that options struct.

Finally, I'd define a container lifecycle hook in that options, and with each call to the WithReplicaSet function, I'd be appending to the PostStarts field of the lifecycle hook. Last thing would be to append the mongo lifecycle hook to the container request before starting the container, right after all the options have been applied.

Does it make sense to you?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@trungdlp-wolffun please let us know if you need anything from us 🙏

Copy link
Author

Choose a reason for hiding this comment

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

@mdelapenya Thanks for your help, I will back soon.

// WithReplicaSet TODO: help me fill this func comment
func WithReplicaSet(rsName string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) {
req.Env["MONGO_REPLICASET_NAME"] = rsName
Copy link
Member

Choose a reason for hiding this comment

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

why is setting the env var also required?

Comment on lines +128 to +129
mongodb.WithReplicaSet("rs0"),
testcontainers.WithWaitStrategy(wait.ForLog("Waiting for connections")),
Copy link
Member

Choose a reason for hiding this comment

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

IMO, the replicaSet opt-in should be just WithReplicaSet() and it should be stored in a flag used to set the right wait strategy and exec the commands in the container lifecycle. However, not sure how important is setting the replica set name but having a default could be just enough?

Copy link
Member

@eddumelendez eddumelendez left a comment

Choose a reason for hiding this comment

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

Thanks for the update and sorry for the delay. Just left a few more comments to improve experience.

if c.username != "" && c.password != "" {
connStr.WriteString(fmt.Sprintf("%s:%s@", c.username, c.password))
return fmt.Sprintf("mongodb://%s:%s@%s:%s", c.username, c.password, host, port.Port()), nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

IIUC from this block, if there is an username and password, the connection string won't have anything about the replicaSet, even though it was passed as an option, right?

If not sure, you can extract the url-builder part to a function and add unit tests for it

@mdelapenya
Copy link
Collaborator

Hi @trungdlp-wolffun I think #2469 has better readability so I think we are going to build on top of that. This PR will be closed once that one is merged.

I'm sorry if the review took so long 🙏

Thanks!

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.

None yet

3 participants