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); } }