From 166558d2f3f3bce58c64fe68d29c0c11495ed33a Mon Sep 17 00:00:00 2001 From: Chou Hu Date: Fri, 23 Feb 2024 16:22:20 +0800 Subject: [PATCH] feat: support Windows container local dumps (#3684) --- parts/windows/windowscsehelper.tests.ps1 | 10 +++++++++ staging/cse/windows/containerdfunc.ps1 | 24 ++++++++++++++++++--- staging/cse/windows/containerdtemplate.toml | 4 ++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/parts/windows/windowscsehelper.tests.ps1 b/parts/windows/windowscsehelper.tests.ps1 index 5d49a2b88c7..0d8a96f8241 100644 --- a/parts/windows/windowscsehelper.tests.ps1 +++ b/parts/windows/windowscsehelper.tests.ps1 @@ -45,6 +45,16 @@ Describe 'Install-Containerd-Based-On-Kubernetes-Version' { Assert-MockCalled -CommandName "Install-Containerd" -Exactly -Times 1 -ParameterFilter { $ContainerdUrl -eq $expectedURL } } + # It retrieves the containerd version from containerd URL in Install-Containerd in staging/cse/windows/containerdfunc.ps1 + It 'validate whether containerd URL has the correct version' { + $fileName = [IO.Path]::GetFileName($global:StableContainerdPackage) + $containerdVersion = $fileName.Split("-")[1].SubString(1) + {Write-Host ([version]$containerdVersion)} | Should -Not -Throw + + $fileName = [IO.Path]::GetFileName($global:LatestContainerdPackage) + $containerdVersion = $fileName.Split("-")[1].SubString(1) + {Write-Host ([version]$containerdVersion)} | Should -Not -Throw + } } Describe 'Get-WindowsVersion and Get-WindowsPauseVersion' { diff --git a/staging/cse/windows/containerdfunc.ps1 b/staging/cse/windows/containerdfunc.ps1 index 8d75a1279d9..195cc4a25a8 100644 --- a/staging/cse/windows/containerdfunc.ps1 +++ b/staging/cse/windows/containerdfunc.ps1 @@ -138,7 +138,10 @@ function Install-Containerd { } # TODO: check if containerd is already installed and is the same version before this. - + + $fileName = [IO.Path]::GetFileName($ContainerdUrl) + $containerdVersion = $fileName.Split("-")[1].SubString(1) # not full version, just the version number. For example, 1.7.9. The full version is v1.7.9-azure.1 + # Extract the package # upstream containerd package is a tar $tarfile = [Io.path]::Combine($ENV:TEMP, "containerd.tar.gz") @@ -161,6 +164,8 @@ function Install-Containerd { $windowsVersion = Get-WindowsPauseVersion $hypervRuntimes = "" $hypervHandlers = $global:ContainerdWindowsRuntimeHandlers.split(",", [System.StringSplitOptions]::RemoveEmptyEntries) + $containerAnnotations = 'container_annotations = ["io.microsoft.container.processdumplocation", "io.microsoft.wcow.processdumptype", "io.microsoft.wcow.processdumpcount"]' + $podAnnotations = 'pod_annotations = ["io.microsoft.container.processdumplocation","io.microsoft.wcow.processdumptype", "io.microsoft.wcow.processdumpcount"]' # configure if ($global:DefaultContainerdWindowsSandboxIsolation -eq "hyperv") { @@ -171,18 +176,31 @@ function Install-Containerd { $template = Get-Content -Path "c:\AzureData\windows\containerdtemplate.toml" if ($sandboxIsolation -eq 0 -And $hypervHandlers.Count -eq 0) { # remove the value hypervisor place holder - $template = $template | Select-String -Pattern 'hypervisors' -NotMatch | Out-String + $template = $template | Select-String -Pattern 'hypervisors' -NotMatch } else { $hypervRuntimes = CreateHypervisorRuntimes -builds @($hypervHandlers) -image $pauseImage } + # remove the value containerAnnotations and podAnnotations place holder since it is not supported in containerd versions older than 1.7.9 + if (([version]$containerdVersion).CompareTo([version]"1.7.9") -lt 0) { + # remove the value containerAnnotations place holder + $template = $template | Select-String -Pattern 'containerAnnotations' -NotMatch + # remove the value podAnnotations place holder + $template = $template | Select-String -Pattern 'podAnnotations' -NotMatch + } + + # Need to convert the template to string to replace the place holders but + # `Select-String -Pattern [PATTERN] -NotMatch` does not work after converting the template to string + $template = $template | Out-String $template.Replace('{{sandboxIsolation}}', $sandboxIsolation). Replace('{{pauseImage}}', $pauseImage). Replace('{{hypervisors}}', $hypervRuntimes). Replace('{{cnibin}}', $formatedbin). Replace('{{cniconf}}', $formatedconf). - Replace('{{currentversion}}', $windowsVersion) | ` + Replace('{{currentversion}}', $windowsVersion). + Replace('{{containerAnnotations}}', $containerAnnotations). + Replace('{{podAnnotations}}', $podAnnotations) | ` Out-File -FilePath "$configFile" -Encoding ascii RegisterContainerDService -KubeDir $KubeDir diff --git a/staging/cse/windows/containerdtemplate.toml b/staging/cse/windows/containerdtemplate.toml index 1338f52a290..9001d15b026 100644 --- a/staging/cse/windows/containerdtemplate.toml +++ b/staging/cse/windows/containerdtemplate.toml @@ -37,6 +37,8 @@ state = "C:\\ProgramData\\containerd\\state" no_pivot = false [plugins.cri.containerd.default_runtime] runtime_type = "io.containerd.runhcs.v1" + {{containerAnnotations}} + {{podAnnotations}} [plugins.cri.containerd.default_runtime.options] Debug = false DebugType = 0 @@ -46,6 +48,8 @@ state = "C:\\ProgramData\\containerd\\state" [plugins.cri.containerd.runtimes] [plugins.cri.containerd.runtimes.runhcs-wcow-process] runtime_type = "io.containerd.runhcs.v1" + {{containerAnnotations}} + {{podAnnotations}} [plugins.cri.containerd.runtimes.runhcs-wcow-process.options] Debug = true DebugType = 2