Skip to content

Commit 8488b2f

Browse files
committed
Add initial support for an explicit "SharedTags" field
Things we know we'll need to be able to do with the new `SharedTags` (and are thus included in this initial file format support): - get list of all shared tags from a manifest (`manifest.GetAllSharedTags()`) - get list of shared tags from an entry (`entry.SharedTags`) - get list of entries given a shared tag (`manifest.GetSharedTag(tag)`)
1 parent 08ef5a9 commit 8488b2f

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

manifest/rfc2822.go

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ type Manifest2822Entry struct {
2727

2828
Maintainers []string `delim:"," strip:"\n\r\t "`
2929

30-
Tags []string `delim:"," strip:"\n\r\t "`
30+
Tags []string `delim:"," strip:"\n\r\t "`
31+
SharedTags []string `delim:"," strip:"\n\r\t "`
3132

3233
GitRepo string
3334
GitFetch string
@@ -46,6 +47,7 @@ func (entry Manifest2822Entry) Clone() Manifest2822Entry {
4647
// SLICES! grr
4748
entry.Maintainers = append([]string{}, entry.Maintainers...)
4849
entry.Tags = append([]string{}, entry.Tags...)
50+
entry.SharedTags = append([]string{}, entry.SharedTags...)
4951
entry.Constraints = append([]string{}, entry.Constraints...)
5052
return entry
5153
}
@@ -60,6 +62,10 @@ func (entry Manifest2822Entry) TagsString() string {
6062
return strings.Join(entry.Tags, StringSeparator2822)
6163
}
6264

65+
func (entry Manifest2822Entry) SharedTagsString() string {
66+
return strings.Join(entry.SharedTags, StringSeparator2822)
67+
}
68+
6369
func (entry Manifest2822Entry) ConstraintsString() string {
6470
return strings.Join(entry.Constraints, StringSeparator2822)
6571
}
@@ -77,6 +83,9 @@ func (entry Manifest2822Entry) ClearDefaults(defaults Manifest2822Entry) Manifes
7783
if entry.TagsString() == defaults.TagsString() {
7884
entry.Tags = nil
7985
}
86+
if entry.SharedTagsString() == defaults.SharedTagsString() {
87+
entry.SharedTags = nil
88+
}
8089
if entry.GitRepo == defaults.GitRepo {
8190
entry.GitRepo = ""
8291
}
@@ -103,6 +112,9 @@ func (entry Manifest2822Entry) String() string {
103112
if str := entry.TagsString(); str != "" {
104113
ret = append(ret, "Tags: "+str)
105114
}
115+
if str := entry.SharedTagsString(); str != "" {
116+
ret = append(ret, "SharedTags: "+str)
117+
}
106118
if str := entry.GitRepo; str != "" {
107119
ret = append(ret, "GitRepo: "+str)
108120
}
@@ -145,6 +157,16 @@ func (entry Manifest2822Entry) HasTag(tag string) bool {
145157
return false
146158
}
147159

160+
// HasSharedTag returns true if the given tag exists in entry.SharedTags.
161+
func (entry Manifest2822Entry) HasSharedTag(tag string) bool {
162+
for _, existingTag := range entry.SharedTags {
163+
if tag == existingTag {
164+
return true
165+
}
166+
}
167+
return false
168+
}
169+
148170
func (manifest Manifest2822) GetTag(tag string) *Manifest2822Entry {
149171
for _, entry := range manifest.Entries {
150172
if entry.HasTag(tag) {
@@ -154,6 +176,27 @@ func (manifest Manifest2822) GetTag(tag string) *Manifest2822Entry {
154176
return nil
155177
}
156178

179+
// GetSharedTag returns a list of entries with the given tag in entry.SharedTags (or the empty list if there are no entries with the given tag).
180+
func (manifest Manifest2822) GetSharedTag(tag string) []Manifest2822Entry {
181+
ret := []Manifest2822Entry{}
182+
for _, entry := range manifest.Entries {
183+
if entry.HasSharedTag(tag) {
184+
ret = append(ret, entry)
185+
}
186+
}
187+
return ret
188+
}
189+
190+
// GetAllSharedTags returns a list of the sum of all SharedTags in all entries of this image manifest (in the order they appear in the file).
191+
func (manifest Manifest2822) GetAllSharedTags() []string {
192+
fakeEntry := Manifest2822Entry{}
193+
for _, entry := range manifest.Entries {
194+
fakeEntry.SharedTags = append(fakeEntry.SharedTags, entry.SharedTags...)
195+
}
196+
fakeEntry.DeduplicateSharedTags()
197+
return fakeEntry.SharedTags
198+
}
199+
157200
func (manifest *Manifest2822) AddEntry(entry Manifest2822Entry) error {
158201
if len(entry.Tags) < 1 {
159202
return fmt.Errorf("missing Tags")
@@ -165,20 +208,36 @@ func (manifest *Manifest2822) AddEntry(entry Manifest2822Entry) error {
165208
return fmt.Errorf("Tags %q has invalid Maintainers: %q (expected format %q)", strings.Join(invalidMaintainers, ", "), MaintainersFormat)
166209
}
167210

211+
entry.DeduplicateSharedTags()
212+
168213
seenTag := map[string]bool{}
169214
for _, tag := range entry.Tags {
170215
if otherEntry := manifest.GetTag(tag); otherEntry != nil {
171216
return fmt.Errorf("Tags %q includes duplicate tag: %q (duplicated in %q)", entry.TagsString(), tag, otherEntry.TagsString())
172217
}
218+
if otherEntries := manifest.GetSharedTag(tag); len(otherEntries) > 0 {
219+
return fmt.Errorf("Tags %q includes tag conflicting with a shared tag: %q (shared tag in %q)", entry.TagsString(), tag, otherEntries[0].TagsString())
220+
}
173221
if seenTag[tag] {
174222
return fmt.Errorf("Tags %q includes duplicate tag: %q", entry.TagsString(), tag)
175223
}
176224
seenTag[tag] = true
177225
}
226+
for _, tag := range entry.SharedTags {
227+
if otherEntry := manifest.GetTag(tag); otherEntry != nil {
228+
return fmt.Errorf("Tags %q includes conflicting shared tag: %q (duplicated in %q)", entry.TagsString(), tag, otherEntry.TagsString())
229+
}
230+
if seenTag[tag] {
231+
return fmt.Errorf("Tags %q includes duplicate tag: %q (in SharedTags)", entry.TagsString(), tag)
232+
}
233+
seenTag[tag] = true
234+
}
178235

179236
for i, existingEntry := range manifest.Entries {
180237
if existingEntry.SameBuildArtifacts(entry) {
181238
manifest.Entries[i].Tags = append(existingEntry.Tags, entry.Tags...)
239+
manifest.Entries[i].SharedTags = append(existingEntry.SharedTags, entry.SharedTags...)
240+
manifest.Entries[i].DeduplicateSharedTags()
182241
return nil
183242
}
184243
}
@@ -210,6 +269,20 @@ func (entry Manifest2822Entry) InvalidMaintainers() []string {
210269
return invalid
211270
}
212271

272+
// DeduplicateSharedTags will remove duplicate values from entry.SharedTags, preserving order.
273+
func (entry *Manifest2822Entry) DeduplicateSharedTags() {
274+
aggregate := []string{}
275+
seen := map[string]bool{}
276+
for _, tag := range entry.SharedTags {
277+
if seen[tag] {
278+
continue
279+
}
280+
seen[tag] = true
281+
aggregate = append(aggregate, tag)
282+
}
283+
entry.SharedTags = aggregate
284+
}
285+
213286
type decoderWrapper struct {
214287
*control.Decoder
215288
}

0 commit comments

Comments
 (0)