@@ -549,84 +549,61 @@ public static ExecCredentialResponse ExecuteExternalCommand(ExternalExecution co
549
549
550
550
var process = CreateRunnableExternalProcess ( config ) ;
551
551
552
+ var stdout = "" ;
553
+ var stderr = "" ;
554
+
555
+ process . OutputDataReceived += ( sender , args ) => stdout += args . Data ;
556
+ process . ErrorDataReceived += ( sender , args ) => stderr += args . Data ;
557
+
552
558
try
553
559
{
554
560
process . Start ( ) ;
561
+ process . BeginOutputReadLine ( ) ;
562
+ process . BeginErrorReadLine ( ) ;
555
563
}
556
564
catch ( Exception ex )
557
565
{
558
566
throw new KubeConfigException ( $ "external exec failed due to: { ex . Message } ") ;
559
567
}
560
568
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 ) ) )
564
571
{
565
- var readTask = process . StandardError . ReadAsync ( buffer , 0 , buffer . Length ) ;
566
-
567
- if ( readTask . Wait ( TimeSpan . FromSeconds ( 2 ) ) )
572
+ try
568
573
{
569
- var bytesRead = readTask . Result ;
570
- if ( bytesRead == 0 )
574
+ var responseObject = KubernetesJson . Deserialize < ExecCredentialResponse > ( stdout ) ;
575
+ if ( responseObject == null || responseObject . ApiVersion != config . ApiVersion )
571
576
{
572
- break ; // end of stream reached
577
+ throw new KubeConfigException (
578
+ $ "external exec failed because api version { responseObject . ApiVersion } does not match { config . ApiVersion } ") ;
573
579
}
574
580
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
+ }
576
589
}
577
- else
590
+ catch ( JsonException ex )
578
591
{
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 } ") ;
581
597
}
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 ;
599
598
}
600
599
else
601
600
{
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 ) )
609
602
{
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 } ") ;
612
604
}
613
605
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" ) ;
630
607
}
631
608
}
632
609
0 commit comments