5
5
6
6
namespace Microsoft . NET . Build . Containers ;
7
7
8
+ record Label ( string name , string value ) ;
9
+
8
10
public class Image
9
11
{
10
12
public JsonNode manifest ;
@@ -15,12 +17,16 @@ public class Image
15
17
16
18
internal List < Layer > newLayers = new ( ) ;
17
19
20
+ private HashSet < Label > labels ;
21
+
18
22
public Image ( JsonNode manifest , JsonNode config , string name , Registry ? registry )
19
23
{
20
24
this . manifest = manifest ;
21
25
this . config = config ;
22
26
this . OriginatingName = name ;
23
27
this . originatingRegistry = registry ;
28
+ // labels are inherited from the parent image, so we need to seed our new image with them.
29
+ this . labels = ReadLabelsFromConfig ( config ) ;
24
30
}
25
31
26
32
public IEnumerable < Descriptor > LayerDescriptors
@@ -61,7 +67,39 @@ private void RecalculateDigest()
61
67
manifest [ "config" ] ! [ "digest" ] = GetDigest ( config ) ;
62
68
}
63
69
64
- static JsonArray ToJsonArray ( string [ ] items ) => new JsonArray ( items . Where ( s => ! string . IsNullOrEmpty ( s ) ) . Select ( s => ( JsonValue ) s ) . ToArray ( ) ) ;
70
+ private static HashSet < Label > ReadLabelsFromConfig ( JsonNode inputConfig )
71
+ {
72
+ if ( inputConfig is JsonObject config && config [ "Labels" ] is JsonObject labelsJson )
73
+ {
74
+ // read label mappings from object
75
+ var labels = new HashSet < Label > ( ) ;
76
+ foreach ( var property in labelsJson )
77
+ {
78
+ if ( property . Key is { } propertyName && property . Value is JsonValue propertyValue )
79
+ {
80
+ labels . Add ( new ( propertyName , propertyValue . ToString ( ) ) ) ;
81
+ }
82
+ }
83
+ return labels ;
84
+ }
85
+ else
86
+ {
87
+ // initialize and empty labels map
88
+ return new HashSet < Label > ( ) ;
89
+ }
90
+ }
91
+
92
+ private JsonObject CreateLabelMap ( )
93
+ {
94
+ var container = new JsonObject ( ) ;
95
+ foreach ( var label in labels )
96
+ {
97
+ container . Add ( label . name , label . value ) ;
98
+ }
99
+ return container ;
100
+ }
101
+
102
+ static JsonArray ToJsonArray ( string [ ] items ) => new JsonArray ( items . Where ( s => ! string . IsNullOrEmpty ( s ) ) . Select ( s => ( JsonValue ) s ) . ToArray ( ) ) ;
65
103
66
104
public void SetEntrypoint ( string [ ] executableArgs , string [ ] ? args = null )
67
105
{
@@ -86,14 +124,23 @@ public void SetEntrypoint(string[] executableArgs, string[]? args = null)
86
124
RecalculateDigest ( ) ;
87
125
}
88
126
89
- public string WorkingDirectory {
90
- get => ( string ? ) manifest [ "config" ] ! [ "WorkingDir" ] ?? "" ;
91
- set {
127
+ public string WorkingDirectory
128
+ {
129
+ get => ( string ? ) config [ "config" ] ! [ "WorkingDir" ] ?? "" ;
130
+ set
131
+ {
92
132
config [ "config" ] ! [ "WorkingDir" ] = value ;
93
133
RecalculateDigest ( ) ;
94
134
}
95
135
}
96
136
137
+ public void Label ( string name , string value )
138
+ {
139
+ labels . Add ( new ( name , value ) ) ;
140
+ config [ "config" ] ! [ "Labels" ] = CreateLabelMap ( ) ;
141
+ RecalculateDigest ( ) ;
142
+ }
143
+
97
144
public string GetDigest ( JsonNode json )
98
145
{
99
146
string hashString ;
0 commit comments