| 1 |
package inline |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"encoding/json" |
| 6 |
"slices" |
| 7 |
|
| 8 |
"github.com/containerd/containerd/v2/pkg/labels" |
| 9 |
"github.com/moby/buildkit/cache/remotecache" |
| 10 |
v1 "github.com/moby/buildkit/cache/remotecache/v1" |
| 11 |
"github.com/moby/buildkit/session" |
| 12 |
"github.com/moby/buildkit/solver" |
| 13 |
"github.com/moby/buildkit/util/bklog" |
| 14 |
"github.com/moby/buildkit/util/compression" |
| 15 |
digest "github.com/opencontainers/go-digest" |
| 16 |
"github.com/pkg/errors" |
| 17 |
) |
| 18 |
|
| 19 |
func ResolveCacheExporterFunc() remotecache.ResolveCacheExporterFunc { |
| 20 |
return func(ctx context.Context, _ session.Group, _ map[string]string) (remotecache.Exporter, error) { |
| 21 |
return NewExporter(), nil |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
func NewExporter() remotecache.Exporter { |
| 26 |
cc := v1.NewCacheChains() |
| 27 |
return &exporter{CacheExporterTarget: cc, chains: cc} |
| 28 |
} |
| 29 |
|
| 30 |
type exporter struct { |
| 31 |
solver.CacheExporterTarget |
| 32 |
chains *v1.CacheChains |
| 33 |
} |
| 34 |
|
| 35 |
func (*exporter) Name() string { |
| 36 |
return "exporting inline cache" |
| 37 |
} |
| 38 |
|
| 39 |
func (ce *exporter) Config() remotecache.Config { |
| 40 |
return remotecache.Config{ |
| 41 |
Compression: compression.New(compression.Default), |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func (ce *exporter) Finalize(ctx context.Context) (map[string]string, error) { |
| 46 |
return nil, nil |
| 47 |
} |
| 48 |
|
| 49 |
func (ce *exporter) reset() { |
| 50 |
cc := v1.NewCacheChains() |
| 51 |
ce.CacheExporterTarget = cc |
| 52 |
ce.chains = cc |
| 53 |
} |
| 54 |
|
| 55 |
func (ce *exporter) ExportForLayers(ctx context.Context, layers []digest.Digest) ([]byte, error) { |
| 56 |
config, descs, err := ce.chains.Marshal(ctx) |
| 57 |
if err != nil { |
| 58 |
return nil, err |
| 59 |
} |
| 60 |
|
| 61 |
layerBlobDigests := make([][]digest.Digest, len(layers)) |
| 62 |
|
| 63 |
descs2 := map[digest.Digest]v1.DescriptorProviderPair{} |
| 64 |
for i, k := range layers { |
| 65 |
if v, ok := descs[k]; ok { |
| 66 |
descs2[k] = v |
| 67 |
layerBlobDigests[i] = append(layerBlobDigests[i], k) |
| 68 |
} |
| 69 |
// fallback for uncompressed digests |
| 70 |
for _, v := range descs { |
| 71 |
if uc := v.Descriptor.Annotations[labels.LabelUncompressed]; uc == string(k) { |
| 72 |
descs2[v.Descriptor.Digest] = v |
| 73 |
layerBlobDigests[i] = append(layerBlobDigests[i], v.Descriptor.Digest) |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
cc := v1.NewCacheChains() |
| 79 |
if err := v1.ParseConfig(*config, descs2, cc); err != nil { |
| 80 |
return nil, err |
| 81 |
} |
| 82 |
|
| 83 |
cfg, _, err := cc.Marshal(ctx) |
| 84 |
if err != nil { |
| 85 |
return nil, err |
| 86 |
} |
| 87 |
|
| 88 |
if len(cfg.Layers) == 0 { |
| 89 |
bklog.G(ctx).Warn("failed to match any cache with layers") |
| 90 |
return nil, nil |
| 91 |
} |
| 92 |
|
| 93 |
// reorder layers based on the order in the image |
| 94 |
blobIndexes := make(map[digest.Digest]int, len(layers)) |
| 95 |
for i, blobs := range layerBlobDigests { |
| 96 |
for _, blob := range blobs { |
| 97 |
blobIndexes[blob] = i |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
for i, r := range cfg.Records { |
| 102 |
for j, rr := range r.Results { |
| 103 |
resultBlobs := layerToBlobs(rr.LayerIndex, cfg.Layers) |
| 104 |
// match being true means the result is in the same order as the image |
| 105 |
var match bool |
| 106 |
if len(resultBlobs) <= len(layers) { |
| 107 |
match = true |
| 108 |
for k, resultBlob := range resultBlobs { |
| 109 |
matchesBlob := slices.Contains(layerBlobDigests[k], resultBlob) |
| 110 |
if !matchesBlob { |
| 111 |
match = false |
| 112 |
break |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
if match { |
| 117 |
// The layers of the result are in the same order as the image, so we can |
| 118 |
// specify it just using the CacheResult struct and specifying LayerIndex |
| 119 |
// as the top-most layer of the result. |
| 120 |
rr.LayerIndex = len(resultBlobs) - 1 |
| 121 |
r.Results[j] = rr |
| 122 |
} else { |
| 123 |
// The layers of the result are not in the same order as the image, so we |
| 124 |
// have to use ChainedResult to specify each layer of the result individually. |
| 125 |
chainedResult := v1.ChainedResult{} |
| 126 |
for _, resultBlob := range resultBlobs { |
| 127 |
idx, ok := blobIndexes[resultBlob] |
| 128 |
if !ok { |
| 129 |
return nil, errors.Errorf("failed to find blob %s in layers", resultBlob) |
| 130 |
} |
| 131 |
chainedResult.LayerIndexes = append(chainedResult.LayerIndexes, idx) |
| 132 |
} |
| 133 |
r.Results[j] = v1.CacheResult{} |
| 134 |
r.ChainedResults = append(r.ChainedResults, chainedResult) |
| 135 |
} |
| 136 |
// remove any CacheResults that had to be converted to the ChainedResult format. |
| 137 |
var filteredResults []v1.CacheResult |
| 138 |
for _, rr := range r.Results { |
| 139 |
if rr != (v1.CacheResult{}) { |
| 140 |
filteredResults = append(filteredResults, rr) |
| 141 |
} |
| 142 |
} |
| 143 |
r.Results = filteredResults |
| 144 |
cfg.Records[i] = r |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
dt, err := json.Marshal(cfg.Records) |
| 149 |
if err != nil { |
| 150 |
return nil, err |
| 151 |
} |
| 152 |
ce.reset() |
| 153 |
|
| 154 |
return dt, nil |
| 155 |
} |
| 156 |
|
| 157 |
func layerToBlobs(idx int, layers []v1.CacheLayer) []digest.Digest { |
| 158 |
var ds []digest.Digest |
| 159 |
for idx != -1 { |
| 160 |
layer := layers[idx] |
| 161 |
ds = append(ds, layer.Blob) |
| 162 |
idx = layer.ParentIndex |
| 163 |
} |
| 164 |
// reverse so they go lowest to highest |
| 165 |
slices.Reverse(ds) |
| 166 |
return ds |
| 167 |
} |