Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename the dcp resource on restart #5832

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal static void AddLifeCycleCommands(this IResource resource)
{
var executor = context.ServiceProvider.GetRequiredService<ApplicationExecutor>();

await executor.StartResourceAsync(context.ResourceName, context.CancellationToken).ConfigureAwait(false);
await executor.StartResourceAsync(resource, context.ResourceName, context.CancellationToken).ConfigureAwait(false);
return CommandResults.Success();
},
updateState: context =>
Expand Down Expand Up @@ -49,7 +49,7 @@ internal static void AddLifeCycleCommands(this IResource resource)
{
var executor = context.ServiceProvider.GetRequiredService<ApplicationExecutor>();

await executor.StopResourceAsync(context.ResourceName, context.CancellationToken).ConfigureAwait(false);
await executor.StopResourceAsync(resource, context.ResourceName, context.CancellationToken).ConfigureAwait(false);
return CommandResults.Success();
},
updateState: context =>
Expand Down Expand Up @@ -77,8 +77,8 @@ internal static void AddLifeCycleCommands(this IResource resource)
{
var executor = context.ServiceProvider.GetRequiredService<ApplicationExecutor>();

await executor.StopResourceAsync(context.ResourceName, context.CancellationToken).ConfigureAwait(false);
await executor.StartResourceAsync(context.ResourceName, context.CancellationToken).ConfigureAwait(false);
await executor.StopResourceAsync(resource, context.ResourceName, context.CancellationToken).ConfigureAwait(false);
await executor.StartResourceAsync(resource, context.ResourceName, context.CancellationToken).ConfigureAwait(false);
return CommandResults.Success();
},
updateState: context =>
Expand Down
44 changes: 20 additions & 24 deletions src/Aspire.Hosting/Dcp/ApplicationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ private async Task ProcessResourceChange<T>(WatchEventType watchEventType, T res
{
replicaAnnotation.Instances.TryRemove(resource.Metadata.Name, out _);
}

await notificationService.PublishUpdateAsync(appModelResource, resource.Metadata.Name, s => s with { State = "Hidden" }).ConfigureAwait(false);
}
else
{
Expand All @@ -411,6 +413,14 @@ private async Task ProcessResourceChange<T>(WatchEventType watchEventType, T res
_logger.LogTrace("Updating application model resource {ResourceName} with {ResourceKind} resource {ResourceName}", appModelResource.Name, resourceKind, resource.Metadata.Name);
}

if (watchEventType == WatchEventType.Added)
{
if (appModelResource.TryGetLastAnnotation<ReplicaInstancesAnnotation>(out var replicaAnnotation))
{
replicaAnnotation.Instances.TryAdd(resource.Metadata.Name, resource.Metadata.Name);
}
}

if (_hiddenResources.TryAdd(appModelResource, true))
{
// Hide the application model resource because we have the DCP resource
Expand Down Expand Up @@ -595,15 +605,6 @@ await notificationService.PublishUpdateAsync(appModelResource, cr.Metadata.Name,

private CustomResourceSnapshot ToSnapshot(Container container, CustomResourceSnapshot previous)
{
if (container.AppModelResourceName is not null &&
_applicationModel.TryGetValue(container.AppModelResourceName, out var appModelResource))
{
if (appModelResource.TryGetLastAnnotation<ReplicaInstancesAnnotation>(out var replicaAnnotation))
{
replicaAnnotation.Instances.TryAdd(container.Metadata.Name, container.Metadata.Name);
}
}

var containerId = container.Status?.ContainerId;
var urls = GetUrls(container);
var volumes = GetVolumes(container);
Expand Down Expand Up @@ -657,11 +658,6 @@ private CustomResourceSnapshot ToSnapshot(Executable executable, CustomResourceS
_applicationModel.TryGetValue(executable.AppModelResourceName, out var appModelResource))
{
projectPath = appModelResource is ProjectResource p ? p.GetProjectMetadata().ProjectPath : null;

if (appModelResource.TryGetLastAnnotation<ReplicaInstancesAnnotation>(out var replicaAnnotation))
{
replicaAnnotation.Instances.TryAdd(executable.Metadata.Name, executable.Metadata.Name);
}
}

var state = executable.AppModelInitialState is "Hidden" ? "Hidden" : executable.Status?.State;
Expand Down Expand Up @@ -1983,9 +1979,9 @@ internal static V1Patch CreatePatch<T>(T obj, Action<T> change) where T : Custom
return new V1Patch(jsonPatch, V1Patch.PatchType.JsonPatch);
}

internal async Task StopResourceAsync(string resourceName, CancellationToken cancellationToken)
internal async Task StopResourceAsync(IResource resource, string resourceName, CancellationToken cancellationToken)
{
var matchingResource = GetMatchingResource(resourceName);
var matchingResource = GetMatchingResource(resource, resourceName);

V1Patch patch;
switch (matchingResource.DcpResource)
Expand All @@ -2007,10 +2003,10 @@ internal async Task StopResourceAsync(string resourceName, CancellationToken can
}
}

private AppResource GetMatchingResource(string resourceName)
private AppResource GetMatchingResource(IResource resource, string resourceName)
{
var matchingResource = _appResources
.Where(r => r.DcpResource is not Service)
.Where(r => r.ModelResource == resource)
.SingleOrDefault(r => string.Equals(r.DcpResource.Metadata.Name, resourceName, StringComparisons.ResourceName));
if (matchingResource == null)
{
Expand All @@ -2020,17 +2016,17 @@ private AppResource GetMatchingResource(string resourceName)
return matchingResource;
}

internal async Task StartResourceAsync(string resourceName, CancellationToken cancellationToken)
internal async Task StartResourceAsync(IResource resource, string resourceName, CancellationToken cancellationToken)
{
var matchingResource = GetMatchingResource(resourceName);
var matchingResource = GetMatchingResource(resource, resourceName);

switch (matchingResource.DcpResource)
{
case Container c:
await StartExecutableOrContainerAsync(c).ConfigureAwait(false);
await StartExecutableOrContainerAsync(resource, c).ConfigureAwait(false);
break;
case Executable e:
await StartExecutableOrContainerAsync(e).ConfigureAwait(false);
await StartExecutableOrContainerAsync(resource, e).ConfigureAwait(false);
break;
case ExecutableReplicaSet rs:
var replicas = matchingResource.ModelResource.GetReplicaCount();
Expand All @@ -2042,7 +2038,7 @@ internal async Task StartResourceAsync(string resourceName, CancellationToken ca
throw new InvalidOperationException($"Unexpected resource type: {matchingResource.DcpResource.GetType().FullName}");
}

async Task StartExecutableOrContainerAsync<T>(T resource) where T : CustomResource
async Task StartExecutableOrContainerAsync<T>(IResource r, T resource) where T : CustomResource
{
var resourceName = resource.Metadata.Name;
_logger.LogDebug("Starting {ResouceType} '{ResourceName}'.", typeof(T).Name, resourceName);
Expand Down Expand Up @@ -2092,7 +2088,7 @@ await execution.ExecuteAsync(async (attemptCancellationToken) =>
}
}, cancellationToken).ConfigureAwait(false);
}

resource.Metadata.Name = $"{r.Name}-{GetRandomNameSuffix()}";
await kubernetesService.CreateAsync(resource, cancellationToken).ConfigureAwait(false);
}
}
Expand Down
Loading