Skip to content

Commit

Permalink
chore: support cse duration log for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
junjiezhang1997 committed Jan 8, 2024
1 parent f393d6e commit 3536228
Show file tree
Hide file tree
Showing 15 changed files with 586 additions and 252 deletions.
53 changes: 36 additions & 17 deletions parts/windows/kuberneteswindowssetup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ param(
)
# Do not parse the start time from $LogFile to simplify the logic
$StartTime=Get-Date
$cseFormattedStartTime=$(Get-Date -Format "yyyy-mm-dd HH:mm:ss.fff")
$global:ExitCode=0
$global:ErrorMessage=""

Expand Down Expand Up @@ -157,8 +158,8 @@ $global:AzureCNIConfDir = [Io.path]::Combine("$global:AzureCNIDir", "netconf")
# $global:NetworkPolicy = "{{GetParameter "networkPolicy"}}" # BUG: unused
$global:NetworkPlugin = "{{GetParameter "networkPlugin"}}"
$global:VNetCNIPluginsURL = "{{GetParameter "vnetCniWindowsPluginsURL"}}"
$global:IsDualStackEnabled = {{if IsIPv6DualStackFeatureEnabled}}$true{{else}}$false{{end}}
$global:IsAzureCNIOverlayEnabled = {{if IsAzureCNIOverlayFeatureEnabled}}$true{{else}}$false{{end}}
$global:IsDualStackEnabled = {{if IsIPv6DualStackFeatureEnabled}}1{{else}}0{{end}}
$global:IsAzureCNIOverlayEnabled = {{if IsAzureCNIOverlayFeatureEnabled}}1{{else}}0{{end}}

# CSI Proxy settings
$global:EnableCsiProxy = [System.Convert]::ToBoolean("{{GetVariable "windowsEnableCSIProxy" }}");
Expand Down Expand Up @@ -244,7 +245,7 @@ try
# Download CSE function scripts
Write-Log "Getting CSE scripts"
$tempfile = 'c:\csescripts.zip'
DownloadFileOverHttp -Url $global:CSEScriptsPackageUrl -DestinationPath $tempfile -ExitCode $global:WINDOWS_CSE_ERROR_DOWNLOAD_CSE_PACKAGE
Logs-To-Events "AKS.WindowsCSE.DownloadCSEScriptPackageUrl" DownloadFileOverHttp -Url $global:CSEScriptsPackageUrl -DestinationPath $tempfile -ExitCode $global:WINDOWS_CSE_ERROR_DOWNLOAD_CSE_PACKAGE
Expand-Archive $tempfile -DestinationPath "C:\\AzureData\\windows"
Remove-Item -Path $tempfile -Force

Expand All @@ -266,16 +267,16 @@ try
}

Write-Log "Apply telemetry data setting"
Set-TelemetrySetting -WindowsTelemetryGUID $global:WindowsTelemetryGUID
Logs-To-Events "AKS.WindowsCSE.SetTelemetrySetting" Set-TelemetrySetting -WindowsTelemetryGUID $global:WindowsTelemetryGUID

Write-Log "Resize os drive if possible"
Resize-OSDrive
Logs-To-Events "AKS.WindowsCSE.ResizeOSDrive" Resize-OSDrive

Write-Log "Initialize data disks"
Initialize-DataDisks
Logs-To-Events "AKS.WindowsCSE.InitializeDataDisks" Initialize-DataDisks

Write-Log "Create required data directories as needed"
Initialize-DataDirectories
Logs-To-Events "AKS.WindowsCSE.InitializeDataDirectories" Initialize-DataDirectories

