Skip to content

Commit

Permalink
refactor to use util/bklog instead of using logurs directly
Browse files Browse the repository at this point in the history
Signed-off-by: Morlay <morlay.null@gmail.com>
  • Loading branch information
morlay committed Jul 13, 2021
1 parent 3f0d4a4 commit b715f9a
Show file tree
Hide file tree
Showing 41 changed files with 227 additions and 177 deletions.
4 changes: 2 additions & 2 deletions cache/migrate_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
"github.com/containerd/containerd/snapshots"
"github.com/moby/buildkit/cache/metadata"
"github.com/moby/buildkit/snapshot"
"github.com/moby/buildkit/util/bklog"
digest "github.com/opencontainers/go-digest"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

func migrateChainID(si *metadata.StorageItem, all map[string]*metadata.StorageItem) (digest.Digest, digest.Digest, error) {
Expand Down Expand Up @@ -252,7 +252,7 @@ func MigrateV2(ctx context.Context, from, to string, cs content.Store, s snapsho
}

for _, item := range byID {
logrus.Infof("migrated %s parent:%q snapshot:%v committed:%v blob:%v diffid:%v chainID:%v blobChainID:%v",
bklog.G(ctx).Infof("migrated %s parent:%q snapshot:%v committed:%v blob:%v diffid:%v chainID:%v blobChainID:%v",
item.ID(), getParent(item), getSnapshotID(item), getCommitted(item), getBlob(item), getDiffID(item), getChainID(item), getBlobChainID(item))
}

Expand Down
2 changes: 1 addition & 1 deletion cache/remotecache/gha/gha.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func (ci *importer) Resolve(ctx context.Context, _ specs.Descriptor, id string,
if err != nil {
return nil, err
}
cms = append(cms, solver.NewCacheManager(id, keysStorage, resultStorage))
cms = append(cms, solver.NewCacheManager(ctx, id, keysStorage, resultStorage))
}

return solver.NewCombinedCacheManager(cms, nil), nil
Expand Down
4 changes: 2 additions & 2 deletions cache/remotecache/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (ci *contentCacheImporter) Resolve(ctx context.Context, desc ocispec.Descri
if err != nil {
return nil, err
}
return solver.NewCacheManager(id, keysStorage, resultStorage), nil
return solver.NewCacheManager(ctx, id, keysStorage, resultStorage), nil
}

func readBlob(ctx context.Context, provider content.Provider, desc ocispec.Descriptor) ([]byte, error) {
Expand Down Expand Up @@ -229,7 +229,7 @@ func (ci *contentCacheImporter) importInlineCache(ctx context.Context, dt []byte
if err != nil {
return nil, err
}
cms = append(cms, solver.NewCacheManager(id, keysStorage, resultStorage))
cms = append(cms, solver.NewCacheManager(ctx, id, keysStorage, resultStorage))
}

return solver.NewCombinedCacheManager(cms, nil), nil
Expand Down
12 changes: 6 additions & 6 deletions client/solve.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"github.com/moby/buildkit/session/filesync"
"github.com/moby/buildkit/session/grpchijack"
"github.com/moby/buildkit/solver/pb"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/entitlements"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
fstypes "github.com/tonistiigi/fsutil/types"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -109,7 +109,7 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
}
}

cacheOpt, err := parseCacheOptions(opt)
cacheOpt, err := parseCacheOptions(ctx, opt)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -181,7 +181,7 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
<-time.After(3 * time.Second)
cancelStatus()
}()
logrus.Debugf("stopping session")
bklog.G(ctx).Debugf("stopping session")
s.Close()
}()
var pbd *pb.Definition
Expand Down Expand Up @@ -370,7 +370,7 @@ type cacheOptions struct {
frontendAttrs map[string]string
}

