From 9eadc53b782b0ae4afd4a74092f34acab808804c Mon Sep 17 00:00:00 2001 From: Naman Agarwal Date: Fri, 25 Aug 2023 15:07:52 +0530 Subject: [PATCH] Enable Windows OVS Container to run on pristine Host Environment Signed-off-by: Naman Agarwal --- .../antrea-windows-containerd-with-ovs.yml | 143 +++++++++++++++++- 1 file changed, 141 insertions(+), 2 deletions(-) diff --git a/build/yamls/antrea-windows-containerd-with-ovs.yml b/build/yamls/antrea-windows-containerd-with-ovs.yml index cca93bae5bd..3a78b944694 100644 --- a/build/yamls/antrea-windows-containerd-with-ovs.yml +++ b/build/yamls/antrea-windows-containerd-with-ovs.yml @@ -22,6 +22,145 @@ data: & antrea-agent --config=$mountPath/etc/antrea/antrea-agent.conf --logtostderr=false --log_dir=c:/var/log/antrea --alsologtostderr --log_file_max_size=100 --log_file_max_num=4 --v=0 Run-AntreaOVS-Containerd.ps1: | $ErrorActionPreference = "Stop" + $OVSDownloadURL = "https://downloads.antrea.io/ovs/ovs-3.0.5-antrea.0-win64.zip" + $OVSPublishedHash = 'fd27703ef7314b26b98cffb7aea27d569530ebd3ac3c98daa981ca2654373032' + $WorkDir = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition) + $OVSDownloadDir = $WorkDir + $OVSInstallDir = "C:\openvswitch" + $OVSZip = "$OVSDownloadDir\ovs-win64.zip" + $ImportCertificate = $true + $CheckFileHash = $true + $PowerShellModuleBase = "C:\Windows\System32\WindowsPowerShell\v1.0\Modules" + $InstallLog = "$OVSDownloadDir\install_ovs.log" + + function Log($Info) { + $time = $(get-date -Format g) + "$time $Info `n`r" | Tee-Object $InstallLog -Append | Write-Host + } + + function CreatePath($Path){ + if ($(Test-Path $Path)) { + mv $Path $($Path + "_bak") + } + mkdir -p $Path | Out-Null + } + + function SetEnvVar($key, $value) { + [Environment]::SetEnvironmentVariable($key, $value, [EnvironmentVariableTarget]::Machine) + } + + function WaitExpandFiles($Src, $Dest) { + Log "Extract $Src to $Dest" + Expand-Archive -Path $Src -DestinationPath $Dest | Out-Null + } + + function ServiceExists($ServiceName) { + If (Get-Service $ServiceName -ErrorAction SilentlyContinue) { + return $true + } + return $false + } + + function CheckIfOVSInstalled() { + if (Test-Path -Path $OVSInstallDir) { + Log "$OVSInstallDir already exists, exit OVS installation." + exit 1 + } + If (ServiceExists("ovs-vswitchd")) { + Log "Found existing OVS service, exit OVS installation." + exit 0 + } + } + + If (!(Test-Path $OVSDownloadDir)) { + mkdir -p $OVSDownloadDir + } + Log "Downloading OVS package from $OVSDownloadURL to $OVSZip" + curl.exe -sLo $OVSZip $OVSDownloadURL + If (!$?) { + Log "Download OVS failed, URL: $OVSDownloadURL" + exit 1 + } + + if ($CheckFileHash) { + $FileHash = Get-FileHash $OVSZip + If ($OVSPublishedHash -ne "" -And $FileHash.Hash -ne $OVSPublishedHash) { + Log "SHA256 mismatch for OVS download" + exit 1 + } + } + + WaitExpandFiles $OVSZip $OVSDownloadDir + # Copy OVS package to target dir. + Log "Copying OVS package from $OVSDownloadDir\openvswitch to $OVSInstallDir" + mv "$OVSDownloadDir\openvswitch" $OVSInstallDir + rm $OVSZip + # Create log and run dir. + $OVS_LOG_PATH = $OVSInstallDir + "\var\log\openvswitch" + CreatePath $OVS_LOG_PATH + $OVSRunDir = $OVSInstallDir + "\var\run\openvswitch" + CreatePath $OVSRunDir + $OVSDriverDir = "$OVSInstallDir\driver" + + # Install OVS driver certificate. + $DriverFile="$OVSDriverDir\OVSExt.sys" + if ($ImportCertificate) { + $CertificateFile = "$OVSDriverDir\package.cer" + if (!(Test-Path $CertificateFile)) { + $ExportType = [System.Security.Cryptography.X509Certificates.X509ContentType]::Cert; + $Cert = (Get-AuthenticodeSignature $DriverFile).SignerCertificate; + [System.IO.File]::WriteAllBytes($CertificateFile, $Cert.Export($ExportType)); + } + Log "Installing OVS driver certificate." + Import-Certificate -FilePath "$CertificateFile" -CertStoreLocation cert:\LocalMachine\TrustedPublisher + Import-Certificate -FilePath "$CertificateFile" -CertStoreLocation cert:\LocalMachine\Root + } + + # Install Microsoft Visual C++ Redistributable Package. + if (Test-Path $OVSInstallDir\redist) { + Log "Installing Microsoft Visual C++ Redistributable Package." + $RedistFiles = Get-ChildItem "$OVSInstallDir\redist" -Filter *.exe + $RedistFiles | ForEach-Object { + Log "Installing $_" + Start-Process -FilePath $_.FullName -Args '/install /passive /norestart' -Verb RunAs -Wait + } + } + + # Install powershell modules + if (Test-Path $OVSInstallDir\scripts) { + Log "Installing powershell modules." + $PSModuleFiles = Get-ChildItem "$OVSInstallDir\scripts" -Filter *.psm1 + $PSModuleFiles | ForEach-Object { + $PSModulePath = Join-Path -Path $PowerShellModuleBase -ChildPath $_.BaseName + if (!(Test-Path $PSModulePath)) { + Log "Installing $_" + mkdir -p $PSModulePath + Copy-Item $_.FullName $PSModulePath + } + } + } + + # Install OVS kernel driver. + Log "Installing OVS kernel driver" + $VMMSStatus = $(Get-Service vmms -ErrorAction SilentlyContinue).Status + if (!$VMMSStatus) { + $VMMSStatus = "not exist" + } + Log "Hyper-V Virtual Machine Management service status: $VMMSStatus" + if ($VMMSStatus -eq "Running") { + cmd /c "cd $OVSDriverDir && install.cmd" + } else { + cd $OVSDriverDir ; netcfg -l .\ovsext.inf -c s -i OVSExt; cd $WorkDir + } + if (!$?) { + Log "Install OVS kernel driver failed, exit" + exit 1 + } + $OVS_BIN_PATH="$OVSInstallDir\usr\bin;$OVSInstallDir\usr\sbin" + $env:Path += ";$OVS_BIN_PATH" + SetEnvVar "Path" $env:Path + + $mountPath = $env:CONTAINER_SANDBOX_MOUNT_POINT $mountPath = ($mountPath.Replace('\', '/')).TrimEnd('/') $env:PATH = $env:PATH + ";$mountPath/Windows/System32;$mountPath/openvswitch/usr/bin;$mountPath/openvswitch/usr/sbin" @@ -197,7 +336,7 @@ data: # Provide the address of Kubernetes apiserver, to override any value provided in kubeconfig or InClusterConfig. # Defaults to "". It must be a host string, a host:port pair, or a URL to the base of the apiserver. - #kubeAPIServerOverride: "" + kubeAPIServerOverride: "https://10.221.121.58:6443" # Option antreaProxy contains AntreaProxy related configuration options. antreaProxy: @@ -366,4 +505,4 @@ spec: type: DirectoryOrCreate name: var-log-antrea updateStrategy: - type: RollingUpdate + type: RollingUpdate \ No newline at end of file