-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathDeploymentClientV1Beta1.cs
230 lines (214 loc) · 10.1 KB
/
DeploymentClientV1Beta1.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
using HTTPlease;
using Microsoft.AspNetCore.JsonPatch;
using System;
using System.Threading.Tasks;
using System.Threading;
using System.Net;
namespace KubeClient.ResourceClients
{
using Models;
/// <summary>
/// A client for the Kubernetes Deployments (v1beta2) API.
/// </summary>
public class DeploymentClientV1Beta1
: KubeResourceClient
{
/// <summary>
/// Create a new <see cref="DeploymentClientV1Beta1"/>.
/// </summary>
/// <param name="client">
/// The Kubernetes API client.
/// </param>
public DeploymentClientV1Beta1(KubeApiClient client)
: base(client)
{
}
/// <summary>
/// Get the Deployment with the specified name.
/// </summary>
/// <param name="name">
/// The name of the Deployment 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="DeploymentV1Beta1"/> representing the current state for the Deployment, or <c>null</c> if no Deployment was found with the specified name and namespace.
/// </returns>
public async Task<DeploymentV1Beta1> 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<DeploymentV1Beta1>(
Requests.ByName.WithTemplateParameters(new
{
Name = name,
Namespace = kubeNamespace ?? KubeClient.DefaultNamespace
}),
cancellationToken: cancellationToken
);
}
/// <summary>
/// Get all Deployments in the specified namespace, optionally matching a label selector.
/// </summary>
/// <param name="labelSelector">
/// An optional Kubernetes label selector expression used to filter the Deployments.
/// </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="DeploymentListV1Beta1"/> containing the Deployments.
/// </returns>
public async Task<DeploymentListV1Beta1> List(string labelSelector = null, string kubeNamespace = null, CancellationToken cancellationToken = default)
{
return await GetResourceList<DeploymentListV1Beta1>(
Requests.Collection.WithTemplateParameters(new
{
Namespace = kubeNamespace ?? KubeClient.DefaultNamespace,
LabelSelector = labelSelector
}),
cancellationToken: cancellationToken
);
}
/// <summary>
/// Watch for events relating to Deployments.
/// </summary>
/// <param name="labelSelector">
/// An optional Kubernetes label selector expression used to filter the Deployments.
/// </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<DeploymentV1Beta1>> WatchAll(string labelSelector = null, string kubeNamespace = null)
{
return ObserveEvents<DeploymentV1Beta1>(
Requests.Collection.WithTemplateParameters(new
{
Namespace = kubeNamespace ?? KubeClient.DefaultNamespace,
LabelSelector = labelSelector,
Watch = true
})
);
}
/// <summary>
/// Request creation of a <see cref="DeploymentV1Beta1"/>.
/// </summary>
/// <param name="newDeployment">
/// A <see cref="DeploymentV1Beta1"/> representing the Deployment to create.
/// </param>
/// <param name="cancellationToken">
/// An optional <see cref="CancellationToken"/> that can be used to cancel the request.
/// </param>
/// <returns>
/// A <see cref="DeploymentV1Beta1"/> representing the current state for the newly-created Deployment.
/// </returns>
public async Task<DeploymentV1Beta1> Create(DeploymentV1Beta1 newDeployment, CancellationToken cancellationToken = default)
{
if (newDeployment == null)
throw new ArgumentNullException(nameof(newDeployment));
return await Http
.PostAsJsonAsync(
Requests.Collection.WithTemplateParameters(new
{
Namespace = newDeployment?.Metadata?.Namespace ?? KubeClient.DefaultNamespace
}),
postBody: newDeployment,
cancellationToken: cancellationToken
)
.ReadContentAsAsync<DeploymentV1Beta1, StatusV1>();
}
/// <summary>
/// Request update (PATCH) of a <see cref="DeploymentV1Beta1"/>.
/// </summary>
/// <param name="name">
/// The name of the target Deployment.
/// </param>
/// <param name="patchAction">
/// A delegate that customises the patch operation.
/// </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="DeploymentV1Beta1"/> representing the current state for the updated Deployment.
/// </returns>
public async Task<DeploymentV1Beta1> Update(string name, Action<JsonPatchDocument<DeploymentV1Beta1>> patchAction, 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));
if (patchAction == null)
throw new ArgumentNullException(nameof(patchAction));
return await PatchResource(patchAction,
Requests.ByName.WithTemplateParameters(new
{
Name = name,
Namespace = kubeNamespace ?? KubeClient.DefaultNamespace
}),
cancellationToken
);
}
/// <summary>
/// Request deletion of the specified Deployment.
/// </summary>
/// <param name="name">
/// The name of the Deployment 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="DeploymentV1Beta1"/> representing the deployment's most recent state before it was deleted, if <paramref name="propagationPolicy"/> is <see cref="DeletePropagationPolicy.Foreground"/>; otherwise, a <see cref="StatusV1"/>.
/// </returns>
public async Task<KubeObjectV1> Delete(string name, string kubeNamespace = null, DeletePropagationPolicy propagationPolicy = DeletePropagationPolicy.Background, CancellationToken cancellationToken = default)
{
var request = Http.DeleteAsJsonAsync(
Requests.ByName.WithTemplateParameters(new
{
Name = name,
Namespace = kubeNamespace ?? KubeClient.DefaultNamespace
}),
deleteBody: new DeleteOptionsV1
{
PropagationPolicy = propagationPolicy
},
cancellationToken: cancellationToken
);
if (propagationPolicy == DeletePropagationPolicy.Foreground)
return await request.ReadContentAsObjectV1Async<DeploymentV1Beta1>(HttpStatusCode.OK);
return await request.ReadContentAsObjectV1Async<StatusV1>(HttpStatusCode.OK, HttpStatusCode.NotFound);
}
/// <summary>
/// Request templates for the Deployment (v1beta2) API.
/// </summary>
static class Requests
{
/// <summary>
/// A collection-level Deployment (v1beta2) request.
/// </summary>
public static readonly HttpRequest Collection = KubeRequest.Create("apis/apps/v1beta1/namespaces/{Namespace}/deployments?labelSelector={LabelSelector?}&watch={Watch?}");
/// <summary>
/// A get-by-name Deployment (v1beta2) request.
/// </summary>
public static readonly HttpRequest ByName = KubeRequest.Create("apis/apps/v1beta1/namespaces/{Namespace}/deployments/{Name}");
}
}
}