-
Notifications
You must be signed in to change notification settings - Fork 808
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #432 from ggmaresca/features/kubernetes-client
Use kubernetes-client and Kubernetes Service Discovery Enhancements
- Loading branch information
Showing
18 changed files
with
408 additions
and
286 deletions.
There are no files selected for viewing
This file contains 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 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
30 changes: 30 additions & 0 deletions
30
src/HealthChecks.UI/Core/Discovery/K8S/Extensions/IKubernetesExtensions.cs
This file contains 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,30 @@ | ||
using k8s; | ||
using k8s.Models; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
#nullable enable | ||
namespace HealthChecks.UI.Core.Discovery.K8S.Extensions | ||
{ | ||
internal static class KubernetesHttpClientExtensions | ||
{ | ||
internal static async Task<V1ServiceList> GetServices(this IKubernetes client, string label, IEnumerable<string>? k8sNamespaces, CancellationToken cancellationToken) | ||
{ | ||
if(k8sNamespaces is null || !k8sNamespaces.Any()) | ||
{ | ||
return await client.ListServiceForAllNamespacesAsync(labelSelector: label, cancellationToken: cancellationToken); | ||
} | ||
else | ||
{ | ||
var responses = await Task.WhenAll(k8sNamespaces.Select(k8sNamespace => client.ListNamespacedServiceAsync(k8sNamespace, labelSelector: label, cancellationToken: cancellationToken))); | ||
|
||
return new V1ServiceList() | ||
{ | ||
Items = responses.SelectMany(r => r.Items).ToList() | ||
}; | ||
} | ||
} | ||
} | ||
} |
38 changes: 0 additions & 38 deletions
38
src/HealthChecks.UI/Core/Discovery/K8S/Extensions/KubernetesHttpClientExtensions.cs
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
src/HealthChecks.UI/Core/Discovery/K8S/Extensions/KubernetesIHttpClientBuilderExtensions.cs
This file was deleted.
Oops, something went wrong.
111 changes: 84 additions & 27 deletions
111
src/HealthChecks.UI/Core/Discovery/K8S/KubernetesAddressFactory.cs
This file contains 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 |
---|---|---|
@@ -1,63 +1,120 @@ | ||
using System.Linq; | ||
using k8s.Models; | ||
using System.Linq; | ||
|
||
#nullable enable | ||
namespace HealthChecks.UI.Core.Discovery.K8S | ||
{ | ||
internal class KubernetesAddressFactory | ||
{ | ||
private readonly string _healthPath; | ||
public KubernetesAddressFactory(string healthPath) | ||
private readonly KubernetesDiscoverySettings _settings; | ||
public KubernetesAddressFactory(KubernetesDiscoverySettings discoveryOptions) | ||
{ | ||
_healthPath = healthPath; | ||
_settings = discoveryOptions; | ||
} | ||
public string CreateAddress(Service service) | ||
public string CreateAddress(V1Service service) | ||
{ | ||
string address = string.Empty; | ||
|
||
var port = GetServicePort(service); | ||
var port = GetServicePortValue(service); | ||
|
||
switch (service.Spec.PortType) | ||
switch (service.Spec.Type) | ||
{ | ||
case PortType.LoadBalancer: | ||
case PortType.NodePort: | ||
case ServiceType.LoadBalancer: | ||
case ServiceType.NodePort: | ||
address = GetLoadBalancerAddress(service); | ||
break; | ||
case PortType.ClusterIP: | ||
case ServiceType.ClusterIP: | ||
address = service.Spec.ClusterIP; | ||
break; | ||
case ServiceType.ExternalName: | ||
address = service.Spec.ExternalName; | ||
break; | ||
} | ||
|
||
string healthPath = _settings.HealthPath; | ||
if(!string.IsNullOrEmpty(_settings.ServicesPathAnnotation) && (service.Metadata.Annotations?.ContainsKey(_settings.ServicesPathAnnotation) ?? false)) | ||
{ | ||
healthPath = service.Metadata.Annotations![_settings.ServicesPathAnnotation]!; | ||
} | ||
healthPath = healthPath.TrimStart('/'); | ||
|
||
return $"http://{address}{port}/{_healthPath}"; | ||
string healthScheme = "http"; | ||
if(!string.IsNullOrEmpty(_settings.ServicesSchemeAnnotation) && (service.Metadata.Annotations?.ContainsKey(_settings.ServicesSchemeAnnotation) ?? false)) | ||
{ | ||
healthScheme = service.Metadata.Annotations![_settings.ServicesSchemeAnnotation]!.ToLower(); | ||
} | ||
|
||
// Support IPv6 address hosts | ||
if(address.Contains(":")) | ||
{ | ||
return $"{healthScheme}://[{address}]{port}/{healthPath}"; | ||
} | ||
else | ||
{ | ||
return $"{healthScheme}://{address}{port}/{healthPath}"; | ||
} | ||
} | ||
private string GetLoadBalancerAddress(Service service) | ||
private string GetLoadBalancerAddress(V1Service service) | ||
{ | ||
var ingress = service.Status?.LoadBalancer?.Ingress?.FirstOrDefault(); | ||
if (ingress != null) | ||
var firstIngress = service.Status?.LoadBalancer?.Ingress?.FirstOrDefault(); | ||
if (firstIngress is V1LoadBalancerIngress ingress) | ||
{ | ||
return ingress.Ip ?? ingress.HostName; | ||
return string.IsNullOrEmpty(ingress.Ip) ? ingress.Hostname : ingress.Ip; | ||
} | ||
|
||
return service.Spec.ClusterIP; | ||
} | ||
private string GetServicePort(Service service) | ||
private string GetServicePortValue(V1Service service) | ||
{ | ||
string port = string.Empty; | ||
|
||
switch (service.Spec.PortType) | ||
int? port; | ||
switch (service.Spec.Type) | ||
{ | ||
case PortType.LoadBalancer: | ||
case PortType.ClusterIP: | ||
port = service.Spec?.Ports?.FirstOrDefault()?.PortNumber.ToString() ?? ""; | ||
case ServiceType.LoadBalancer: | ||
case ServiceType.ClusterIP: | ||
port = GetServicePort(service)?.Port; | ||
break; | ||
case ServiceType.NodePort: | ||
port = GetServicePort(service)?.NodePort; | ||
break; | ||
case ServiceType.ExternalName: | ||
if(GetServicePortAnnotation(service) is string servicePortAnnotation && int.TryParse(servicePortAnnotation, out var servicePort)) | ||
{ | ||
port = servicePort; | ||
} | ||
else | ||
{ | ||
port = null; | ||
} | ||
break; | ||
case (PortType.NodePort): | ||
port = service.Spec?.Ports?.FirstOrDefault()?.NodePort.ToString() ?? ""; | ||
default: | ||
port = null; | ||
break; | ||
} | ||
|
||
if (!string.IsNullOrEmpty(port)) | ||
return port is null ? string.Empty : $":{port.Value}"; | ||
} | ||
private V1ServicePort? GetServicePort(V1Service service) | ||
{ | ||
if(GetServicePortAnnotation(service) is string portAnnotationValue) | ||
{ | ||
if(int.TryParse(portAnnotationValue, out var portAnnotationIntValue)) { | ||
return service.Spec?.Ports?.Where(p => p.Port == portAnnotationIntValue)?.FirstOrDefault(); | ||
} else { | ||
return service.Spec?.Ports?.Where(p => p.Name == portAnnotationValue)?.FirstOrDefault(); | ||
} | ||
} | ||
else | ||
{ | ||
return service.Spec?.Ports?.FirstOrDefault(); | ||
} | ||
} | ||
private string? GetServicePortAnnotation(V1Service service) | ||
{ | ||
if(!string.IsNullOrEmpty(_settings.ServicesPortAnnotation) && (service.Metadata.Annotations?.ContainsKey(_settings.ServicesPortAnnotation) ?? false)) | ||
{ | ||
port = $":{port}"; | ||
return service.Metadata.Annotations![_settings.ServicesPortAnnotation]!; | ||
} | ||
return port; | ||
return null; | ||
} | ||
} | ||
} |
7 changes: 0 additions & 7 deletions
7
src/HealthChecks.UI/Core/Discovery/K8S/KubernetesApiEndpoints.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.