Skip to content

Commit

Permalink
Add sleep for network rule application
Browse files Browse the repository at this point in the history
  • Loading branch information
benbp committed Jul 3, 2024
1 parent b99573d commit 16a7a95
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions eng/common/TestResources/Remove-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ $verifyDeleteScript = {
# Get any resources that can be purged after the resource group is deleted coerced into a collection even if empty.
$purgeableResources = Get-PurgeableGroupResources $ResourceGroupName

SetStorageNetworkAccessRules -ResourceGroupName $ResourceGroupName -AllowIpRanges $AllowIpRanges -Override -CI:$CI
Remove-WormStorageAccounts -GroupPrefix $ResourceGroupName
SetResourceNetworkAccessRules -ResourceGroupName $ResourceGroupName -AllowIpRanges $AllowIpRanges -Override -CI:$CI
Remove-WormStorageAccounts -GroupPrefix $ResourceGroupName -CI:$CI

Log "Deleting resource group '$ResourceGroupName'"
if ($Force -and !$purgeableResources) {
Expand Down
22 changes: 17 additions & 5 deletions eng/common/scripts/Helpers/Resource-Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ function Wait-PurgeableResourceJob {
function Remove-WormStorageAccounts() {
[CmdletBinding(SupportsShouldProcess = $True)]
param(
[string]$GroupPrefix
[string]$GroupPrefix,
[switch]$CI
)

$ErrorActionPreference = 'Stop'
Expand All @@ -222,8 +223,8 @@ function Remove-WormStorageAccounts() {
# DO NOT REMOVE THIS
# We call this script from live test pipelines as well, and a string mismatch/error could blow away
# some static storage accounts we rely on
if (!$groupPrefix -or !$GroupPrefix.StartsWith('rg-')) {
throw "The -GroupPrefix parameter must start with 'rg-'"
if (!$groupPrefix -or ($CI -and !$GroupPrefix.StartsWith('rg-'))) {
throw "The -GroupPrefix parameter must not be empty, or must start with 'rg-' in CI contexts"
}

$groups = Get-AzResourceGroup | Where-Object { $_.ResourceGroupName.StartsWith($GroupPrefix) } | Where-Object { $_.ProvisioningState -ne 'Deleting' }
Expand Down Expand Up @@ -274,12 +275,14 @@ function Remove-WormStorageAccounts() {
try {
Write-Host "Removing immutability policies - account: $($ctx.StorageAccountName), group: $($group.ResourceGroupName)"
$null = $ctx | Get-AzStorageContainer | Get-AzStorageBlob | Remove-AzStorageBlobImmutabilityPolicy
} catch {}
}
catch {}

try {
$ctx | Get-AzStorageContainer | Get-AzStorageBlob | Remove-AzStorageBlob -Force
$succeeded = $true
} catch {
}
catch {
Write-Warning "Failed to remove blobs - account: $($ctx.StorageAccountName), group: $($group.ResourceGroupName)"
Write-Warning $_
}
Expand Down Expand Up @@ -314,6 +317,7 @@ function SetStorageNetworkAccessRules([string]$ResourceGroupName, [array]$AllowI
$storageAccounts = Retry { Get-AzResource -ResourceGroupName $ResourceGroupName -ResourceType "Microsoft.Storage/storageAccounts" }
# Add client IP to storage account when running as local user. Pipeline's have their own vnet with access
if ($storageAccounts) {
$appliedRule = $false
foreach ($account in $storageAccounts) {
$rules = Get-AzStorageAccountNetworkRuleSet -ResourceGroupName $ResourceGroupName -AccountName $account.Name
if ($rules -and ($Override -or $rules.DefaultAction -eq "Allow")) {
Expand All @@ -322,13 +326,15 @@ function SetStorageNetworkAccessRules([string]$ResourceGroupName, [array]$AllowI
if ($CI -and $env:PoolSubnet) {
Write-Host "Enabling access to '$($account.Name)' from pipeline subnet $($env:PoolSubnet)"
Retry { Add-AzStorageAccountNetworkRule -ResourceGroupName $ResourceGroupName -Name $account.Name -VirtualNetworkResourceId $env:PoolSubnet }
$appliedRule = $true
}
elseif ($AllowIpRanges) {
Write-Host "Enabling access to '$($account.Name)' to $($AllowIpRanges.Length) IP ranges"
$ipRanges = $AllowIpRanges | ForEach-Object {
@{ Action = 'allow'; IPAddressOrRange = $_ }
}
Retry { Update-AzStorageAccountNetworkRuleSet -ResourceGroupName $ResourceGroupName -Name $account.Name -IPRule $ipRanges | Out-Null }
$appliedRule = $true
}
elseif (!$CI) {
Write-Host "Enabling access to '$($account.Name)' from client IP"
Expand All @@ -343,8 +349,14 @@ function SetStorageNetworkAccessRules([string]$ResourceGroupName, [array]$AllowI
}
}
Retry { Add-AzStorageAccountNetworkRule -ResourceGroupName $ResourceGroupName -Name $account.Name -IPAddressOrRange $clientIp | Out-Null }
$appliedRule = $true
}
}

if ($appliedRule) {
Write-Host "Sleeping for 15 seconds to allow network rules to take effect"
Start-Sleep 15
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion eng/scripts/live-test-resource-cleanup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ function DeleteAndPurgeGroups([array]$toDelete) {
# can be left around which prevent deletion.
if ($rg.Tags?.ContainsKey('ServiceDirectory') -and $rg.Tags.ServiceDirectory -like '*storage*') {
SetStorageNetworkAccessRules -ResourceGroupName $rg.ResourceGroupName -Override -CI:($null -ne $env:SYSTEM_TEAMPROJECTID)
Remove-WormStorageAccounts -GroupPrefix $rg.ResourceGroupName
Remove-WormStorageAccounts -GroupPrefix $rg.ResourceGroupName -CI:($null -ne $env:SYSTEM_TEAMPROJECTID)
} else {
Write-Host ($rg | Remove-AzResourceGroup -Force -AsJob).Name
}
Expand Down

0 comments on commit 16a7a95

Please sign in to comment.