introduce cordon (#1231)

* introduce cordon

* clean import

* force one by one
This commit is contained in:
Boshi Lian
2023-03-13 13:46:48 -07:00
committed by GitHub
parent 6b34392f52
commit 89d36aa98d
7 changed files with 89 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
using Json.Patch;
using k8s.Models;
using System.Text.Json;
namespace k8s.kubectl.beta;
public partial class AsyncKubectl
{
public async Task Cordon(string nodeName, CancellationToken cancellationToken = default)
{
await PatchNodeUnschedulable(nodeName, true, cancellationToken).ConfigureAwait(false);
}
public async Task Uncordon(string nodeName, CancellationToken cancellationToken = default)
{
await PatchNodeUnschedulable(nodeName, false, cancellationToken).ConfigureAwait(false);
}
private async Task PatchNodeUnschedulable(string nodeName, bool desired, CancellationToken cancellationToken = default)
{
var node = await client.CoreV1.ReadNodeAsync(nodeName, cancellationToken: cancellationToken).ConfigureAwait(false);
var old = JsonSerializer.SerializeToDocument(node);
node.Spec.Unschedulable = desired;
var patch = old.CreatePatch(node);
await client.CoreV1.PatchNodeAsync(new V1Patch(patch, V1Patch.PatchType.JsonPatch), nodeName, cancellationToken: cancellationToken).ConfigureAwait(false);
}
}

View File

@@ -0,0 +1,14 @@
namespace k8s.kubectl.beta;
public partial class Kubectl
{
public void Cordon(string nodeName)
{
client.Cordon(nodeName).GetAwaiter().GetResult();
}
public void Uncordon(string nodeName)
{
client.Uncordon(nodeName).GetAwaiter().GetResult();
}
}

View File

@@ -7,6 +7,9 @@
<RootNamespace>k8s.kubectl</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JsonPatch.Net" Version="2.0.6" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\KubernetesClient\KubernetesClient.csproj" />
</ItemGroup>

View File

@@ -24,6 +24,7 @@
<ItemGroup>
<ProjectReference Include="..\..\src\KubernetesClient\KubernetesClient.csproj" />
<ProjectReference Include="..\..\src\KubernetesClient.Kubectl\KubernetesClient.Kubectl.csproj" />
<ProjectReference Include="..\SkipTestLogger\SkipTestLogger.csproj" />
</ItemGroup>

View File

@@ -0,0 +1,32 @@
using k8s.kubectl.beta;
using System.Linq;
using Xunit;
namespace k8s.E2E;
[Collection(nameof(Onebyone))]
public class KubectlTests
{
[MinikubeFact]
public void CordonTest()
{
var client = MinikubeTests.CreateClient();
var node = client.CoreV1.ListNode().Items.First();
var nodeName = node.Metadata.Name;
var kubectl = new Kubectl(client);
// cordon
kubectl.Cordon(nodeName);
// check node status
var cordonNode = client.CoreV1.ReadNode(nodeName);
Assert.True(cordonNode.Spec.Unschedulable);
// uncordon
kubectl.Uncordon(nodeName);
cordonNode = client.CoreV1.ReadNode(nodeName);
Assert.True(cordonNode.Spec.Unschedulable == null || cordonNode.Spec.Unschedulable == false);
}
}

View File

@@ -20,6 +20,7 @@ using Xunit;
namespace k8s.E2E
{
[Collection(nameof(Onebyone))]
public class MinikubeTests
{
[MinikubeFact]

View File

@@ -0,0 +1,8 @@
using Xunit;
namespace k8s.E2E;
[CollectionDefinition(nameof(Onebyone), DisableParallelization = true)]
public class Onebyone
{
}