feat: enhance Kubernetes client with watch functionality (#1667)
* feat: enhance Kubernetes client with watch functionality * refactor: simplify watch event handling in Kubernetes client example * refactor: update Kubernetes watch functionality to use new event handling methods and add async enumerable support * fix * fix * fix: correct usage of Pod list items in client example and update Obsolete attribute formatting * fix: update client example to use correct Pod list method and improve Obsolete attribute formatting * refactor: enhance type resolution for list items in TypeHelper by adding TryGetItemTypeFromSchema method * feat: mark Watch methods as obsolete to prepare for future deprecation * fix * refactor: update WatcherExt class to internal and remove obsolete attributes; improve example method signature in Program.cs * refactor: change WatcherExt class from internal to public and mark methods as obsolete for future deprecation
This commit is contained in:
@@ -224,13 +224,12 @@ namespace k8s.E2E
|
||||
var started = new AsyncManualResetEvent();
|
||||
var connectionClosed = new AsyncManualResetEvent();
|
||||
|
||||
var watcher = kubernetes.BatchV1.ListNamespacedJobWithHttpMessagesAsync(
|
||||
var watcher = kubernetes.BatchV1.WatchListNamespacedJob(
|
||||
job.Metadata.NamespaceProperty,
|
||||
fieldSelector: $"metadata.name={job.Metadata.Name}",
|
||||
resourceVersion: job.Metadata.ResourceVersion,
|
||||
timeoutSeconds: 30,
|
||||
watch: true).Watch<V1Job, V1JobList>(
|
||||
(type, source) =>
|
||||
onEvent: (type, source) =>
|
||||
{
|
||||
Debug.WriteLine($"Watcher 1: {type}, {source}");
|
||||
events.Add(new Tuple<WatchEventType, V1Job>(type, source));
|
||||
@@ -250,6 +249,86 @@ namespace k8s.E2E
|
||||
new V1DeleteOptions() { PropagationPolicy = "Foreground" }).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[MinikubeFact]
|
||||
public async Task WatcherIntegrationTestAsyncEnumerable()
|
||||
{
|
||||
using var kubernetes = CreateClient();
|
||||
|
||||
var job = await kubernetes.BatchV1.CreateNamespacedJobAsync(
|
||||
new V1Job()
|
||||
{
|
||||
ApiVersion = "batch/v1",
|
||||
Kind = V1Job.KubeKind,
|
||||
Metadata = new V1ObjectMeta() { Name = nameof(WatcherIntegrationTestAsyncEnumerable).ToLowerInvariant() },
|
||||
Spec = new V1JobSpec()
|
||||
{
|
||||
Template = new V1PodTemplateSpec()
|
||||
{
|
||||
Spec = new V1PodSpec()
|
||||
{
|
||||
Containers = new List<V1Container>()
|
||||
{
|
||||
new V1Container()
|
||||
{
|
||||
Image = "ubuntu",
|
||||
Name = "runner",
|
||||
Command = new List<string>() { "/bin/bash", "-c", "--" },
|
||||
Args = new List<string>()
|
||||
{
|
||||
"trap : TERM INT; sleep infinity & wait",
|
||||
},
|
||||
},
|
||||
},
|
||||
RestartPolicy = "Never",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"default").ConfigureAwait(false);
|
||||
|
||||
var events = new Collection<Tuple<WatchEventType, V1Job>>();
|
||||
|
||||
var started = new AsyncManualResetEvent();
|
||||
var watchCompleted = new AsyncManualResetEvent();
|
||||
|
||||
// Start async enumerable watch in background task to mimic callback behavior
|
||||
var watchTask = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await foreach (var (type, source) in kubernetes.BatchV1.WatchListNamespacedJobAsync(
|
||||
job.Metadata.NamespaceProperty,
|
||||
fieldSelector: $"metadata.name={job.Metadata.Name}",
|
||||
resourceVersion: job.Metadata.ResourceVersion,
|
||||
timeoutSeconds: 30).ConfigureAwait(false))
|
||||
{
|
||||
Debug.WriteLine($"AsyncEnumerable Watcher: {type}, {source}");
|
||||
events.Add(new Tuple<WatchEventType, V1Job>(type, source));
|
||||
job = source;
|
||||
started.Set();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"Watch exception: {ex.GetType().FullName}: {ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
watchCompleted.Set();
|
||||
}
|
||||
});
|
||||
|
||||
await started.WaitAsync().ConfigureAwait(false);
|
||||
|
||||
await Task.WhenAny(watchCompleted.WaitAsync(), Task.Delay(TimeSpan.FromMinutes(3))).ConfigureAwait(false);
|
||||
Assert.True(watchCompleted.IsSet);
|
||||
|
||||
var st = await kubernetes.BatchV1.DeleteNamespacedJobAsync(
|
||||
job.Metadata.Name,
|
||||
job.Metadata.NamespaceProperty,
|
||||
new V1DeleteOptions() { PropagationPolicy = "Foreground" }).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[MinikubeFact]
|
||||
public void LeaderIntegrationTest()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user