-
Notifications
You must be signed in to change notification settings - Fork 33
/
PodClientV1.cs
453 lines (429 loc) · 20.8 KB
/
PodClientV1.cs
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
using HTTPlease;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Threading;
using System.Net;
namespace KubeClient.ResourceClients
{
using Models;
/// <summary>
/// A client for the Kubernetes Pods (v1) API.
/// </summary>
public class PodClientV1
: KubeResourceClient, IPodClientV1
{
/// <summary>
/// Create a new <see cref="PodClientV1"/>.
/// </summary>
/// <param name="client">
/// The Kubernetes API client.
/// </param>
public PodClientV1(IKubeApiClient client)
: base(client)
{
}
/// <summary>
/// Get the Pod with the specified name.
/// </summary>
/// <param name="name">
/// The name of the Pod to retrieve.
/// </param>
/// <param name="kubeNamespace">
/// The target Kubernetes namespace (defaults to <see cref="KubeApiClient.DefaultNamespace"/>).
/// </param>
/// <param name="cancellationToken">
/// An optional <see cref="CancellationToken"/> that can be used to cancel the request.
/// </param>
/// <returns>
/// A <see cref="PodV1"/> representing the current state for the Pod, or <c>null</c> if no Pod was found with the specified name and namespace.
/// </returns>
public async Task<PodV1> Get(string name, string kubeNamespace = null, CancellationToken cancellationToken = default)
{
if (String.IsNullOrWhiteSpace(name))
throw new ArgumentException("Argument cannot be null, empty, or entirely composed of whitespace: 'name'.", nameof(name));
return await GetSingleResource<PodV1>(
Requests.ByName.WithTemplateParameters(new
{
Name = name,
Namespace = kubeNamespace ?? KubeClient.DefaultNamespace
}),
cancellationToken: cancellationToken
);
}
/// <summary>
/// Get all Pods in the specified namespace, optionally matching a label selector.
/// </summary>
/// <param name="labelSelector">
/// An optional Kubernetes label selector expression used to filter the Pods.
/// </param>
/// <param name="fieldSelector">
/// An optional Kubernetes field selector expression used to filter the Pods.
/// </param>
/// <param name="kubeNamespace">
/// The target Kubernetes namespace (defaults to <see cref="KubeApiClient.DefaultNamespace"/>).
/// </param>
/// <param name="cancellationToken">
/// An optional <see cref="CancellationToken"/> that can be used to cancel the request.
/// </param>
/// <returns>
/// A <see cref="PodListV1"/> containing the Pods.
/// </returns>
public async Task<PodListV1> List(string labelSelector = null, string fieldSelector = null, string kubeNamespace = null, CancellationToken cancellationToken = default)
{
return await GetResourceList<PodListV1>(
Requests.Collection.WithTemplateParameters(new
{
Namespace = kubeNamespace ?? KubeClient.DefaultNamespace,
LabelSelector = labelSelector,
FieldSelector = fieldSelector
}),
cancellationToken: cancellationToken
);
}
/// <summary>
/// Watch for events relating to Pods.
/// </summary>
/// <param name="labelSelector">
/// An optional Kubernetes label selector expression used to filter the Pods.
/// </param>
/// <param name="fieldSelector">
/// An optional Kubernetes field selector expression used to filter the Pods.
/// </param>
/// <param name="kubeNamespace">
/// The target Kubernetes namespace (defaults to <see cref="KubeApiClient.DefaultNamespace"/>).
/// </param>
/// <returns>
/// An <see cref="IObservable{T}"/> representing the event stream.
/// </returns>
public IObservable<IResourceEventV1<PodV1>> WatchAll(string labelSelector = null, string fieldSelector = null, string kubeNamespace = null)
{
return ObserveEvents<PodV1>(
Requests.Collection.WithTemplateParameters(new
{
Namespace = kubeNamespace ?? KubeClient.DefaultNamespace,
LabelSelector = labelSelector,
FieldSelector = fieldSelector,
Watch = true
}),
operationDescription: $"watch all v1/Pods with label selector '{labelSelector ?? "<none>"}' and field selector '{fieldSelector ?? "<none>"}' in namespace {kubeNamespace ?? KubeClient.DefaultNamespace}"
);
}
/// <summary>
/// Get the combined logs for the Pod with the specified name.
/// </summary>
/// <param name="name">
/// The name of the target Pod.
/// </param>
/// <param name="containerName">
/// The name of the container.
///
/// Not required if the pod only has a single container.
/// </param>
/// <param name="kubeNamespace">
/// The target Kubernetes namespace (defaults to <see cref="KubeApiClient.DefaultNamespace"/>).
/// </param>
/// <param name="limitBytes">
/// Limit the number of bytes returned (optional).
/// </param>
/// <param name="tailLines">
/// The number of lines from the end of the log to show (optional).
///
/// If not specified, logs are since from the creation of the container.
/// </param>
/// <param name="previous">
/// Return previous terminated container logs (defaults to <c>false</c>)?
/// </param>
/// <param name="cancellationToken">
/// An optional <see cref="CancellationToken"/> that can be used to cancel the request.
/// </param>
/// <returns>
/// A string containing the logs.
/// </returns>
public async Task<string> Logs(string name, string containerName = null, string kubeNamespace = null, int? limitBytes = null, int? tailLines = null, bool? previous = null, CancellationToken cancellationToken = default)
{
if (String.IsNullOrWhiteSpace(name))
throw new ArgumentException("Argument cannot be null, empty, or entirely composed of whitespace: 'name'.", nameof(name));
HttpResponseMessage responseMessage = await Http.GetAsync(
Requests.Logs.WithTemplateParameters(new
{
Name = name,
ContainerName = containerName,
Namespace = kubeNamespace ?? KubeClient.DefaultNamespace,
LimitBytes = limitBytes,
TailLines = tailLines,
Previous = previous?.ToString().ToLowerInvariant()
}),
cancellationToken
);
using (responseMessage)
{
if (responseMessage.IsSuccessStatusCode)
return await responseMessage.Content.ReadAsStringAsync();
throw new KubeClientException($"Unable to retrieve logs for container '{containerName ?? "<default>"}' of v1/Pod '{name}' in namespace '{kubeNamespace ?? KubeClient.DefaultNamespace}'.",
innerException: new HttpRequestException<StatusV1>(responseMessage.StatusCode,
response: await responseMessage.ReadContentAsStatusV1Async(responseMessage.StatusCode).ConfigureAwait(false)
));
}
}
/// <summary>
/// Stream the combined logs for the Pod with the specified name.
/// </summary>
/// <param name="name">
/// The name of the target Pod.
/// </param>
/// <param name="containerName">
/// The name of the container.
///
/// Not required if the pod only has a single container.
/// </param>
/// <param name="kubeNamespace">
/// The target Kubernetes namespace (defaults to <see cref="KubeApiClient.DefaultNamespace"/>).
/// </param>
/// <param name="limitBytes">
/// Limit the number of bytes returned (optional).
/// </param>
/// <param name="tailLines">
/// The number of lines from the end of the log to show (optional).
///
/// If not specified, logs are since from the creation of the container.
/// </param>
/// <returns>
/// An <see cref="IObservable{T}"/> sequence of lines from the log.
/// </returns>
public IObservable<string> StreamLogs(string name, string containerName = null, string kubeNamespace = null, int? limitBytes = null, int? tailLines = null)
{
if (String.IsNullOrWhiteSpace(name))
throw new ArgumentException("Argument cannot be null, empty, or entirely composed of whitespace: 'name'.", nameof(name));
return ObserveLines(
Requests.Logs.WithTemplateParameters(new
{
Name = name,
ContainerName = containerName,
Namespace = kubeNamespace ?? KubeClient.DefaultNamespace,
LimitBytes = limitBytes,
TailLines = tailLines,
Follow = "true"
}),
operationDescription: $"stream logs for v1/Pod '{name}' (container '{containerName ?? "<default>"}') in namespace {kubeNamespace ?? KubeClient.DefaultNamespace}"
);
}
/// <summary>
/// Request creation of a <see cref="PodV1"/>.
/// </summary>
/// <param name="newPod">
/// A <see cref="PodV1"/> representing the Pod to create.
/// </param>
/// <param name="cancellationToken">
/// An optional <see cref="CancellationToken"/> that can be used to cancel the request.
/// </param>
/// <returns>
/// A <see cref="PodV1"/> representing the current state for the newly-created Pod.
/// </returns>
public async Task<PodV1> Create(PodV1 newPod, CancellationToken cancellationToken = default)
{
if (newPod == null)
throw new ArgumentNullException(nameof(newPod));
return await Http
.PostAsJsonAsync(
Requests.Collection.WithTemplateParameters(new
{
Namespace = newPod?.Metadata?.Namespace ?? KubeClient.DefaultNamespace
}),
postBody: newPod,
cancellationToken: cancellationToken
)
.ReadContentAsObjectV1Async<PodV1>(
operationDescription: $"create v1/Pod resource in namespace {newPod?.Metadata?.Namespace ?? KubeClient.DefaultNamespace}"
);
}
/// <summary>
/// Request deletion of the specified Pod.
/// </summary>
/// <param name="name">
/// The name of the Pod to delete.
/// </param>
/// <param name="kubeNamespace">
/// The target Kubernetes namespace (defaults to <see cref="KubeApiClient.DefaultNamespace"/>).
/// </param>
/// <param name="propagationPolicy">
/// A <see cref="DeletePropagationPolicy"/> indicating how child resources should be deleted (if at all).
/// </param>
/// <param name="cancellationToken">
/// An optional <see cref="CancellationToken"/> that can be used to cancel the request.
/// </param>
/// <returns>
/// A <see cref="PodV1"/> representing the pod's most recent state before it was deleted, if <paramref name="propagationPolicy"/> is <see cref="DeletePropagationPolicy.Foreground"/>; otherwise, a <see cref="StatusV1"/> indicating the operation result.
/// </returns>
public Task<KubeResourceResultV1<PodV1>> Delete(string name, string kubeNamespace = null, DeletePropagationPolicy? propagationPolicy = null, CancellationToken cancellationToken = default)
{
return DeleteResource<PodV1>(Requests.ByName, name, kubeNamespace, propagationPolicy, cancellationToken);
}
/// <summary>
/// Request templates for the Pods (v1) API.
/// </summary>
public static class Requests
{
/// <summary>
/// A collection-level Pod (v1) request.
/// </summary>
public static readonly HttpRequest Collection = KubeRequest.Create("api/v1/namespaces/{Namespace}/pods?labelSelector={LabelSelector?}&fieldSelector={FieldSelector?}&watch={Watch?}");
/// <summary>
/// A get-by-name Pod (v1) request.
/// </summary>
public static readonly HttpRequest ByName = KubeRequest.Create("api/v1/namespaces/{Namespace}/pods/{Name}");
/// <summary>
/// A get-logs Pod (v1) request.
/// </summary>
public static readonly HttpRequest Logs = ByName.WithRelativeUri("log?container={ContainerName?}&follow={Follow?}&limitBytes={LimitBytes?}&tailLines={TailLines?}&previous={Previous?}");
}
}
/// <summary>
/// Represents a client for the Kubernetes Pods (v1) API.
/// </summary>
public interface IPodClientV1
: IKubeResourceClient
{
/// <summary>
/// Get the Pod with the specified name.
/// </summary>
/// <param name="name">
/// The name of the Pod to retrieve.
/// </param>
/// <param name="kubeNamespace">
/// The target Kubernetes namespace (defaults to <see cref="KubeApiClient.DefaultNamespace"/>).
/// </param>
/// <param name="cancellationToken">
/// An optional <see cref="CancellationToken"/> that can be used to cancel the request.
/// </param>
/// <returns>
/// A <see cref="PodV1"/> representing the current state for the Pod, or <c>null</c> if no Pod was found with the specified name and namespace.
/// </returns>
Task<PodV1> Get(string name, string kubeNamespace = null, CancellationToken cancellationToken = default);
/// <summary>
/// Get all Pods in the specified namespace, optionally matching a label selector.
/// </summary>
/// <param name="labelSelector">
/// An optional Kubernetes label selector expression used to filter the Pods.
/// </param>
/// <param name="fieldSelector">
/// An optional Kubernetes field selector expression used to filter the Pods.
/// </param>
/// <param name="kubeNamespace">
/// The target Kubernetes namespace (defaults to <see cref="KubeApiClient.DefaultNamespace"/>).
/// </param>
/// <param name="cancellationToken">
/// An optional <see cref="CancellationToken"/> that can be used to cancel the request.
/// </param>
/// <returns>
/// A <see cref="PodListV1"/> containing the Pods.
/// </returns>
Task<PodListV1> List(string labelSelector = null, string fieldSelector = null, string kubeNamespace = null, CancellationToken cancellationToken = default);
/// <summary>
/// Watch for events relating to Pods.
/// </summary>
/// <param name="labelSelector">
/// An optional Kubernetes label selector expression used to filter the Pods.
/// </param>
/// <param name="fieldSelector">
/// An optional Kubernetes field selector expression used to filter the Pods.
/// </param>
/// <param name="kubeNamespace">
/// The target Kubernetes namespace (defaults to <see cref="KubeApiClient.DefaultNamespace"/>).
/// </param>
/// <returns>
/// An <see cref="IObservable{T}"/> representing the event stream.
/// </returns>
IObservable<IResourceEventV1<PodV1>> WatchAll(string labelSelector = null, string fieldSelector = null, string kubeNamespace = null);
/// <summary>
/// Get the combined logs for the Pod with the specified name.
/// </summary>
/// <param name="name">
/// The name of the target Pod.
/// </param>
/// <param name="containerName">
/// The name of the container.
///
/// Not required if the pod only has a single container.
/// </param>
/// <param name="kubeNamespace">
/// The target Kubernetes namespace (defaults to <see cref="KubeApiClient.DefaultNamespace"/>).
/// </param>
/// <param name="limitBytes">
/// Limit the number of bytes returned (optional).
/// </param>
/// <param name="tailLines">
/// The number of lines from the end of the log to show (optional).
///
/// If not specified, logs are since from the creation of the container.
/// </param>
/// <param name="previous">
/// Return previous terminated container logs (defaults to <c>false</c>)?
/// </param>
/// <param name="cancellationToken">
/// An optional <see cref="CancellationToken"/> that can be used to cancel the request.
/// </param>
/// <returns>
/// A string containing the logs.
/// </returns>
Task<string> Logs(string name, string containerName = null, string kubeNamespace = null, int? limitBytes = null, int? tailLines = null, bool? previous = null, CancellationToken cancellationToken = default);
/// <summary>
/// Stream the combined logs for the Pod with the specified name.
/// </summary>
/// <param name="name">
/// The name of the target Pod.
/// </param>
/// <param name="containerName">
/// The name of the container.
///
/// Not required if the pod only has a single container.
/// </param>
/// <param name="kubeNamespace">
/// The target Kubernetes namespace (defaults to <see cref="KubeApiClient.DefaultNamespace"/>).
/// </param>
/// <param name="limitBytes">
/// Limit the number of bytes returned (optional).
/// </param>
/// <param name="tailLines">
/// The number of lines from the end of the log to show (optional).
///
/// If not specified, logs are since from the creation of the container.
/// </param>
/// <returns>
/// An <see cref="IObservable{T}"/> sequence of lines from the log.
/// </returns>
IObservable<string> StreamLogs(string name, string containerName = null, string kubeNamespace = null, int? limitBytes = null, int? tailLines = null);
/// <summary>
/// Request creation of a <see cref="PodV1"/>.
/// </summary>
/// <param name="newPod">
/// A <see cref="PodV1"/> representing the Pod to create.
/// </param>
/// <param name="cancellationToken">
/// An optional <see cref="CancellationToken"/> that can be used to cancel the request.
/// </param>
/// <returns>
/// A <see cref="PodV1"/> representing the current state for the newly-created Pod.
/// </returns>
Task<PodV1> Create(PodV1 newPod, CancellationToken cancellationToken = default);
/// <summary>
/// Request deletion of the specified Pod.
/// </summary>
/// <param name="name">
/// The name of the Pod to delete.
/// </param>
/// <param name="kubeNamespace">
/// The target Kubernetes namespace (defaults to <see cref="KubeApiClient.DefaultNamespace"/>).
/// </param>
/// <param name="propagationPolicy">
/// A <see cref="DeletePropagationPolicy"/> indicating how child resources should be deleted (if at all).
/// </param>
/// <param name="cancellationToken">
/// An optional <see cref="CancellationToken"/> that can be used to cancel the request.
/// </param>
/// <returns>
/// A <see cref="PodV1"/> representing the pod's most recent state before it was deleted, if <paramref name="propagationPolicy"/> is <see cref="DeletePropagationPolicy.Foreground"/>; otherwise, a <see cref="StatusV1"/> indicating the operation result.
/// </returns>
Task<KubeResourceResultV1<PodV1>> Delete(string name, string kubeNamespace = null, DeletePropagationPolicy? propagationPolicy = null, CancellationToken cancellationToken = default);
}
}