-
Notifications
You must be signed in to change notification settings - Fork 801
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 #630 from Xabaril/k8s-operator-cluster-and-namespaced
[K8s operator] Cluster and Namespaced services support
- Loading branch information
Showing
12 changed files
with
196 additions
and
17 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
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
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
44 changes: 44 additions & 0 deletions
44
src/HealthChecks.UI.K8s.Operator/Handlers/NotificationHandler.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,44 @@ | ||
using k8s; | ||
using k8s.Models; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace HealthChecks.UI.K8s.Operator.Handlers | ||
{ | ||
public class NotificationHandler | ||
{ | ||
private readonly IKubernetes _client; | ||
private readonly IHttpClientFactory _httpClientFactory; | ||
private readonly ILogger<K8sOperator> _logger; | ||
|
||
public NotificationHandler(IKubernetes client, IHttpClientFactory httpClientFactory, ILogger<K8sOperator> logger) | ||
{ | ||
_client = client ?? throw new ArgumentNullException(nameof(client)); | ||
_httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory)); | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
public async Task NotifyDiscoveredServiceAsync(WatchEventType type, V1Service service, HealthCheckResource resource) | ||
{ | ||
var uiService = await _client.ListNamespacedOwnedServiceAsync(resource.Metadata.NamespaceProperty, resource.Metadata.Uid); | ||
var secret = await _client.ListNamespacedOwnedSecretAsync(resource.Metadata.NamespaceProperty, resource.Metadata.Uid); | ||
|
||
if (!service.Metadata.Labels.ContainsKey(resource.Spec.ServicesLabel)) | ||
{ | ||
type = WatchEventType.Deleted; | ||
} | ||
|
||
await HealthChecksPushService.PushNotification( | ||
type, | ||
resource, | ||
uiService, | ||
service, | ||
secret, | ||
_logger, | ||
_httpClientFactory); | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/HealthChecks.UI.K8s.Operator/Operator/ClusterServiceWatcher.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,65 @@ | ||
using HealthChecks.UI.K8s.Operator.Diagnostics; | ||
using HealthChecks.UI.K8s.Operator.Handlers; | ||
using k8s; | ||
using k8s.Models; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace HealthChecks.UI.K8s.Operator.Operator | ||
{ | ||
internal class ClusterServiceWatcher | ||
{ | ||
private readonly IKubernetes _client; | ||
private readonly ILogger<K8sOperator> _logger; | ||
private readonly OperatorDiagnostics _diagnostics; | ||
private readonly NotificationHandler _notificationHandler; | ||
private Watcher<V1Service> _watcher; | ||
public ClusterServiceWatcher( | ||
IKubernetes client, | ||
ILogger<K8sOperator> logger, | ||
OperatorDiagnostics diagnostics, | ||
NotificationHandler notificationHandler, | ||
IHttpClientFactory httpClientFactory) | ||
{ | ||
_client = client ?? throw new ArgumentNullException(nameof(client)); | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
_diagnostics = diagnostics ?? throw new ArgumentNullException(nameof(diagnostics)); | ||
_notificationHandler = notificationHandler ?? throw new ArgumentNullException(nameof(notificationHandler)); | ||
} | ||
|
||
internal Task Watch(HealthCheckResource resource, CancellationToken token) | ||
{ | ||
var response = _client.ListServiceForAllNamespacesWithHttpMessagesAsync( | ||
labelSelector: $"{resource.Spec.ServicesLabel}", | ||
watch: true, | ||
cancellationToken: token); | ||
|
||
_watcher = response.Watch<V1Service, V1ServiceList>( | ||
onEvent: async (type, item) => await _notificationHandler.NotifyDiscoveredServiceAsync(type, item, resource), | ||
onError: e => _diagnostics.ServiceWatcherThrow(e) | ||
); | ||
|
||
_diagnostics.ServiceWatcherStarting("All"); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
internal void Stopwatch(HealthCheckResource resource) | ||
{ | ||
Dispose(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if(_watcher != null && _watcher.Watching) | ||
{ | ||
_watcher.Dispose(); | ||
} | ||
} | ||
} | ||
} |
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
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