diff --git a/staging/cse/windows/kubernetesfunc.ps1 b/staging/cse/windows/kubernetesfunc.ps1 index 0cc784796ea..ec2ff53c04b 100644 --- a/staging/cse/windows/kubernetesfunc.ps1 +++ b/staging/cse/windows/kubernetesfunc.ps1 @@ -182,6 +182,7 @@ function Check-APIServerConnectivity { ) Logs-To-Event -TaskName "AKS.WindowsCSE.CheckAPIServerConnectivity" -TaskMessage "Start to check API server connectivity." $retryCount=0 + $LastException=$null do { $retryString="${retryCount}/${MaxRetryCount}" @@ -201,17 +202,30 @@ function Check-APIServerConnectivity { } $tcpClient.Close() } catch [System.AggregateException] { - Write-Log "Retry ${retryString}: Failed to connect to API server $MasterIP. AggregateException: " + $_.Exception.ToString() + Logs-To-Event -TaskName "AKS.WindowsCSE.CheckAPIServerConnectivity" -TaskMessage "Retry ${retryString}: Failed to connect to API server $MasterIP. AggregateException: " + $_.Exception.ToString() + $LastException = $_.Exception.ToString() } catch { - Write-Log "Retry ${retryString}: Failed to connect to API server $MasterIP. Error: $_" - } + Logs-To-Event -TaskName "AKS.WindowsCSE.CheckAPIServerConnectivity" -TaskMessage "Retry ${retryString}: Failed to connect to API server $MasterIP. Error: $_" + $LastException = "$_" + } $retryCount++ Write-Log "Retry ${retryString}: Sleep $RetryInterval and then retry to connect to API server" Sleep $RetryInterval } while ($retryCount -lt $MaxRetryCount) - Set-ExitCode -ExitCode $global:WINDOWS_CSE_ERROR_CHECK_API_SERVER_CONNECTIVITY -ErrorMessage "Failed to connect to API server $MasterIP after $retryCount retries" + $lastExceptionMessage = "" + if ($LastException -ne $null) { + if ($LastException.Exception -ne $null -and -not [string]::IsNullOrEmpty($LastException.Exception.Message)) { + $lastExceptionMessage = $LastException.Exception.Message + } else { + $lastExceptionMessage = $LastException.ToString() + } + # Normalize any CR/LF in the exception message to spaces to keep ErrorMessage single-line. + $lastExceptionMessage = $lastExceptionMessage -replace "(`r|`n)+", " " + } + + Set-ExitCode -ExitCode $global:WINDOWS_CSE_ERROR_CHECK_API_SERVER_CONNECTIVITY -ErrorMessage "Failed to connect to API server $MasterIP after $retryCount retries. Last exception: $lastExceptionMessage" } function Get-CACertificates {