|
| 1 | +using KubeClient.Models; |
| 2 | +using Ocelot.Logging; |
| 3 | +using Ocelot.Provider.Kubernetes.Interfaces; |
| 4 | +using Ocelot.Values; |
| 5 | + |
| 6 | +namespace Ocelot.Provider.Kubernetes; |
| 7 | + |
| 8 | +// Dispose() won't be called because provider wasn't resolved from DI |
| 9 | +public class WatchKube : IServiceDiscoveryProvider, IDisposable |
| 10 | +{ |
| 11 | + private readonly KubeRegistryConfiguration _configuration; |
| 12 | + private readonly IOcelotLogger _logger; |
| 13 | + private readonly IKubeApiClient _kubeApi; |
| 14 | + private readonly IKubeServiceBuilder _serviceBuilder; |
| 15 | + |
| 16 | + private List<Service> _services = null; |
| 17 | + private readonly IDisposable _subscription; |
| 18 | + |
| 19 | + public WatchKube( |
| 20 | + KubeRegistryConfiguration configuration, |
| 21 | + IOcelotLoggerFactory factory, |
| 22 | + IKubeApiClient kubeApi, |
| 23 | + IKubeServiceBuilder serviceBuilder) |
| 24 | + { |
| 25 | + _configuration = configuration; |
| 26 | + _logger = factory.CreateLogger<Kube>(); |
| 27 | + _kubeApi = kubeApi; |
| 28 | + _serviceBuilder = serviceBuilder; |
| 29 | + |
| 30 | + _subscription = CreateSubscription(); |
| 31 | + } |
| 32 | + |
| 33 | + public virtual async Task<List<Service>> GetAsync() |
| 34 | + { |
| 35 | + // need to wait for first result fetching somehow |
| 36 | + if (_services is null) |
| 37 | + { |
| 38 | + await Task.Delay(1000); |
| 39 | + } |
| 40 | + |
| 41 | + if (_services is not { Count: > 0 }) |
| 42 | + { |
| 43 | + _logger.LogWarning(() => GetMessage("Subscription to service endpoints gave no results!")); |
| 44 | + } |
| 45 | + |
| 46 | + return _services; |
| 47 | + } |
| 48 | + |
| 49 | + private IDisposable CreateSubscription() => |
| 50 | + _kubeApi |
| 51 | + .EndpointsV1() |
| 52 | + .Watch(_configuration.KeyOfServiceInK8s, _configuration.KubeNamespace) |
| 53 | + .Subscribe( |
| 54 | + onNext: endpointEvent => |
| 55 | + { |
| 56 | + _services = endpointEvent.EventType switch |
| 57 | + { |
| 58 | + ResourceEventType.Deleted or ResourceEventType.Error => new(), |
| 59 | + _ when (endpointEvent.Resource?.Subsets?.Count ?? 0) == 0 => new(), |
| 60 | + _ => _serviceBuilder.BuildServices(_configuration, endpointEvent.Resource).ToList(), |
| 61 | + }; |
| 62 | + }, |
| 63 | + onError: ex => |
| 64 | + { |
| 65 | + // recreate subscription in case of exceptions? |
| 66 | + _logger.LogError(() => GetMessage("Endpoints subscription error occured"), ex); |
| 67 | + }, |
| 68 | + onCompleted: () => |
| 69 | + { |
| 70 | + // called only when subscription is cancelled |
| 71 | + _logger.LogWarning(() => GetMessage("Subscription to service endpoints completed")); |
| 72 | + }); |
| 73 | + |
| 74 | + private string GetMessage(string message) |
| 75 | + => $"{nameof(WatchKube)} provider. Namespace:{_configuration.KubeNamespace}, Service:{_configuration.KeyOfServiceInK8s}; {message}"; |
| 76 | + |
| 77 | + public void Dispose() => _subscription.Dispose(); |
| 78 | +} |
0 commit comments