1
0
mirror of https://github.com/moby/buildkit.git synced 2025-07-30 15:03:06 +03:00

lint: add fatcontext

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi
2025-05-01 21:44:14 -07:00
parent e291d88c1b
commit 1e512a15c3
14 changed files with 23 additions and 18 deletions

View File

@ -9,6 +9,7 @@ linters:
- durationcheck - durationcheck
- errname - errname
- errorlint - errorlint
- fatcontext
- forbidigo - forbidigo
- gocritic - gocritic
- gosec - gosec

View File

@ -45,7 +45,7 @@ func (sr *immutableRef) tryComputeOverlayBlob(ctx context.Context, lower, upper
defer func() { defer func() {
if cw != nil { if cw != nil {
ctx = context.WithoutCancel(ctx) ctx := context.WithoutCancel(ctx)
// after commit success cw will be set to nil, if cw isn't nil, error // after commit success cw will be set to nil, if cw isn't nil, error
// happened before commit, we should abort this ingest, and because the // happened before commit, we should abort this ingest, and because the
// error may incured by ctx cancel, use a new context here. And since // error may incured by ctx cancel, use a new context here. And since

View File

@ -41,6 +41,7 @@ func AttachAppContext(app *cli.App) error {
} }
} }
ctx := ctx
ctx, span = tracer.Start(ctx, name, trace.WithAttributes( ctx, span = tracer.Start(ctx, name, trace.WithAttributes(
attribute.StringSlice("command", os.Args), attribute.StringSlice("command", os.Args),
)) ))

View File

@ -20,7 +20,7 @@ import (
func ReadAll(ctx context.Context, s session.Group, att exporter.Attestation) ([]byte, error) { func ReadAll(ctx context.Context, s session.Group, att exporter.Attestation) ([]byte, error) {
var content []byte var content []byte
if att.ContentFunc != nil { if att.ContentFunc != nil {
data, err := att.ContentFunc() data, err := att.ContentFunc(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -166,7 +166,7 @@ func unbundle(root string, bundle exporter.Attestation) ([]exporter.Attestation,
Kind: gatewaypb.AttestationKind_InToto, Kind: gatewaypb.AttestationKind_InToto,
Metadata: bundle.Metadata, Metadata: bundle.Metadata,
Path: path.Join(bundle.Path, entry.Name()), Path: path.Join(bundle.Path, entry.Name()),
ContentFunc: func() ([]byte, error) { return predicate, nil }, ContentFunc: func(context.Context) ([]byte, error) { return predicate, nil },
InToto: result.InTotoAttestation{ InToto: result.InTotoAttestation{
PredicateType: stmt.PredicateType, PredicateType: stmt.PredicateType,
Subjects: subjects, Subjects: subjects,

View File

@ -118,7 +118,7 @@ func supplementSBOM(ctx context.Context, s session.Group, target cache.Immutable
return exporter.Attestation{ return exporter.Attestation{
Kind: att.Kind, Kind: att.Kind,
Path: att.Path, Path: att.Path,
ContentFunc: func() ([]byte, error) { return content, nil }, ContentFunc: func(context.Context) ([]byte, error) { return content, nil },
InToto: att.InToto, InToto: att.InToto,
}, nil }, nil
} }

View File

@ -40,7 +40,7 @@ func (s *server) run(ctx context.Context, l net.Listener, id string) error {
opts := make(map[string][]string) opts := make(map[string][]string)
opts[KeySSHID] = []string{id} opts[KeySSHID] = []string{id}
ctx = metadata.NewOutgoingContext(ctx, opts) ctx := metadata.NewOutgoingContext(ctx, opts)
stream, err := client.ForwardAgent(ctx) stream, err := client.ForwardAgent(ctx)
if err != nil { if err != nil {

View File

@ -66,8 +66,8 @@ func ProvenanceProcessor(attrs map[string]string) llbsolver.Processor {
PredicateType: slsa02.PredicateSLSAProvenance, PredicateType: slsa02.PredicateSLSAProvenance,
}, },
Path: filename, Path: filename,
ContentFunc: func() ([]byte, error) { ContentFunc: func(ctx context.Context) ([]byte, error) {
pr, err := pc.Predicate() pr, err := pc.Predicate(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -333,7 +333,7 @@ type ProvenanceCreator struct {
pr *provenancetypes.ProvenancePredicate pr *provenancetypes.ProvenancePredicate
j *solver.Job j *solver.Job
sampler *resources.SysSampler sampler *resources.SysSampler
addLayers func() error addLayers func(context.Context) error
} }
func NewProvenanceCreator(ctx context.Context, cp *provenance.Capture, res solver.ResultProxy, attrs map[string]string, j *solver.Job, usage *resources.SysSampler) (*ProvenanceCreator, error) { func NewProvenanceCreator(ctx context.Context, cp *provenance.Capture, res solver.ResultProxy, attrs map[string]string, j *solver.Job, usage *resources.SysSampler) (*ProvenanceCreator, error) {
@ -377,7 +377,7 @@ func NewProvenanceCreator(ctx context.Context, cp *provenance.Capture, res solve
pr.Builder.ID = attrs["builder-id"] pr.Builder.ID = attrs["builder-id"]
var addLayers func() error var addLayers func(context.Context) error
switch mode { switch mode {
case "min": case "min":
@ -408,7 +408,7 @@ func NewProvenanceCreator(ctx context.Context, cp *provenance.Capture, res solve
return nil, errors.Errorf("invalid worker ref %T", r.Sys()) return nil, errors.Errorf("invalid worker ref %T", r.Sys())
} }
addLayers = func() error { addLayers = func(ctx context.Context) error {
e := newCacheExporter() e := newCacheExporter()
if wref.ImmutableRef != nil { if wref.ImmutableRef != nil {
@ -460,12 +460,12 @@ func NewProvenanceCreator(ctx context.Context, cp *provenance.Capture, res solve
return pc, nil return pc, nil
} }
func (p *ProvenanceCreator) Predicate() (*provenancetypes.ProvenancePredicate, error) { func (p *ProvenanceCreator) Predicate(ctx context.Context) (*provenancetypes.ProvenancePredicate, error) {
end := p.j.RegisterCompleteTime() end := p.j.RegisterCompleteTime()
p.pr.Metadata.BuildFinishedOn = &end p.pr.Metadata.BuildFinishedOn = &end
if p.addLayers != nil { if p.addLayers != nil {
if err := p.addLayers(); err != nil { if err := p.addLayers(ctx); err != nil {
return nil, err return nil, err
} }
} }

View File

@ -237,7 +237,7 @@ func (s *Solver) recordBuildHistory(ctx context.Context, id string, req frontend
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
pr, err := prc.Predicate() pr, err := prc.Predicate(ctx)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -756,7 +756,7 @@ func runCacheExporters(ctx context.Context, exporters []RemoteCacheExporter, j *
err = inBuilderContext(ctx, j, exp.Name(), id, func(ctx context.Context, _ session.Group) error { err = inBuilderContext(ctx, j, exp.Name(), id, func(ctx context.Context, _ session.Group) error {
prepareDone := progress.OneOff(ctx, "preparing build cache for export") prepareDone := progress.OneOff(ctx, "preparing build cache for export")
if err := result.EachRef(cached, inp, func(res solver.CachedResult, ref cache.ImmutableRef) error { if err := result.EachRef(cached, inp, func(res solver.CachedResult, ref cache.ImmutableRef) error {
ctx = withDescHandlerCacheOpts(ctx, ref) ctx := withDescHandlerCacheOpts(ctx, ref)
// Configure compression // Configure compression
compressionConfig := exp.Config().Compression compressionConfig := exp.Config().Compression

View File

@ -1,6 +1,8 @@
package result package result
import ( import (
"context"
pb "github.com/moby/buildkit/frontend/gateway/pb" pb "github.com/moby/buildkit/frontend/gateway/pb"
digest "github.com/opencontainers/go-digest" digest "github.com/opencontainers/go-digest"
) )
@ -23,7 +25,7 @@ type Attestation[T any] struct {
Ref T Ref T
Path string Path string
ContentFunc func() ([]byte, error) ContentFunc func(context.Context) ([]byte, error)
InToto InTotoAttestation InToto InTotoAttestation
} }

View File

@ -74,6 +74,7 @@ func (e *Engine) Evaluate(ctx context.Context, op *pb.SourceOp) (bool, error) {
return mutated, errors.Wrapf(ErrTooManyOps, "too many mutations on a single source") return mutated, errors.Wrapf(ErrTooManyOps, "too many mutations on a single source")
} }
ctx := ctx
if i == 0 { if i == 0 {
ctx = bklog.WithLogger(ctx, bklog.G(ctx).WithField("orig", op)) ctx = bklog.WithLogger(ctx, bklog.G(ctx).WithField("orig", op))
} else { } else {

View File

@ -25,11 +25,11 @@ func Context() context.Context {
ctx := context.Background() ctx := context.Background()
for _, f := range inits { for _, f := range inits {
ctx = f(ctx) ctx = f(ctx) //nolint:fatcontext
} }
ctx, cancel := context.WithCancelCause(ctx) ctx, cancel := context.WithCancelCause(ctx)
appContextCache = ctx appContextCache = ctx //nolint:fatcontext
go func() { go func() {
for { for {

View File

@ -86,7 +86,7 @@ func withDetachedNetNSIfAny(ctx context.Context, fn func(context.Context) error)
detachedNetNS := filepath.Join(stateDir, "netns") detachedNetNS := filepath.Join(stateDir, "netns")
if _, err := os.Lstat(detachedNetNS); !errors.Is(err, os.ErrNotExist) { if _, err := os.Lstat(detachedNetNS); !errors.Is(err, os.ErrNotExist) {
return ns.WithNetNSPath(detachedNetNS, func(_ ns.NetNS) error { return ns.WithNetNSPath(detachedNetNS, func(_ ns.NetNS) error {
ctx = context.WithValue(ctx, contextKeyDetachedNetNS, detachedNetNS) ctx := context.WithValue(ctx, contextKeyDetachedNetNS, detachedNetNS)
bklog.G(ctx).Debugf("Entering RootlessKit's detached netns %q", detachedNetNS) bklog.G(ctx).Debugf("Entering RootlessKit's detached netns %q", detachedNetNS)
err2 := fn(ctx) err2 := fn(ctx)
bklog.G(ctx).WithError(err2).Debugf("Leaving RootlessKit's detached netns %q", detachedNetNS) bklog.G(ctx).WithError(err2).Debugf("Leaving RootlessKit's detached netns %q", detachedNetNS)