Skip to content

Commit

Permalink
Hotfix: Garbage collection and ConfigMap watcher
Browse files Browse the repository at this point in the history
- Fixed garbage collection profile (broken commit)
- Fixed ConfigMap mirror being disabled (broken commit)
  • Loading branch information
winromulus committed Oct 16, 2021
1 parent e5961d5 commit 5d80de4
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 134 deletions.
2 changes: 1 addition & 1 deletion src/ES.Kubernetes.Reflector.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31710.8
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ES.Kubernetes.Reflector", "ES.Kubernetes.Reflector\ES.Kubernetes.Reflector.csproj", "{96CDE0CF-7782-490B-8AF6-4219DB0236B3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ES.Kubernetes.Reflector", "ES.Kubernetes.Reflector\ES.Kubernetes.Reflector.csproj", "{96CDE0CF-7782-490B-8AF6-4219DB0236B3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
public static class Annotations
{
public const string Prefix = "reflector.v1.k8s.emberstack.com";
public const string MetaPrefix = $"{Prefix}";

public static class Reflection
{
Expand All @@ -14,79 +13,10 @@ public static class Reflection
public static string Reflects => $"{Prefix}/reflects";


public static string MetaAutoReflects => $"{MetaPrefix}/auto-reflects";
public static string MetaReflectedVersion => $"{MetaPrefix}/reflected-version";
public static string MetaReflectedAt => $"{MetaPrefix}/reflected-at";
public static string MetaAutoReflects => $"{Prefix}/auto-reflects";
public static string MetaReflectedVersion => $"{Prefix}/reflected-version";
public static string MetaReflectedAt => $"{Prefix}/reflected-at";


#region Forti

public static string FortiEnabled => $"{Prefix}/reflection-forti-enabled";
public static string FortiHosts => $"{Prefix}/reflection-forti-hosts";
public static string FortiCertificate => $"{Prefix}/reflection-forti-certificate";

#endregion


#region Ubiquiti

public static string UbiquitiEnabled => $"{Prefix}/reflection-ubiquiti-enabled";
public static string UbiquitiHosts => $"{Prefix}/reflection-ubiquiti-hosts";
public static string UbiquitiCertificate => $"{Prefix}/reflection-ubiquiti-certificate";

#endregion

#region VMware

public static string VMwareEnabled => $"{Prefix}/reflection-vmware-enabled";
public static string VMwareHosts => $"{Prefix}/reflection-vmware-hosts";
public static string VMwareCertificate => $"{Prefix}/reflection-vmware-certificate";

#endregion

#region FreeNAS

public static string FreeNasEnabled => $"{Prefix}/reflection-freenas-enabled";
public static string FreeNasHosts => $"{Prefix}/reflection-freenas-hosts";
public static string FreeNasCertificate => $"{Prefix}/reflection-freenas-certificate";

#endregion
}

public static class CertManagerCertificate
{
public static string SecretReflectionAllowed => $"{Prefix}/secret-reflection-allowed";
public static string SecretReflectionAllowedNamespaces => $"{Prefix}/secret-reflection-allowed-namespaces";

public static string SecretReflectionAutoEnabled => $"{Prefix}/secret-reflection-auto-enabled";
public static string SecretReflectionAutoNamespaces => $"{Prefix}/secret-reflection-auto-namespaces";

public static string SecretFortiEnabled => $"{Prefix}/secret-reflection-forti-enabled";
public static string SecretFortiHosts => $"{Prefix}/secret-reflection-forti-hosts";
public static string SecretFortiCertificate => $"{Prefix}/secret-reflection-forti-certificate";

#region Ubiquiti

public static string SecretUbiquitiEnabled => $"{Prefix}/secret-reflection-ubiquiti-enabled";
public static string SecretUbiquitiHosts => $"{Prefix}/secret-reflection-ubiquiti-hosts";
public static string SecretUbiquitiCertificate => $"{Prefix}/secret-reflection-ubiquiti-certificate";

#endregion

#region VMware

public static string SecretVMwareEnabled => $"{Prefix}/secret-reflection-vmware-enabled";
public static string SecretVMwareHosts => $"{Prefix}/secret-reflection-vmware-hosts";
public static string SecretVMwareCertificate => $"{Prefix}/secret-reflection-vmware-certificate";

#endregion

#region FreeNAS

public static string SecretFreeNasEnabled => $"{Prefix}/secret-reflection-freenas-enabled";
public static string SecretFreeNasHosts => $"{Prefix}/secret-reflection-freenas-hosts";
public static string SecretFreeNasCertificate => $"{Prefix}/secret-reflection-freenas-certificate";

#endregion
}
}
107 changes: 50 additions & 57 deletions src/ES.Kubernetes.Reflector/Core/Mirroring/ResourceMirror.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public async Task Handle(WatcherEvent notification, CancellationToken cancellati
case TResource resource:
if (await OnResourceIgnoreCheck(resource)) return;

Logger.LogTrace("Handling {eventType} {resourceType} {resourceRef}", notification.Type, resource.Kind, resource.GetRef());
Logger.LogTrace("Handling {eventType} {resourceType} {resourceRef}", notification.Type, resource.Kind,
resource.GetRef());


var itemRef = resource.GetRef();
Expand All @@ -68,66 +69,67 @@ public async Task Handle(WatcherEvent notification, CancellationToken cancellati
{
case WatchEventType.Added:
case WatchEventType.Modified:
{
await HandleUpsert(resource, notification.Type, cancellationToken);
}
{
await HandleUpsert(resource, notification.Type, cancellationToken);
}
break;
case WatchEventType.Deleted:
{
_propertiesCache.Remove(itemRef, out _);
var properties = resource.GetReflectionProperties();


if (!properties.IsReflection)
{
_propertiesCache.Remove(itemRef, out _);
var properties = resource.GetReflectionProperties();


if (!properties.IsReflection)
{
if (properties.Allowed && properties.AutoEnabled &&
_autoReflectionCache.TryGetValue(itemRef, out var reflectionList))
foreach (var reflectionId in reflectionList.ToArray())
{
Logger.LogDebug("Deleting {id} - Source {sourceId} has been deleted", reflectionId,
itemRef);
await OnResourceDelete(reflectionId);
}

_autoSources.Remove(itemRef, out _);
_directReflectionCache.Remove(itemRef, out _);
_autoReflectionCache.Remove(itemRef, out _);
}
else
{
foreach (var item in _directReflectionCache) item.Value.Remove(itemRef);
foreach (var item in _autoReflectionCache) item.Value.Remove(itemRef);
}
if (properties.Allowed && properties.AutoEnabled &&
_autoReflectionCache.TryGetValue(itemRef, out var reflectionList))
foreach (var reflectionId in reflectionList.ToArray())
{
Logger.LogDebug("Deleting {id} - Source {sourceId} has been deleted", reflectionId,
itemRef);
await OnResourceDelete(reflectionId);
}

_autoSources.Remove(itemRef, out _);
_directReflectionCache.Remove(itemRef, out _);
_autoReflectionCache.Remove(itemRef, out _);
}
else
{
foreach (var item in _directReflectionCache) item.Value.Remove(itemRef);
foreach (var item in _autoReflectionCache) item.Value.Remove(itemRef);
}
}
break;
default:
return;
}

break;
case V1Namespace ns:
{
if (notification.Type != WatchEventType.Added) return;
Logger.LogTrace("Handling {eventType} {resourceType} {resourceRef}", notification.Type, ns.Kind, ns.GetRef());
{
if (notification.Type != WatchEventType.Added) return;
Logger.LogTrace("Handling {eventType} {resourceType} {resourceRef}", notification.Type, ns.Kind,
ns.GetRef());


foreach (var autoSourceRef in _autoSources.Keys)
foreach (var autoSourceRef in _autoSources.Keys)
{
var properties = _propertiesCache[autoSourceRef];
if (properties.CanBeAutoReflectedToNamespace(ns.Name()))
{
var properties = _propertiesCache[autoSourceRef];
if (properties.CanBeAutoReflectedToNamespace(ns.Name()))
{
var reflectionRef = new KubeRef(ns.Name(), autoSourceRef.Name);
var autoReflectionList = _autoReflectionCache.GetOrAdd(autoSourceRef, new List<KubeRef>());
var reflectionRef = new KubeRef(ns.Name(), autoSourceRef.Name);
var autoReflectionList = _autoReflectionCache.GetOrAdd(autoSourceRef, new List<KubeRef>());

if (autoReflectionList.Contains(reflectionRef)) return;
if (autoReflectionList.Contains(reflectionRef)) return;

await ResourceReflect(autoSourceRef, reflectionRef, null, null, true);
await ResourceReflect(autoSourceRef, reflectionRef, null, null, true);

if (!autoReflectionList.Contains(reflectionRef))
autoReflectionList.Add(reflectionRef);
}
if (!autoReflectionList.Contains(reflectionRef))
autoReflectionList.Add(reflectionRef);
}
}
}
break;
}
}
Expand Down Expand Up @@ -250,7 +252,6 @@ private async Task HandleUpsert(TResource resource, WatchEventType eventType, Ca
}

await ResourceReflect(sourceRef, resourceRef, null, resource, false);

}


Expand All @@ -261,7 +262,8 @@ private async Task HandleUpsert(TResource resource, WatchEventType eventType, Ca
}
}

private async Task TriggerAutoReflectionForSource(KubeRef resourceRef, KubeRef reflectionRef, CancellationToken cancellationToken)
private async Task TriggerAutoReflectionForSource(KubeRef resourceRef, KubeRef reflectionRef,
CancellationToken cancellationToken)
{
if (_notFoundCache.ContainsKey(resourceRef))
{
Expand All @@ -279,6 +281,7 @@ private async Task TriggerAutoReflectionForSource(KubeRef resourceRef, KubeRef r
await OnResourceDelete(reflectionRef);
return;
}

properties = resourceCached.GetReflectionProperties();
}
else
Expand Down Expand Up @@ -326,17 +329,14 @@ private async Task AutoReflectionForSource(KubeRef resourceRef, TResource? resou
foreach (var kubeRef in toDelete) await OnResourceDelete(kubeRef);

resourceInstance ??= await TryResourceGet(resourceRef);
if (resourceInstance is null)
{
return;
}
if (resourceInstance is null) return;
var resource = resourceInstance;

var toCreate = namespaces
.Where(s => s.Name() != resourceRef.Namespace)
.Where(s =>
matches.All(m => m.Namespace() != s.Name()) && properties.CanBeAutoReflectedToNamespace(s.Name()))
.Select(s => new KubeRef(s.Name(),resource.Name())).ToList();
.Select(s => new KubeRef(s.Name(), resource.Name())).ToList();

var toUpdate = matches
.Where(s => s.Namespace() != resourceRef.Namespace)
Expand All @@ -359,16 +359,11 @@ private async Task AutoReflectionForSource(KubeRef resourceRef, TResource? resou
autoReflectionList.AddRange(toSkip);
autoReflectionList.AddRange(toUpdate);

foreach (var reflectionRef in toCreate)
{
await ResourceReflect(resourceRef, reflectionRef, resource, null, true);

}
foreach (var reflectionRef in toCreate) await ResourceReflect(resourceRef, reflectionRef, resource, null, true);
foreach (var reflectionRef in toUpdate)
{
var reflection = matches.Single(s => s.GetRef().Equals(reflectionRef));
await ResourceReflect(resourceRef, reflectionRef, resource, reflection, true);

}
}

Expand Down Expand Up @@ -466,8 +461,6 @@ private async Task ResourceReflect(KubeRef sourceId, KubeRef targetId, TResource
}




protected abstract Task OnResourceApplyPatch(V1Patch source, KubeRef refId);
protected abstract Task OnResourceConfigurePatch(TResource source, JsonPatchDocument<TResource> patchDoc);
protected abstract Task OnResourceCreate(TResource item, string ns);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
try
{
Logger.LogInformation("Requesting {type} resources", typeof(TResource).Name);
var watcher = OnGetWatcher(stoppingToken);
using var watcher = OnGetWatcher(stoppingToken);
var watchList = watcher.WatchAsync<TResource, TResourceList>();

await foreach (var (type, item) in watcher.WatchAsync<TResource, TResourceList>()
await foreach (var (type, item) in watchList
.WithCancellation(stoppingToken))
await Mediator.Publish(new WatcherEvent
{
Expand Down
1 change: 1 addition & 0 deletions src/ES.Kubernetes.Reflector/ES.Kubernetes.Reflector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<ServerGarbageCollection>fakse</ServerGarbageCollection>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/ES.Kubernetes.Reflector/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
container.RegisterType<SecretMirror>().AsImplementedInterfaces().SingleInstance();
container.RegisterType<ConfigMapWatcher>().AsImplementedInterfaces().SingleInstance();
//container.RegisterType<ConfigMapMirror>().AsImplementedInterfaces().SingleInstance();
container.RegisterType<ConfigMapMirror>().AsImplementedInterfaces().SingleInstance();
});

builder.WebHost.UseUrls("http://*:25080");
Expand Down

0 comments on commit 5d80de4

Please sign in to comment.