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

Customize MongoReplicaSetResource options #70

Open
quoctintran opened this issue Nov 20, 2020 · 0 comments
Open

Customize MongoReplicaSetResource options #70

quoctintran opened this issue Nov 20, 2020 · 0 comments

Comments

@quoctintran
Copy link
Contributor

quoctintran commented Nov 20, 2020

Hi

Is your feature request related to a problem? Please describe.
We use MongoDB with replica sets to support transactions. We wanted to define another image and prefer the local image instead of pulling it every time (due to Docker Hub's new pull rate limit). Therefore we have to implement an own options class to override the Configure method. But to use Mongo with replica sets and our options we have to create a custom class which also inherits from MongoResource but with the own options class as generic parameter and this looks the same as the MongoReplicaSetResource.

    public class MongoContainerOptions : MongoDefaultOptions
    {
        public MongoContainerOptions(string replicaSetName)
        {
            ReplicaSetName = replicaSetName;
        }

        public MongoContainerOptions()
            : this("rs0")
        {
        }

        internal string ReplicaSetName { get; }

        public override void Configure(ContainerResourceBuilder builder)
        {
            base.Configure(builder);

            builder.AddCmd("--replSet", ReplicaSetName)
                .Image("mongo:bionic")
                .PreferLocalImage();
        }
    }

    public class MongoContainerResource : MongoResource<MongoContainerOptions>
    {
        public override IMongoClient Client => new MongoClient(ConnectionString);

        public async override Task InitializeAsync()
        {
            await base.InitializeAsync();
            var client = new MongoClient(ConnectionString + "/?connect=direct");
            var rsConfig = CreateReplicaSetConfiguration();
            var command = new BsonDocumentCommand<BsonDocument>(new BsonDocument
            {
                { "replSetInitiate", rsConfig },
            });

            await client.GetDatabase("admin").RunCommandAsync(command);

            await Initializer.WaitAsync(new MongoReplicaSetStatus(ConnectionString));
        }

        private BsonDocument CreateReplicaSetConfiguration()
        {
            var membersDocument = new BsonArray
            {
                new BsonDocument
                {
                    { "_id", 0 },
                    { "host", $"{Manager.Instance.Address}:{Settings.InternalPort}" },
                },
            };

            var cfg = new BsonDocument
            {
                { "_id", ResourceOptions.ReplicaSetName },
                { "members", membersDocument },
            };

            return cfg;
        }
    }

Describe the solution you'd like
Is it possible also to provide a MongoReplicaSetResource class where we can inherit with a custom options class without implementing again the logic of the current MongoReplicaSetResource class (as you have done with MongoResource)?

    public class MongoContainerOptions : MongoReplicaSetDefaultOptions
    {
        public MongoContainerOptions(string replicaSetName)
            : base(replicaSetName)
        {
        }

        public MongoContainerOptions()
            : this("rs0")
        {
        }

        public override void Configure(ContainerResourceBuilder builder)
        {
            base.Configure(builder);

            builder.AddCmd("--replSet", ReplicaSetName)
                .Image("mongo:bionic")
                .PreferLocalImage();
        }
    }

    public class MongoContainerResource : MongoReplicaSetResource<MongoContainerOptions>
    {
    }

Additional context
Locally we use WSL2 as docker backend and on our build server we use Windows Server 2019 1809 with LCOW to run linux containers in Hyper-V isolation.

Thank you!

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

No branches or pull requests

1 participant