Skip to content

add patch and replace to generic #1040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/KubernetesClient/GenericClient.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using k8s.Models;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -86,6 +87,34 @@ public async Task<T> DeleteNamespacedAsync<T>(string ns, string name, Cancellati
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);
Expand Down
87 changes: 60 additions & 27 deletions tests/E2E.Tests/MinikubeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -504,45 +504,78 @@ void Cleanup()
{
Cleanup();

await genericPods.CreateNamespacedAsync(
new V1Pod()
{
Metadata = new V1ObjectMeta { Name = podName, },
Spec = new V1PodSpec
// create + list
{
await genericPods.CreateNamespacedAsync(
new V1Pod()
{
Containers = new[] { new V1Container() { Name = "k8scsharp-e2e", Image = "nginx", }, },
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);
namespaceParameter).ConfigureAwait(false);

var pods = await genericPods.ListNamespacedAsync<V1PodList>(namespaceParameter).ConfigureAwait(false);
Assert.Contains(pods.Items, p => p.Metadata.Name == podName);
var pods = await genericPods.ListNamespacedAsync<V1PodList>(namespaceParameter).ConfigureAwait(false);
Assert.Contains(pods.Items, p => p.Metadata.Name == podName);
}

int retry = 5;
while (retry-- > 0)
// replace + get
{
try
{
await genericPods.DeleteNamespacedAsync<V1Pod>(namespaceParameter, podName).ConfigureAwait(false);
}
catch (HttpOperationException e)
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)
{
if (e.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
try
{
return;
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;
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);
}

await Task.Delay(TimeSpan.FromSeconds(2.5)).ConfigureAwait(false);
Assert.DoesNotContain(pods.Items, p => p.Metadata.Name == podName);
}

Assert.DoesNotContain(pods.Items, p => p.Metadata.Name == podName);
}
finally
{
Expand Down