Skip to content

Commit

Permalink
Fix lint issues
Browse files Browse the repository at this point in the history
google.golang.org/protobuf made all proto structs with a mutex to prevent
copies.

Signed-off-by: Kazuyoshi Kato <[email protected]>
  • Loading branch information
kzys committed Jan 20, 2024
1 parent 6bdaa2a commit 68d8112
Show file tree
Hide file tree
Showing 43 changed files with 194 additions and 163 deletions.
8 changes: 6 additions & 2 deletions cache/contenthash/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,12 @@ func (cc *cacheContext) HandleChange(kind fsutil.ChangeKind, p string, fi os.Fil
ln := path.Join("/", filepath.ToSlash(stat.Linkname))
v, ok := cc.txn.Get(convertPathToKey([]byte(ln)))
if ok {
cp := *v.(*CacheRecord)
cr = &cp
cp := v.(*CacheRecord)
cr = &CacheRecord{
Digest: cp.Digest,
Type: cp.Type,
Linkname: cp.Linkname,
}
}
cc.linkMap[ln] = append(cc.linkMap[ln], k)
}
Expand Down
6 changes: 3 additions & 3 deletions client/llb/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type DefinitionOp struct {
mu sync.Mutex
ops map[digest.Digest]*pb.Op
defs map[digest.Digest][]byte
metas map[digest.Digest]pb.OpMetadata
metas map[digest.Digest]*pb.OpMetadata
sources map[digest.Digest][]*SourceLocation
platforms map[digest.Digest]*ocispecs.Platform
dgst digest.Digest
Expand Down Expand Up @@ -101,7 +101,7 @@ func NewDefinitionOp(def *pb.Definition) (*DefinitionOp, error) {
return &DefinitionOp{
ops: ops,
defs: defs,
metas: util.FromPointerMap[digest.Digest](def.Metadata),
metas: util.FromStringMap[digest.Digest](def.Metadata),
sources: srcs,
platforms: platforms,
dgst: dgst,
Expand Down Expand Up @@ -168,7 +168,7 @@ func (d *DefinitionOp) Marshal(ctx context.Context, c *Constraints) (digest.Dige
defer d.mu.Unlock()

meta := d.metas[d.dgst]
return d.dgst, d.defs[d.dgst], &meta, d.sources[d.dgst], nil
return d.dgst, d.defs[d.dgst], meta, d.sources[d.dgst], nil
}

func (d *DefinitionOp) Output() Output {
Expand Down
12 changes: 6 additions & 6 deletions client/llb/fileop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,24 +693,24 @@ func TestFileCreatedTime(t *testing.T) {
require.Equal(t, dt3.UnixNano(), copy.Timestamp)
}

func parseDef(t *testing.T, def [][]byte) (map[digest.Digest]pb.Op, []pb.Op) {
m := map[digest.Digest]pb.Op{}
arr := make([]pb.Op, 0, len(def))
func parseDef(t *testing.T, def [][]byte) (map[digest.Digest]*pb.Op, []*pb.Op) {
m := map[digest.Digest]*pb.Op{}
arr := make([]*pb.Op, 0, len(def))

for _, dt := range def {
var op pb.Op
err := proto.Unmarshal(dt, &op)
require.NoError(t, err)
dgst := digest.FromBytes(dt)
m[dgst] = op
arr = append(arr, op)
m[dgst] = &op
arr = append(arr, &op)
// fmt.Printf(":: %T %+v\n", op.Op, op)
}

return m, arr
}

func last(t *testing.T, arr []pb.Op) (digest.Digest, int) {
func last(t *testing.T, arr []*pb.Op) (digest.Digest, int) {
require.True(t, len(arr) > 1)

op := arr[len(arr)-1]
Expand Down
3 changes: 3 additions & 0 deletions client/llb/llbbuild/llbbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func (b *build) Marshal(ctx context.Context, c *llb.Constraints) (digest.Digest,
pbo.Attrs[pb.AttrLLBDefinitionFilename] = b.info.DefinitionFilename
}

if b.constraints.Metadata == nil {
b.constraints.Metadata = &pb.OpMetadata{}
}
if b.constraints.Metadata.Caps == nil {
b.constraints.Metadata.Caps = make(map[string]bool)
}
Expand Down
2 changes: 1 addition & 1 deletion client/llb/llbtest/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func toOp(e solver.Edge) *pb.Op {

func platform(e solver.Edge) ocispecs.Platform {
op := toOp(e)
p := *op.Platform
p := op.Platform
return ocispecs.Platform{
OS: p.OS,
Architecture: p.Architecture,
Expand Down
6 changes: 3 additions & 3 deletions client/llb/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ func MarshalConstraints(base, override *Constraints) (*pb.Op, *pb.OpMetadata) {
}

c.WorkerConstraints = append(c.WorkerConstraints, override.WorkerConstraints...)
md := mergeMetadata(&c.Metadata, &override.Metadata)
c.Metadata = *md
md := mergeMetadata(c.Metadata, override.Metadata)
c.Metadata = md

if c.Platform == nil {
defaultPlatform := platforms.Normalize(platforms.DefaultSpec())
Expand All @@ -112,7 +112,7 @@ func MarshalConstraints(base, override *Constraints) (*pb.Op, *pb.OpMetadata) {
Constraints: &pb.WorkerConstraints{
Filter: c.WorkerConstraints,
},
}, &c.Metadata
}, c.Metadata
}

type MarshalCache struct {
Expand Down
8 changes: 4 additions & 4 deletions client/llb/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func ulimit(name UlimitName, soft int64, hard int64) StateOption {
if err != nil {
return nil, err
}
return append(v, pb.Ulimit{
return append(v, &pb.Ulimit{
Name: string(name),
Soft: soft,
Hard: hard,
Expand All @@ -270,14 +270,14 @@ func ulimit(name UlimitName, soft int64, hard int64) StateOption {
}
}

func getUlimit(s State) func(context.Context, *Constraints) ([]pb.Ulimit, error) {
return func(ctx context.Context, c *Constraints) ([]pb.Ulimit, error) {
func getUlimit(s State) func(context.Context, *Constraints) ([]*pb.Ulimit, error) {
return func(ctx context.Context, c *Constraints) ([]*pb.Ulimit, error) {
v, err := s.getValue(keyUlimit)(ctx, c)
if err != nil {
return nil, err
}
if v != nil {
return v.([]pb.Ulimit), nil
return v.([]*pb.Ulimit), nil
}
return nil, nil
}
Expand Down
3 changes: 3 additions & 0 deletions client/llb/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,9 @@ func platformSpecificSource(id string) bool {
}

func addCap(c *Constraints, id apicaps.CapID) {
if c.Metadata == nil {
c.Metadata = &pb.OpMetadata{}
}
if c.Metadata.Caps == nil {
c.Metadata.Caps = make(map[string]bool)
}
Expand Down
26 changes: 25 additions & 1 deletion client/llb/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,12 @@ func (fn constraintsOptFunc) SetGitOption(gi *GitInfo) {
}

func mergeMetadata(m1, m2 *pb.OpMetadata) *pb.OpMetadata {
if m1 == nil {
m1 = &pb.OpMetadata{}
}
if m2 == nil {
m2 = &pb.OpMetadata{}
}
if m2.IgnoreCache {
m1.IgnoreCache = true
}
Expand Down Expand Up @@ -597,11 +603,17 @@ func mergeMetadata(m1, m2 *pb.OpMetadata) *pb.OpMetadata {
}

var IgnoreCache = constraintsOptFunc(func(c *Constraints) {
if c.Metadata == nil {
c.Metadata = &pb.OpMetadata{}
}
c.Metadata.IgnoreCache = true
})

func WithDescription(m map[string]string) ConstraintsOpt {
return constraintsOptFunc(func(c *Constraints) {
if c.Metadata == nil {
c.Metadata = &pb.OpMetadata{}
}
if c.Metadata.Description == nil {
c.Metadata.Description = map[string]string{}
}
Expand All @@ -624,6 +636,9 @@ func WithCustomNamef(name string, a ...interface{}) ConstraintsOpt {
// WithExportCache forces results for this vertex to be exported with the cache
func WithExportCache() ConstraintsOpt {
return constraintsOptFunc(func(c *Constraints) {
if c.Metadata == nil {
c.Metadata = &pb.OpMetadata{}
}
c.Metadata.ExportCache = &pb.ExportCache{Value: true}
})
}
Expand All @@ -632,6 +647,9 @@ func WithExportCache() ConstraintsOpt {
// the cache
func WithoutExportCache() ConstraintsOpt {
return constraintsOptFunc(func(c *Constraints) {
if c.Metadata == nil {
c.Metadata = &pb.OpMetadata{}
}
// ExportCache with value false means to disable exporting
c.Metadata.ExportCache = &pb.ExportCache{Value: false}
})
Expand All @@ -641,6 +659,9 @@ func WithoutExportCache() ConstraintsOpt {
// the default defined by the build configuration.
func WithoutDefaultExportCache() ConstraintsOpt {
return constraintsOptFunc(func(c *Constraints) {
if c.Metadata == nil {
c.Metadata = &pb.OpMetadata{}
}
// nil means no vertex based config has been set
c.Metadata.ExportCache = nil
})
Expand All @@ -664,7 +685,7 @@ func (cw *constraintsWrapper) applyConstraints(f func(c *Constraints)) {
type Constraints struct {
Platform *ocispecs.Platform
WorkerConstraints []string
Metadata pb.OpMetadata
Metadata *pb.OpMetadata
LocalUniqueID string
Caps *apicaps.CapSet
SourceLocations []*SourceLocation
Expand All @@ -684,6 +705,9 @@ func LocalUniqueID(v string) ConstraintsOpt {

func ProgressGroup(id, name string, weak bool) ConstraintsOpt {
return constraintsOptFunc(func(c *Constraints) {
if c.Metadata == nil {
c.Metadata = &pb.OpMetadata{}
}
c.Metadata.ProgressGroup = &pb.ProgressGroup{Id: id, Name: name, Weak: weak}
})
}
Expand Down
3 changes: 1 addition & 2 deletions client/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
controlapi "github.com/moby/buildkit/api/services/control"
apitypes "github.com/moby/buildkit/api/types"
"github.com/moby/buildkit/solver/pb"
"github.com/moby/buildkit/util"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -40,7 +39,7 @@ func (c *Client) ListWorkers(ctx context.Context, opts ...ListWorkersOption) ([]
wi = append(wi, &WorkerInfo{
ID: w.ID,
Labels: w.Labels,
Platforms: pb.ToSpecPlatforms(util.FromPointerSlice(w.Platforms)),
Platforms: pb.ToSpecPlatforms(w.Platforms),
GCPolicy: fromAPIGCPolicy(w.GCPolicy),
BuildkitVersion: fromAPIBuildkitVersion(w.BuildkitVersion),
})
Expand Down
4 changes: 2 additions & 2 deletions cmd/buildctl/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ func read(r io.Reader, clicontext *cli.Context) (*llb.Definition, error) {
if !ok {
opMetadata = &pb.OpMetadata{}
}
c := llb.Constraints{Metadata: *opMetadata}
c := llb.Constraints{Metadata: opMetadata}
llb.IgnoreCache(&c)
def.Metadata[dgst] = &c.Metadata
def.Metadata[dgst] = c.Metadata
}
}
return def, nil
Expand Down
8 changes: 4 additions & 4 deletions cmd/buildctl/debug/dumpllb.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ func dumpLLB(clicontext *cli.Context) error {
}

type llbOp struct {
Op pb.Op
Op *pb.Op
Digest digest.Digest
OpMetadata pb.OpMetadata
OpMetadata *pb.OpMetadata
}

func loadLLB(r io.Reader) ([]llbOp, error) {
Expand All @@ -75,7 +75,7 @@ func loadLLB(r io.Reader) ([]llbOp, error) {
return nil, errors.Wrap(err, "failed to parse op")
}
dgst := digest.FromBytes(dt)
ent := llbOp{Op: op, Digest: dgst, OpMetadata: *def.Metadata[dgst]}
ent := llbOp{Op: &op, Digest: dgst, OpMetadata: def.Metadata[dgst]}
ops = append(ops, ent)
}
return ops, nil
Expand Down Expand Up @@ -104,7 +104,7 @@ func writeDot(ops []llbOp, w io.Writer) {
}
}

func attr(dgst digest.Digest, op pb.Op) (string, string) {
func attr(dgst digest.Digest, op *pb.Op) (string, string) {
switch op := op.Op.(type) {
case *pb.Op_Source:
return op.Source.Identifier, "ellipse"
Expand Down
6 changes: 3 additions & 3 deletions control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ func (c *Controller) ListWorkers(ctx context.Context, r *controlapi.ListWorkersR
resp.Record = append(resp.Record, &apitypes.WorkerRecord{
ID: w.ID(),
Labels: w.Labels(),
Platforms: util.PointerSlice(pb.PlatformsFromSpec(w.Platforms(true))),
Platforms: pb.PlatformsFromSpec(w.Platforms(true)),
GCPolicy: toPBGCPolicy(w.GCPolicy()),
BuildkitVersion: toPBBuildkitVersion(w.BuildkitVersion()),
})
Expand Down Expand Up @@ -646,7 +646,7 @@ func findDuplicateCacheOptions(cacheOpts []*controlapi.CacheOptionsEntry) ([]*co
seen := map[string]*controlapi.CacheOptionsEntry{}
duplicate := map[string]struct{}{}
for _, opt := range cacheOpts {
k, err := cacheOptKey(*opt)
k, err := cacheOptKey(opt)
if err != nil {
return nil, err
}
Expand All @@ -663,7 +663,7 @@ func findDuplicateCacheOptions(cacheOpts []*controlapi.CacheOptionsEntry) ([]*co
return duplicates, nil
}

func cacheOptKey(opt controlapi.CacheOptionsEntry) (string, error) {
func cacheOptKey(opt *controlapi.CacheOptionsEntry) (string, error) {
if opt.Type == "registry" && opt.Attrs["ref"] != "" {
return opt.Attrs["ref"], nil
}
Expand Down
2 changes: 1 addition & 1 deletion exporter/local/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func CreateFS(ctx context.Context, sessionID string, k string, ref cache.Immutab
}
names[name] = struct{}{}

st := fstypes.Stat{
st := &fstypes.Stat{
Mode: 0600,
Path: name,
ModTime: defaultTime.UnixNano(),
Expand Down
2 changes: 1 addition & 1 deletion frontend/dockerfile/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,5 +275,5 @@ func wrapSource(err error, sm *llb.SourceMap, ranges []parser.Range) error {
},
})
}
return errdefs.WithSource(err, s)
return errdefs.WithSource(err, &s)
}
2 changes: 1 addition & 1 deletion frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ type dispatchOpt struct {
buildPlatforms []ocispecs.Platform
extraHosts []llb.HostIP
shmSize int64
ulimit []pb.Ulimit
ulimit []*pb.Ulimit
cgroupParent string
llbCaps *apicaps.CapSet
sourceMap *llb.SourceMap
Expand Down
6 changes: 3 additions & 3 deletions frontend/dockerui/attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ func parseShmSize(v string) (int64, error) {
return kb, nil
}

func parseUlimits(v string) ([]pb.Ulimit, error) {
func parseUlimits(v string) ([]*pb.Ulimit, error) {
if v == "" {
return nil, nil
}
out := make([]pb.Ulimit, 0)
out := make([]*pb.Ulimit, 0)
csvReader := csv.NewReader(strings.NewReader(v))
fields, err := csvReader.Read()
if err != nil {
Expand All @@ -90,7 +90,7 @@ func parseUlimits(v string) ([]pb.Ulimit, error) {
if err != nil {
return nil, err
}
out = append(out, pb.Ulimit{
out = append(out, &pb.Ulimit{
Name: ulimit.Name,
Soft: ulimit.Soft,
Hard: ulimit.Hard,
Expand Down
4 changes: 2 additions & 2 deletions frontend/dockerui/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Config struct {
NetworkMode pb.NetMode
ShmSize int64
Target string
Ulimits []pb.Ulimit
Ulimits []*pb.Ulimit

CacheImports []client.CacheOptionsEntry
TargetPlatforms []ocispecs.Platform // nil means default
Expand Down Expand Up @@ -218,7 +218,7 @@ func (bc *Client) init() error {
var cacheImports []client.CacheOptionsEntry
// new API
if cacheImportsStr := opts[keyCacheImports]; cacheImportsStr != "" {
var cacheImportsUM []controlapi.CacheOptionsEntry
var cacheImportsUM []*controlapi.CacheOptionsEntry
if err := json.Unmarshal([]byte(cacheImportsStr), &cacheImportsUM); err != nil {
return errors.Wrapf(err, "failed to unmarshal %s (%q)", keyCacheImports, cacheImportsStr)
}
Expand Down
Loading

0 comments on commit 68d8112

Please sign in to comment.