Skip to content

Commit 2ccd51e

Browse files
committed
Adjust types to use Google's protobuf code generator
The code generator is less customizable and doesn't allow custom types. Signed-off-by: Kazuyoshi Kato <[email protected]>
1 parent faec25d commit 2ccd51e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+583
-467
lines changed

cache/contenthash/checksum.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/pkg/errors"
2323
"github.com/tonistiigi/fsutil"
2424
fstypes "github.com/tonistiigi/fsutil/types"
25+
"google.golang.org/protobuf/proto"
2526
)
2627

2728
var errNotFound = errors.Errorf("not found")
@@ -241,7 +242,7 @@ func (cc *cacheContext) load() error {
241242
}
242243

243244
var l CacheRecords
244-
if err := l.Unmarshal(dt); err != nil {
245+
if err := proto.Unmarshal(dt, &l); err != nil {
245246
return err
246247
}
247248

@@ -271,7 +272,7 @@ func (cc *cacheContext) save() error {
271272
return false
272273
})
273274

274-
dt, err := l.Marshal()
275+
dt, err := proto.Marshal(&l)
275276
if err != nil {
276277
return err
277278
}
@@ -311,7 +312,7 @@ func (cc *cacheContext) HandleChange(kind fsutil.ChangeKind, p string, fi os.Fil
311312
if _, ok := cc.node.Get([]byte{0}); !ok {
312313
cc.txn.Insert([]byte{0}, &CacheRecord{
313314
Type: CacheRecordTypeDirHeader,
314-
Digest: digest.FromBytes(nil),
315+
Digest: digest.FromBytes(nil).String(),
315316
})
316317
cc.txn.Insert([]byte(""), &CacheRecord{
317318
Type: CacheRecordTypeDir,
@@ -363,16 +364,16 @@ func (cc *cacheContext) HandleChange(kind fsutil.ChangeKind, p string, fi os.Fil
363364
k = append(k, 0)
364365
p += "/"
365366
}
366-
cr.Digest = h.Digest()
367+
cr.Digest = h.Digest().String()
367368

368369
// if we receive a hardlink just use the digest of the source
369370
// note that the source may be called later because data writing is async
370371
if fi.Mode()&os.ModeSymlink == 0 && stat.Linkname != "" {
371372
ln := path.Join("/", filepath.ToSlash(stat.Linkname))
372373
v, ok := cc.txn.Get(convertPathToKey([]byte(ln)))
373374
if ok {
374-
cp := *v.(*CacheRecord)
375-
cr = &cp
375+
cp := v.(*CacheRecord)
376+
cr = cp
376377
}
377378
cc.linkMap[ln] = append(cc.linkMap[ln], k)
378379
}
@@ -422,7 +423,7 @@ func (cc *cacheContext) Checksum(ctx context.Context, mountable cache.Mountable,
422423
if err != nil {
423424
return "", err
424425
}
425-
includedPaths[i].record = &CacheRecord{Digest: dgst}
426+
includedPaths[i].record = &CacheRecord{Digest: dgst.String()}
426427
}
427428
}
428429
}
@@ -431,7 +432,7 @@ func (cc *cacheContext) Checksum(ctx context.Context, mountable cache.Mountable,
431432
}
432433

433434
if len(includedPaths) == 1 && path.Base(p) == path.Base(includedPaths[0].path) {
434-
return includedPaths[0].record.Digest, nil
435+
return digest.Digest(includedPaths[0].record.Digest), nil
435436
}
436437

437438
digester := digest.Canonical.Digester()
@@ -464,7 +465,7 @@ func (cc *cacheContext) checksumFollow(ctx context.Context, m *mount, p string,
464465
i++
465466
p = link
466467
} else {
467-
return cr.Digest, nil
468+
return digest.Digest(cr.Digest), nil
468469
}
469470
}
470471
}
@@ -957,7 +958,7 @@ func (cc *cacheContext) checksum(ctx context.Context, root *iradix.Node, txn *ir
957958
}
958959

959960
cr2 := &CacheRecord{
960-
Digest: dgst,
961+
Digest: dgst.String(),
961962
Type: cr.Type,
962963
Linkname: cr.Linkname,
963964
}

