Ocelot icon indicating copy to clipboard operation
Ocelot copied to clipboard

Configuration for use by extensions, custom pipelines, or other extensions

Open philproctor opened this issue 6 years ago • 14 comments

New Feature

Add some container for storing configuration values into the configuration container into both the GlobalConfiguration and into the individual ReRoute configurations. Additionally, the creator methods for reroutes and possibly other configurations (such as service discovery) should have hooks that provide a mechanism for reloading these configurations on configuration change and adding properties to the configured object (such as the ReRoute object)

Motivation for New Feature

Currently the Ocelot configuration has predefined values based on what Ocelot internally uses. This works for official extensions, however it makes it difficult for third parties to extend with configurations that are not already in the standard FileConfiguration. When an option needs to be applied to an individual reroute, using the standard IConfiguration options from ASP.NET require knowing the array index and other information that may not be easily available.

philproctor avatar Jan 08 '19 04:01 philproctor

I really like this feature

margaale avatar Jan 10 '19 12:01 margaale

@margaale Any ideas as to what that configuration container should look like? That's where I'm a bit unsure as we need some way of providing a JSON Scheme onto FileConfiguration.

My initial thought is it would be a Dictionary<string, Dictionary<string, object>> and the "extension" would receive the Dictionary<string, object> from the key that matches the extension name. So it would be something like this:

"Extensions": {
    "ExtensionName": {
        "Key1": "Value1",
        "Key2": true
    }
}

Then the extension named "ExtensionName" would receive the Dictionary<string, object> that contains "Key1" with the string "Value1" and "Key2" with the boolean "true"

philproctor avatar Jan 10 '19 14:01 philproctor

That would be my first choice too, but probably we could Json.Deserialize the configuration into an user defined class

margaale avatar Jan 10 '19 17:01 margaale

The ASP.NET configuration used doesn't allow that out of the box without defining the structure up front, that I'm aware of. I think in order to do that we'd have to implement our own way of loading the configuration from disk instead of relying on ASP.NET's

philproctor avatar Jan 10 '19 18:01 philproctor

In OcelotMiddlewareExtensions.cs we have this:

            // make configuration from file system?
            // earlier user needed to add ocelot files in startup configuration stuff, asp.net will map it to this
            var fileConfig = builder.ApplicationServices.GetService<IOptionsMonitor<FileConfiguration>>();

            // now create the config
            var internalConfigCreator = builder.ApplicationServices.GetService<IInternalConfigurationCreator>();
            var internalConfig = await internalConfigCreator.Create(fileConfig.CurrentValue);

As you can see, it's already been noted that config could be made from the filesystem. If we did that instead, we could deserialize into a JObject easily and allow extensions to define their own configuration structured.

However, this comes with at least two costs... Cons to this approach are:

  • We would have to implement our own file change monitor. The IOptionsMonitor currently takes care of that for us.
  • Current code would break as the previous pattern of loading the config into ASP.NET would no longer work and we would require a config file location in UseOcelot()

philproctor avatar Jan 10 '19 20:01 philproctor

I know that part of the code, the optionmonitor was my contribution :-) There's is an administration API where you are able to modify the configuration and save it to the filesystem that we have to take into account too. We can load the configuration as your original idea and then add an extra layer where we transform it to the desired class. I don't want that the third party add-ons have to use the options extension. They would have to use that layer (service) to fetch their configuration. Sorry if I'm not being clear, English is not my first language...

margaale avatar Jan 10 '19 21:01 margaale

I'm leaning towards providing the Dictionary with options, at least with the initial implementation. Perhaps we could allow some further customization in an update, but doing it with the Dictionary requires the least amount of disruption with the current configuration system.

The transform layer would be difficult for any configuration that is not flat, we would need to reload the raw configuration. That may be feasible but would require a lot of mangling of the config, the Dictionary implementation is the simplest short-term to at least prove out the concept.

philproctor avatar Jan 11 '19 14:01 philproctor

Sure, let's start with the simple solution, we will have time to further refine it. Tell me if i can help you. I'm gonna give it a shota at the caching issue in the meanwhile

margaale avatar Jan 11 '19 16:01 margaale

Sounds good, I'll start work on this one soon. I'll probably also pick up #737 as I have found that one would resolve a number of other issues/requests in the backlog.

philproctor avatar Jan 11 '19 17:01 philproctor

This is interesting, I am not really clear what problem it is solving in my head or what it would look like though!

TomPallister avatar Jan 13 '19 12:01 TomPallister

hi, this feature would be great, any update?

gchadid avatar Oct 30 '19 21:10 gchadid

Related to

  • #651
  • #1183

raman-m avatar Dec 08 '23 12:12 raman-m

@philproctor What's up, Phil?

Your feature has been accepted. So, the feature is in development now officially. Congrats! 🎉 Are you still with Ocelot?

raman-m avatar Dec 11 '23 13:12 raman-m

@vantm Welcome! You are assigned!

raman-m avatar Mar 05 '24 09:03 raman-m

Closed by #1843

raman-m avatar May 21 '24 20:05 raman-m