Skip to content

Commit

Permalink
Change how we deactivate all revisions (#4108)
Browse files Browse the repository at this point in the history
Co-authored-by: Přemek Vysoký <[email protected]>
  • Loading branch information
dkurepa and premun authored Oct 29, 2024
1 parent 6931aed commit c1e940d
Showing 1 changed file with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -100,8 +98,10 @@ public async Task<int> RunAsync()
if (newRevisionActive)
{
await AssignLabelAndTransferTraffic(newRevisionName, inactiveRevisionLabel);

if (!string.IsNullOrEmpty(activeRevisionTrafficWeight.RevisionName))
{
await RemoveRevisionLabel(activeRevisionTrafficWeight.RevisionName, activeRevisionTrafficWeight.Label);
await DeactivateRevision(activeRevisionTrafficWeight.RevisionName);
}
}
Expand All @@ -126,30 +126,33 @@ public async Task<int> 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<ContainerAppRevisionTrafficWeight> revisionsTrafficWeight)
{
// Cleanup all revision labels
foreach (var revisionTrafficWeight in revisionsTrafficWeight)
IEnumerable<ContainerAppRevisionResource> 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);
}
}

Expand Down

0 comments on commit c1e940d

Please sign in to comment.