Skip to content

Customize MongoReplicaSetResource options #70

@quoctintran

Description

@quoctintran

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions