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,45 +504,78 @@ namespace k8s.E2E
{
Cleanup();
await genericPods.CreateNamespacedAsync(
new V1Pod()
{
Metadata = new V1ObjectMeta { Name = podName, },
Spec = new V1PodSpec
{
Containers = new[] { new V1Container() { Name = "k8scsharp-e2e", Image = "nginx", }, },
},
},
namespaceParameter).ConfigureAwait(false);
var pods = await genericPods.ListNamespacedAsync<V1PodList>(namespaceParameter).ConfigureAwait(false);
Assert.Contains(pods.Items, p => p.Metadata.Name == podName);
int retry = 5;
while (retry-- > 0)
// create + list
{
try
{
await genericPods.DeleteNamespacedAsync<V1Pod>(namespaceParameter, podName).ConfigureAwait(false);
}
catch (HttpOperationException e)
{
if (e.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
await genericPods.CreateNamespacedAsync(
new V1Pod()
{
return;
}
}
Metadata = new V1ObjectMeta { Name = podName, Labels = new Dictionary<string, string> { { "place", "holder" }, }, },
Spec = new V1PodSpec
{
Containers = new[] { new V1Container() { Name = "k8scsharp-e2e", Image = "nginx", }, },
},
},
namespaceParameter).ConfigureAwait(false);
pods = await genericPods.ListNamespacedAsync<V1PodList>(namespaceParameter).ConfigureAwait(false);
if (!pods.Items.Any(p => p.Metadata.Name == podName))
{
break;
}
await Task.Delay(TimeSpan.FromSeconds(2.5)).ConfigureAwait(false);
var pods = await genericPods.ListNamespacedAsync<V1PodList>(namespaceParameter).ConfigureAwait(false);
Assert.Contains(pods.Items, p => p.Metadata.Name == podName);
}
Assert.DoesNotContain(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)
{
try
{
await genericPods.DeleteNamespacedAsync<V1Pod>(namespaceParameter, podName).ConfigureAwait(false);
}
catch (HttpOperationException e)
{
if (e.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return;
}
}
pods = await genericPods.ListNamespacedAsync<V1PodList>(namespaceParameter).ConfigureAwait(false);
if (!pods.Items.Any(p => p.Metadata.Name == podName))
{
break;
}
await Task.Delay(TimeSpan.FromSeconds(2.5)).ConfigureAwait(false);
}
Assert.DoesNotContain(pods.Items, p => p.Metadata.Name == podName);
}
}
finally
{