Skip to content

Commit 4464fde

Browse files
vvolandk8s-infra-cherrypick-robot
authored andcommitted
push: always inherit distribution sources from parent
Propagate parent distribution source labels to each of its children even if they're not missing. This allows to cross-repo mount blobs when the child content has different distribution source label from its parent manifest/index. This could happen when different parts of image were fetched from different sources. Signed-off-by: Paweł Gronowski <[email protected]>
1 parent 9c65471 commit 4464fde

1 file changed

Lines changed: 33 additions & 23 deletions

File tree

remotes/handlers.go

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,15 @@ func annotateDistributionSourceHandler(f images.HandlerFunc, provider content.In
361361
return children, nil
362362
}
363363

364-
// parentInfo can be used to inherit info for non-existent blobs
365-
var parentInfo *content.Info
364+
parentSourceAnnotations := desc.Annotations
365+
var parentLabels map[string]string
366+
if pi, err := provider.Info(ctx, desc.Digest); err != nil {
367+
if !errdefs.IsNotFound(err) {
368+
return nil, err
369+
}
370+
} else {
371+
parentLabels = pi.Labels
372+
}
366373

367374
for i := range children {
368375
child := children[i]
@@ -372,32 +379,35 @@ func annotateDistributionSourceHandler(f images.HandlerFunc, provider content.In
372379
if !errdefs.IsNotFound(err) {
373380
return nil, err
374381
}
375-
if parentInfo == nil {
376-
pi, err := provider.Info(ctx, desc.Digest)
377-
if err != nil {
378-
return nil, err
379-
}
380-
parentInfo = &pi
381-
}
382-
// Blob may not exist locally, annotate with parent labels for cross repo
383-
// mount or fetch. Parent sources may apply to all children since most
384-
// registries enforce that children exist before the manifests.
385-
info = *parentInfo
386382
}
383+
copyDistributionSourceLabels(info.Labels, &child)
387384

388-
for k, v := range info.Labels {
389-
if !strings.HasPrefix(k, labels.LabelDistributionSource+".") {
390-
continue
391-
}
392-
393-
if child.Annotations == nil {
394-
child.Annotations = map[string]string{}
395-
}
396-
child.Annotations[k] = v
397-
}
385+
// Annotate with parent labels for cross repo mount or fetch.
386+
// Parent sources may apply to all children since most registries
387+
// enforce that children exist before the manifests.
388+
copyDistributionSourceLabels(parentSourceAnnotations, &child)
389+
copyDistributionSourceLabels(parentLabels, &child)
398390

399391
children[i] = child
400392
}
401393
return children, nil
402394
}
403395
}
396+
397+
func copyDistributionSourceLabels(from map[string]string, to *ocispec.Descriptor) {
398+
for k, v := range from {
399+
if !strings.HasPrefix(k, labels.LabelDistributionSource+".") {
400+
continue
401+
}
402+
403+
if to.Annotations == nil {
404+
to.Annotations = make(map[string]string)
405+
} else {
406+
// Only propagate the parent label if the child doesn't already have it.
407+
if _, has := to.Annotations[k]; has {
408+
continue
409+
}
410+
}
411+
to.Annotations[k] = v
412+
}
413+
}

0 commit comments

Comments
 (0)