Create-Directory -FullPath "c:\k"
Write-Log "Remove `"NT AUTHORITY\Authenticated Users`" write permissions on files in c:\k"
Expand Down Expand Up @@ -309,9 +310,9 @@ try
$cniBinPath = $global:CNIPath
$cniConfigPath = $global:CNIConfigPath
}
Install-Containerd-Based-On-Kubernetes-Version -ContainerdUrl $global:ContainerdUrl -CNIBinDir $cniBinPath -CNIConfDir $cniConfigPath -KubeDir $global:KubeDir -KubernetesVersion $global:KubeBinariesVersion
Logs-To-Events "AKS.WindowsCSE.InstallContainerdBasedOnKubernetesVersion" Install-Containerd-Based-On-Kubernetes-Version -ContainerdUrl $global:ContainerdUrl -CNIBinDir $cniBinPath -CNIConfDir $cniConfigPath -KubeDir $global:KubeDir -KubernetesVersion $global:KubeBinariesVersion

Retag-ImagesForAzureChinaCloud -TargetEnvironment $TargetEnvironment
Logs-To-Events "AKS.WindowsCSE.RetagImagesForAzureChinaCloud" Retag-ImagesForAzureChinaCloud -TargetEnvironment $TargetEnvironment

# For AKSClustomCloud, TargetEnvironment must be set to AzureStackCloud
Write-Log "Write Azure cloud provider config"
Expand Down Expand Up @@ -387,14 +388,14 @@ try

# Configure network policy.
Get-HnsPsm1 -HNSModule $global:HNSModule
Import-Module $global:HNSModule
Logs-To-Events "AKS.WindowsCSE.ImportHNSModule" Import-Module $global:HNSModule

Write-Log "Installing Azure VNet plugins"
Install-VnetPlugins -AzureCNIConfDir $global:AzureCNIConfDir `
Logs-To-Events "AKS.WindowsCSE.InstallVnetPlugins" Install-VnetPlugins -AzureCNIConfDir $global:AzureCNIConfDir `
-AzureCNIBinDir $global:AzureCNIBinDir `
-VNetCNIPluginsURL $global:VNetCNIPluginsURL

Set-AzureCNIConfig -AzureCNIConfDir $global:AzureCNIConfDir `
Logs-To-Events "AKS.WindowsCSE.SetAzureCNIConfig" Set-AzureCNIConfig -AzureCNIConfDir $global:AzureCNIConfDir `
-KubeDnsSearchPath $global:KubeDnsSearchPath `
-KubeClusterCIDR $global:KubeClusterCIDR `
-KubeServiceCIDR $global:KubeServiceCIDR `
Expand All @@ -403,7 +404,7 @@ try
-IsAzureCNIOverlayEnabled $global:IsAzureCNIOverlayEnabled

if ($TargetEnvironment -ieq "AzureStackCloud") {
GenerateAzureStackCNIConfig `
Logs-To-Events "AKS.WindowsCSE.GenerateAzureStackCNIConfig" GenerateAzureStackCNIConfig `
-TenantId $global:TenantId `
-SubscriptionId $global:SubscriptionId `
-ResourceGroup $global:ResourceGroup `
Expand All @@ -415,9 +416,9 @@ try
-IdentitySystem "{{ GetIdentitySystem }}"
}

New-ExternalHnsNetwork -IsDualStackEnabled $global:IsDualStackEnabled
Logs-To-Events "AKS.WindowsCSE.NewExternalHnsNetwork" New-ExternalHnsNetwork -IsDualStackEnabled $global:IsDualStackEnabled

Install-KubernetesServices `
Logs-To-Events "AKS.WindowsCSE.InstallKubernetesServices" Install-KubernetesServices `
-KubeDir $global:KubeDir

Write-Log "Disable Internet Explorer compat mode and set homepage"
Expand Down Expand Up @@ -478,10 +479,10 @@ try
Remove-Item $kubeConfigFile
}

Enable-GuestVMLogs -IntervalInMinutes $global:LogGeneratorIntervalInMinutes
Logs-To-Events "AKS.WindowsCSE.EnableGuestVMLogs" Enable-GuestVMLogs -IntervalInMinutes $global:LogGeneratorIntervalInMinutes

Write-Log "Setup Complete, starting NodeResetScriptTask to register Winodws node without reboot"
Start-ScheduledTask -TaskName "k8s-restart-job"
Logs-To-Events "AKS.WindowsCSE.StartScheduledTask" Start-ScheduledTask -TaskName "k8s-restart-job"

