Skip to content

Commit

Permalink
Enable Windows OVS Container to run on pristine Host Environment
Browse files Browse the repository at this point in the history
Signed-off-by: Naman Agarwal <naman.agarwal75@gmail.com>
  • Loading branch information
NamanAg30 committed Sep 8, 2023
1 parent 4cc6f63 commit 7f09ecb
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 35 deletions.
2 changes: 2 additions & 0 deletions build/images/Dockerfile.build.windows
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@ COPY --from=windows-ovs /Windows/System32/vcruntime140.dll /Windows/System32/
COPY --from=windows-ovs /Windows/System32/libeay32.dll /Windows/System32/
COPY --from=windows-ovs /Windows/System32/ssleay32.dll /Windows/System32/

COPY build/images/scripts/* /

RUN mkdir C:\openvswitch
COPY --from=windows-ovs /openvswitch /openvswitch/
156 changes: 156 additions & 0 deletions build/images/scripts/start_ovs_windows_containerd.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
$ErrorActionPreference = "Stop"
$OVSVersion = "3.0.5-antrea.0"
$OVSDownloadURL = "https://downloads.antrea.io/ovs/ovs-$OVSVersion-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 CheckIfOVSInstalled() {
if (Test-Path -Path $OVSInstallDir) {
Log "$OVSInstallDir already exists, exit OVS installation."
exit 1
}
}

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
}

$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"
$OVS_DB_SCHEMA_PATH = "$mountPath/openvswitch/usr/share/openvswitch/vswitch.ovsschema"
$OVS_DB_PATH = "C:\openvswitch\etc\openvswitch\conf.db"
if ($(Test-Path $OVS_DB_SCHEMA_PATH) -and !$(Test-Path $OVS_DB_PATH)) {
ovsdb-tool create "$OVS_DB_PATH" "$OVS_DB_SCHEMA_PATH"
}
ovsdb-server $OVS_DB_PATH -vfile:info --remote=punix:db.sock --log-file=/var/log/antrea/openvswitch/ovsdb-server.log --pidfile --detach
ovs-vsctl --no-wait init

# Set OVS version.
$OVS_VERSION=$(Get-Item $mountPath\openvswitch\driver\OVSExt.sys).VersionInfo.ProductVersion
ovs-vsctl --no-wait set Open_vSwitch . ovs_version=$OVS_VERSION

ovs-vswitchd --log-file=/var/log/antrea/openvswitch/ovs-vswitchd.log --pidfile -vfile:info --detach

$SleepInterval = 30
Write-Host "Started the loop that checks OVS status every $SleepInterval seconds"
while ($true) {
if ( !( Get-Process ovsdb-server ) ) {
Write-Host "ovsdb-server is not running, starting it again..."
ovsdb-server $OVS_DB_PATH -vfile:info --remote=punix:db.sock --log-file=/var/log/antrea/openvswitch/ovsdb-server.log --pidfile --detach
}
if ( !( Get-Process ovs-vswitchd ) ) {
Write-Host "ovs-vswitchd is not running, starting it again..."
ovs-vswitchd --log-file=/var/log/antrea/openvswitch/ovs-vswitchd.log --pidfile -vfile:info --detach
}
Start-Sleep -Seconds $SleepInterval
}
40 changes: 5 additions & 35 deletions build/yamls/antrea-windows-containerd-with-ovs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,6 @@ data:
$mountPath = ($mountPath.Replace('\', '/')).TrimEnd('/')
$env:PATH = $env:PATH + ";$mountPath/Windows/System32;$mountPath/k/antrea/bin;$mountPath/openvswitch/usr/bin;$mountPath/openvswitch/usr/sbin"
& 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"
$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"
$OVS_DB_SCHEMA_PATH = "$mountPath/openvswitch/usr/share/openvswitch/vswitch.ovsschema"
$OVS_DB_PATH = "C:\openvswitch\etc\openvswitch\conf.db"
if ($(Test-Path $OVS_DB_SCHEMA_PATH) -and !$(Test-Path $OVS_DB_PATH)) {
ovsdb-tool create "$OVS_DB_PATH" "$OVS_DB_SCHEMA_PATH"
}
ovsdb-server $OVS_DB_PATH -vfile:info --remote=punix:db.sock --log-file=/var/log/antrea/openvswitch/ovsdb-server.log --pidfile --detach
ovs-vsctl --no-wait init
# Set OVS version.
$OVS_VERSION=$(Get-Item $mountPath\openvswitch\driver\OVSExt.sys).VersionInfo.ProductVersion
ovs-vsctl --no-wait set Open_vSwitch . ovs_version=$OVS_VERSION
ovs-vswitchd --log-file=/var/log/antrea/openvswitch/ovs-vswitchd.log --pidfile -vfile:info --detach
$SleepInterval = 30
Write-Host "Started the loop that checks OVS status every $SleepInterval seconds"
while ($true) {
if ( !( Get-Process ovsdb-server ) ) {
Write-Host "ovsdb-server is not running, starting it again..."
ovsdb-server $OVS_DB_PATH -vfile:info --remote=punix:db.sock --log-file=/var/log/antrea/openvswitch/ovsdb-server.log --pidfile --detach
}
if ( !( Get-Process ovs-vswitchd ) ) {
Write-Host "ovs-vswitchd is not running, starting it again..."
ovs-vswitchd --log-file=/var/log/antrea/openvswitch/ovs-vswitchd.log --pidfile -vfile:info --detach
}
Start-Sleep -Seconds $SleepInterval
}
VMSwitchExtension-AntreaAgent-Containerd.ps1: |
Param(
[parameter(Mandatory = $false)] [ValidateSet("enable", "disable")] [string] $VMSwitchExtension = "disable"
Expand Down Expand Up @@ -311,8 +279,10 @@ spec:
- mountPath: /var/log/antrea/
name: var-log-antrea
- args:
- -file
- $env:CONTAINER_SANDBOX_MOUNT_POINT/var/lib/antrea-windows/Run-AntreaOVS-Containerd.ps1
- -command
- |
Move-Item -Path "$env:CONTAINER_SANDBOX_MOUNT_POINT/start_ovs_windows_containerd.ps1" -Destination "$env:CONTAINER_SANDBOX_MOUNT_POINT/var/lib/antrea-windows/start_ovs_windows_containerd.ps1" -Force
. "$env:CONTAINER_SANDBOX_MOUNT_POINT/var/lib/antrea-windows/start_ovs_windows_containerd.ps1"
command:
- powershell
image: antrea/antrea-windows:latest
Expand Down Expand Up @@ -366,4 +336,4 @@ spec:
type: DirectoryOrCreate
name: var-log-antrea
updateStrategy:
type: RollingUpdate
type: RollingUpdate

0 comments on commit 7f09ecb

Please sign in to comment.