2023-09-04 07:08:34 +00:00
|
|
|
|
using Utils;
|
2023-04-14 07:54:07 +00:00
|
|
|
|
|
|
|
|
|
namespace KubernetesWorkflow
|
|
|
|
|
{
|
|
|
|
|
public class CommandRunner
|
|
|
|
|
{
|
2023-05-04 06:25:48 +00:00
|
|
|
|
private readonly K8sClient client;
|
2023-04-14 07:54:07 +00:00
|
|
|
|
private readonly string k8sNamespace;
|
2023-11-06 13:33:47 +00:00
|
|
|
|
private readonly string podName;
|
2023-04-14 07:54:07 +00:00
|
|
|
|
private readonly string containerName;
|
|
|
|
|
private readonly string command;
|
|
|
|
|
private readonly string[] arguments;
|
|
|
|
|
private readonly List<string> lines = new List<string>();
|
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
public CommandRunner(K8sClient client, string k8sNamespace, string podName, string containerName, string command, string[] arguments)
|
2023-04-14 07:54:07 +00:00
|
|
|
|
{
|
|
|
|
|
this.client = client;
|
|
|
|
|
this.k8sNamespace = k8sNamespace;
|
2023-11-06 13:33:47 +00:00
|
|
|
|
this.podName = podName;
|
2023-04-14 07:54:07 +00:00
|
|
|
|
this.containerName = containerName;
|
|
|
|
|
this.command = command;
|
|
|
|
|
this.arguments = arguments;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Run()
|
|
|
|
|
{
|
|
|
|
|
var input = new[] { command }.Concat(arguments).ToArray();
|
|
|
|
|
|
2023-05-04 06:25:48 +00:00
|
|
|
|
Time.Wait(client.Run(c => c.NamespacedPodExecAsync(
|
2023-11-06 13:33:47 +00:00
|
|
|
|
podName, k8sNamespace, containerName, input, false, Callback, new CancellationToken())));
|
2023-04-14 07:54:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetStdOut()
|
|
|
|
|
{
|
|
|
|
|
return string.Join(Environment.NewLine, lines);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Task Callback(Stream stdIn, Stream stdOut, Stream stdErr)
|
|
|
|
|
{
|
|
|
|
|
using var streamReader = new StreamReader(stdOut);
|
|
|
|
|
var line = streamReader.ReadLine();
|
|
|
|
|
while (line != null)
|
|
|
|
|
{
|
|
|
|
|
lines.Add(line);
|
|
|
|
|
line = streamReader.ReadLine();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|