-
Notifications
You must be signed in to change notification settings - Fork 525
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
Fix Azure ServiceBus persistent container support #7136
Draft
sebastienros
wants to merge
2
commits into
main
Choose a base branch
from
sebros/sbpersist
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+61
−11
Draft
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Reflection; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using Aspire.Hosting.ApplicationModel; | ||
using Aspire.Hosting.Azure; | ||
using Aspire.Hosting.Azure.ServiceBus; | ||
using Aspire.Hosting.Utils; | ||
using Azure.Messaging.ServiceBus; | ||
using Azure.Provisioning; | ||
using Microsoft.Extensions.Configuration.UserSecrets; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.SecretManager.Tools.Internal; | ||
using AzureProvisioning = Azure.Provisioning.ServiceBus; | ||
|
||
namespace Aspire.Hosting; | ||
|
@@ -233,9 +235,29 @@ public static IResourceBuilder<AzureServiceBusResource> RunAsEmulator(this IReso | |
return builder; | ||
} | ||
|
||
var lifetime = ContainerLifetime.Session; | ||
|
||
if (configureContainer != null) | ||
{ | ||
var surrogate = new AzureServiceBusEmulatorResource(builder.Resource); | ||
var surrogateBuilder = builder.ApplicationBuilder.CreateResourceBuilder(surrogate); | ||
configureContainer(surrogateBuilder); | ||
|
||
if (surrogate.TryGetLastAnnotation<ContainerLifetimeAnnotation>(out var lifetimeAnnotation)) | ||
{ | ||
lifetime = lifetimeAnnotation.Lifetime; | ||
} | ||
} | ||
|
||
// Create a default file mount. This could be replaced by a user-provided file mount. | ||
|
||
var configHostFile = Path.Combine(Directory.CreateTempSubdirectory("AspireServiceBusEmulator").FullName, "Config.json"); | ||
|
||
if (lifetime == ContainerLifetime.Persistent && builder.ApplicationBuilder.ExecutionContext.IsRunMode && builder.ApplicationBuilder.AppHostAssembly is not null) | ||
{ | ||
configHostFile = GetOrSetUserSecret(builder.ApplicationBuilder.AppHostAssembly, "Parameters:ServiceBusEmulatorConfigFile", configHostFile); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a comment as to what this does? It is using the same volume path across runs? |
||
} | ||
|
||
var defaultConfigFileMount = new ContainerMountAnnotation( | ||
configHostFile, | ||
AzureServiceBusEmulatorResource.EmulatorConfigJsonPath, | ||
|
@@ -246,7 +268,8 @@ public static IResourceBuilder<AzureServiceBusResource> RunAsEmulator(this IReso | |
|
||
// Add emulator container | ||
|
||
var password = PasswordGenerator.Generate(16, true, true, true, true, 0, 0, 0, 0); | ||
// The password must be at least 8 characters long and contain characters from three of the following four sets: Uppercase letters, Lowercase letters, Base 10 digits, and Symbols | ||
var passwordParameter = ParameterResourceBuilderExtensions.CreateDefaultPasswordParameter(builder.ApplicationBuilder, $"{builder.Resource.Name}-sqledge-pwd", minLower: 1, minUpper: 1, minNumeric: 1); | ||
|
||
builder | ||
.WithEndpoint(name: "emulator", targetPort: 5672) | ||
|
@@ -264,15 +287,19 @@ public static IResourceBuilder<AzureServiceBusResource> RunAsEmulator(this IReso | |
.WithImageRegistry(ServiceBusEmulatorContainerImageTags.AzureSqlEdgeRegistry) | ||
.WithEndpoint(targetPort: 1433, name: "tcp") | ||
.WithEnvironment("ACCEPT_EULA", "Y") | ||
.WithEnvironment("MSSQL_SA_PASSWORD", password); | ||
.WithEnvironment(context => | ||
{ | ||
context.EnvironmentVariables["MSSQL_SA_PASSWORD"] = passwordParameter; | ||
}) | ||
.WithLifetime(lifetime); | ||
|
||
builder.WithAnnotation(new EnvironmentCallbackAnnotation((EnvironmentCallbackContext context) => | ||
{ | ||
var sqlEndpoint = sqlEdgeResource.Resource.GetEndpoint("tcp"); | ||
|
||
context.EnvironmentVariables.Add("ACCEPT_EULA", "Y"); | ||
context.EnvironmentVariables.Add("SQL_SERVER", $"{sqlEndpoint.Resource.Name}:{sqlEndpoint.TargetPort}"); | ||
context.EnvironmentVariables.Add("MSSQL_SA_PASSWORD", password); | ||
context.EnvironmentVariables.Add("MSSQL_SA_PASSWORD", passwordParameter); | ||
})); | ||
|
||
ServiceBusClient? serviceBusClient = null; | ||
|
@@ -411,13 +438,6 @@ public static IResourceBuilder<AzureServiceBusResource> RunAsEmulator(this IReso | |
|
||
var healthCheckKey = $"{builder.Resource.Name}_check"; | ||
|
||
if (configureContainer != null) | ||
{ | ||
var surrogate = new AzureServiceBusEmulatorResource(builder.Resource); | ||
var surrogateBuilder = builder.ApplicationBuilder.CreateResourceBuilder(surrogate); | ||
configureContainer(surrogateBuilder); | ||
} | ||
|
||
// To use the existing ServiceBus health check we would need to know if there is any queue or topic defined. | ||
// We can register a health check for a queue and then no-op if there are no queues. Same for topics. | ||
// If no queues or no topics are defined then the health check will be successful. | ||
|
@@ -484,4 +504,26 @@ public static IResourceBuilder<AzureServiceBusEmulatorResource> WithHostPort(thi | |
endpoint.Port = port; | ||
}); | ||
} | ||
|
||
private static string GetOrSetUserSecret(Assembly assembly, string name, string value) | ||
{ | ||
if (assembly.GetCustomAttribute<UserSecretsIdAttribute>()?.UserSecretsId is { } userSecretsId) | ||
{ | ||
// Save the value to the secret store | ||
try | ||
{ | ||
var secretsStore = new SecretsStore(userSecretsId); | ||
if(secretsStore.ContainsKey(name)) | ||
{ | ||
return secretsStore[name]!; | ||
} | ||
secretsStore.Set(name, value); | ||
secretsStore.Save(); | ||
return value; | ||
} | ||
catch (Exception) { } | ||
} | ||
|
||
return value; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we can centralize this logic.