function Set-HelmDeploymentVersions { <# .SYNOPSIS Updates the helm values file of a given environment to reflect the tags of the provided microservice names/versions. .PARAMETER RepoPath The path to the gitops repository containing the helm environment definitions. .PARAMETER EnvironmentName The name of the environment to update. .PARAMETER Packages The list of microservice name/versions to update into the values file. .EXAMPLE # This updates the version tags of microservices for Red14 $repoPath = ".\alkami.gitops.kubernetes" $envName = "red14" $packages = Format-ParseChocoPackages -Delimiter " " -Text @" alkami.microservices.iaccountservicecontract.netcore 1.2.3 alkami.services.tde-datafeed 0.0.9000 "@ Set-HelmDeploymentVersions -RepoPath $repoPath -EnvironmentName $envName -Packages $packages -Verbose #> [CmdletBinding()] param( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$RepoPath, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$EnvironmentName, [Parameter(Mandatory = $false)] [object[]]$Packages ) $logLead = Get-LogLeadName if (Test-IsCollectionNullOrEmpty -Collection $Packages) { Write-Warning "$logLead : No packages to update. Exiting." return } # Fetch the values filepath/yaml for the environment. # A null response here is presumably valid. The parent will call Write-Error so EAP needs to be Stop if you want it to break # The parent will TC BuildProblem if there are issues reading things $appYamls = Get-HelmApplicationYamls -RepoPath $RepoPath -EnvironmentName $EnvironmentName $values = $appYamls.values $valuesPath = $appYamls.valuesPath # $appName = $appYamls.applicationName # Create a mapping from image packageId to subchart name so we can avoid a O(n^2) lookup. # Package names provided to this function will be based on the package id in the image repository, not the name of the subchart. $imageToSubchartLookup = @{} [array]$microserviceKeys = $values.Keys | Where-Object { $_ -like "alk-svc-*" } foreach ($microservice in $microserviceKeys) { $image = $values[$microservice].image.repository.split("/")[-1] $imageToSubchartLookup[$image] = $microservice } # Update the image tags if they have changes. $dirty = $false foreach ($package in $Packages) { $packageName = $package.Name # TODO: Do we need a better descriptive name here for the block? $blockName = "$packageName" Write-Host "##teamcity[blockOpened name='$blockName']" Write-Host "$logLead : Packagename - $packageName" $subchartName = $imageToSubchartLookup[$packageName] if ($null -eq $subchartName) { # While we don't normally do TC messages in Module files, this seems appropriate per conversations and use-case $errorMessage = "Could not find subchart definition for microservice `"$packageName`". Does the microservice exist in the $EnvironmentName values file?" $identity = "SetHelm_$packageName" Write-Host "##teamcity[buildProblem description='$errorMessage' identity='$identity']" Write-Error "$logLead : $errorMessage" # Need to process all the packages before erroring so multiple can be fixed at one time? continue } ## TODO: Can .image ever be null? Breaks the set down below $currentTag = $values[$subchartName].image.tag if ($currentTag -ne $package.Version) { Write-Host "$logLead : Updating $subchartName tag from [$currentTag] to [$($package.Version)]" $values[$subchartName].image.tag = $package.Version $dirty = $true } Write-Host "##teamcity[blockClosed name='$blockName']" } # Save the modified values file. if ($dirty) { Write-Host "$logLead : Updating image tags in values file for $EnvironmentName" ConvertTo-Yaml -Data $values -OutFile $valuesPath -Force return $valuesPath } else { Write-Host "$logLead : Environment values file for $EnvironmentName requires no updates. Exiting." return $null } }