Feature/removed consul and its deps to other package (#539)

* #529 removed consul deps and introduced delegate to find service discovery provider

* +semver: breaking moved consul configuration to package..introduced mechanism for packages to configure Ocelot pipeline
This commit is contained in:
Tom Pallister
2018-08-12 17:28:41 +05:30
committed by GitHub
parent a91235b405
commit 87348e5d1b
11 changed files with 58 additions and 92 deletions

View File

@ -67,7 +67,7 @@ namespace Ocelot.Configuration.Repository
private async Task Poll()
{
_logger.LogInformation("Started polling consul");
_logger.LogInformation("Started polling");
var fileConfig = await _repo.Get();
@ -91,7 +91,7 @@ namespace Ocelot.Configuration.Repository
_previousAsJson = asJson;
}
_logger.LogInformation("Finished polling consul");
_logger.LogInformation("Finished polling");
}
/// <summary>

View File

@ -1,12 +0,0 @@
using Ocelot.Errors;
namespace Ocelot.Configuration.Repository
{
public class UnableToSetConfigInConsulError : Error
{
public UnableToSetConfigInConsulError(string message)
: base(message, OcelotErrorCode.UnableToSetConfigInConsulError)
{
}
}
}

View File

@ -29,7 +29,6 @@
UnableToFindLoadBalancerError,
RequestTimedOutError,
UnableToFindQoSProviderError,
UnableToSetConfigInConsulError,
UnmappableRequestError,
RateLimitOptionsError,
PathTemplateDoesntStartWithForwardSlash,

View File

@ -0,0 +1,7 @@
namespace Ocelot.Middleware
{
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
public delegate Task OcelotMiddlewareConfigurationDelegate(IApplicationBuilder builder);
}

View File

@ -134,16 +134,17 @@
internalConfigRepo.AddOrReplace(newInternalConfig.Data);
});
var fileConfigRepo = builder.ApplicationServices.GetService<IFileConfigurationRepository>();
var adminPath = builder.ApplicationServices.GetService<IAdministrationPath>();
if (UsingConsul(fileConfigRepo))
var configurations = builder.ApplicationServices.GetServices<OcelotMiddlewareConfigurationDelegate>();
// Todo - this has just been added for consul so far...will there be an ordering problem in the future? Should refactor all config into this pattern?
foreach (var configuration in configurations)
{
//Lots of jazz happens in here..check it out if you are using consul to store your config.
await SetFileConfigInConsul(builder, fileConfigRepo, fileConfig, internalConfigCreator, internalConfigRepo);
await configuration(builder);
}
else if(AdministrationApiInUse(adminPath))
if(AdministrationApiInUse(adminPath))
{
//We have to make sure the file config is set for the ocelot.env.json and ocelot.json so that if we pull it from the
//admin api it works...boy this is getting a spit spags boll.
@ -160,49 +161,6 @@
return adminPath != null;
}
private static async Task SetFileConfigInConsul(IApplicationBuilder builder,
IFileConfigurationRepository fileConfigRepo, IOptionsMonitor<FileConfiguration> fileConfig,
IInternalConfigurationCreator internalConfigCreator, IInternalConfigurationRepository internalConfigRepo)
{
// get the config from consul.
var fileConfigFromConsul = await fileConfigRepo.Get();
if (IsError(fileConfigFromConsul))
{
ThrowToStopOcelotStarting(fileConfigFromConsul);
}
else if (ConfigNotStoredInConsul(fileConfigFromConsul))
{
//there was no config in consul set the file in config in consul
await fileConfigRepo.Set(fileConfig.CurrentValue);
}
else
{
// create the internal config from consul data
var internalConfig = await internalConfigCreator.Create(fileConfigFromConsul.Data);
if (IsError(internalConfig))
{
ThrowToStopOcelotStarting(internalConfig);
}
else
{
// add the internal config to the internal repo
var response = internalConfigRepo.AddOrReplace(internalConfig.Data);
if (IsError(response))
{
ThrowToStopOcelotStarting(response);
}
}
if (IsError(internalConfig))
{
ThrowToStopOcelotStarting(internalConfig);
}
}
}
private static async Task SetFileConfig(IFileConfigurationSetter fileConfigSetter, IOptionsMonitor<FileConfiguration> fileConfig)
{
var response = await fileConfigSetter.Set(fileConfig.CurrentValue);
@ -213,11 +171,6 @@
}
}
private static bool ConfigNotStoredInConsul(Responses.Response<FileConfiguration> fileConfigFromConsul)
{
return fileConfigFromConsul.Data == null;
}
private static bool IsError(Response response)
{
return response == null || response.IsError;
@ -240,12 +193,6 @@
throw new Exception($"Unable to start Ocelot, errors are: {string.Join(",", config.Errors.Select(x => x.ToString()))}");
}
private static bool UsingConsul(IFileConfigurationRepository fileConfigRepo)
{
//todo - remove coupling by string
return fileConfigRepo.GetType().Name == "ConsulFileConfigurationRepository";
}
private static void CreateAdministrationArea(IApplicationBuilder builder, IInternalConfiguration configuration)
{
if (!string.IsNullOrEmpty(configuration.AdministrationPath))

View File

@ -0,0 +1,8 @@
namespace Ocelot.ServiceDiscovery
{
using System;
using Ocelot.Configuration;
using Providers;
public delegate IServiceDiscoveryProvider ServiceDiscoveryFinderDelegate(IServiceProvider provider, ServiceProviderConfiguration config, string key);
}

View File

@ -60,14 +60,16 @@ namespace Ocelot.ServiceDiscovery
return new EurekaServiceDiscoveryProvider(key, _eurekaClient);
}
// Todo - dont just hardcode this...only expect Consul at the momement so works.
var finderDelegate = _delegates.FirstOrDefault();
foreach (var serviceDiscoveryFinderDelegate in _delegates)
{
var provider = serviceDiscoveryFinderDelegate?.Invoke(_provider, config, key);
if (provider != null)
{
return provider;
}
}
var provider = finderDelegate?.Invoke(_provider, config, key);
return provider;
return null;
}
}
public delegate IServiceDiscoveryProvider ServiceDiscoveryFinderDelegate(IServiceProvider provider, ServiceProviderConfiguration config, string key);
}