func parseCacheOptions(opt SolveOpt) (*cacheOptions, error) {
func parseCacheOptions(ctx context.Context, opt SolveOpt) (*cacheOptions, error) {
var (
cacheExports []*controlapi.CacheOptionsEntry
cacheImports []*controlapi.CacheOptionsEntry
Expand Down Expand Up @@ -423,14 +423,14 @@ func parseCacheOptions(opt SolveOpt) (*cacheOptions, error) {
}
cs, err := contentlocal.NewStore(csDir)
if err != nil {
logrus.Warning("local cache import at " + csDir + " not found due to err: " + err.Error())
bklog.G(ctx).Warning("local cache import at " + csDir + " not found due to err: " + err.Error())
continue
}
// if digest is not specified, load from "latest" tag
if attrs["digest"] == "" {
idx, err := ociindex.ReadIndexJSONFileLocked(filepath.Join(csDir, "index.json"))
if err != nil {
logrus.Warning("local cache import at " + csDir + " not found due to err: " + err.Error())
bklog.G(ctx).Warning("local cache import at " + csDir + " not found due to err: " + err.Error())
continue
}
for _, m := range idx.Manifests {
Expand Down
10 changes: 7 additions & 3 deletions cmd/buildkitd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"time"

"github.com/BurntSushi/toml"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/pkg/seed"
"github.com/containerd/containerd/pkg/userns"
"github.com/containerd/containerd/platforms"
Expand Down Expand Up @@ -45,8 +46,8 @@ import (
"github.com/moby/buildkit/util/appcontext"
"github.com/moby/buildkit/util/appdefaults"
"github.com/moby/buildkit/util/archutil"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/grpcerrors"
_ "github.com/moby/buildkit/util/log"
"github.com/moby/buildkit/util/profiler"
"github.com/moby/buildkit/util/resolver"
"github.com/moby/buildkit/util/stack"
Expand Down Expand Up @@ -76,6 +77,9 @@ func init() {

seed.WithTimeAndRand()
reexec.Init()

// overwrites containerd/log.G
log.G = bklog.GetLogger
}

var propagators = propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})
Expand Down Expand Up @@ -296,10 +300,10 @@ func main() {
err = ctx.Err()
}

logrus.Infof("stopping server")
bklog.G(ctx).Infof("stopping server")
if os.Getenv("NOTIFY_SOCKET") != "" {
notified, notifyErr := sddaemon.SdNotify(false, sddaemon.SdNotifyStopping)
logrus.Debugf("SdNotifyStopping notified=%v, err=%v", notified, notifyErr)
bklog.G(ctx).Debugf("SdNotifyStopping notified=%v, err=%v", notified, notifyErr)
}
server.GracefulStop()

Expand Down
3 changes: 2 additions & 1 deletion cmd/buildkitd/main_containerd_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"context"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -249,7 +250,7 @@ func containerdWorkerInitializer(c *cli.Context, common workerInitializerOpt) ([
}
opt.Platforms = platforms
}
w, err := base.NewWorker(opt)
w, err := base.NewWorker(context.TODO(), opt)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/buildkitd/main_oci_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func ociWorkerInitializer(c *cli.Context, common workerInitializerOpt) ([]worker
}
opt.Platforms = platforms
}
w, err := base.NewWorker(opt)
w, err := base.NewWorker(context.TODO(), opt)
if err != nil {
return nil, err
}
Expand Down
33 changes: 18 additions & 15 deletions control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"sync/atomic"
"time"

"github.com/moby/buildkit/util/bklog"

controlapi "github.com/moby/buildkit/api/services/control"
apitypes "github.com/moby/buildkit/api/types"
"github.com/moby/buildkit/cache/remotecache"
Expand All @@ -23,7 +25,6 @@ import (
"github.com/moby/buildkit/util/tracing/transform"
"github.com/moby/buildkit/worker"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
tracev1 "go.opentelemetry.io/proto/otlp/collector/trace/v1"
v1 "go.opentelemetry.io/proto/otlp/collector/trace/v1"
Expand Down Expand Up @@ -57,7 +58,7 @@ type Controller struct { // TODO: ControlService
}

func NewController(opt Opt) (*Controller, error) {
cache := solver.NewCacheManager("local", opt.CacheKeyStorage, worker.NewCacheResultStorage(opt.WorkerController))
cache := solver.NewCacheManager(context.TODO(), "local", opt.CacheKeyStorage, worker.NewCacheResultStorage(opt.WorkerController))

gatewayForwarder := controlgateway.NewGatewayForwarder()

Expand Down Expand Up @@ -142,7 +143,7 @@ func (c *Controller) Prune(req *controlapi.PruneRequest, stream controlapi.Contr
ReleaseUnreferenced() error
}); ok {
if err := c.ReleaseUnreferenced(); err != nil {
logrus.Errorf("failed to release cache metadata: %+v", err)
bklog.G(ctx).Errorf("failed to release cache metadata: %+v", err)
}
}
}
Expand Down Expand Up @@ -284,7 +285,11 @@ func (c *Controller) Solve(ctx context.Context, req *controlapi.SolveRequest) (*
if err != nil {
return nil, err
}
cacheExportMode = parseCacheExportMode(e.Attrs["mode"])
if exportMode, supported := parseCacheExportMode(e.Attrs["mode"]); !supported {
bklog.G(ctx).Debugf("skipping invalid cache export mode: %s", e.Attrs["mode"])
} else {
cacheExportMode = exportMode
}
}
for _, im := range req.Cache.Imports {
cacheImports = append(cacheImports, frontend.CacheOptionsEntry{
Expand Down Expand Up @@ -384,7 +389,8 @@ func (c *Controller) Status(req *controlapi.StatusRequest, stream controlapi.Con
}

func (c *Controller) Session(stream controlapi.Control_SessionServer) error {
logrus.Debugf("session started")
bklog.G(stream.Context()).Debugf("session started")

conn, closeCh, opts := grpchijack.Hijack(stream)
defer conn.Close()

Expand All @@ -395,7 +401,7 @@ func (c *Controller) Session(stream controlapi.Control_SessionServer) error {
}()

err := c.opt.SessionManager.HandleConn(ctx, conn, opts)
logrus.Debugf("session finished: %v", err)
bklog.G(ctx).Debugf("session finished: %v", err)
return err
}

Expand Down Expand Up @@ -451,25 +457,22 @@ func (c *Controller) gc() {
err = eg.Wait()
close(ch)
if err != nil {
logrus.Errorf("gc error: %+v", err)
bklog.G(ctx).Errorf("gc error: %+v", err)
}
<-done
if size > 0 {
logrus.Debugf("gc cleaned up %d bytes", size)
bklog.G(ctx).Debugf("gc cleaned up %d bytes", size)
}
}

func parseCacheExportMode(mode string) solver.CacheExportMode {
func parseCacheExportMode(mode string) (solver.CacheExportMode, bool) {
switch mode {
case "min":
return solver.CacheExportModeMin
return solver.CacheExportModeMin, true
case "max":
return solver.CacheExportModeMax
case "":
default:
logrus.Debugf("skipping invalid cache export mode: %s", mode)
return solver.CacheExportModeMax, true
}
return solver.CacheExportModeMin
return solver.CacheExportModeMin, false
}

func toPBGCPolicy(in []client.PruneInfo) []*apitypes.GCPolicy {
Expand Down
7 changes: 4 additions & 3 deletions executor/containerdexecutor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"syscall"
"time"

"github.com/moby/buildkit/util/bklog"

"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/mount"
Expand All @@ -26,7 +28,6 @@ import (
"github.com/moby/buildkit/util/network"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

type containerdExecutor struct {
Expand Down Expand Up @@ -153,7 +154,7 @@ func (w *containerdExecutor) Run(ctx context.Context, id string, root executor.M
defer namespace.Close()

if meta.NetMode == pb.NetMode_HOST {
logrus.Info("enabling HostNetworking")
bklog.G(ctx).Info("enabling HostNetworking")
}

opts := []containerdoci.SpecOpts{oci.WithUIDGID(uid, gid, sgids)}
Expand Down Expand Up @@ -364,7 +365,7 @@ func (w *containerdExecutor) runProcess(ctx context.Context, p containerd.Proces
}
err = p.Resize(resizeCtx, size.Cols, size.Rows)
if err != nil {
logrus.Warnf("Failed to resize %s: %s", p.ID(), err)
bklog.G(resizeCtx).Warnf("Failed to resize %s: %s", p.ID(), err)
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions executor/runcexecutor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"syscall"
"time"

"github.com/moby/buildkit/util/bklog"

"github.com/containerd/containerd/mount"
containerdoci "github.com/containerd/containerd/oci"
"github.com/containerd/continuity/fs"
Expand All @@ -27,7 +29,6 @@ import (
"github.com/moby/buildkit/util/stack"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

type Opt struct {
Expand Down Expand Up @@ -166,7 +167,7 @@ func (w *runcExecutor) Run(ctx context.Context, id string, root executor.Mount,
defer namespace.Close()

if meta.NetMode == pb.NetMode_HOST {
logrus.Info("enabling HostNetworking")
bklog.G(ctx).Info("enabling HostNetworking")
}

resolvConf, err := oci.GetResolvConf(ctx, w.root, w.idmap, w.dns)
Expand Down Expand Up @@ -303,7 +304,7 @@ func (w *runcExecutor) Run(ctx context.Context, id string, root executor.Mount,
case <-ctx.Done():
killCtx, timeout := context.WithTimeout(context.Background(), 7*time.Second)
if err := w.runc.Kill(killCtx, id, int(syscall.SIGKILL), nil); err != nil {
logrus.Errorf("failed to kill runc %s: %+v", id, err)
bklog.G(ctx).Errorf("failed to kill runc %s: %+v", id, err)
select {
case <-killCtx.Done():
timeout()
Expand All @@ -324,7 +325,7 @@ func (w *runcExecutor) Run(ctx context.Context, id string, root executor.Mount,
}
}()

logrus.Debugf("> creating %s %v", id, meta.Args)
bklog.G(ctx).Debugf("> creating %s %v", id, meta.Args)
// this is a cheat, we have not actually started, but as close as we can get with runc for now
if started != nil {
startedOnce.Do(func() {
Expand Down
8 changes: 4 additions & 4 deletions executor/runcexecutor/executor_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
runc "github.com/containerd/go-runc"
"github.com/docker/docker/pkg/signal"
"github.com/moby/buildkit/executor"
"github.com/moby/buildkit/util/bklog"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -74,7 +74,7 @@ func (w *runcExecutor) callWithIO(ctx context.Context, id, bundle string, proces
cancel() // this will shutdown resize loop
err := eg.Wait()
if err != nil {
logrus.Warningf("error while shutting down tty io: %s", err)
bklog.G(ctx).Warningf("error while shutting down tty io: %s", err)
}
}()

Expand Down Expand Up @@ -135,11 +135,11 @@ func (w *runcExecutor) callWithIO(ctx context.Context, id, bundle string, proces
Width: uint16(resize.Cols),
})
if err != nil {
logrus.Errorf("failed to resize ptm: %s", err)
bklog.G(ctx).Errorf("failed to resize ptm: %s", err)
}
err = runcProcess.Signal(signal.SIGWINCH)
if err != nil {
logrus.Errorf("failed to send SIGWINCH to process: %s", err)
bklog.G(ctx).Errorf("failed to send SIGWINCH to process: %s", err)
}
}
}
Expand Down
Loading

0 comments on commit b715f9a

Please sign in to comment.