-
Notifications
You must be signed in to change notification settings - Fork 3
Optimized the Kubernetes API calls. ~97% reduction, removed unused code #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 0 additions & 56 deletions
56
src/Vecc.K8s.MultiCluster.Api.Tests/Services/Default/DefaultNamespaceManagerTests.cs
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/Vecc.K8s.MultiCluster.Api/Services/Default/DefaultBasicCache.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| using Microsoft.Extensions.Caching.Memory; | ||
| using M = Microsoft.Extensions.Caching.Memory; | ||
|
|
||
| namespace Vecc.K8s.MultiCluster.Api.Services.Default | ||
| { | ||
| public class BasicCache : IBasicCache | ||
| { | ||
| private readonly M.MemoryCache _cache; | ||
|
|
||
| public BasicCache(M.IMemoryCache cache) | ||
| { | ||
| _cache = cache as M.MemoryCache ?? throw new ArgumentException("Expected MemoryCache", nameof(cache)); | ||
| } | ||
|
|
||
| public IEnumerable<object> Keys => _cache.Keys; | ||
|
|
||
| public T? Get<T>(string key) | ||
| => _cache.Get<T>(key); | ||
|
|
||
| public T? GetOrCreate<T>(string key, Func<T> createFunc) | ||
| => _cache.GetOrCreate(key, (_) => createFunc()); | ||
|
|
||
| public bool TryGetValue<T>(string key, out T? value) | ||
| => _cache.TryGetValue(key, out value); | ||
|
|
||
| public void Set<T>(string key, T value) | ||
| => _cache.Set(key, value); | ||
|
|
||
| public void Remove(string key) | ||
| => _cache.Remove(key); | ||
| } | ||
| } |
136 changes: 136 additions & 0 deletions
136
src/Vecc.K8s.MultiCluster.Api/Services/Default/DefaultCache.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| using Microsoft.Extensions.Options; | ||
| using Vecc.K8s.MultiCluster.Api.Models.K8sEntities; | ||
| using k8s.Models; | ||
|
|
||
| namespace Vecc.K8s.MultiCluster.Api.Services.Default; | ||
|
|
||
| public class MemoryCache( | ||
| IBasicCache _cache, | ||
| IKubernetesCache _kubernetesCache, | ||
| IOptions<MultiClusterOptions> _options) : ICache | ||
| { | ||
| public Task<DateTime?> GetClusterHeartbeatTimeAsync(string clusterIdentifier) | ||
| => _kubernetesCache.GetClusterHeartbeatTimeAsync(clusterIdentifier); | ||
|
|
||
| public Task<string[]> GetClusterIdentifiersAsync() | ||
| => _kubernetesCache.GetClusterIdentifiersAsync(); | ||
|
|
||
| public async Task<int> GetEndpointsCountAsync(string ns, string name) | ||
| { | ||
| var service = await GetOrCreateServiceCache(ns, name, false); | ||
| var result = service?.EndpointCount ?? 0; | ||
| return result; | ||
| } | ||
|
|
||
| public Task<Models.Core.Host?> GetHostInformationAsync(string hostname) | ||
| => _kubernetesCache.GetHostInformationAsync(hostname); | ||
|
|
||
| public Task<string[]> GetHostnamesAsync() | ||
| => _kubernetesCache.GetHostnamesAsync(); | ||
|
|
||
| public Task<Models.Core.Host[]?> GetHostsAsync(string clusterIdentifier) | ||
| => _kubernetesCache.GetHostsAsync(clusterIdentifier); | ||
|
|
||
| public Task<string> GetLastResourceVersionAsync(string uniqueIdentifier) | ||
| { | ||
| lock (_cache) | ||
| { | ||
| _cache.TryGetValue(GetResourceVersionKey(uniqueIdentifier), out string? cacheValue); | ||
| var result = cacheValue ?? string.Empty; | ||
| return Task.FromResult(result); | ||
| } | ||
| } | ||
|
|
||
| public Task<bool> IsServiceMonitoredAsync(string ns, string name) | ||
| => Task.FromResult(_cache.TryGetValue<V1ServiceCache>(GetServiceCacheKey(ns, name), out _)); | ||
|
|
||
| public Task RemoveClusterCacheAsync(string clusterIdentifier) | ||
| => _kubernetesCache.RemoveClusterCacheAsync(clusterIdentifier); | ||
|
|
||
| public Task SetClusterCacheAsync(string identifier, Models.Core.Host[] hosts) | ||
| => _kubernetesCache.SetClusterCacheAsync(identifier, hosts); | ||
|
|
||
| public Task SetClusterHeartbeatAsync(string clusterIdentifier, DateTime heartbeat) | ||
| => _kubernetesCache.SetClusterHeartbeatAsync(clusterIdentifier, heartbeat); | ||
|
|
||
| public async Task SetEndpointsCountAsync(string ns, string name, int count) | ||
| { | ||
| var serviceCache = await GetOrCreateServiceCache(ns, name); | ||
| serviceCache!.EndpointCount = count; | ||
| } | ||
|
|
||
| public Task SetResourceVersionAsync(string uniqueIdentifier, string version) | ||
| { | ||
| lock (_cache) | ||
| { | ||
| _cache.Set(GetResourceVersionKey(uniqueIdentifier), version); | ||
| } | ||
| return Task.CompletedTask; | ||
|
|
||
| } | ||
|
|
||
| public Task SynchronizeCachesAsync() | ||
| => _kubernetesCache.SynchronizeCachesAsync(); | ||
|
|
||
| public Task TrackServiceAsync(string ns, string name) | ||
| => GetOrCreateServiceCache(ns, name); | ||
|
|
||
| public Task UntrackAllServicesAsync() | ||
| { | ||
| var rawKeys = Array.Empty<object>(); | ||
|
|
||
| lock (_cache) | ||
| { | ||
| rawKeys = _cache.Keys.ToArray(); | ||
| } | ||
|
|
||
| var keys = rawKeys.OfType<string>() | ||
| .Where(key => key.StartsWith("service-")); | ||
|
|
||
| foreach (var key in keys) | ||
| { | ||
| _cache.Remove(key); | ||
| } | ||
|
|
||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| private Task<V1ServiceCache?> GetOrCreateServiceCache(string namespaceName, string name, bool createMissing = true) | ||
| { | ||
| if (createMissing) | ||
| { | ||
| var cacheKey = GetServiceCacheKey(namespaceName, name); | ||
| var result = _cache.GetOrCreate(cacheKey, () => | ||
| { | ||
| var serviceCache = new V1ServiceCache(); | ||
| var metadata = serviceCache.EnsureMetadata(); | ||
| var labels = metadata.EnsureLabels(); | ||
|
|
||
| labels["namespace"] = namespaceName; | ||
| labels["name"] = name; | ||
|
|
||
| metadata.Name = namespaceName + "." + name; | ||
| metadata.SetNamespace(_options.Value.Namespace); | ||
| serviceCache.Service = new V1ObjectReference | ||
| { | ||
| Name = name, | ||
| NamespaceProperty = namespaceName | ||
| }; | ||
| return serviceCache; | ||
| } | ||
| ); | ||
| return Task.FromResult(result); | ||
| } | ||
|
|
||
| return Task.FromResult(_cache.Get<V1ServiceCache>(GetServiceCacheKey(namespaceName, name))); | ||
| } | ||
|
|
||
| private string GetIngressKey(string ns, string name) | ||
| => $"ingress-{ns}-{name}"; | ||
|
|
||
| private string GetResourceVersionKey(string uniqueIdentifier) | ||
| => $"resourceVersion-{uniqueIdentifier}"; | ||
|
|
||
| private string GetServiceCacheKey(string ns, string name) | ||
| => $"service-{ns}-{name}"; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.