Skip to content

Commit c87a2a0

Browse files
azure-sdkbenbp
andauthored
Sync eng/common directory with azure-sdk-tools for PR 2363 (Azure#25718)
* Use stress test environment defaults for group and subscription * Fix parameter passing from deploy-stress-tests.ps1 script * Redact stress deployment logs and simplify image handling * Use DevopsLogging parameter to prevent secret logging in non-devops CI environments * Use switch type for DevopsLogging parameter * Remove boolean parameter usage in favor of [switch] * Add default parameter set usage comment * Throw when clusterGroup and/or subscription is not specified for custom environments * Add helper function for logging azure pipelines vso commands * Invert SuppressVsoCommands binary default value * Vso command fixes Co-authored-by: Ben Broderick Phillips <[email protected]>
1 parent ba1c38e commit c87a2a0

File tree

4 files changed

+100
-56
lines changed

4 files changed

+100
-56
lines changed

eng/common/TestResources/New-TestResources.ps1

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ param (
7676
[switch] $Force,
7777

7878
[Parameter()]
79-
[switch] $OutFile
79+
[switch] $OutFile,
80+
81+
[Parameter()]
82+
[switch] $SuppressVsoCommands = ($null -eq $env:SYSTEM_TEAMPROJECTID)
8083
)
8184

8285
. $PSScriptRoot/SubConfig-Helpers.ps1
@@ -91,6 +94,17 @@ function Log($Message)
9194
Write-Host ('{0} - {1}' -f [DateTime]::Now.ToLongTimeString(), $Message)
9295
}
9396

