From c1e940ddfcca070d353e3178e2f96dc0a9153c8a Mon Sep 17 00:00:00 2001 From: Djuradj Kurepa <91743470+dkurepa@users.noreply.github.com> Date: Tue, 29 Oct 2024 11:44:43 +0100 Subject: [PATCH] Change how we deactivate all revisions (#4108) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Přemek Vysoký --- .../Operations/DeploymentOperation.cs | 45 ++++++++++--------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/ProductConstructionService/ProductConstructionService.Cli/Operations/DeploymentOperation.cs b/src/ProductConstructionService/ProductConstructionService.Cli/Operations/DeploymentOperation.cs index 58bbc7e6fc..96be626e87 100644 --- a/src/ProductConstructionService/ProductConstructionService.Cli/Operations/DeploymentOperation.cs +++ b/src/ProductConstructionService/ProductConstructionService.Cli/Operations/DeploymentOperation.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using Azure; -using Azure.Core; -using Azure.ResourceManager; using Azure.ResourceManager.AppContainers; using Azure.ResourceManager.AppContainers.Models; using Azure.ResourceManager.Resources; @@ -100,8 +98,10 @@ public async Task RunAsync() if (newRevisionActive) { await AssignLabelAndTransferTraffic(newRevisionName, inactiveRevisionLabel); + if (!string.IsNullOrEmpty(activeRevisionTrafficWeight.RevisionName)) { + await RemoveRevisionLabel(activeRevisionTrafficWeight.RevisionName, activeRevisionTrafficWeight.Label); await DeactivateRevision(activeRevisionTrafficWeight.RevisionName); } } @@ -126,30 +126,33 @@ public async Task RunAsync() } } + private async Task RemoveRevisionLabel(string revisionName, string label) + { + var result = await InvokeAzCLI( + ["containerapp", "revision", "label", "remove"], + ["--label", label]); + result.ThrowIfFailed($"Failed to remove label {label} from revision {revisionName}."); + } + private async Task CleanupRevisionsAsync(IEnumerable revisionsTrafficWeight) { - // Cleanup all revision labels - foreach (var revisionTrafficWeight in revisionsTrafficWeight) + IEnumerable activeRevisions = _containerApp.GetContainerAppRevisions() + .ToEnumerable() + .Where(revision => revision.Data.IsActive ?? false) + .Where(revision => revision.Data.TrafficWeight != 100); + + var revisionsToDeactivate = activeRevisions + .Select(revision => ( + revision.Data.Name, + revisionsTrafficWeight.FirstOrDefault(trafficWeight => trafficWeight.RevisionName == revision.Data.Name)?.Label)); + + foreach (var revision in revisionsToDeactivate) { - if (!string.IsNullOrEmpty(revisionTrafficWeight.Label)) + if (!string.IsNullOrEmpty(revision.Label)) { - var result = await InvokeAzCLI([ - "containerapp", "revision", "label", "remove", - ], - [ - "--label", revisionTrafficWeight.Label - ]); - result.ThrowIfFailed($"Failed to remove label {revisionTrafficWeight.Label} from revision {revisionTrafficWeight.RevisionName}. Stderr: {result.StandardError}"); + await RemoveRevisionLabel(revision.Name, revision.Label); } - } - - // Now deactivate all revisions in the list - foreach (var revisionTrafficWeight in revisionsTrafficWeight) - { - _containerApp = await _containerApp.GetAsync(); - ContainerAppRevisionResource revision = (await _containerApp.GetContainerAppRevisionAsync(revisionTrafficWeight.RevisionName)).Value; - - await revision.DeactivateRevisionAsync(); + await DeactivateRevision(revision.Name); } }