-
-
Notifications
You must be signed in to change notification settings - Fork 511
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
Conversation
✅ Deploy Preview for testcontainers-go ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
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{ |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 🙏
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
mongodb.WithReplicaSet("rs0"), | ||
testcontainers.WithWaitStrategy(wait.ForLog("Waiting for connections")), |
There was a problem hiding this comment.
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?
There was a problem hiding this 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 |
There was a problem hiding this comment.
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
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! |
What does this PR do?
Fix issue #2138
Why is it important?
Related issues