AppendDistributionSourceLabel updates the label of blob with distribution source.
(manager content.Manager, ref string)
| 32 | |
| 33 | // AppendDistributionSourceLabel updates the label of blob with distribution source. |
| 34 | func AppendDistributionSourceLabel(manager content.Manager, ref string) (images.HandlerFunc, error) { |
| 35 | refspec, err := reference.Parse(ref) |
| 36 | if err != nil { |
| 37 | return nil, err |
| 38 | } |
| 39 | |
| 40 | u, err := url.Parse("dummy://" + refspec.Locator) |
| 41 | if err != nil { |
| 42 | return nil, err |
| 43 | } |
| 44 | |
| 45 | source, repo := u.Hostname(), strings.TrimPrefix(u.Path, "/") |
| 46 | return func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) { |
| 47 | info, err := manager.Info(ctx, desc.Digest) |
| 48 | if err != nil { |
| 49 | return nil, err |
| 50 | } |
| 51 | |
| 52 | key := distributionSourceLabelKey(source) |
| 53 | |
| 54 | originLabel := "" |
| 55 | if info.Labels != nil { |
| 56 | originLabel = info.Labels[key] |
| 57 | } |
| 58 | value := appendDistributionSourceLabel(originLabel, repo) |
| 59 | |
| 60 | // The repo name has been limited under 256 and the distribution |
| 61 | // label might hit the limitation of label size, when blob data |
| 62 | // is used as the very, very common layer. |
| 63 | if err := labels.Validate(key, value); err != nil { |
| 64 | log.G(ctx).Warnf("skip to append distribution label: %s", err) |
| 65 | return nil, nil |
| 66 | } |
| 67 | |
| 68 | info = content.Info{ |
| 69 | Digest: desc.Digest, |
| 70 | Labels: map[string]string{ |
| 71 | key: value, |
| 72 | }, |
| 73 | } |
| 74 | _, err = manager.Update(ctx, info, fmt.Sprintf("labels.%s", key)) |
| 75 | return nil, err |
| 76 | }, nil |
| 77 | } |
| 78 | |
| 79 | func appendDistributionSourceLabel(originLabel, repo string) string { |
| 80 | repos := []string{} |
no test coverage detected
searching dependent graphs…