Skip to content

Commit

Permalink
chore: support cse log for windows (#3944)
Browse files Browse the repository at this point in the history
  • Loading branch information
junjiezhang1997 authored Feb 20, 2024
1 parent 8cadd53 commit 021c6a0
Show file tree
Hide file tree
Showing 21 changed files with 607 additions and 644 deletions.
76 changes: 31 additions & 45 deletions parts/windows/kuberneteswindowssetup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,11 @@ Expand-Archive scripts.zip -DestinationPath "C:\\AzureData\\"
. c:\AzureData\windows\windowscsehelper.ps1
# util functions only can be used after this line, for example, Write-Log

$global:OperationId = New-Guid

try
{
Write-Log ".\CustomDataSetupScript.ps1 -MasterIP $MasterIP -KubeDnsServiceIp $KubeDnsServiceIp -MasterFQDNPrefix $MasterFQDNPrefix -Location $Location -AADClientId $AADClientId -NetworkAPIVersion $NetworkAPIVersion -TargetEnvironment $TargetEnvironment"
Logs-To-Event -TaskName "AKS.WindowsCSE.ExecuteCustomDataSetupScript" -TaskMessage ".\CustomDataSetupScript.ps1 -MasterIP $MasterIP -KubeDnsServiceIp $KubeDnsServiceIp -MasterFQDNPrefix $MasterFQDNPrefix -Location $Location -AADClientId $AADClientId -NetworkAPIVersion $NetworkAPIVersion -TargetEnvironment $TargetEnvironment"

# Exit early if the script has been executed
if (Test-Path -Path $CSEResultFilePath -PathType Leaf) {
Expand All @@ -241,13 +243,14 @@ try
$global:CSEScriptsPackageUrl = $global:CSEScriptsPackageUrl + $WindowsCSEScriptsPackage
Write-Log "CSEScriptsPackageUrl is set to $global:CSEScriptsPackageUrl"
}

# Download CSE function scripts
Write-Log "Getting CSE scripts"
Logs-To-Event -TaskName "AKS.WindowsCSE.DownloadAndExpandCSEScriptPackageUrl" -TaskMessage "Start to get CSE scripts. CSEScriptsPackageUrl: $global:CSEScriptsPackageUrl"
$tempfile = 'c:\csescripts.zip'
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

# Dot-source cse scripts with functions that are called in this script
. c:\AzureData\windows\azurecnifunc.ps1
. c:\AzureData\windows\calicofunc.ps1
Expand All @@ -261,22 +264,18 @@ try
$sshEnabled = [System.Convert]::ToBoolean("{{ WindowsSSHEnabled }}")

if ( $sshEnabled ) {
Write-Log "Install OpenSSH"
Install-OpenSSH -SSHKeys $SSHKeys
}

Write-Log "Apply telemetry data setting"
Set-TelemetrySetting -WindowsTelemetryGUID $global:WindowsTelemetryGUID

Write-Log "Resize os drive if possible"
Resize-OSDrive

Write-Log "Initialize data disks"

Initialize-DataDisks

Write-Log "Create required data directories as needed"

Initialize-DataDirectories


Logs-To-Event -TaskName "AKS.WindowsCSE.GetProvisioningAndLogCollectionScripts" -TaskMessage "Start to get provisioning scripts and log collection scripts"
Create-Directory -FullPath "c:\k"
Write-Log "Remove `"NT AUTHORITY\Authenticated Users`" write permissions on files in c:\k"
icacls.exe "c:\k" /inheritance:r
Expand All @@ -287,25 +286,23 @@ try
icacls.exe "c:\k"
Get-ProvisioningScripts
Get-LogCollectionScripts

Write-KubeClusterConfig -MasterIP $MasterIP -KubeDnsServiceIp $KubeDnsServiceIp

Write-Log "Download kubelet binaries and unzip"

Get-KubePackage -KubeBinariesSASURL $global:KubeBinariesPackageSASURL

Write-Log "Installing ContainerD"

$cniBinPath = $global:AzureCNIBinDir
$cniConfigPath = $global:AzureCNIConfDir
if ($global:NetworkPlugin -eq "kubenet") {
$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

Install-Containerd-Based-On-Kubernetes-Version -ContainerdUrl $global:ContainerdUrl -CNIBinDir $cniBinPath -CNIConfDir $cniConfigPath -KubeDir $global:KubeDir -KubernetesVersion $global:KubeBinariesVersion

Retag-ImagesForAzureChinaCloud -TargetEnvironment $TargetEnvironment

# For AKSClustomCloud, TargetEnvironment must be set to AzureStackCloud
Write-Log "Write Azure cloud provider config"
Write-AzureConfig `
-KubeDir $global:KubeDir `
-AADClientId $AADClientId `
Expand Down Expand Up @@ -338,22 +335,20 @@ try
Get-CACertificates
{{end}}

Write-Log "Write ca root"
Write-CACert -CACertificate $global:CACertificate `
-KubeDir $global:KubeDir

if ($global:EnableCsiProxy) {
New-CsiProxyService -CsiProxyPackageUrl $global:CsiProxyUrl -KubeDir $global:KubeDir
}

if ($global:TLSBootstrapToken) {
Write-Log "Write TLS bootstrap kubeconfig"
Write-BootstrapKubeConfig -CACertificate $global:CACertificate `
-KubeDir $global:KubeDir `
-MasterFQDNPrefix $MasterFQDNPrefix `
-MasterIP $MasterIP `
-TLSBootstrapToken $global:TLSBootstrapToken

# NOTE: we need kubeconfig to setup calico even if TLS bootstrapping is enabled
# This kubeconfig will deleted after calico installation.
# TODO(hbc): once TLS bootstrap is fully enabled, remove this if block
Expand All @@ -368,9 +363,8 @@ try
-MasterIP $MasterIP `
-AgentKey $AgentKey `
-AgentCertificate $global:AgentCertificate

if ($global:EnableHostsConfigAgent) {
Write-Log "Starting hosts config agent"
New-HostsConfigService
}

Expand All @@ -379,20 +373,19 @@ try
# Configure network policy.
Get-HnsPsm1 -HNSModule $global:HNSModule
Import-Module $global:HNSModule

Write-Log "Installing Azure VNet plugins"

Install-VnetPlugins -AzureCNIConfDir $global:AzureCNIConfDir `
-AzureCNIBinDir $global:AzureCNIBinDir `
-VNetCNIPluginsURL $global:VNetCNIPluginsURL

Set-AzureCNIConfig -AzureCNIConfDir $global:AzureCNIConfDir `
-KubeDnsSearchPath $global:KubeDnsSearchPath `
-KubeClusterCIDR $global:KubeClusterCIDR `
-KubeServiceCIDR $global:KubeServiceCIDR `
-VNetCIDR $global:VNetCIDR `
-IsDualStackEnabled $global:IsDualStackEnabled `
-IsAzureCNIOverlayEnabled $global:IsAzureCNIOverlayEnabled

if ($TargetEnvironment -ieq "AzureStackCloud") {
GenerateAzureStackCNIConfig `
-TenantId $global:TenantId `
Expand All @@ -407,32 +400,25 @@ try
}

New-ExternalHnsNetwork -IsDualStackEnabled $global:IsDualStackEnabled

Install-KubernetesServices `
-KubeDir $global:KubeDir

Write-Log "Disable Internet Explorer compat mode and set homepage"
Set-Explorer

Write-Log "Adjust pagefile size"
Adjust-PageFileSize

Write-Log "Start preProvisioning script"
Logs-To-Event -TaskName "AKS.WindowsCSE.PreprovisionExtension" -TaskMessage "Start preProvisioning script"
PREPROVISION_EXTENSION

Write-Log "Update service failure actions"
Update-ServiceFailureActions
Adjust-DynamicPortRange
Register-LogsCleanupScriptTask
Register-NodeResetScriptTask
Update-DefenderPreferences


$windowsVersion = Get-WindowsVersion
if ($windowsVersion -ne "1809") {
Write-Log "Skip secure TLS protocols for Windows version: $windowsVersion"
Logs-To-Event -TaskName "AKS.WindowsCSE.EnableSecureTLS" -TaskMessage "Skip secure TLS protocols for Windows version: $windowsVersion"
} else {
Write-Log "Enable secure TLS protocols"
Logs-To-Event -TaskName "AKS.WindowsCSE.EnableSecureTLS" -TaskMessage "Start to enable secure TLS protocols"
try {
. C:\k\windowssecuretls.ps1
Enable-SecureTls
Expand All @@ -444,19 +430,17 @@ try

Enable-FIPSMode -FipsEnabled $fipsEnabled
if ($global:WindowsGmsaPackageUrl) {
Write-Log "Start to install Windows gmsa package"
Install-GmsaPlugin -GmsaPackageUrl $global:WindowsGmsaPackageUrl
}

Check-APIServerConnectivity -MasterIP $MasterIP

if ($global:WindowsCalicoPackageURL) {
Write-Log "Start calico installation"
Start-InstallCalico -RootDir "c:\" -KubeServiceCIDR $global:KubeServiceCIDR -KubeDnsServiceIp $KubeDnsServiceIp
}

Start-InstallGPUDriver -EnableInstall $global:ConfigGPUDriverIfNeeded -GpuDriverURL $global:GpuDriverURL

if (Test-Path $CacheDir)
{
Write-Log "Removing aks cache directory"
Expand All @@ -472,10 +456,10 @@ try
Enable-GuestVMLogs -IntervalInMinutes $global:LogGeneratorIntervalInMinutes

if ($global:RebootNeeded) {
Write-Log "Setup Complete, calling Postpone-RestartComputer with reboot"
Logs-To-Event -TaskName "AKS.WindowsCSE.RestartComputer" -TaskMessage "Setup Complete, calling Postpone-RestartComputer with reboot"
Postpone-RestartComputer
} else {
Write-Log "Setup Complete, starting NodeResetScriptTask to register Winodws node without reboot"
Logs-To-Event -TaskName "AKS.WindowsCSE.StartScheduledTask" -TaskMessage "Setup Complete, start NodeResetScriptTask to register Windows node without reboot"
Start-ScheduledTask -TaskName "k8s-restart-job"

$timeout = 180 ## seconds
Expand Down Expand Up @@ -504,6 +488,8 @@ finally
# Generate CSE result so it can be returned as the CSE response in csecmd.ps1
$ExecutionDuration=$(New-Timespan -Start $StartTime -End $(Get-Date))
Write-Log "CSE ExecutionDuration: $ExecutionDuration. ExitCode: $global:ExitCode"
Logs-To-Event -TaskName "AKS.WindowsCSE.cse_main" -TaskMessage "ExitCode: $global:ExitCode. ErrorMessage: $global:ErrorMessage."
# Please not use Write-Log or Logs-To-Events after Stop-Transcript
Stop-Transcript

# Remove the parameters in the log file to avoid leaking secrets
Expand Down
58 changes: 57 additions & 1 deletion parts/windows/windowscsehelper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ $global:StableContainerdPackage = "v1.6.21-azure.1/binaries/containerd-v1.6.21-a
# The latest containerd version
$global:LatestContainerdPackage = "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\"
$global:TaskName = ""
$global:TaskTimeStamp = ""

# 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 @@ -224,7 +227,7 @@ function Set-ExitCode

function Postpone-RestartComputer
{
Write-Log "Creating an one-time task to restart the VM"
Logs-To-Event -TaskName "AKS.WindowsCSE.PostponeRestartComputer" -TaskMessage "Start to create an one-time task to restart the VM"
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument " -Command `"Restart-Computer -Force`""
$principal = New-ScheduledTaskPrincipal -UserId SYSTEM -LogonType ServiceAccount -RunLevel Highest
# trigger this task once
Expand Down Expand Up @@ -376,6 +379,8 @@ function Install-Containerd-Based-On-Kubernetes-Version {
$KubernetesVersion
)

Logs-To-Event -TaskName "AKS.WindowsCSE.InstallContainerdBasedOnKubernetesVersion" -TaskMessage "Start to install ContainerD based on kubernetes version. ContainerdUrl: $global:ContainerdUrl, KubernetesVersion: $global:KubeBinariesVersion"

# In the past, $global:ContainerdUrl is a full URL to download Windows containerd package.
# Example: "https://acs-mirror.azureedge.net/containerd/windows/v0.0.46/binaries/containerd-v0.0.46-windows-amd64.tar.gz"
# To support multiple containerd versions, we only set the endpoint in $global:ContainerdUrl.
Expand All @@ -394,5 +399,56 @@ function Install-Containerd-Based-On-Kubernetes-Version {
}
$ContainerdUrl = $ContainerdUrl + $containerdPackage
}
Logs-To-Event -TaskName "AKS.WindowsCSE.InstallContainerd" -TaskMessage "Start to install ContainerD. ContainerdUrl: $ContainerdUrl"
Install-Containerd -ContainerdUrl $ContainerdUrl -CNIBinDir $CNIBinDir -CNIConfDir $CNIConfDir -KubeDir $KubeDir
}

function Logs-To-Event {
Param(
[Parameter(Mandatory = $true)][string]
$TaskName,
[Parameter(Mandatory = $true)][string]
$TaskMessage
)
$eventLevel="Informational"
if ($global:ExitCode -ne 0) {
$eventLevel="Error"
}

$eventsFileName=[DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()
$currentTime=$(Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff")

$lastTaskName = ""
$lastTaskDuration = 0
if ($global:TaskTimeStamp -ne "") {
$lastTaskName = $global:TaskName
$lastTaskDuration = $(New-Timespan -Start $global:TaskTimeStamp -End $currentTime)
}

$global:TaskName = $TaskName
$global:TaskTimeStamp = $currentTime

Write-Log "$global:TaskName - $TaskMessage"
$TaskMessage = (echo $TaskMessage | ConvertTo-Json)
$messageJson = @"
{
"HostName": "$env:computername",
"LastTaskName": "$lastTaskName",
"LastTaskDuration": "$lastTaskDuration",
"CurrentTaskMessage": $TaskMessage
}
"@
$messageJson = (echo $messageJson | ConvertTo-Json)

$jsonString = @"
{
"Timestamp": "$global:TaskTimeStamp",
"OperationId": "$global:OperationId",
"Version": "1.10",
"TaskName": "$global:TaskName",
"EventLevel": "$eventLevel",
"Message": $messageJson
}
"@
echo $jsonString | Set-Content ${global:EventsLoggingDir}${eventsFileName}.json
}
Loading

0 comments on commit 021c6a0

Please sign in to comment.