Skip to content

Commit

Permalink
Add antrea monitor service for cleanup.
Browse files Browse the repository at this point in the history
The antrea-monitor service would run
at system start up and clean up OVS
configuration.

Also fix typo and style issues.
Also update documentation.

Fixes #4122

Signed-off-by: Anand Kumar <kumaranand@vmware.com>
  • Loading branch information
Anandkumar26 committed Oct 10, 2022
1 parent 0dceb75 commit 804be28
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 60 deletions.
8 changes: 5 additions & 3 deletions docs/external-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ spec:
change `vm-ns` to the right Namespace.

```bash
kubectl apply -f https://raw.githubusercontent.com/antrea-io/antrea/feature/externalnode/build/yamls/externalnode/vm-agent-rbac.yml
kubectl apply -f https://raw.githubusercontent.com/antrea-io/antrea/main/build/yamls/externalnode/vm-agent-rbac.yml
```

4. Create `antrea-agent.kubeconfig` file for `antrea-agent` to access the K8S
Expand All @@ -204,8 +204,9 @@ spec:
```bash
export CLUSTER_NAME="kubernetes"
export SERVICE_ACCOUNT="vm-agent"
export NAMESPACE="vm-ns"
APISERVER=$(kubectl config view -o jsonpath="{.clusters[?(@.name==\"$CLUSTER_NAME\")].cluster.server}")
TOKEN=$(kubectl -n vm-ns get secrets -o jsonpath="{.items[?(@.metadata.annotations['kubernetes\.io/service-account\.name']=='$SERVICE_ACCOUNT')].data.token}"|base64 --decode)
TOKEN=$(kubectl -n $NAMESPACE get secrets -o jsonpath="{.items[?(@.metadata.annotations['kubernetes\.io/service-account\.name']=='$SERVICE_ACCOUNT')].data.token}"|base64 --decode)
kubectl config --kubeconfig=antrea-agent.kubeconfig set-cluster $CLUSTER_NAME --server=$APISERVER --insecure-skip-tls-verify=true
kubectl config --kubeconfig=antrea-agent.kubeconfig set-credentials antrea-agent --token=$TOKEN
kubectl config --kubeconfig=antrea-agent.kubeconfig set-context antrea-agent@$CLUSTER_NAME --cluster=$CLUSTER_NAME --user=antrea-agent
Expand All @@ -221,7 +222,8 @@ spec:
# to be exposed via the Node IP or a public IP that is reachable from the VM
export ANTREA_API_SERVER="https://172.18.0.1:443"
export ANTREA_CLUSTER_NAME="antrea"
TOKEN=$(kubectl -n vm-ns get secrets -o jsonpath="{.items[?(@.metadata.annotations['kubernetes\.io/service-account\.name']=='$SERVICE_ACCOUNT')].data.token}"|base64 --decode)
export NAMESPACE="vm-ns"
TOKEN=$(kubectl -n $NAMESPACE get secrets -o jsonpath="{.items[?(@.metadata.annotations['kubernetes\.io/service-account\.name']=='$SERVICE_ACCOUNT')].data.token}"|base64 --decode)
kubectl config --kubeconfig=antrea-agent.antrea.kubeconfig set-cluster $ANTREA_CLUSTER_NAME --server=$ANTREA_API_SERVER --insecure-skip-tls-verify=true
kubectl config --kubeconfig=antrea-agent.antrea.kubeconfig set-credentials antrea-agent --token=$TOKEN
kubectl config --kubeconfig=antrea-agent.antrea.kubeconfig set-context antrea-agent@$ANTREA_CLUSTER_NAME --cluster=$ANTREA_CLUSTER_NAME --user=antrea-agent
Expand Down
127 changes: 94 additions & 33 deletions hack/externalnode/install-vm.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<#
.SYNOPSIS
Installs Antrea-Agent service.
Installs Antrea-Agent and Antrea-Monitor service.
.PARAMETER Namespace
ExternalNode Namespace to be used.
Expand All @@ -17,8 +17,17 @@
.PARAMETER AntreaKubeConfigPath
Specifies the path of the kubeconfig to access Antrea API Server.
.PARAMETER NodeName
Specifies the ExternalNode name to be used by the antrea-agent.
.PARAMETER OVSBridge
Specifies the OVS bridge name.
.PARAMETER InstallDir
The target installation directory. The default path is "C:\antrea-agent".
.PARAMETER ClearOVS
Clears OVS configuration.
#>
Param(
[parameter(Mandatory = $true)] [string] $Namespace,
Expand All @@ -27,28 +36,30 @@ Param(
[parameter(Mandatory = $true)] [string] $KubeConfigPath,
[parameter(Mandatory = $true)] [string] $AntreaKubeConfigPath,
[parameter(Mandatory = $false)] [string] $NodeName = $(hostname),
[parameter(Mandatory = $false)] [string] $InstallDir = "C:\antrea-agent"
[parameter(Mandatory = $false)] [string] $OVSBridge = "br-int",
[parameter(Mandatory = $false)] [string] $InstallDir = "C:\antrea-agent",
[parameter(Mandatory = $false)] [string] $ClearOVS = "false"
)

$ErrorActionPreference = "Stop"

$WorkDir = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
$InstallLog = "$WorkDir\install_vm.log"

# Antrea paths
$AntreaAgentPath = [io.path]::combine($InstallDir, "antrea-agent.exe")
$AntreaAgentConfDir = [io.path]::combine($InstallDir, "conf")
$AntreaAgentLogDir = [io.path]::combine($InstallDir, "logs")
$AntreaAgentConfPath = [io.path]::combine($AntreaAgentConfDir, "antrea-agent.conf")
$AntreaAgentLogFile = [io.path]::combine($AntreaAgentLogDir, "antrea-agent.log")
$InstallLog = [io.path]::combine($AntreaAgentLogDir, "install_vm.log")

# Constants
$AntreaAgent = "antrea-agent"
$AntreaCleanup = "antrea-cleanup"
$AntreaSwitch = "antrea-switch"
$OVSServices = "ovsdb-server", "ovs-vswitchd"
$K8sKubeconfig = "antrea-agent.kubeconfig"
$AntreaKubeconfig = "antrea-agent.antrea.kubeconfig"
$OVSServices = "ovsdb-server", "ovs-vswitchd"
$AntreaAgent = "antrea-agent"
$Kubeconfig = "kubeconfig"
$Bridge = "ovsBridge"
$ExternalNodeNamespace = "externalNodeNamespace"
$Kubeconfig = "kubeconfig"

# List of supported OS versions, verified by antrea
# Versions are named like Major.Minor.Build
Expand All @@ -66,8 +77,20 @@ function ServiceExists($ServiceName) {
return $false
}

function StartService($name, $ignoreError=$false) {
try {
Start-Service $name
} catch {
if ( -Not $ignoreError) {
Log "Failed to start $name service, rc $_"
exit 1
}
Log "Ignoring start error for $name service"
}
}

function CheckSupportedVersions() {
echo "Checking supported Windows OS versions"
Log "Checking supported Windows OS versions"
$OSVersion = [System.Environment]::OSVersion.Version
$Version = $OSVersion.Major.ToString() + "." + $OSVersion.Minor.ToString() + "." + $OSVersion.Build.ToString()
foreach ($v in $SupportedVersions) {
Expand All @@ -79,16 +102,14 @@ function CheckSupportedVersions() {
exit 1
}

function PrintPrerequisites()
{
echo "Please execute these commands to enable Hyper-V"
echo "Install-WindowsFeature Hyper-V-Powershell"
echo "Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All -NoRestart"
function PrintPrerequisites() {
Write-Host "Please execute these commands to enable Hyper-V"
Write-Host "Install-WindowsFeature Hyper-V-Powershell"
Write-Host "Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All -NoRestart"
exit 1
}

function CheckPrerequisites()
{
function CheckPrerequisites() {
CheckSupportedVersions
$valid = $true
Log "Check Hyper-v feature is enabled"
Expand Down Expand Up @@ -116,7 +137,6 @@ function CheckPrerequisites()
}

function SetupInstallDir() {
Log "Create install directories"
if (-Not (Test-Path $AntreaAgentConfDir)) {
New-Item $AntreaAgentConfDir -type directory -Force | Out-Null
}
Expand Down Expand Up @@ -167,41 +187,82 @@ function UpdateAgentConf() {
Log "Updating $AntreaAgentConfPath with ${ExternalNodeNamespace}: ${Namespace}"
[System.IO.File]::AppendAllText($AntreaAgentConfPath, " ${ExternalNodeNamespace}: ${Namespace}" +
([Environment]::NewLine))
} elseif ($line -like "*$Bridge*") {
Log "Updating $AntreaAgentConfPath with ${Bridge}: ${OVSBridge}"
[System.IO.File]::AppendAllText($AntreaAgentConfPath, "${Bridge}: ${OVSBridge}" +
([Environment]::NewLine))
} else {
[System.IO.File]::AppendAllText($AntreaAgentConfPath, $line +
([Environment]::NewLine))
}
}
}

function ClearOVSConfig() {
Log "Deleting OVS bridge $OVSBridge"
Stop-Service $AntreaAgent
try {
$adapterName = (Get-VMNetworkAdapter -SwitchName $AntreaSwitch -ManagementOS).Name
ovs-vsctl.exe del-br $OVSBridge
} catch {
Log "Failed to get VMSwitch $AntreaSwitch , rc $_"
exit 1
}

try {
Remove-VMSwitch -ComputerName $(hostname.exe) $AntreaSwitch -Force
} catch {
Log "Ignore error while removing VMSwitch, rc $_"
}

try {
Rename-NetAdapter -Name "$adapterName~" -NewName "$adapterName"
} catch {
Log "Failed to rename network adapter $adapterName~ to $adapterName, rc $_"
exit 1
}
Start-Service $AntreaAgent
Log "Done Deleting OVS bridge $OVSBridge"
}

function ConfigureAntreaCleanupService() {
Log "Copying install script to $AntreaAgentConfDir"
Copy-Item $myInvocation.PSCommandPath "$AntreaAgentConfDir\install-vm.ps1"
$Binary = (Get-Command Powershell).Source
$Arguments = "-ExecutionPolicy Bypass -NoProfile -File $AntreaAgentConfDir\install-vm.ps1 -ClearOVS true -OVSBridge $OVSBridge -Namespace $Namespace -BinaryPath $BinaryPath -ConfigPath $ConfigPath -KubeConfigPath $KubeConfigPath -AntreaKubeConfigPath $AntreaKubeConfigPath"
& nssm install $AntreaCleanup $Binary $Arguments
# Configure service to avoid restarts upon exit
nssm set $AntreaCleanup AppExit Default Exit
}

function ConfigureAntreaAgentService() {
# Set environment variables
[Environment]::SetEnvironmentVariable("NODE_NAME", $NodeName, [System.EnvironmentVariableTarget]::Machine)
# Assume nssm is installed and configure service
$AntreaAgentArgs = "--config $AntreaAgentConfPath --log_file $AntreaAgentLogFile --logtostderr=false"
log "Creating service $AntreaAgent $AntreaAgentPath $AntreaAgentArgs"
Log "Creating service $AntreaAgent $AntreaAgentPath $AntreaAgentArgs"
try {
# Configured to auto-restart upon reboot
& nssm install $AntreaAgent $AntreaAgentPath $AntreaAgentArgs
} catch {
log "Failed to create service for $AntreaAgent, rc $_"
Log "Failed to create service for $AntreaAgent, rc $_"
exit 1
}
}

function StartAntreaAgentService()
{
try {
& nssm start $AntreaAgent
} catch {
log "Failed to start service for $AntreaAgent, rc $_"
exit 1
}
function StartAntreaServices() {
StartService $AntreaCleanup $true
StartService $AntreaAgent
}

CheckPrerequisites
SetupInstallDir
CopyAntreaAgentFiles
UpdateAgentConf
ConfigureAntreaAgentService
StartAntreaAgentService
if ( $ClearOVS -eq "true") {
ClearOVSConfig
} else {
CheckPrerequisites
CopyAntreaAgentFiles
UpdateAgentConf
ConfigureAntreaCleanupService
ConfigureAntreaAgentService
StartAntreaServices
}
Loading

0 comments on commit 804be28

Please sign in to comment.