-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed hns clean only in case of reboot
Signed-off-by: Roberto Bonafiglia <[email protected]>
- Loading branch information
Showing
4 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
$softwareRegistryKey = "HKLM:\Software\Tigera" | ||
$calicoRegistryKey = $softwareRegistryKey + "\Calico" | ||
|
||
function Get-LastBootTime() | ||
{ | ||
$bootTime = (Get-CimInstance win32_operatingsystem | select @{LABEL='LastBootUpTime';EXPRESSION={$_.lastbootuptime}}).LastBootUpTime | ||
if (($bootTime -EQ $null) -OR ($bootTime.length -EQ 0)) | ||
{ | ||
throw "Failed to get last boot time" | ||
} | ||
|
||
# This function is used in conjunction with Get-StoredLastBootTime, which | ||
# returns a string, so convert the datetime value to a string using the "general" standard format. | ||
return $bootTime.ToString("G") | ||
} | ||
|
||
function Get-StoredLastBootTime() | ||
{ | ||
try | ||
{ | ||
return (Get-ItemProperty $calicoRegistryKey -ErrorAction Ignore).LastBootTime | ||
} | ||
catch | ||
{ | ||
$PSItem.Exception.Message | ||
} | ||
} | ||
|
||
function ensureRegistryKey() | ||
{ | ||
if (! (Test-Path $softwareRegistryKey)) | ||
{ | ||
New-Item $softwareRegistryKey | ||
} | ||
if (! (Test-Path $calicoRegistryKey)) | ||
{ | ||
New-Item $calicoRegistryKey | ||
} | ||
} | ||
|
||
function Set-StoredLastBootTime($lastBootTime) | ||
{ | ||
ensureRegistryKey | ||
|
||
return Set-ItemProperty $calicoRegistryKey -Name LastBootTime -Value $lastBootTime | ||
} | ||
|
||
$lastBootTime = Get-LastBootTime | ||
|
||
# Check if the node has been rebooted. If so, the HNS networks will be in unknown state so we need to | ||
# clean them up and recreate them. | ||
$prevLastBootTime = Get-StoredLastBootTime | ||
Write-Host "StoredLastBootTime $prevLastBootTime, CurrentLastBootTime $lastBootTime" | ||
if ($prevLastBootTime -NE $lastBootTime) | ||
{ | ||
if ((Get-HNSNetwork | ? Type -NE nat)) | ||
{ | ||
Write-Host "First time Calico has run since boot up, cleaning out any old network state." | ||
Get-HNSNetwork | ? Type -NE nat | Remove-HNSNetwork | ||
do | ||
{ | ||
Write-Host "Waiting for network deletion to complete." | ||
Start-Sleep 1 | ||
} while ((Get-HNSNetwork | ? Type -NE nat)) | ||
} | ||
# After deletion of all hns networks, wait for an interface to have an IP that is not a 169.254.0.0/16 (or 127.0.0.0/8) address, | ||
# before creation of External network. | ||
$isValidIP = $false | ||
$IPRegEx1='(^127\.0\.0\.)' | ||
$IPRegEx2='(^169\.254\.)' | ||
while(!($isValidIP) -AND ($timeout -gt 0)) | ||
{ | ||
$IPAddress = (Get-NetIPAddress -AddressFamily IPv4).IPAddress | ||
Write-Host "`nTimeout Remaining: $timeout sec" | ||
Write-Host "List of IP Address before initialising Calico: $IPAddress" | ||
Foreach ($ip in $IPAddress) | ||
{ | ||
if (($ip -NotMatch $IPRegEx1) -AND ($ip -NotMatch $IPRegEx2)) | ||
{ | ||
$isValidIP = $true | ||
Write-Host "`nFound valid IP: $ip" | ||
break | ||
} | ||
} | ||
if (!($isValidIP)) | ||
{ | ||
Start-Sleep -s 5 | ||
$timeout = $timeout - 5 | ||
} | ||
} | ||
} | ||
|
||
Set-StoredLastBootTime $lastBootTime | ||
$Stored = Get-StoredLastBootTime | ||
Write-Host "Stored new lastBootTime $Stored" | ||
|