Skip to content

Commit de0aa5a

Browse files
committed
fix: simplify logic
1 parent 6e53437 commit de0aa5a

File tree

1 file changed

+12
-31
lines changed

1 file changed

+12
-31
lines changed

src/KubernetesClient/KubernetesClientConfiguration.ConfigFile.cs

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -558,46 +558,27 @@ public static ExecCredentialResponse ExecuteExternalCommand(ExternalExecution co
558558
throw new KubeConfigException($"external exec failed due to: {ex.Message}");
559559
}
560560

561-
var stderrBuilder = new StringBuilder();
562561

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

565-
var buffer = new char[1024];
564+
var buffer = new char[4096];
565+
var stdErrorTask = process.StandardError.ReadAsync(buffer, 0, buffer.Length); // Assumes process will continue
566566

567-
var stopwatch = Stopwatch.StartNew();
567+
var result = Task.WaitAny(new Task[] { stdErrorTask, stdOutTask }, TimeSpan.FromSeconds(10));
568568

569-
// Wait for a maximum of 5 seconds, if a response takes longer probably something went wrong...
570-
while (!stdOutTask.IsCompleted && stopwatch.Elapsed < TimeSpan.FromSeconds(5))
569+
if (result == -1)
571570
{
572-
var readTask = process.StandardError.ReadAsync(buffer, 0, buffer.Length);
573-
574-
if (readTask.Wait(TimeSpan.FromMilliseconds(500)))
575-
{
576-
var bytesRead = readTask.Result;
577-
if (bytesRead == 0)
578-
{
579-
break; // end of stream reached
580-
}
581-
582-
stderrBuilder.Append(buffer, 0, bytesRead);
583-
}
584-
else
585-
{
586-
// timeout occurred
587-
break;
588-
}
571+
throw new KubeConfigException("external exec failed due to timeout");
589572
}
590-
591-
var stdError = stderrBuilder.ToString();
592-
593-
if (!string.IsNullOrWhiteSpace(stdError))
573+
else if (result == 0)
594574
{
595-
throw new KubeConfigException($"external exec failed due to: {stdError}");
596-
}
575+
var stderrBuilder = new StringBuilder();
576+
stderrBuilder.Append(buffer, 0, stdErrorTask.Result);
597577

598-
if (!stdOutTask.IsCompleted)
599-
{
600-
throw new KubeConfigException("external exec failed due to timeout");
578+
if (stderrBuilder.Length > 0)
579+
{
580+
throw new KubeConfigException($"external exec failed due to: {stderrBuilder}");
581+
}
601582
}
602583

603584
try

0 commit comments

Comments
 (0)