Skip to content

Commit

Permalink
Add support for Labels in metadata, fixes dotnet#95
Browse files Browse the repository at this point in the history
  • Loading branch information
timheuer committed Aug 12, 2022
1 parent 3eb955c commit a54435b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Microsoft.NET.Build.Containers/CreateNewImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public class CreateNewImage : Microsoft.Build.Utilities.Task
/// </summary>
public ITaskItem[] EntrypointArgs { get; set; }

/// <summary>
/// Labels that the image configuration will include in metadata
/// </summary>
public ITaskItem[] Labels { get; set; }

public CreateNewImage()
{
BaseRegistry = "";
Expand All @@ -78,6 +83,7 @@ public CreateNewImage()
WorkingDirectory = "";
Entrypoint = Array.Empty<ITaskItem>();
EntrypointArgs = Array.Empty<ITaskItem>();
Labels = Array.Empty<ITaskItem>();
}


Expand Down Expand Up @@ -112,6 +118,11 @@ public override bool Execute()
image.WorkingDirectory = WorkingDirectory;
image.SetEntrypoint(Entrypoint.Select(i => i.ItemSpec).ToArray(), EntrypointArgs.Select(i => i.ItemSpec).ToArray());

foreach (var label in Labels)
{
image.Label(label.ItemSpec, label.GetMetadata("Value"));
}

if (OutputRegistry.StartsWith("docker://"))
{
try
Expand Down
21 changes: 21 additions & 0 deletions Microsoft.NET.Build.Containers/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace Microsoft.NET.Build.Containers;

record Label(string name, string value);

public class Image
{
public JsonNode manifest;
Expand All @@ -15,6 +17,8 @@ public class Image

internal List<Layer> newLayers = new();

internal HashSet<Label> labels = new();

public Image(JsonNode manifest, JsonNode config, string name, Registry? registry)
{
this.manifest = manifest;
Expand Down Expand Up @@ -61,6 +65,16 @@ private void RecalculateDigest()
manifest["config"]!["digest"] = GetDigest(config);
}

private JsonObject CreateLabelMap()
{
var container = new JsonObject();
foreach (var label in labels)
{
container.Add(label.name, label.value);
}
return container;
}

static JsonArray ToJsonArray(string[] items) => new JsonArray(items.Where(s => !string.IsNullOrEmpty(s)).Select(s =>(JsonValue) s).ToArray());

public void SetEntrypoint(string[] executableArgs, string[]? args = null)
Expand Down Expand Up @@ -94,6 +108,13 @@ public string WorkingDirectory {
}
}

public void Label(string name, string value)
{
labels.Add(new(name, value));
config["config"]!["Labels"] = CreateLabelMap();
RecalculateDigest();
}

public string GetDigest(JsonNode json)
{
string hashString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
PublishDirectory="$(PublishDir)"
WorkingDirectory="$(ContainerWorkingDirectory)"
Entrypoint="@(ContainerEntrypoint)"
EntrypointArgs="@(ContainerEntrypointArgs)"/>
EntrypointArgs="@(ContainerEntrypointArgs)"
Labels="@(ContainerLabel)"/>
</Target>
</Project>

0 comments on commit a54435b

Please sign in to comment.