forked from dotnet/yarp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathV1IngressResourceStatusUpdater.cs
More file actions
67 lines (62 loc) · 2.91 KB
/
V1IngressResourceStatusUpdater.cs
File metadata and controls
67 lines (62 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Linq;
using k8s;
using k8s.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Threading.Tasks;
using Yarp.Kubernetes.Controller.Caching;
using System.Threading;
namespace Yarp.Kubernetes.Controller.Client;
internal sealed class V1IngressResourceStatusUpdater : IIngressResourceStatusUpdater
{
private readonly IKubernetes _client;
private readonly YarpOptions _options;
private readonly ICache _cache;
private readonly ILogger _logger;
public V1IngressResourceStatusUpdater(
IKubernetes client,
IOptions<YarpOptions> options,
ICache cache,
ILogger<V1ServiceResourceInformer> logger)
{
ArgumentNullException.ThrowIfNull(options?.Value);
_options = options.Value;
_client = client;
_cache = cache;
_logger = logger;
}
public async Task UpdateStatusAsync(CancellationToken cancellationToken)
{
var service = await _client.CoreV1.ReadNamespacedServiceStatusAsync(_options.ControllerServiceName, _options.ControllerServiceNamespace, cancellationToken: cancellationToken);
if (service.Status?.LoadBalancer?.Ingress is { } loadBalancerIngresses)
{
var status = new V1IngressStatus
{
LoadBalancer = new V1IngressLoadBalancerStatus
{
Ingress = loadBalancerIngresses?.Select(ingress => new V1IngressLoadBalancerIngress
{
Hostname = ingress.Hostname,
Ip = ingress.Ip,
Ports = ingress.Ports?.Select(port => new V1IngressPortStatus
{
Port = port.Port, Protocol = port.Protocol, Error = port.Error
}).ToArray()
}).ToArray()
}
};
var ingresses = _cache.GetIngresses().ToArray();
foreach (var ingress in ingresses)
{
_logger.LogInformation("Updating ingress {IngressClassNamespace}/{IngressClassName} status.", ingress.Metadata.NamespaceProperty, ingress.Metadata.Name);
var ingressStatus = await _client.NetworkingV1.ReadNamespacedIngressStatusAsync(ingress.Metadata.Name, ingress.Metadata.NamespaceProperty, cancellationToken: cancellationToken);
ingressStatus.Status = status;
await _client.NetworkingV1.ReplaceNamespacedIngressStatusAsync(ingressStatus, ingress.Metadata.Name, ingress.Metadata.NamespaceProperty, cancellationToken: cancellationToken);
_logger.LogInformation("Updated ingress {IngressClassNamespace}/{IngressClassName} status.", ingress.Metadata.NamespaceProperty, ingress.Metadata.Name);
}
}
}
}