97+
# vso commands are specially formatted log lines that are parsed by Azure Pipelines
98+
# to perform additional actions, most commonly marking values as secrets.
99+
# https://docs.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands
100+
function LogVsoCommand([string]$message)
101+
{
102+
if (!$CI -or $SuppressVsoCommands) {
103+
return
104+
}
105+
Write-Host $message
106+
}
107+
94108
function Retry([scriptblock] $Action, [int] $Attempts = 5)
95109
{
96110
$attempt = 0
@@ -224,13 +238,13 @@ function SetDeploymentOutputs([string]$serviceName, [object]$azContext, [object]
224238
if (ShouldMarkValueAsSecret $serviceDirectoryPrefix $key $value $notSecretValues) {
225239
# Treat all ARM template output variables as secrets since "SecureString" variables do not set values.
226240
# In order to mask secrets but set environment variables for any given ARM template, we set variables twice as shown below.
227-
Write-Host "##vso[task.setvariable variable=_$key;issecret=true;]$value"
228-
Write-Host "Setting variable as secret '$key': $value"
241+
LogVsoCommand "##vso[task.setvariable variable=_$key;issecret=true;]$value"
242+
Write-Host "Setting variable as secret '$key'"
229243
} else {
230244
Write-Host "Setting variable '$key': $value"
231245
$notSecretValues += $value
232246
}
233-
Write-Host "##vso[task.setvariable variable=$key;]$value"
247+
LogVsoCommand "##vso[task.setvariable variable=$key;]$value"
234248
} else {
235249
Write-Host ($shellExportFormat -f $key, $value)
236250
}
@@ -474,7 +488,7 @@ try {
474488

475489
# Set the resource group name variable.
476490
Write-Host "Setting variable 'AZURE_RESOURCEGROUP_NAME': $ResourceGroupName"
477-
Write-Host "##vso[task.setvariable variable=AZURE_RESOURCEGROUP_NAME;]$ResourceGroupName"
491+
LogVsoCommand "##vso[task.setvariable variable=AZURE_RESOURCEGROUP_NAME;]$ResourceGroupName"
478492
if ($EnvironmentVariables.ContainsKey('AZURE_RESOURCEGROUP_NAME') -and `
479493
$EnvironmentVariables['AZURE_RESOURCEGROUP_NAME'] -ne $ResourceGroupName)
480494
{
@@ -866,6 +880,11 @@ The environment file will be named for the test resources template that it was
866880
generated for. For ARM templates, it will be test-resources.json.env. For
867881
Bicep templates, test-resources.bicep.env.
868882
883+
.PARAMETER SuppressVsoCommands
884+
By default, the -CI parameter will print out secrets to logs with Azure Pipelines log
885+
commands that cause them to be redacted. For CI environments that don't support this (like
886+
stress test clusters), this flag can be set to $false to avoid printing out these secrets to the logs.
887+
869888
.EXAMPLE
870889
Connect-AzAccount -Subscription 'REPLACE_WITH_SUBSCRIPTION_ID'
871890
New-TestResources.ps1 keyvault

eng/common/TestResources/New-TestResources.ps1.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ New-TestResources.ps1 [-BaseName <String>] [-ResourceGroupName <String>] [-Servi
1818
[-TestApplicationId <String>] [-TestApplicationSecret <String>] [-TestApplicationOid <String>]
1919
[-SubscriptionId <String>] [-DeleteAfterHours <Int32>] [-Location <String>] [-Environment <String>]
2020
[-ArmTemplateParameters <Hashtable>] [-AdditionalParameters <Hashtable>] [-EnvironmentVariables <Hashtable>]
21-
[-CI] [-Force] [-OutFile] [-WhatIf] [-Confirm] [<CommonParameters>]
21+
[-CI] [-Force] [-OutFile] [-SuppressVsoCommands] [-WhatIf] [-Confirm] [<CommonParameters>]
2222
```
2323

2424
### Provisioner
@@ -28,7 +28,8 @@ New-TestResources.ps1 [-BaseName <String>] [-ResourceGroupName <String>] [-Servi
2828
-TenantId <String> [-SubscriptionId <String>] -ProvisionerApplicationId <String>
2929
-ProvisionerApplicationSecret <String> [-DeleteAfterHours <Int32>] [-Location <String>]
3030
[-Environment <String>] [-ArmTemplateParameters <Hashtable>] [-AdditionalParameters <Hashtable>]
31-
[-EnvironmentVariables <Hashtable>] [-CI] [-Force] [-OutFile] [-WhatIf] [-Confirm] [<CommonParameters>]
31+
[-EnvironmentVariables <Hashtable>] [-CI] [-Force] [-OutFile] [-SuppressVsoCommands] [-WhatIf] [-Confirm]
32+
[<CommonParameters>]
3233
```
3334

3435
## DESCRIPTION
@@ -557,6 +558,24 @@ Accept pipeline input: False
557558
Accept wildcard characters: False
558559
```
559560
561+
### -SuppressVsoCommands
562+
By default, the -CI parameter will print out secrets to logs with Azure Pipelines log
563+
commands that cause them to be redacted.
564+
For CI environments that don't support this (like
565+
stress test clusters), this flag can be set to $false to avoid printing out these secrets to the logs.
566+
567+
```yaml
568+
Type: SwitchParameter
569+
Parameter Sets: (All)
570+
Aliases:
571+
572+
Required: False
573+
Position: Named
574+
Default value: ($null -eq $env:SYSTEM_TEAMPROJECTID)
575+
Accept pipeline input: False
576+
Accept wildcard characters: False
577+
```
578+
560579
### -WhatIf
561580
Shows what would happen if the cmdlet runs.
562581
The cmdlet is not run.

eng/common/scripts/stress-testing/deploy-stress-tests.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Set a default parameter set here so we can call this script without requiring -Login and -Subscription,
2+
# but if it IS called with either of those, then both parameters need to be required. Not defining a
3+
# default parameter set makes Login/Subscription required all the time.
4+
[CmdletBinding(DefaultParameterSetName = 'Default')]
15
param(
26
[string]$SearchDirectory,
37
[hashtable]$Filters,

eng/common/scripts/stress-testing/stress-test-deployment-lib.ps1

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,3 @@
1-
[CmdletBinding(DefaultParameterSetName = 'Default')]
2-
param(
3-
[string]$SearchDirectory,
4-
[hashtable]$Filters,
5-
[string]$Environment,
6-
[string]$Repository,
7-
[switch]$PushImages,
8-
[string]$ClusterGroup,
9-
[string]$DeployId,
10-
11-
[Parameter(ParameterSetName = 'DoLogin', Mandatory = $true)]
12-
[switch]$Login,
13-
14-
[Parameter(ParameterSetName = 'DoLogin')]
15-
[string]$Subscription,
16-
17-
# Default to true in Azure Pipelines environments
18-
[switch] $CI = ($null -ne $env:SYSTEM_TEAMPROJECTID)
19-
)
20-
211
$ErrorActionPreference = 'Stop'
222

233
. $PSScriptRoot/find-all-stress-packages.ps1
@@ -47,7 +27,7 @@ function RunOrExitOnFailure()
4727
}
4828
}
4929

50-
function Login([string]$subscription, [string]$clusterGroup, [boolean]$pushImages)
30+
function Login([string]$subscription, [string]$clusterGroup, [switch]$pushImages)
5131
{
5232
Write-Host "Logging in to subscription, cluster and container registry"
5333
az account show *> $null
@@ -77,13 +57,38 @@ function DeployStressTests(
7757
[string]$searchDirectory = '.',
7858
[hashtable]$filters = @{},
7959
[string]$environment = 'test',
80-
[string]$repository = 'images',
81-
[boolean]$pushImages = $false,
82-
[string]$clusterGroup = 'rg-stress-cluster-test',
60+
[string]$repository = '',
61+
[switch]$pushImages,
62+
[string]$clusterGroup = '',
8363
[string]$deployId = 'local',
84-
[string]$subscription = 'Azure SDK Developer Playground'
64+
[switch]$login,
65+
[string]$subscription = '',
66+
[switch]$ci
8567
) {
86-
if ($PSCmdlet.ParameterSetName -eq 'DoLogin') {
68+
if ($environment -eq 'test') {
69+
if ($clusterGroup -or $subscription) {
70+
Write-Warning "Overriding cluster group and subscription with defaults for 'test' environment."
71+
}
72+
$clusterGroup = 'rg-stress-cluster-test'
73+
$subscription = 'Azure SDK Developer Playground'
74+
} elseif ($environment -eq 'prod') {
75+
if ($clusterGroup -or $subscription) {
76+
Write-Warning "Overriding cluster group and subscription with defaults for 'prod' environment."
77+
}
78+
$clusterGroup = 'rg-stress-cluster-prod'
79+
$subscription = 'Azure SDK Test Resources'
80+
}
81+
82+
if (!$repository) {
83+
$repository = if ($env:USER) { $env:USER } else { "${env:USERNAME}" }
84+
# Remove spaces, etc. that may be in $namespace
85+
$repository -replace '\W'
86+
}
87+
88+
if ($login) {
89+
if (!$clusterGroup -or !$subscription) {
90+
throw "clusterGroup and subscription parameters must be specified when logging into an environment that is not test or prod."
91+
}
8792
Login $subscription $clusterGroup $pushImages
8893
}
8994

@@ -96,7 +101,7 @@ function DeployStressTests(
96101
Write-Host $pkgs.Directory ""
97102
foreach ($pkg in $pkgs) {
98103
Write-Host "Deploying stress test at '$($pkg.Directory)'"
99-
DeployStressPackage $pkg $deployId $environment $repository $pushImages
104+
DeployStressPackage $pkg $deployId $environment $repository $pushImages $login
100105
}
101106

102107
Write-Host "Releases deployed by $deployId"
@@ -117,8 +122,9 @@ function DeployStressPackage(
117122
[object]$pkg,
118123
[string]$deployId,
119124
[string]$environment,
120-
[string]$repository,
121-
[boolean]$pushImages
125+
[string]$repositoryBase,
126+
[switch]$pushImages,
127+
[switch]$login
122128
) {
123129
$registry = RunOrExitOnFailure az acr list -g $clusterGroup --subscription $subscription -o json
124130
$registryName = ($registry | ConvertFrom-Json).name
@@ -131,26 +137,23 @@ function DeployStressPackage(
131137
if ($LASTEXITCODE) { return }
132138
}
133139

140+
$imageTag = "${registryName}.azurecr.io"
141+
if ($repositoryBase) {
142+
$imageTag += "/$repositoryBase"
143+
}
144+
$imageTag += "/$($pkg.Namespace)/$($pkg.ReleaseName):${deployId}"
145+
134146
if ($pushImages) {
135-
$dockerFiles = Get-ChildItem "$($pkg.Directory)/Dockerfile*"
136-
foreach ($dockerFile in $dockerFiles) {
137-
# Infer docker image name from parent directory name, if file is named `Dockerfile`
138-
# or from suffix, is file is named like `Dockerfile.myimage` (for multiple dockerfiles).
139-
$prefix, $imageName = $dockerFile.Name.Split(".")
140-
if (!$imageName) {
141-
$imageName = $dockerFile.Directory.Name
142-
}
143-
$imageTag = "${registryName}.azurecr.io/$($repository.ToLower())/$($imageName):$deployId"
144-
Write-Host "Building and pushing stress test docker image '$imageTag'"
145-
Run docker build -t $imageTag -f $dockerFile.FullName $dockerFile.DirectoryName
146-
if ($LASTEXITCODE) { return }
147-
Run docker push $imageTag
148-
if ($LASTEXITCODE) {
149-
if ($PSCmdlet.ParameterSetName -ne 'DoLogin') {
150-
Write-Warning "If docker push is failing due to authentication issues, try calling this script with '-Login'"
151-
}
152-
return
147+
Write-Host "Building and pushing stress test docker image '$imageTag'"
148+
$dockerFile = Get-ChildItem "$($pkg.Directory)/Dockerfile"
149+
Run docker build -t $imageTag -f $dockerFile.FullName $dockerFile.DirectoryName
150+
if ($LASTEXITCODE) { return }
151+
Run docker push $imageTag
152+
if ($LASTEXITCODE) {
153+
if ($login) {
154+
Write-Warning "If docker push is failing due to authentication issues, try calling this script with '-Login'"
153155
}
156+
return
154157
}
155158
}
156159

@@ -162,8 +165,7 @@ function DeployStressPackage(
162165
Run helm upgrade $pkg.ReleaseName $pkg.Directory `
163166
-n $pkg.Namespace `
164167
--install `
165-
--set repository=$registryName.azurecr.io/$repository `
166-
--set tag=$deployId `
168+
--set image=$imageTag `
167169
--set stress-test-addons.env=$environment
168170
if ($LASTEXITCODE) {
169171
# Issues like 'UPGRADE FAILED: another operation (install/upgrade/rollback) is in progress'

0 commit comments

Comments
 (0)