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

Write details of container volumes to the publishing manifest #2742

Merged
merged 3 commits into from Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions playground/TestShop/AppHost/Properties/launchSettings.json
Expand Up @@ -14,14 +14,11 @@
},
"generate-manifest": {
"commandName": "Project",
"launchBrowser": true,
"dotnetRunMessages": true,
"commandLineArgs": "--publisher manifest --output-path aspire-manifest.json",
"applicationUrl": "http://localhost:15888",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:16031"
"DOTNET_ENVIRONMENT": "Development"
}
}
},
Expand Down
35 changes: 35 additions & 0 deletions src/Aspire.Hosting/Publishing/ManifestPublishingContext.cs
Expand Up @@ -81,6 +81,7 @@ public async Task WriteContainerAsync(ContainerResource container)
Writer.WriteString("entrypoint", container.Entrypoint);
}

// Write args if they are present
if (container.TryGetAnnotationsOfType<CommandLineArgsCallbackAnnotation>(out var argsCallback))
{
var args = new List<string>();
Expand All @@ -104,6 +105,40 @@ public async Task WriteContainerAsync(ContainerResource container)
}
}

// Write volume details
if (container.TryGetAnnotationsOfType<ContainerMountAnnotation>(out var mounts))
{
var volumes = mounts.Where(mounts => mounts.Type == ContainerMountType.Named).ToList();

// Only write out details for volumes (no bind mounts)
if (volumes.Count > 0)
{
// Volumes are written as an array of objects as anonymous volumes do not have a name
Writer.WriteStartArray("volumes");

foreach (var volume in volumes)
{
Writer.WriteStartObject();

// This can be null for anonymous volumes
if (volume.Source is not null)
{
Writer.WritePropertyName("name");
Writer.WriteStringValue(volume.Source);
}

Writer.WritePropertyName("target");
Writer.WriteStringValue(volume.Target);

Writer.WriteBoolean("readOnly", volume.IsReadOnly);

Writer.WriteEndObject();
}

Writer.WriteEndArray();
}
}

await WriteEnvironmentVariablesAsync(container).ConfigureAwait(false);
WriteBindings(container, emitContainerPort: true);
WriteInputs(container);
Expand Down
41 changes: 41 additions & 0 deletions tests/Aspire.Hosting.Tests/ManifestGenerationTests.cs
Expand Up @@ -183,6 +183,47 @@ public void EnsureContainerWithArgsEmitsContainerArgs()
arg => Assert.Equal("more", arg.GetString()));
}

[Fact]
public async Task EnsureContainerWithVolumesEmitsVolumes()
{
using var program = CreateTestProgramJsonDocumentManifestPublisher();

var container = program.AppBuilder.AddContainer("containerwithvolumes", "image/name")
.WithVolume("myvolume", "/mount/here")
.WithBindMount("./some/source", "/bound") // This should be ignored and not written to the manifest
.WithVolume("myreadonlyvolume", "/mount/there", isReadOnly: true)
.WithVolume(null! /* anonymous volume */, "/mount/everywhere");

program.Build();

var manifest = await ManifestUtils.GetManifest(container.Resource);

var expectedManifest = """
{
"type": "container.v0",
"image": "image/name:latest",
"volumes": [
{
"name": "myvolume",
"target": "/mount/here",
"readOnly": false
},
{
"name": "myreadonlyvolume",
"target": "/mount/there",
"readOnly": true
},
{
"target": "/mount/everywhere",
"readOnly": false
}
]
}
""";

Assert.Equal(expectedManifest, manifest.ToString());
}

[Theory]
[InlineData(new string[] { "args1", "args2" }, new string[] { "withArgs1", "withArgs2" })]
[InlineData(new string[] { }, new string[] { "withArgs1", "withArgs2" })]
Expand Down