Skip to content
Open
Changes from all 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
22 changes: 18 additions & 4 deletions staging/cse/windows/kubernetesfunc.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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()
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logs-To-Event -TaskMessage "..." + $_.Exception.ToString() is likely to be parsed in argument mode as multiple arguments ("...", '+', and the exception string), which can break parameter binding at runtime. Wrap the concatenation in parentheses or use a single interpolated string/subexpression so -TaskMessage receives one string value.

Suggested change
Logs-To-Event -TaskName "AKS.WindowsCSE.CheckAPIServerConnectivity" -TaskMessage "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())"

Copilot uses AI. Check for mistakes.
$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 {
Comment on lines +217 to +221
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$LastException is assigned string values earlier ($_.Exception.ToString() / "$_"), but here it's treated like an exception object ($LastException.Exception.Message). As a result, the .Exception.Message branch will never be used and you may lose useful exception details. Store the exception object consistently (e.g., $_.Exception) and then derive the final single-line message from its .Message/.ToString().

Copilot uses AI. Check for mistakes.
$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"
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If $LastException is never set (e.g., failures that don't throw), the final error message will still include Last exception: with an empty value, which makes the message noisier. Consider only appending the "Last exception" suffix when $lastExceptionMessage is non-empty.

Suggested change
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"
$baseErrorMessage = "Failed to connect to API server $MasterIP after $retryCount retries."
$errorMessage = $baseErrorMessage
if (-not [string]::IsNullOrEmpty($lastExceptionMessage)) {
$errorMessage = "$baseErrorMessage Last exception: $lastExceptionMessage"
}
Set-ExitCode -ExitCode $global:WINDOWS_CSE_ERROR_CHECK_API_SERVER_CONNECTIVITY -ErrorMessage $errorMessage

Copilot uses AI. Check for mistakes.
}

function Get-CACertificates {
Expand Down
Loading