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 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
32 changes: 21 additions & 11 deletions src/KubernetesClient/KubernetesClientConfiguration.ConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ public partial class KubernetesClientConfiguration
// For testing
internal static string KubeConfigEnvironmentVariable { get; set; } = "KUBECONFIG";

/// <summary>
/// Exec process timeout
/// </summary>
public static TimeSpan ExecTimeout { get; set; } = TimeSpan.FromMinutes(2);

/// <summary>
/// Exec process Standard Errors
/// </summary>
public static event EventHandler<DataReceivedEventArgs> ExecStdError;

/// <summary>
/// Initializes a new instance of the <see cref="KubernetesClientConfiguration" /> from default locations
/// If the KUBECONFIG environment variable is set, then that will be used.
Expand Down Expand Up @@ -551,25 +561,25 @@ public static ExecCredentialResponse ExecuteExternalCommand(ExternalExecution co
try
{
process.Start();
if (ExecStdError != null)
{
process.ErrorDataReceived += (s, e) => ExecStdError.Invoke(s, e);
process.BeginErrorReadLine();
}
}
catch (Exception ex)
{
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)
{
throw new KubeConfigException($"external exec failed due to: {stderr}");
}

// Wait for a maximum of 5 seconds, if a response takes longer probably something went wrong...
process.WaitForExit(5);

try
{
var responseObject = KubernetesJson.Deserialize<ExecCredentialResponse>(stdout);
if (!process.WaitForExit((int)(ExecTimeout.TotalMilliseconds)))
{
throw new KubeConfigException("external exec failed due to timeout");
}

var responseObject = KubernetesJson.Deserialize<ExecCredentialResponse>(process.StandardOutput.ReadToEnd());
if (responseObject == null || responseObject.ApiVersion != config.ApiVersion)
{
throw new KubeConfigException(
Expand Down