Skip to content
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

fix: Exec hangs #1267

Merged
merged 18 commits into from
May 1, 2023
Merged
Changes from 12 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
28 changes: 21 additions & 7 deletions src/KubernetesClient/KubernetesClientConfiguration.ConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Net;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace k8s
Expand Down Expand Up @@ -557,19 +558,32 @@ public static ExecCredentialResponse ExecuteExternalCommand(ExternalExecution co
throw new KubeConfigException($"external exec failed due to: {ex.Message}");
}

var stdout = process.StandardOutput.ReadToEnd();
var stderr = process.StandardError.ReadToEnd();
if (string.IsNullOrWhiteSpace(stderr) == false)

var stdOutTask = process.StandardOutput.ReadToEndAsync(); // Assumes process exits

var buffer = new char[4096];
var stdErrorTask = process.StandardError.ReadAsync(buffer, 0, buffer.Length); // Assumes process will continue

var result = Task.WaitAny(new Task[] { stdErrorTask, stdOutTask }, TimeSpan.FromSeconds(10));
IvanJosipovic marked this conversation as resolved.
Show resolved Hide resolved

if (result == -1)
{
throw new KubeConfigException($"external exec failed due to: {stderr}");
throw new KubeConfigException("external exec failed due to timeout");
}
else if (result == 0)
{
var stderrBuilder = new StringBuilder();
stderrBuilder.Append(buffer, 0, stdErrorTask.Result);

// Wait for a maximum of 5 seconds, if a response takes longer probably something went wrong...
process.WaitForExit(5);
if (stderrBuilder.Length > 0)
IvanJosipovic marked this conversation as resolved.
Show resolved Hide resolved
{
throw new KubeConfigException($"external exec failed due to: {stderrBuilder}");
}
}

try
{
var responseObject = KubernetesJson.Deserialize<ExecCredentialResponse>(stdout);
var responseObject = KubernetesJson.Deserialize<ExecCredentialResponse>(stdOutTask.Result);
IvanJosipovic marked this conversation as resolved.
Show resolved Hide resolved
if (responseObject == null || responseObject.ApiVersion != config.ApiVersion)
{
throw new KubeConfigException(
Expand Down