cache/contenthash/checksum_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,7 @@ func parseChange(str string) *change {
12691269
st.Linkname = f[3][1:]
12701270
} else {
12711271
c.data = f[3]
1272-
st.Size_ = int64(len(f[3]))
1272+
st.Size = int64(len(f[3]))
12731273
}
12741274
}
12751275
st.Mode |= 0644

cache/contenthash/filehash.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func NewFileHash(path string, fi os.FileInfo) (hash.Hash, error) {
2424

2525
stat := &fstypes.Stat{
2626
Mode: uint32(fi.Mode()),
27-
Size_: fi.Size(),
27+
Size: fi.Size(),
2828
ModTime: fi.ModTime().UnixNano(),
2929
Linkname: link,
3030
}
@@ -86,7 +86,7 @@ func (s *statInfo) Name() string {
8686
return filepath.Base(s.Stat.Path)
8787
}
8888
func (s *statInfo) Size() int64 {
89-
return s.Stat.Size_
89+
return s.Stat.Size
9090
}
9191
func (s *statInfo) Mode() os.FileMode {
9292
return os.FileMode(s.Stat.Mode)

client/build_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func testWarnings(t *testing.T, sb integration.Sandbox) {
198198
Data: []byte("filedata"),
199199
},
200200
Range: []*pb.Range{
201-
{Start: pb.Position{Line: 2}, End: pb.Position{Line: 4}},
201+
{Start: &pb.Position{Line: 2}, End: &pb.Position{Line: 4}},
202202
},
203203
Detail: [][]byte{[]byte("this is detail"), []byte("and more detail")},
204204
URL: "https://example.com",

client/client_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -6411,9 +6411,9 @@ func testSourceMap(t *testing.T, sb integration.Sandbox) {
64116411

64126412
st := llb.Scratch().Run(
64136413
llb.Shlex("not-exist"),
6414-
sm1.Location([]*pb.Range{{Start: pb.Position{Line: 7}}}),
6415-
sm2.Location([]*pb.Range{{Start: pb.Position{Line: 8}}}),
6416-
sm1.Location([]*pb.Range{{Start: pb.Position{Line: 9}}}),
6414+
sm1.Location([]*pb.Range{{Start: &pb.Position{Line: 7}}}),
6415+
sm2.Location([]*pb.Range{{Start: &pb.Position{Line: 8}}}),
6416+
sm1.Location([]*pb.Range{{Start: &pb.Position{Line: 9}}}),
64176417
)
64186418

64196419
def, err := st.Marshal(sb.Context())
@@ -6466,7 +6466,7 @@ func testSourceMapFromRef(t *testing.T, sb integration.Sandbox) {
64666466
frontend := func(ctx context.Context, c gateway.Client) (*gateway.Result, error) {
64676467
st := llb.Scratch().File(
64686468
llb.Mkdir("foo/bar", 0600), // fails because /foo doesn't exist
6469-
sm.Location([]*pb.Range{{Start: pb.Position{Line: 3, Character: 1}}}),
6469+
sm.Location([]*pb.Range{{Start: &pb.Position{Line: 3, Character: 1}}}),
64706470
)
64716471

64726472
def, err := st.Marshal(sb.Context())

client/diskusage.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,21 @@ func (c *Client) DiskUsage(ctx context.Context, opts ...DiskUsageOption) ([]*Usa
3939
var du []*UsageInfo
4040

4141
for _, d := range resp.Record {
42+
var lastUsedAt *time.Time
43+
if d.LastUsedAt != nil {
44+
t := d.LastUsedAt.AsTime()
45+
lastUsedAt = &t
46+
}
4247
du = append(du, &UsageInfo{
4348
ID: d.ID,
4449
Mutable: d.Mutable,
4550
InUse: d.InUse,
46-
Size: d.Size_,
51+
Size: d.Size,
4752
Parents: d.Parents,
48-
CreatedAt: d.CreatedAt,
53+
CreatedAt: d.CreatedAt.AsTime(),
4954
Description: d.Description,
5055
UsageCount: int(d.UsageCount),
51-
LastUsedAt: d.LastUsedAt,
56+
LastUsedAt: lastUsedAt,
5257
RecordType: UsageRecordType(d.RecordType),
5358
Shared: d.Shared,
5459
})

client/llb/definition.go

+14-9
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import (
55
"sync"
66

77
"github.com/moby/buildkit/solver/pb"
8+
"github.com/moby/buildkit/util"
89
digest "github.com/opencontainers/go-digest"
910
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
1011
"github.com/pkg/errors"
12+
"google.golang.org/protobuf/proto"
1113
)
1214

1315
// DefinitionOp implements llb.Vertex using a marshalled definition.
@@ -40,7 +42,7 @@ func NewDefinitionOp(def *pb.Definition) (*DefinitionOp, error) {
4042
var dgst digest.Digest
4143
for _, dt := range def.Def {
4244
var op pb.Op
43-
if err := (&op).Unmarshal(dt); err != nil {
45+
if err := proto.Unmarshal(dt, &op); err != nil {
4446
return nil, errors.Wrap(err, "failed to parse llb proto op")
4547
}
4648
dgst = digest.FromBytes(dt)
@@ -89,14 +91,14 @@ func NewDefinitionOp(def *pb.Definition) (*DefinitionOp, error) {
8991

9092
var index pb.OutputIndex
9193
if dgst != "" {
92-
index = ops[dgst].Inputs[0].Index
93-
dgst = ops[dgst].Inputs[0].Digest
94+
index = pb.OutputIndex(ops[dgst].Inputs[0].Index)
95+
dgst = digest.Digest(ops[dgst].Inputs[0].Digest)
9496
}
9597

9698
return &DefinitionOp{
9799
ops: ops,
98100
defs: defs,
99-
metas: def.Metadata,
101+
metas: util.FromPointerMap[digest.Digest](def.Metadata),
100102
sources: srcs,
101103
platforms: platforms,
102104
dgst: dgst,
@@ -207,7 +209,10 @@ func (d *DefinitionOp) Inputs() []Output {
207209
for _, input := range op.Inputs {
208210
var vtx *DefinitionOp
209211
d.mu.Lock()
210-
if existingIndexes, ok := d.loadInputCache(input.Digest); ok {
212+
213+
dgst := digest.Digest(input.Digest)
214+
215+
if existingIndexes, ok := d.loadInputCache(dgst); ok {
211216
if int(input.Index) < len(existingIndexes) && existingIndexes[input.Index] != nil {
212217
vtx = existingIndexes[input.Index]
213218
}
@@ -218,19 +223,19 @@ func (d *DefinitionOp) Inputs() []Output {
218223
defs: d.defs,
219224
metas: d.metas,
220225
platforms: d.platforms,
221-
dgst: input.Digest,
222-
index: input.Index,
226+
dgst: dgst,
227+
index: pb.OutputIndex(input.Index),
223228
inputCache: d.inputCache,
224229
sources: d.sources,
225230
}
226-
existingIndexes, _ := d.loadInputCache(input.Digest)
231+
existingIndexes, _ := d.loadInputCache(dgst)
227232
indexDiff := int(input.Index) - len(existingIndexes)
228233
if indexDiff >= 0 {
229234
// make room in the slice for the new index being set
230235
existingIndexes = append(existingIndexes, make([]*DefinitionOp, indexDiff+1)...)
231236
}
232237
existingIndexes[input.Index] = vtx
233-
d.storeInputCache(input.Digest, existingIndexes)
238+
d.storeInputCache(dgst, existingIndexes)
234239
}
235240
d.mu.Unlock()
236241

client/llb/definition_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
digest "github.com/opencontainers/go-digest"
1212
"github.com/stretchr/testify/require"
1313
"golang.org/x/sync/errgroup"
14+
"google.golang.org/protobuf/proto"
1415
)
1516

1617
func TestDefinitionEquivalence(t *testing.T) {
@@ -56,7 +57,9 @@ func TestDefinitionEquivalence(t *testing.T) {
5657
}
5758

5859
for dgst := range def.Metadata {
59-
require.Equal(t, def.Metadata[dgst], def2.Metadata[dgst])
60+
md1 := def.Metadata[dgst]
61+
md2 := def2.Metadata[dgst]
62+
require.True(t, proto.Equal(md1, md2))
6063
}
6164

6265
expectedPlatform, err := tc.state.GetPlatform(ctx)
@@ -108,7 +111,7 @@ func TestDefinitionInputCache(t *testing.T) {
108111

109112
st2 := NewState(op.Output())
110113
marshalDef := &Definition{
111-
Metadata: make(map[digest.Digest]pb.OpMetadata, 0),
114+
Metadata: make(map[digest.Digest]*pb.OpMetadata, 0),
112115
}
113116
constraints := &Constraints{}
114117
smc := newSourceMapCollector()

client/llb/diff.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/moby/buildkit/solver/pb"
77
digest "github.com/opencontainers/go-digest"
8+
"google.golang.org/protobuf/proto"
89
)
910

1011
type DiffOp struct {
@@ -38,36 +39,36 @@ func (m *DiffOp) Marshal(ctx context.Context, constraints *Constraints) (digest.
3839
return "", nil, nil, nil, err
3940
}
4041

41-
proto, md := MarshalConstraints(constraints, &m.constraints)
42-
proto.Platform = nil // diff op is not platform specific
42+
pbc, md := MarshalConstraints(constraints, &m.constraints)
43+
pbc.Platform = nil // diff op is not platform specific
4344

4445
op := &pb.DiffOp{}
4546

46-
op.Lower = &pb.LowerDiffInput{Input: pb.InputIndex(len(proto.Inputs))}
47+
op.Lower = &pb.LowerDiffInput{Input: int64(len(pbc.Inputs))}
4748
if m.lower == nil {
48-
op.Lower.Input = pb.Empty
49+
op.Lower.Input = int64(pb.Empty)
4950
} else {
5051
pbLowerInput, err := m.lower.ToInput(ctx, constraints)
5152
if err != nil {
5253
return "", nil, nil, nil, err
5354
}
54-
proto.Inputs = append(proto.Inputs, pbLowerInput)
55+
pbc.Inputs = append(pbc.Inputs, pbLowerInput)
5556
}
5657

57-
op.Upper = &pb.UpperDiffInput{Input: pb.InputIndex(len(proto.Inputs))}
58+
op.Upper = &pb.UpperDiffInput{Input: int64(len(pbc.Inputs))}
5859
if m.upper == nil {
59-
op.Upper.Input = pb.Empty
60+
op.Upper.Input = int64(pb.Empty)
6061
} else {
6162
pbUpperInput, err := m.upper.ToInput(ctx, constraints)
6263
if err != nil {
6364
return "", nil, nil, nil, err
6465
}
65-
proto.Inputs = append(proto.Inputs, pbUpperInput)
66+
pbc.Inputs = append(pbc.Inputs, pbUpperInput)
6667
}
6768

68-
proto.Op = &pb.Op_Diff{Diff: op}
69+
pbc.Op = &pb.Op_Diff{Diff: op}
6970

70-
dt, err := proto.Marshal()
71+
dt, err := proto.Marshal(pbc)
7172
if err != nil {
7273
return "", nil, nil, nil, err
7374
}

client/llb/exec.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/moby/buildkit/util/system"
1212
digest "github.com/opencontainers/go-digest"
1313
"github.com/pkg/errors"
14+
"google.golang.org/protobuf/proto"
1415
)
1516

1617
func NewExecOp(base State, proxyEnv *ProxyEnv, readOnly bool, c Constraints) *ExecOp {
@@ -325,7 +326,7 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
325326
newInput := true
326327

327328
for i, inp2 := range pop.Inputs {
328-
if *inp == *inp2 {
329+
if proto.Equal(inp, inp2) {
329330
inputIndex = pb.InputIndex(i)
330331
newInput = false
331332
break
@@ -346,10 +347,10 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
346347
}
347348

348349
pm := &pb.Mount{
349-
Input: inputIndex,
350+
Input: int64(inputIndex),
350351
Dest: m.target,
351352
Readonly: m.readonly,
352-
Output: outputIndex,
353+
Output: int64(outputIndex),
353354
Selector: m.selector,
354355
}
355356
if m.cacheID != "" {
@@ -369,7 +370,7 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
369370
if m.tmpfs {
370371
pm.MountType = pb.MountType_TMPFS
371372
pm.TmpfsOpt = &pb.TmpfsOpt{
372-
Size_: m.tmpfsOpt.Size,
373+
Size: m.tmpfsOpt.Size,
373374
}
374375
}
375376
peo.Mounts = append(peo.Mounts, pm)
@@ -413,7 +414,7 @@ func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
413414
peo.Mounts = append(peo.Mounts, pm)
414415
}
415416

416-
dt, err := pop.Marshal()
417+
dt, err := proto.Marshal(pop)
417418
if err != nil {
418419
return "", nil, nil, nil, err
419420
}

0 commit comments

Comments
 (0)