diff --git a/eng/common/scripts/Helpers/PSModule-Helpers.ps1 b/eng/common/scripts/Helpers/PSModule-Helpers.ps1 index 96f34ff71f4..fae40d650e4 100644 --- a/eng/common/scripts/Helpers/PSModule-Helpers.ps1 +++ b/eng/common/scripts/Helpers/PSModule-Helpers.ps1 @@ -48,8 +48,15 @@ function Update-PSModulePath() } # If we want to use another default repository other then PSGallery we can update the default parameters -function Install-ModuleIfNotInstalled($moduleName, $version, $repositoryUrl = $DefaultPSRepositoryUrl) +function Install-ModuleIfNotInstalled() { + [CmdletBinding(SupportsShouldProcess = $true)] + param( + [string]$moduleName, + [string]$version, + [string]$repositoryUrl = $DefaultPSRepositoryUrl + ) + # Check installed modules $modules = (Get-Module -ListAvailable $moduleName) if ($version -as [Version]) { @@ -94,4 +101,4 @@ function Install-ModuleIfNotInstalled($moduleName, $version, $repositoryUrl = $D return $modules[0] } -Update-PSModulePath \ No newline at end of file +Update-PSModulePath diff --git a/eng/common/scripts/common.ps1 b/eng/common/scripts/common.ps1 index c472d180341..73fe5b7d039 100644 --- a/eng/common/scripts/common.ps1 +++ b/eng/common/scripts/common.ps1 @@ -54,4 +54,4 @@ $GetOnboardedDocsMsPackagesForMonikerFn = "Get-${Language}-OnboardedDocsMsPackag $GetDocsMsTocDataFn = "Get-${Language}-DocsMsTocData" $GetDocsMsTocChildrenForManagementPackagesFn = "Get-${Language}-DocsMsTocChildrenForManagementPackages" $UpdateDocsMsTocFn = "Get-${Language}-UpdatedDocsMsToc" -$GetPackageLevelReadmeFn = "Get-${Language}-PackageLevelReadme" \ No newline at end of file +$GetPackageLevelReadmeFn = "Get-${Language}-PackageLevelReadme" diff --git a/eng/common/scripts/stress-testing/deploy-stress-tests.ps1 b/eng/common/scripts/stress-testing/deploy-stress-tests.ps1 index f4da78bd522..01920bdcbfe 100644 --- a/eng/common/scripts/stress-testing/deploy-stress-tests.ps1 +++ b/eng/common/scripts/stress-testing/deploy-stress-tests.ps1 @@ -21,7 +21,10 @@ param( [switch] $CI = ($null -ne $env:SYSTEM_TEAMPROJECTID), # Optional namespace override, otherwise the shell user or chart annotation will be used - [string]$Namespace + [string]$Namespace, + + # Override remote stress-test-addons with local on-disk addons for development + [System.IO.FileInfo]$LocalAddonsPath ) . $PSScriptRoot/stress-test-deployment-lib.ps1 diff --git a/eng/common/scripts/stress-testing/find-all-stress-packages.ps1 b/eng/common/scripts/stress-testing/find-all-stress-packages.ps1 index 3456cee6895..24c27da485f 100644 --- a/eng/common/scripts/stress-testing/find-all-stress-packages.ps1 +++ b/eng/common/scripts/stress-testing/find-all-stress-packages.ps1 @@ -9,6 +9,7 @@ class StressTestPackageInfo { [string]$ReleaseName [string]$Dockerfile [string]$DockerBuildDir + [string]$Deployer } function FindStressPackages( @@ -50,6 +51,17 @@ function MatchesAnnotations([hashtable]$chart, [hashtable]$filters) { return $true } +function GetUsername() { + # Check GITHUB_USER for users in codespaces environments, since the default user is `codespaces` and + # we would like to avoid namespace overlaps for different codespaces users. + $stressUser = $env:GITHUB_USER ?? $env:USER ?? $env:USERNAME + # Remove spaces, underscores, etc. that may be in $namespace. + # Value must be a valid RFC 1123 DNS label: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names + $stressUser = $stressUser -replace '_|\W', '-' + + return $stressUser.ToLower() +} + function NewStressTestPackageInfo( [hashtable]$chart, [System.IO.FileInfo]$chartFile, @@ -61,18 +73,7 @@ function NewStressTestPackageInfo( } elseif ($CI) { $chart.annotations.namespace } else { - # Check GITHUB_USER for users in codespaces environments, since the default user is `codespaces` and - # we would like to avoid namespace overlaps for different codespaces users. - $namespace = if ($env:GITHUB_USER) { - $env:GITHUB_USER - } elseif ($env:USER) { - $env:USER - } else { - $env:USERNAME - } - # Remove spaces, underscores, etc. that may be in $namespace. Value must be a valid RFC 1123 DNS label: - # https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names - $namespace -replace '_|\W', '-' + GetUsername } return [StressTestPackageInfo]@{ diff --git a/eng/common/scripts/stress-testing/stress-test-deployment-lib.ps1 b/eng/common/scripts/stress-testing/stress-test-deployment-lib.ps1 index da91279fd63..0b57f1fb933 100644 --- a/eng/common/scripts/stress-testing/stress-test-deployment-lib.ps1 +++ b/eng/common/scripts/stress-testing/stress-test-deployment-lib.ps1 @@ -67,11 +67,18 @@ function DeployStressTests( [string]$repository = '', [switch]$pushImages, [string]$clusterGroup = '', - [string]$deployId = 'local', + [string]$deployId = '', [switch]$login, [string]$subscription = '', [switch]$CI, - [string]$Namespace + [string]$Namespace, + [ValidateScript({ + if (!(Test-Path $_)) { + throw "LocalAddonsPath $LocalAddonsPath does not exist" + } + return $true + })] + [System.IO.FileInfo]$LocalAddonsPath ) { if ($environment -eq 'test') { if ($clusterGroup -or $subscription) { @@ -94,10 +101,21 @@ function DeployStressTests( Login -subscription $subscription -clusterGroup $clusterGroup -pushImages:$pushImages } - RunOrExitOnFailure helm repo add stress-test-charts https://stresstestcharts.blob.core.windows.net/helm/ + $chartRepoName = 'stress-test-charts' + if ($LocalAddonsPath) { + $absAddonsPath = Resolve-Path $LocalAddonsPath + if (!(helm plugin list | Select-String 'file')) { + RunOrExitOnFailure helm plugin add (Join-Path $absAddonsPath file-plugin) + } + RunOrExitOnFailure helm repo add --force-update $chartRepoName file://$absAddonsPath + } else { + RunOrExitOnFailure helm repo add --force-update $chartRepoName https://stresstestcharts.blob.core.windows.net/helm/ + } + Run helm repo update if ($LASTEXITCODE) { return $LASTEXITCODE } + $deployer = if ($deployId) { $deployId } else { GetUsername } $pkgs = FindStressPackages -directory $searchDirectory -filters $filters -CI:$CI -namespaceOverride $Namespace Write-Host "" "Found $($pkgs.Length) stress test packages:" Write-Host $pkgs.Directory "" @@ -105,15 +123,15 @@ function DeployStressTests( Write-Host "Deploying stress test at '$($pkg.Directory)'" DeployStressPackage ` -pkg $pkg ` - -deployId $deployId ` + -deployId $deployer ` -environment $environment ` -repositoryBase $repository ` -pushImages:$pushImages ` -login:$login } - Write-Host "Releases deployed by $deployId" - Run helm list --all-namespaces -l deployId=$deployId + Write-Host "Releases deployed by $deployer" + Run helm list --all-namespaces -l deployId=$deployer if ($FailedCommands) { Write-Warning "The following commands failed:" diff --git a/eng/pipelines/stress-cluster-provision.yml b/eng/pipelines/stress-cluster-provision.yml new file mode 100644 index 00000000000..eadf0c9545e --- /dev/null +++ b/eng/pipelines/stress-cluster-provision.yml @@ -0,0 +1,20 @@ +pr: none + +trigger: none + +parameters: + - name: Environment + type: string + default: test + values: + - prod + - test + - name: WhatIf + type: boolean + default: false + +extends: + template: /eng/pipelines/templates/jobs/stress-cluster-provision.yml + parameters: + Environment: ${{ parameters.Environment }} + WhatIf: ${{ parameters.WhatIf }} diff --git a/eng/pipelines/stress-test-release.yml b/eng/pipelines/stress-test-release.yml index f3cc89c4bac..09b0bbe274f 100644 --- a/eng/pipelines/stress-test-release.yml +++ b/eng/pipelines/stress-test-release.yml @@ -25,69 +25,9 @@ parameters: type: string default: main -variables: -- template: /eng/pipelines/templates/variables/globals.yml - -jobs: -- job: - strategy: - matrix: - ${{ if or(eq(parameters.TestRepository, 'examples'), eq(parameters.TestRepository, 'all')) }}: - examples: - Repository: Azure/azure-sdk-tools - Filters: '@{ "example" = "true" }' - ${{ if or(eq(parameters.TestRepository, 'javascript'), eq(parameters.TestRepository, 'all')) }}: - javascript: - Repository: Azure/azure-sdk-for-js - Filters: '@{}' - ${{ if or(eq(parameters.TestRepository, 'java'), eq(parameters.TestRepository, 'all')) }}: - java: - Repository: Azure/azure-sdk-for-java - Filters: '@{}' - ${{ if or(eq(parameters.TestRepository, 'net'), eq(parameters.TestRepository, 'all')) }}: - net: - Repository: Azure/azure-sdk-for-net - Filters: '@{}' - ${{ if or(eq(parameters.TestRepository, 'python'), eq(parameters.TestRepository, 'all')) }}: - python: - Repository: Azure/azure-sdk-for-python - Filters: '@{}' - ${{ if or(eq(parameters.TestRepository, 'go'), eq(parameters.TestRepository, 'all')) }}: - go: - Repository: Azure/azure-sdk-for-go - Filters: '@{}' - pool: - vmImage: 'ubuntu-20.04' - #name: 'azsdk-pool-mms-ubuntu-2004-general' - #vmImage: 'MMSUbuntu20.04' - steps: - - template: /eng/common/pipelines/templates/steps/sparse-checkout.yml - parameters: - Repositories: - - Name: $(Repository) - Commitish: ${{ parameters.DeployFromBranchOrCommit }} - WorkingDirectory: $(System.DefaultWorkingDirectory)/$(Repository) - Paths: - - '/*' - - '!sdk/**/test-recordings' - - '!sdk/**/session-records' - - '!sdk/**/SessionRecords' - - - task: AzureCLI@2 - displayName: Build and Deploy Stress Tests - inputs: - ${{ if eq(parameters.Environment, 'prod') }}: - azureSubscription: Azure SDK Test Resources - ${{ if eq(parameters.Environment, 'test') }}: - azureSubscription: Azure SDK Developer Playground - scriptType: pscore - scriptPath: $(System.DefaultWorkingDirectory)/$(Repository)/eng/common/scripts/stress-testing/deploy-stress-tests.ps1 - arguments: - -SearchDirectory '$(System.DefaultWorkingDirectory)/$(Repository)' - -Filters $(Filters) - -Environment '${{ parameters.Environment }}' - -Repository '$(Agent.JobName)' - -PushImages - -Login - -DeployId '$(Build.BuildNumber)' - -CI +extends: + template: /eng/pipelines/templates/jobs/stress-test-release.yml + parameters: + Environment: ${{ parameters.Environment }} + TestRepository: ${{ parameters.TestRepository }} + DeployFromBranchOrCommit: ${{ parameters.DeployFromBranchOrCommit }} diff --git a/eng/pipelines/templates/jobs/stress-cluster-provision.yml b/eng/pipelines/templates/jobs/stress-cluster-provision.yml new file mode 100644 index 00000000000..c9a405ea829 --- /dev/null +++ b/eng/pipelines/templates/jobs/stress-cluster-provision.yml @@ -0,0 +1,28 @@ +parameters: + - name: Environment + type: string + - name: WhatIf + type: boolean + default: true + +jobs: + - job: + variables: + - template: /eng/pipelines/templates/variables/globals.yml + ${{ if eq(parameters.WhatIf, true) }}: + displayName: 'Validate Provision' + ${{ else }}: + displayName: 'Provision' + pool: + name: 'azsdk-pool-mms-ubuntu-2004-general' + vmImage: 'MMSUbuntu20.04' + steps: + - pwsh: | + $subscriptionConfiguration = @' + $(sub-config-azure-cloud-test-resources) + '@ | ConvertFrom-Json -AsHashtable; + + tools/stress-cluster/cluster/provision.ps1 ` + -WhatIf:$${{ parameters.WhatIf }} ` + -Environment ${{ parameters.Environment }} ` + @subscriptionConfiguration diff --git a/eng/pipelines/templates/jobs/stress-test-release.yml b/eng/pipelines/templates/jobs/stress-test-release.yml new file mode 100644 index 00000000000..2a57777c68d --- /dev/null +++ b/eng/pipelines/templates/jobs/stress-test-release.yml @@ -0,0 +1,76 @@ +parameters: + - name: Environment + type: string + default: test + - name: TestRepository + type: string + default: all + - name: DeployFromBranchOrCommit + type: string + default: main + +jobs: +- job: + variables: + - template: /eng/pipelines/templates/variables/globals.yml + strategy: + matrix: + ${{ if or(eq(parameters.TestRepository, 'examples'), eq(parameters.TestRepository, 'all')) }}: + examples: + Repository: Azure/azure-sdk-tools + Filters: '@{ "example" = "true" }' + ${{ if or(eq(parameters.TestRepository, 'javascript'), eq(parameters.TestRepository, 'all')) }}: + javascript: + Repository: Azure/azure-sdk-for-js + Filters: '@{}' + ${{ if or(eq(parameters.TestRepository, 'java'), eq(parameters.TestRepository, 'all')) }}: + java: + Repository: Azure/azure-sdk-for-java + Filters: '@{}' + ${{ if or(eq(parameters.TestRepository, 'net'), eq(parameters.TestRepository, 'all')) }}: + net: + Repository: Azure/azure-sdk-for-net + Filters: '@{}' + ${{ if or(eq(parameters.TestRepository, 'python'), eq(parameters.TestRepository, 'all')) }}: + python: + Repository: Azure/azure-sdk-for-python + Filters: '@{}' + ${{ if or(eq(parameters.TestRepository, 'go'), eq(parameters.TestRepository, 'all')) }}: + go: + Repository: Azure/azure-sdk-for-go + Filters: '@{}' + pool: + name: 'azsdk-pool-mms-ubuntu-2004-general' + vmImage: 'MMSUbuntu20.04' + steps: + - template: /eng/common/pipelines/templates/steps/sparse-checkout.yml + parameters: + Repositories: + - Name: $(Repository) + Commitish: ${{ parameters.DeployFromBranchOrCommit }} + WorkingDirectory: $(System.DefaultWorkingDirectory)/$(Repository) + Paths: + - '/*' + - '!sdk/**/recordings/*' + - '!sdk/**/test-recordings/*' + - '!sdk/**/session-records/*' + - '!sdk/**/SessionRecords/*' + + - task: AzureCLI@2 + displayName: Build and Deploy Stress Tests + inputs: + ${{ if eq(parameters.Environment, 'prod') }}: + azureSubscription: Azure SDK Test Resources + ${{ if eq(parameters.Environment, 'test') }}: + azureSubscription: Azure SDK Playground + scriptType: pscore + scriptPath: $(System.DefaultWorkingDirectory)/$(Repository)/eng/common/scripts/stress-testing/deploy-stress-tests.ps1 + arguments: + -SearchDirectory '$(System.DefaultWorkingDirectory)/$(Repository)' + -Filters $(Filters) + -Environment '${{ parameters.Environment }}' + -Repository '$(Agent.JobName)' + -PushImages + -Login + -DeployId '$(Build.BuildNumber)' + -CI diff --git a/eng/pipelines/templates/stages/archetype-sdk-tool-pwsh.yml b/eng/pipelines/templates/stages/archetype-sdk-tool-pwsh.yml index e9150065024..e800a09645e 100644 --- a/eng/pipelines/templates/stages/archetype-sdk-tool-pwsh.yml +++ b/eng/pipelines/templates/stages/archetype-sdk-tool-pwsh.yml @@ -22,11 +22,11 @@ stages: strategy: matrix: Windows: - Pool: 'azsdk-pool-mms-ubuntu-2004-general' - vmImage: 'MMSUbuntu20.04' - Linux: Pool: 'azsdk-pool-mms-win-2019-general' - vmImage: 'MMS2019' + Image: 'MMS2019' + Linux: + Pool: 'azsdk-pool-mms-ubuntu-2004-general' + Image: 'MMSUbuntu20.04' Mac: Pool: 'Azure Pipelines' Image: 'macOS-10.15' @@ -48,7 +48,7 @@ stages: $config = New-PesterConfiguration $config.CodeCoverage.Enabled = $true $config.TestResult.Enabled = $true - + if ($tags) { $config.Filter.Tag = $tags } @@ -75,4 +75,4 @@ stages: inputs: codeCoverageTool: 'JaCoCo' summaryFileLocation: '$(Build.SourcesDirectory)/${{ parameters.TargetDirectory }}/coverage.xml' - pathToSources: '$(Build.SourcesDirectory)/${{ parameters.TargetDirectory }}' \ No newline at end of file + pathToSources: '$(Build.SourcesDirectory)/${{ parameters.TargetDirectory }}' diff --git a/tools/stress-cluster/chaos/examples/network-stress-example/Chart.yaml b/tools/stress-cluster/chaos/examples/network-stress-example/Chart.yaml index dea9862efbc..9b12d3f9fd6 100644 --- a/tools/stress-cluster/chaos/examples/network-stress-example/Chart.yaml +++ b/tools/stress-cluster/chaos/examples/network-stress-example/Chart.yaml @@ -11,4 +11,4 @@ annotations: dependencies: - name: stress-test-addons version: 0.1.17 - repository: https://stresstestcharts.blob.core.windows.net/helm/ + repository: "@stress-test-charts" diff --git a/tools/stress-cluster/chaos/examples/network-stress-example/templates/network_loss.yaml b/tools/stress-cluster/chaos/examples/network-stress-example/templates/network_loss.yaml index ad410de4f82..0737f334b20 100644 --- a/tools/stress-cluster/chaos/examples/network-stress-example/templates/network_loss.yaml +++ b/tools/stress-cluster/chaos/examples/network-stress-example/templates/network_loss.yaml @@ -1,4 +1,4 @@ -{{- include "stress-test-addons.chaos-wrapper.tpl" (list . "network-chaos") -}} +{{- include "stress-test-addons.chaos-wrapper.tpl" (list . "stress.network-chaos") -}} {{- define "stress.network-chaos" -}} apiVersion: chaos-mesh.org/v1alpha1 kind: NetworkChaos diff --git a/tools/stress-cluster/chaos/examples/stress-debug-share-example/Chart.yaml b/tools/stress-cluster/chaos/examples/stress-debug-share-example/Chart.yaml index 1bb02d79f1d..48dc4d5ba3b 100644 --- a/tools/stress-cluster/chaos/examples/stress-debug-share-example/Chart.yaml +++ b/tools/stress-cluster/chaos/examples/stress-debug-share-example/Chart.yaml @@ -11,4 +11,4 @@ annotations: dependencies: - name: stress-test-addons version: 0.1.17 - repository: https://stresstestcharts.blob.core.windows.net/helm/ + repository: "@stress-test-charts" diff --git a/tools/stress-cluster/chaos/examples/stress-deployment-example/Chart.yaml b/tools/stress-cluster/chaos/examples/stress-deployment-example/Chart.yaml index 58d28cdcd37..3fbcda432b8 100644 --- a/tools/stress-cluster/chaos/examples/stress-deployment-example/Chart.yaml +++ b/tools/stress-cluster/chaos/examples/stress-deployment-example/Chart.yaml @@ -11,4 +11,4 @@ annotations: dependencies: - name: stress-test-addons version: 0.1.17 - repository: https://stresstestcharts.blob.core.windows.net/helm/ + repository: "@stress-test-charts" diff --git a/tools/stress-cluster/ci.yml b/tools/stress-cluster/ci.yml new file mode 100644 index 00000000000..6587606dbba --- /dev/null +++ b/tools/stress-cluster/ci.yml @@ -0,0 +1,20 @@ +trigger: + branches: + include: + - main + paths: + include: + - tools/stress-cluster + +pr: none + +jobs: + - template: /eng/pipelines/templates/jobs/stress-test-release.yml + parameters: + Environment: test + TestRepository: examples + DeployFromBranchOrCommit: $(Build.SourceVersion) + - template: /eng/pipelines/templates/jobs/stress-cluster-provision.yml + parameters: + Environment: test + WhatIf: true diff --git a/tools/stress-cluster/cluster/provision.ps1 b/tools/stress-cluster/cluster/provision.ps1 index a63c9cc9082..9b63771b96b 100644 --- a/tools/stress-cluster/cluster/provision.ps1 +++ b/tools/stress-cluster/cluster/provision.ps1 @@ -1,32 +1,80 @@ +[CmdletBinding(DefaultParameterSetName = 'Default', SupportsShouldProcess = $true)] param ( [string]$Environment = 'dev', [string]$Namespace = 'stress-infra', - [switch]$Development = $false + [switch]$Development = $false, + + [Parameter(ParameterSetName = 'Provisioner', Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [string] $TenantId, + + [Parameter(ParameterSetName = 'Provisioner', Mandatory = $true)] + [ValidatePattern('^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$')] + [string] $ProvisionerApplicationId, + + [Parameter(ParameterSetName = 'Provisioner', Mandatory = $true)] + [string] $ProvisionerApplicationSecret, + + # Enables passing full json credential config without throwing unrecognized parameter errors + [Parameter(ValueFromRemainingArguments = $true)] + $RemainingArguments ) +$ErrorActionPreference = 'Stop' +. (Join-Path $PSScriptRoot "../../../eng/common/scripts/Helpers" PSModule-Helpers.ps1) +Install-ModuleIfNotInstalled -WhatIf:$false "powershell-yaml" "0.4.1" | Import-Module + $STATIC_TEST_DOTENV_NAME="public" $VALUES_FILE = "$PSScriptRoot/kubernetes/stress-test-addons/values.yaml" $STRESS_CLUSTER_RESOURCE_GROUP = "" -function Run() +function _run([string]$CustomWhatIfFlag) { - Write-Host "`n==> $args`n" -ForegroundColor Green - $command, $arguments = $args - & $command $arguments + if ($WhatIfPreference -and [string]::IsNullOrEmpty($CustomWhatIfFlag)) { + Write-Host "`n==> [What if] $args`n" -ForegroundColor Green + return + } else { + $cmdArgs = $args + if ($WhatIfPreference) { + $cmdArgs += $CustomWhatIfFlag + } + Write-Host "`n==> $cmdArgs`n" -ForegroundColor Green + $command, $arguments = $cmdArgs + & $command $arguments + } if ($LASTEXITCODE) { Write-Error "Command '$args' failed with code: $LASTEXITCODE" -ErrorAction 'Continue' } } +function Run() +{ + _run '' @args +} + +function RunSupportingWhatIfFlag([string]$CustomWhatIfFlag) +{ + if ($WhatIfPreference) { + _run $CustomWhatIfFlag @args + } else { + _run @args + } + if ($LASTEXITCODE) { + exit $LASTEXITCODE + } +} + function RunOrExitOnFailure() { - Run @args + $LASTEXITCODE = 0 + _run '' @args if ($LASTEXITCODE) { exit $LASTEXITCODE } } -function DeployStaticResources([hashtable]$params) { +function DeployStaticResources([hashtable]$params) +{ Write-Host "Deploying static resources" RunOrExitOnFailure az group create ` @@ -82,7 +130,9 @@ function DeployStaticResources([hashtable]$params) { $dotenv = $credentials.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)`n" } (-join $dotenv) | Out-File $envFile Run az keyvault secret set --vault-name $params.staticTestSecretsKeyvaultName --file $envFile -n $STATIC_TEST_DOTENV_NAME - Remove-Item -Force $envFile + if (Test-Path $envFile) { + Remove-Item -Force $envFile + } if ($LASTEXITCODE) { exit $LASTEXITCODE } @@ -90,24 +140,28 @@ function DeployStaticResources([hashtable]$params) { SetEnvProvisioner $spInfo } -function GetEnvValues() { +function GetEnvValues() +{ $values = ConvertFrom-Yaml -Ordered (Get-Content -Raw $VALUES_FILE) return $values } -function SetEnvValues([object]$values) { +function SetEnvValues([object]$values) +{ $values | ConvertTo-Yaml | Out-File $VALUES_FILE Write-Warning "Update https://aka.ms/azsdk/stress/dashboard link page with new dashboard link: $($outputs.DASHBOARD_LINK.value)" Write-Warning "$VALUES_FILE has been updated and must be checked in." } -function SetEnvProvisioner([object]$provisioner) { +function SetEnvProvisioner([object]$provisioner) +{ $values = GetEnvValues $values.provisionerAppId.$Environment = $provisioner.appId SetEnvValues $values } -function SetEnvOutputs([hashtable]$params) { +function SetEnvOutputs([hashtable]$params) +{ $outputs = (az deployment sub show ` -o json ` -n "stress-deploy-$($params.groupSuffix)" ` @@ -131,9 +185,10 @@ function SetEnvOutputs([hashtable]$params) { SetEnvValues $values } -function DeployClusterResources([hashtable]$params) { +function DeployClusterResources([hashtable]$params) +{ Write-Host "Deploying stress cluster resources" - RunOrExitOnFailure az deployment sub create ` + RunSupportingWhatIfFlag "--what-if" az deployment sub create ` -o json ` --subscription $params.subscriptionId ` -n "stress-deploy-$($params.groupSuffix)" ` @@ -152,23 +207,30 @@ function DeployClusterResources([hashtable]$params) { --subscription $params.subscriptionId } -function DeployHelmResources() { +function DeployHelmResources() +{ Write-Host "Installing stress infrastructure charts" + $LastWhatIfPreference = $WhatIfPreference + + $WhatIfPreference = $false RunOrExitOnFailure helm repo add chaos-mesh https://charts.chaos-mesh.org RunOrExitOnFailure helm dependency update $PSScriptRoot/kubernetes/stress-infrastructure - RunOrExitOnFailure kubectl create namespace $Namespace --dry-run=client -o yaml | kubectl apply -f - + + $WhatIfPreference = $LastWhatIfPreference + Run kubectl create namespace $Namespace --dry-run=client -o yaml | kubectl apply -f - # Skip installing chaos mesh charts in development mode (i.e. when testing stress watcher only). $deployChaosMesh = "$(!$Development)".ToLower() - RunOrExitOnFailure helm upgrade --install stress-infra ` + RunSupportingWhatIfFlag "--dry-run" helm upgrade --install stress-infra ` -n $Namespace ` --set stress-test-addons.env=$Environment ` --set deploy.chaosmesh=$deployChaosMesh ` $PSScriptRoot/kubernetes/stress-infrastructure } -function LoadEnvParams() { +function LoadEnvParams() +{ try { $params = (Get-Content $PSScriptRoot/azure/parameters/$Environment.json | ConvertFrom-Json -AsHashtable).parameters } catch { @@ -184,9 +246,22 @@ function LoadEnvParams() { return $paramHash } -function main() { - # . (Join-Path $PSScriptRoot "../Helpers" PSModule-Helpers.ps1) - # Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module +function main() +{ + # Force a reset of $LASTEXITCODE 0 so that when running in -WhatIf mode + # we don't inadvertently check a $LASTEXITCODE value from before the script invocation + if ($WhatIfPreference) { + helm -h > $null + } + + if ($PSCmdlet.ParameterSetName -eq 'Provisioner') { + az login ` + --service-principal ` + --username $ProvisionerApplicationId ` + --password $ProvisionerApplicationSecret` + --tenant $TenantId + if ($LASTEXITCODE) { exit $LASTEXITCODE } + } if (!$Development) { $params = LoadEnvParams @@ -197,8 +272,4 @@ function main() { DeployHelmResources } -# Don't call functions when the script is being dot sourced -if ($MyInvocation.InvocationName -ne ".") { - $ErrorActionPreference = 'Stop' - main -} +main