introduce cordon (#1231)
* introduce cordon * clean import * force one by one
This commit is contained in:
30
src/KubernetesClient.Kubectl/Beta/AsyncKubectl.Cordon.cs
Normal file
30
src/KubernetesClient.Kubectl/Beta/AsyncKubectl.Cordon.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
14
src/KubernetesClient.Kubectl/Beta/Kubectl.Cordon.cs
Normal file
14
src/KubernetesClient.Kubectl/Beta/Kubectl.Cordon.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
|
||||
32
tests/E2E.Tests/KubectlTests.cs
Normal file
32
tests/E2E.Tests/KubectlTests.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ using Xunit;
|
||||
|
||||
namespace k8s.E2E
|
||||
{
|
||||
[Collection(nameof(Onebyone))]
|
||||
public class MinikubeTests
|
||||
{
|
||||
[MinikubeFact]
|
||||
|
||||
8
tests/E2E.Tests/Onebyone.cs
Normal file
8
tests/E2E.Tests/Onebyone.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using Xunit;
|
||||
|
||||
namespace k8s.E2E;
|
||||
|
||||
[CollectionDefinition(nameof(Onebyone), DisableParallelization = true)]
|
||||
public class Onebyone
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user