public virtual async Task <KongvergeConfiguration> FromKong(IKongAdminReader kongReader) { Log.Information("Reading configuration from Kong"); Log.Verbose("Getting plugins from Kong"); var plugins = await kongReader.GetPlugins(); Log.Verbose("Getting services from Kong"); var services = await kongReader.GetServices(); Log.Verbose("Getting routes from Kong"); var routes = await kongReader.GetRoutes(); foreach (var existingService in services) { PopulateServiceTree(existingService, routes, plugins); } Log.Information($"Configuration from Kong contains {services.Count} {KongObject.GetName(0, "service")}, {plugins.Count} {KongObject.GetName(0, "plugin")}, {routes.Count} {KongObject.GetName(0, "route")}"); return(new KongvergeConfiguration { Services = services.ToArray(), GlobalConfig = new GlobalConfig { Plugins = plugins.Where(x => x.IsGlobal()).ToArray() } }); }
private static void LogObjectCounts(KongvergeConfiguration configuration) { var routes = configuration.Services.SelectMany(x => x.Routes).ToArray(); var pluginsCount = configuration.GlobalConfig.Plugins.Count; pluginsCount += configuration.Services.Sum(x => x.Plugins.Count) + routes.Sum(x => x.Plugins.Count); Log.Information($"Configuration from files contains {configuration.Services.Count} {KongObject.GetName(0, "service")}, {pluginsCount} {KongObject.GetName(0, "plugin")}, {routes.Length} {KongObject.GetName(0, "route")}"); }
public override string ToString() { return(string.Join(", ", Keys.Select(x => $"{this[x].Count} {KongObject.GetName(this[x].Count, this[x].ObjectName)}"))); }
private async Task ConvergeObjects <T>( string parent, string objectName, IReadOnlyCollection <T> existingObjects, IReadOnlyCollection <T> targetObjects, Func <T, Task> deleteObject, Func <T, Task> createObject, Func <T, Task> updateObject = null, Func <T, T, Task> recurse = null) where T : KongObject, IKongEquatable { existingObjects = existingObjects ?? Array.Empty <T>(); updateObject = updateObject ?? (x => Task.CompletedTask); recurse = recurse ?? ((e, t) => Task.CompletedTask); if (existingObjects.Count == 0 && targetObjects.Count == 0) { Log.Verbose($"Target {parent ?? "configuration"} and existing both have zero {KongObject.GetName(0, objectName)}"); return; } var targetPhrase = $"{targetObjects.Count} target {KongObject.GetName(targetObjects.Count, objectName)}"; var existingPhrase = $"{existingObjects.Count} existing {KongObject.GetName(existingObjects.Count, objectName)}"; var parentPhrase = parent == null ? string.Empty : $" attached to {parent}"; Log.Verbose($"Converging {targetPhrase}{parentPhrase} with {existingPhrase}"); var targetMatchValues = targetObjects.Select(x => x.GetMatchValue()).ToArray(); var toRemove = existingObjects.Where(x => !targetMatchValues.Contains(x.GetMatchValue())).ToArray(); foreach (var existing in toRemove) { Log.Verbose($"Deleting {objectName} {existing}{parentPhrase} which exists in Kong but not in target configuration"); await deleteObject(existing); _deletedStats.Increment <T>(); } foreach (var target in targetObjects) { var existing = target.MatchWithExisting(existingObjects); if (existing == null) { Log.Verbose($"Creating {objectName} {target}{parentPhrase} which exists in target configuration but not in Kong"); await createObject(target); _createdStats.Increment <T>(); } else if (target.Equals(existing)) { Log.Verbose($"Identical {objectName} {existing}{parentPhrase} found in Kong matching target configuration"); } else { var patch = target.DifferencesFrom(existing); Log.Verbose($"Updating {objectName} {existing}{parentPhrase} which exists in both Kong and target configuration, having the following differences:{Environment.NewLine}{patch}"); await updateObject(target); _updatedStats.Increment <T>(); } await recurse(existing, target); } }