add patch and replace to generic (#1040)

This commit is contained in:
Boshi Lian
2022-09-29 10:02:39 -07:00
committed by GitHub
parent f1f35fa10e
commit 8888a16b58
2 changed files with 96 additions and 34 deletions

View File

@@ -1,3 +1,4 @@
using k8s.Models;
using System.Threading;
using System.Threading.Tasks;
@@ -86,6 +87,34 @@ namespace k8s
return KubernetesJson.Deserialize<T>(resp.Body.ToString());
}
public async Task<T> PatchAsync<T>(V1Patch patch, string name, CancellationToken cancel = default)
where T : IKubernetesObject
{
var resp = await kubernetes.CustomObjects.PatchClusterCustomObjectWithHttpMessagesAsync(patch, group, version, plural, name, cancellationToken: cancel).ConfigureAwait(false);
return KubernetesJson.Deserialize<T>(resp.Body.ToString());
}
public async Task<T> PatchNamespacedAsync<T>(V1Patch patch, string ns, string name, CancellationToken cancel = default)
where T : IKubernetesObject
{
var resp = await kubernetes.CustomObjects.PatchNamespacedCustomObjectWithHttpMessagesAsync(patch, group, version, ns, plural, name, cancellationToken: cancel).ConfigureAwait(false);
return KubernetesJson.Deserialize<T>(resp.Body.ToString());
}
public async Task<T> ReplaceAsync<T>(T obj, string name, CancellationToken cancel = default)
where T : IKubernetesObject
{
var resp = await kubernetes.CustomObjects.ReplaceClusterCustomObjectWithHttpMessagesAsync(obj, group, version, plural, name, cancellationToken: cancel).ConfigureAwait(false);
return KubernetesJson.Deserialize<T>(resp.Body.ToString());
}
public async Task<T> ReplaceNamespacedAsync<T>(T obj, string ns, string name, CancellationToken cancel = default)
where T : IKubernetesObject
{
var resp = await kubernetes.CustomObjects.ReplaceNamespacedCustomObjectWithHttpMessagesAsync(obj, group, version, ns, plural, name, cancellationToken: cancel).ConfigureAwait(false);
return KubernetesJson.Deserialize<T>(resp.Body.ToString());
}
public void Dispose()
{
Dispose(true);

View File

@@ -504,10 +504,12 @@ namespace k8s.E2E
{
Cleanup();
// create + list
{
await genericPods.CreateNamespacedAsync(
new V1Pod()
{
Metadata = new V1ObjectMeta { Name = podName, },
Metadata = new V1ObjectMeta { Name = podName, Labels = new Dictionary<string, string> { { "place", "holder" }, }, },
Spec = new V1PodSpec
{
Containers = new[] { new V1Container() { Name = "k8scsharp-e2e", Image = "nginx", }, },
@@ -517,7 +519,37 @@ namespace k8s.E2E
var pods = await genericPods.ListNamespacedAsync<V1PodList>(namespaceParameter).ConfigureAwait(false);
Assert.Contains(pods.Items, p => p.Metadata.Name == podName);
}
// replace + get
{
var pod = await genericPods.ReadNamespacedAsync<V1Pod>(namespaceParameter, podName).ConfigureAwait(false);
var old = JsonSerializer.SerializeToDocument(pod);
var newlabels = new Dictionary<string, string>(pod.Metadata.Labels) { ["test"] = "generic-test-jsonpatch" };
pod.Metadata.Labels = newlabels;
var expected = JsonSerializer.SerializeToDocument(pod);
var patch = old.CreatePatch(expected);
await genericPods.PatchNamespacedAsync<V1Pod>(new V1Patch(patch, V1Patch.PatchType.JsonPatch), namespaceParameter, podName).ConfigureAwait(false);
var pods = await genericPods.ListNamespacedAsync<V1PodList>(namespaceParameter).ConfigureAwait(false);
Assert.Contains(pods.Items, p => p.Labels().Contains(new KeyValuePair<string, string>("test", "generic-test-jsonpatch")));
}
// replace + get
{
var pod = await genericPods.ReadNamespacedAsync<V1Pod>(namespaceParameter, podName).ConfigureAwait(false);
pod.Spec.Containers[0].Image = "httpd";
await genericPods.ReplaceNamespacedAsync(pod, namespaceParameter, podName).ConfigureAwait(false);
pod = await genericPods.ReadNamespacedAsync<V1Pod>(namespaceParameter, podName).ConfigureAwait(false);
Assert.Equal("httpd", pod.Spec.Containers[0].Image);
}
// delete + list
{
V1PodList pods = new V1PodList();
int retry = 5;
while (retry-- > 0)
{
@@ -544,6 +576,7 @@ namespace k8s.E2E
Assert.DoesNotContain(pods.Items, p => p.Metadata.Name == podName);
}
}
finally
{
Cleanup();