$timeout = 180 ## seconds
$timer = [Diagnostics.Stopwatch]::StartNew()
Expand Down Expand Up @@ -511,13 +512,31 @@ finally
{
# Generate CSE result so it can be returned as the CSE response in csecmd.ps1
$ExecutionDuration=$(New-Timespan -Start $StartTime -End $(Get-Date))
$cseFormattedEndTime=$(Get-Date -Format "yyyy-mm-dd HH:mm:ss.fff")
$eventsFileName=[DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()

Write-Log "CSE ExecutionDuration: $ExecutionDuration"

# Windows CSE does not return any error message so we cannot generate below content as the response
# $JsonString = "ExitCode: `"{0}`", Output: `"{1}`", Error: `"{2}`", ExecDuration: `"{3}`"" -f $global:ExitCode, "", $global:ErrorMessage, $ExecutionDuration.TotalSeconds
Write-Log "Generate CSE result to $CSEResultFilePath : $global:ExitCode"
echo $global:ExitCode | Out-File -FilePath $CSEResultFilePath -Encoding utf8

$messageString="ExitCode: $global:ExitCode, E2E: $ExecutionDuration";
$eventJson=@"
{
"Timestamp": "$cseFormattedStartTime",
"OperationId": "$cseFormattedEndTime",
"Version": "1.23",
"TaskName": "AKS.WindowsCSE.cse_start",
"EventLevel": "Informational",
"Message": "$messageString",
"EventPid": "0",
"EventTid": "0"
}
"@
echo $eventJson | Set-Content ${global:EventsLoggingDir}${eventsFileName}.json

# Flush stdout to C:\AzureData\CustomDataSetupScript.log
[Console]::Out.Flush()

Expand Down
36 changes: 35 additions & 1 deletion parts/windows/windowscsehelper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ $global:LatestContainerdPackage = "v1.7.1-azure.1/binaries/containerd-v1.7.1-azu
# The latest containerd version that contains stable ABI
$global:LatestContainerdPackagefor23H2 = "v1.7.9-azure.1/binaries/containerd-v1.7.9-azure.1-windows-amd64.tar.gz"

$global:EventsLoggingDir = "C:\WindowsAzure\Logs\Plugins\Microsoft.Compute.CustomScriptExtension\Events\"


# This filter removes null characters (\0) which are captured in nssm.exe output when logged through powershell
filter RemoveNulls { $_ -replace '\0', '' }
Expand Down Expand Up @@ -122,7 +124,7 @@ function DownloadFileOverHttp {

$downloadTimer = [System.Diagnostics.Stopwatch]::StartNew()
try {
$args = @{Uri=$Url; Method="Get"; OutFile=$DestinationPath}
$args = @{Uri="$Url"; Method="Get"; OutFile=$DestinationPath}
Retry-Command -Command "Invoke-RestMethod" -Args $args -Retries 5 -RetryDelaySeconds 10
} catch {
Set-ExitCode -ExitCode $ExitCode -ErrorMessage "Failed in downloading $Url. Error: $_"
Expand Down Expand Up @@ -335,4 +337,36 @@ function Install-Containerd-Based-On-Kubernetes-Version {
$ContainerdUrl = $ContainerdUrl + $containerdPackage
}
Install-Containerd -ContainerdUrl $ContainerdUrl -CNIBinDir $CNIBinDir -CNIConfDir $CNIConfDir -KubeDir $KubeDir
}

function Logs-To-Events {
$count = $args.Count
$cmd = [string]$args[1..$count]

$task = $args[0]
$eventsFileName=[DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()

$startTime=$(Get-Date -Format "yyyy-mm-dd HH:mm:ss.fff")
Invoke-Expression $cmd
$ret=$?
$endTime=$(Get-Date -Format "yyyy-mm-dd HH:mm:ss.fff")

$json_string = @"
{
"Timestamp": "$startTime",
"OperationId": "$endTime",
"Version": "1.23",
"TaskName": "$task",
"EventLevel": "Informational",
"Message": "Completed: $cmd",
"EventPid": "0",
"EventTid": "0"
}
"@

echo $json_string | Set-Content ${EventsLoggingDir}${eventsFileName}.json

if (-not $ret) {
return $ret
}
}
Loading

0 comments on commit 3536228

Please sign in to comment.