Skip to content

Commit 8e7bf08

Browse files
committed
chore: switch to event based
1 parent 8571591 commit 8e7bf08

File tree

1 file changed

+32
-55
lines changed

1 file changed

+32
-55
lines changed

src/KubernetesClient/KubernetesClientConfiguration.ConfigFile.cs

Lines changed: 32 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -549,84 +549,61 @@ public static ExecCredentialResponse ExecuteExternalCommand(ExternalExecution co
549549

550550
var process = CreateRunnableExternalProcess(config);
551551

552+
var stdout = "";
553+
var stderr = "";
554+
555+
process.OutputDataReceived += (sender, args) => stdout += args.Data;
556+
process.ErrorDataReceived += (sender, args) => stderr += args.Data;
557+
552558
try
553559
{
554560
process.Start();
561+
process.BeginOutputReadLine();
562+
process.BeginErrorReadLine();
555563
}
556564
catch (Exception ex)
557565
{
558566
throw new KubeConfigException($"external exec failed due to: {ex.Message}");
559567
}
560568

561-
var sb = new StringBuilder();
562-
var buffer = new char[1024];
563-
while (true)
569+
// Wait for a maximum of 5 seconds, if a response takes longer probably something went wrong...
570+
if (process.WaitForExitAsync().Wait(TimeSpan.FromSeconds(5)))
564571
{
565-
var readTask = process.StandardError.ReadAsync(buffer, 0, buffer.Length);
566-
567-
if (readTask.Wait(TimeSpan.FromSeconds(2)))
572+
try
568573
{
569-
var bytesRead = readTask.Result;
570-
if (bytesRead == 0)
574+
var responseObject = KubernetesJson.Deserialize<ExecCredentialResponse>(stdout);
575+
if (responseObject == null || responseObject.ApiVersion != config.ApiVersion)
571576
{
572-
break; // end of stream reached
577+
throw new KubeConfigException(
578+
$"external exec failed because api version {responseObject.ApiVersion} does not match {config.ApiVersion}");
573579
}
574580

575-
sb.Append(buffer, 0, bytesRead);
581+
if (responseObject.Status.IsValid())
582+
{
583+
return responseObject;
584+
}
585+
else
586+
{
587+
throw new KubeConfigException($"external exec failed missing token or clientCertificateData field in plugin output");
588+
}
576589
}
577-
else
590+
catch (JsonException ex)
578591
{
579-
// timeout occurred
580-
break;
592+
throw new KubeConfigException($"external exec failed due to failed deserialization process: {ex}");
593+
}
594+
catch (Exception ex)
595+
{
596+
throw new KubeConfigException($"external exec failed due to uncaught exception: {ex}");
581597
}
582-
}
583-
584-
var stderr = sb.ToString();
585-
586-
if (!string.IsNullOrWhiteSpace(stderr))
587-
{
588-
throw new KubeConfigException($"external exec failed due to: {stderr}");
589-
}
590-
591-
// Wait for a maximum of 5 seconds, if a response takes longer probably something went wrong...
592-
var stdOutTask = process.StandardOutput.ReadToEndAsync();
593-
594-
string stdOut;
595-
596-
if (stdOutTask.Wait(TimeSpan.FromSeconds(5)))
597-
{
598-
stdOut = stdOutTask.Result;
599598
}
600599
else
601600
{
602-
throw new KubeConfigException("external exec failed due to timeout");
603-
}
604-
605-
try
606-
{
607-
var responseObject = KubernetesJson.Deserialize<ExecCredentialResponse>(stdOut);
608-
if (responseObject == null || responseObject.ApiVersion != config.ApiVersion)
601+
if (!string.IsNullOrWhiteSpace(stderr))
609602
{
610-
throw new KubeConfigException(
611-
$"external exec failed because api version {responseObject.ApiVersion} does not match {config.ApiVersion}");
603+
throw new KubeConfigException($"external exec failed due to: {stderr}");
612604
}
613605

614-
if (responseObject.Status.IsValid())
615-
{
616-
return responseObject;
617-
}
618-
else
619-
{
620-
throw new KubeConfigException($"external exec failed missing token or clientCertificateData field in plugin output");
621-
}
622-
}
623-
catch (JsonException ex)
624-
{
625-
throw new KubeConfigException($"external exec failed due to failed deserialization process: {ex}");
626-
}
627-
catch (Exception ex)
628-
{
629-
throw new KubeConfigException($"external exec failed due to uncaught exception: {ex}");
606+
throw new KubeConfigException("external exec failed due to timeout");
630607
}
631608
}
632609

0 commit comments

Comments
 (0)