Skip to content
Merged
Changes from 17 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 in seconds
/// </summary>
public static int ExecTimeout { get; set; } = 120;

/// <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)TimeSpan.FromSeconds(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