| 1 |
package cacheimport |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
|
| 6 |
"github.com/moby/buildkit/solver" |
| 7 |
"github.com/moby/buildkit/util/contentutil" |
| 8 |
ocispecs "github.com/opencontainers/image-spec/specs-go/v1" |
| 9 |
"github.com/pkg/errors" |
| 10 |
) |
| 11 |
|
| 12 |
func Parse(configJSON []byte, provider DescriptorProvider, t solver.CacheExporterTarget) error { |
| 13 |
var config CacheConfig |
| 14 |
if err := json.Unmarshal(configJSON, &config); err != nil { |
| 15 |
return errors.WithStack(err) |
| 16 |
} |
| 17 |
|
| 18 |
return ParseConfig(config, provider, t) |
| 19 |
} |
| 20 |
|
| 21 |
func ParseConfig(config CacheConfig, provider DescriptorProvider, t solver.CacheExporterTarget) error { |
| 22 |
cache := map[int]solver.CacheExporterRecord{} |
| 23 |
|
| 24 |
for i := range config.Records { |
| 25 |
if _, err := parseRecord(config, i, provider, t, cache); err != nil { |
| 26 |
return err |
| 27 |
} |
| 28 |
} |
| 29 |
return nil |
| 30 |
} |
| 31 |
|
| 32 |
func parseRecord(cc CacheConfig, idx int, provider DescriptorProvider, t solver.CacheExporterTarget, cache map[int]solver.CacheExporterRecord) (solver.CacheExporterRecord, error) { |
| 33 |
if r, ok := cache[idx]; ok { |
| 34 |
if r == nil { |
| 35 |
return nil, errors.Errorf("invalid looping record") |
| 36 |
} |
| 37 |
return r, nil |
| 38 |
} |
| 39 |
|
| 40 |
cache[idx] = nil |
| 41 |
if idx < 0 || idx >= len(cc.Records) { |
| 42 |
return nil, errors.Errorf("invalid record ID: %d", idx) |
| 43 |
} |
| 44 |
rec := cc.Records[idx] |
| 45 |
|
| 46 |
links := make([][]solver.CacheLink, len(rec.Inputs)) |
| 47 |
|
| 48 |
for i, inputs := range rec.Inputs { |
| 49 |
if len(inputs) == 0 { |
| 50 |
return nil, errors.Errorf("invalid empty input for record %d", idx) |
| 51 |
} |
| 52 |
links[i] = make([]solver.CacheLink, len(inputs)) |
| 53 |
for j, inp := range inputs { |
| 54 |
src, err := parseRecord(cc, inp.LinkIndex, provider, t, cache) |
| 55 |
if err != nil { |
| 56 |
return nil, err |
| 57 |
} |
| 58 |
links[i][j] = solver.CacheLink{ |
| 59 |
Selector: inp.Selector, |
| 60 |
Src: src, |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
results := make([]solver.CacheExportResult, 0, len(rec.Results)) |
| 66 |
for _, res := range rec.Results { |
| 67 |
visited := map[int]struct{}{} |
| 68 |
remote, err := getRemoteChain(cc.Layers, res.LayerIndex, provider, visited) |
| 69 |
if err != nil { |
| 70 |
return nil, err |
| 71 |
} |
| 72 |
if remote != nil { |
| 73 |
results = append(results, solver.CacheExportResult{ |
| 74 |
CreatedAt: res.CreatedAt, |
| 75 |
Result: remote, |
| 76 |
}) |
| 77 |
} |
| 78 |
} |
| 79 |
for _, res := range rec.ChainedResults { |
| 80 |
remote := &solver.Remote{} |
| 81 |
mp := contentutil.NewMultiProvider(nil) |
| 82 |
for _, diff := range res.LayerIndexes { |
| 83 |
if diff < 0 || diff >= len(cc.Layers) { |
| 84 |
return nil, errors.Errorf("invalid layer index %d", diff) |
| 85 |
} |
| 86 |
|
| 87 |
l := cc.Layers[diff] |
| 88 |
|
| 89 |
descPair, ok := provider[l.Blob] |
| 90 |
if !ok { |
| 91 |
remote = nil |
| 92 |
break |
| 93 |
} |
| 94 |
|
| 95 |
remote.Descriptors = append(remote.Descriptors, descPair.Descriptor) |
| 96 |
mp.Add(descPair.Descriptor.Digest, descPair) |
| 97 |
} |
| 98 |
if remote != nil { |
| 99 |
remote.Provider = mp |
| 100 |
results = append(results, solver.CacheExportResult{ |
| 101 |
CreatedAt: res.CreatedAt, |
| 102 |
Result: remote, |
| 103 |
}) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
r, _, err := t.Add(rec.Digest, links, results) |
| 108 |
if err != nil { |
| 109 |
return nil, errors.Wrapf(err, "failed to add record %d", idx) |
| 110 |
} |
| 111 |
cache[idx] = r |
| 112 |
return r, nil |
| 113 |
} |
| 114 |
|
| 115 |
func getRemoteChain(layers []CacheLayer, idx int, provider DescriptorProvider, visited map[int]struct{}) (*solver.Remote, error) { |
| 116 |
if _, ok := visited[idx]; ok { |
| 117 |
return nil, errors.Errorf("invalid looping layer") |
| 118 |
} |
| 119 |
visited[idx] = struct{}{} |
| 120 |
|
| 121 |
if idx < 0 || idx >= len(layers) { |
| 122 |
return nil, errors.Errorf("invalid layer index %d", idx) |
| 123 |
} |
| 124 |
|
| 125 |
l := layers[idx] |
| 126 |
|
| 127 |
descPair, ok := provider[l.Blob] |
| 128 |
if !ok { |
| 129 |
return nil, nil |
| 130 |
} |
| 131 |
|
| 132 |
var r *solver.Remote |
| 133 |
if l.ParentIndex != -1 { |
| 134 |
var err error |
| 135 |
r, err = getRemoteChain(layers, l.ParentIndex, provider, visited) |
| 136 |
if err != nil { |
| 137 |
return nil, err |
| 138 |
} |
| 139 |
if r == nil { |
| 140 |
return nil, nil |
| 141 |
} |
| 142 |
r.Descriptors = append(r.Descriptors, descPair.Descriptor) |
| 143 |
mp := contentutil.NewMultiProvider(r.Provider) |
| 144 |
mp.Add(descPair.Descriptor.Digest, descPair) |
| 145 |
r.Provider = mp |
| 146 |
return r, nil |
| 147 |
} |
| 148 |
return &solver.Remote{ |
| 149 |
Descriptors: []ocispecs.Descriptor{descPair.Descriptor}, |
| 150 |
Provider: descPair, |
| 151 |
}, nil |
| 152 |
} |