Debian Codegraph @

buildkit/cache/remotecache/v1/utils.go

raw
1 package cacheimport
2
3 import (
4 "cmp"
5 "context"
6 "fmt"
7 "slices"
8 "sort"
9
10 cerrdefs "github.com/containerd/errdefs"
11 "github.com/moby/buildkit/solver"
12 digest "github.com/opencontainers/go-digest"
13 "github.com/pkg/errors"
14 )
15
16 // sortConfig sorts the config structure to make sure it is deterministic
17 func sortConfig(cc *CacheConfig) {
18 type indexedLayer struct {
19 oldIndex int
20 newIndex int
21 l CacheLayer
22 }
23
24 unsortedLayers := make([]*indexedLayer, len(cc.Layers))
25 sortedLayers := make([]*indexedLayer, len(cc.Layers))
26
27 for i, l := range cc.Layers {
28 il := &indexedLayer{oldIndex: i, l: l}
29 unsortedLayers[i] = il
30 sortedLayers[i] = il
31 }
32 slices.SortFunc(sortedLayers, func(a, b *indexedLayer) int {
33 return cmp.Or(cmp.Compare(a.l.Blob, b.l.Blob), cmp.Compare(a.l.ParentIndex, b.l.ParentIndex))
34 })
35 for i, l := range sortedLayers {
36 l.newIndex = i
37 }
38
39 layers := make([]CacheLayer, len(sortedLayers))
40 for i, l := range sortedLayers {
41 if pID := l.l.ParentIndex; pID != -1 {
42 l.l.ParentIndex = unsortedLayers[pID].newIndex
43 }
44 layers[i] = l.l
45 }
46
47 type indexedRecord struct {
48 oldIndex int
49 newIndex int
50 r CacheRecord
51 }
52
53 unsortedRecords := make([]*indexedRecord, len(cc.Records))
54 sortedRecords := make([]*indexedRecord, len(cc.Records))
55
56 for i, r := range cc.Records {
57 ir := &indexedRecord{oldIndex: i, r: r}
58 unsortedRecords[i] = ir
59 sortedRecords[i] = ir
60 }
61 sort.Slice(sortedRecords, func(i, j int) bool {
62 ri := sortedRecords[i].r
63 rj := sortedRecords[j].r
64 if ri.Digest != rj.Digest {
65 return ri.Digest < rj.Digest
66 }
67 if len(ri.Inputs) != len(rj.Inputs) {
68 return len(ri.Inputs) < len(rj.Inputs)
69 }
70 for i, inputs := range ri.Inputs {
71 if len(ri.Inputs[i]) != len(rj.Inputs[i]) {
72 return len(ri.Inputs[i]) < len(rj.Inputs[i])
73 }
74 for j := range inputs {
75 if ri.Inputs[i][j].Selector != rj.Inputs[i][j].Selector {
76 return ri.Inputs[i][j].Selector < rj.Inputs[i][j].Selector
77 }
78 inputDigesti := cc.Records[ri.Inputs[i][j].LinkIndex].Digest
79 inputDigestj := cc.Records[rj.Inputs[i][j].LinkIndex].Digest
80 if inputDigesti != inputDigestj {
81 return inputDigesti < inputDigestj
82 }
83 }
84 }
85 return false
86 })
87 for i, l := range sortedRecords {
88 l.newIndex = i
89 }
90
91 records := make([]CacheRecord, len(sortedRecords))
92 for i, r := range sortedRecords {
93 for j := range r.r.Results {
94 r.r.Results[j].LayerIndex = unsortedLayers[r.r.Results[j].LayerIndex].newIndex
95 }
96 for j, inputs := range r.r.Inputs {
97 for k := range inputs {
98 r.r.Inputs[j][k].LinkIndex = unsortedRecords[r.r.Inputs[j][k].LinkIndex].newIndex
99 }
100 slices.SortFunc(inputs, func(a, b CacheInput) int {
101 return cmp.Compare(a.LinkIndex, b.LinkIndex)
102 })
103 }
104 records[i] = r.r
105 }
106
107 cc.Layers = layers
108 cc.Records = records
109 }
110
111 func outputKey(dgst digest.Digest, idx int) digest.Digest {
112 return digest.FromBytes(fmt.Appendf(nil, "%s@%d", dgst, idx))
113 }
114
115 type nlink struct {
116 dgst digest.Digest
117 input int
118 selector string
119 }
120
121 type marshalState struct {
122 layers []CacheLayer
123 chainsByID map[string]int
124 descriptors DescriptorProvider
125
126 records []CacheRecord
127 recordsByItem map[*item]int
128 }
129
130 func marshalRemote(ctx context.Context, r *solver.Remote, state *marshalState) string {
131 if len(r.Descriptors) == 0 {
132 return ""
133 }
134
135 if r.Provider != nil {
136 for _, d := range r.Descriptors {
137 if _, err := r.Provider.Info(ctx, d.Digest); err != nil {
138 if !cerrdefs.IsNotImplemented(err) {
139 return ""
140 }
141 }
142 }
143 }
144
145 var parentID string
146 if len(r.Descriptors) > 1 {
147 r2 := &solver.Remote{
148 Descriptors: r.Descriptors[:len(r.Descriptors)-1],
149 Provider: r.Provider,
150 }
151 parentID = marshalRemote(ctx, r2, state)
152 }
153 desc := r.Descriptors[len(r.Descriptors)-1]
154
155 state.descriptors[desc.Digest] = DescriptorProviderPair{
156 Descriptor: desc,
157 Provider: r.Provider,
158 }
159
160 id := desc.Digest.String() + parentID
161
162 if _, ok := state.chainsByID[id]; ok {
163 return id
164 }
165
166 state.chainsByID[id] = len(state.layers)
167 l := CacheLayer{
168 Blob: desc.Digest,
169 ParentIndex: -1,
170 }
171 if parentID != "" {
172 l.ParentIndex = state.chainsByID[parentID]
173 }
174 state.layers = append(state.layers, l)
175 return id
176 }
177
178 func marshalItem(ctx context.Context, it *item, state *marshalState) error {
179 if _, ok := state.recordsByItem[it]; ok {
180 return nil
181 }
182 state.recordsByItem[it] = -1
183
184 rec := CacheRecord{
185 Digest: it.dgst,
186 Inputs: make([][]CacheInput, len(it.parents)),
187 }
188
189 for i, m := range it.parents {
190 for l := range m {
191 if err := marshalItem(ctx, l.src, state); err != nil {
192 return err
193 }
194 idx, ok := state.recordsByItem[l.src]
195 if !ok {
196 return errors.Errorf("invalid source record: %v", l.src)
197 }
198 if idx == -1 {
199 continue
200 }
201 rec.Inputs[i] = append(rec.Inputs[i], CacheInput{
202 Selector: l.selector,
203 LinkIndex: idx,
204 })
205 }
206 }
207
208 if res := it.bestResult(); res != nil {
209 id := marshalRemote(ctx, res.Result, state)
210 if id != "" {
211 idx, ok := state.chainsByID[id]
212 if !ok {
213 return errors.Errorf("parent chainid not found")
214 }
215 rec.Results = append(rec.Results, CacheResult{LayerIndex: idx, CreatedAt: res.CreatedAt})
216 }
217 }
218
219 state.recordsByItem[it] = len(state.records)
220 state.records = append(state.records, rec)
221 return nil
222 }
223
224 func isSubRemote(sub, main solver.Remote) bool {
225 if len(sub.Descriptors) > len(main.Descriptors) {
226 return false
227 }
228 for i := range sub.Descriptors {
229 if sub.Descriptors[i].Digest != main.Descriptors[i].Digest {
230 return false
231 }
232 }
233 return true
234 }