Skip to content

Commit

Permalink
use bklog.G(ctx) instead of logrus directly
Browse files Browse the repository at this point in the history
Signed-off-by: coryb <cbennett@netflix.com>
  • Loading branch information
coryb committed Mar 12, 2023
1 parent 9c2101e commit 589dba3
Show file tree
Hide file tree
Showing 56 changed files with 184 additions and 176 deletions.
6 changes: 3 additions & 3 deletions cache/blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
"github.com/containerd/containerd/leases"
"github.com/containerd/containerd/mount"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/compression"
"github.com/moby/buildkit/util/flightcontrol"
"github.com/moby/buildkit/util/winlayers"
digest "github.com/opencontainers/go-digest"
imagespecidentity "github.com/opencontainers/image-spec/identity"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -180,7 +180,7 @@ func computeBlobChain(ctx context.Context, sr *immutableRef, createIfNeeded bool
}
}
if logWarnOnErr {
logrus.Warnf("failed to compute blob by overlay differ (ok=%v): %v", ok, err)
bklog.G(ctx).Warnf("failed to compute blob by overlay differ (ok=%v): %v", ok, err)
}
}
if ok {
Expand All @@ -198,7 +198,7 @@ func computeBlobChain(ctx context.Context, sr *immutableRef, createIfNeeded bool
diff.WithCompressor(compressorFunc),
)
if err != nil {
logrus.WithError(err).Warnf("failed to compute blob by buildkit differ")
bklog.G(ctx).WithError(err).Warnf("failed to compute blob by buildkit differ")
}
}

Expand Down
7 changes: 3 additions & 4 deletions cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
imagespecidentity "github.com/opencontainers/image-spec/identity"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -243,7 +242,7 @@ func (cm *cacheManager) GetByBlob(ctx context.Context, desc ocispecs.Descriptor,
if err := cm.LeaseManager.Delete(context.TODO(), leases.Lease{
ID: l.ID,
}); err != nil {
logrus.Errorf("failed to remove lease: %+v", err)
bklog.G(ctx).Errorf("failed to remove lease: %+v", err)
}
}
}()
Expand Down Expand Up @@ -319,7 +318,7 @@ func (cm *cacheManager) init(ctx context.Context) error {

for _, si := range items {
if _, err := cm.getRecord(ctx, si.ID()); err != nil {
logrus.Debugf("could not load snapshot %s: %+v", si.ID(), err)
bklog.G(ctx).Debugf("could not load snapshot %s: %+v", si.ID(), err)
cm.MetadataStore.Clear(si.ID())
cm.LeaseManager.Delete(ctx, leases.Lease{ID: si.ID()})
}
Expand Down Expand Up @@ -597,7 +596,7 @@ func (cm *cacheManager) New(ctx context.Context, s ImmutableRef, sess session.Gr
if err := cm.LeaseManager.Delete(context.TODO(), leases.Lease{
ID: l.ID,
}); err != nil {
logrus.Errorf("failed to remove lease: %+v", err)
bklog.G(ctx).Errorf("failed to remove lease: %+v", err)
}
}
}()
Expand Down
2 changes: 1 addition & 1 deletion cache/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (cm *cacheManager) Search(ctx context.Context, idx string) ([]RefMetadata,

// callers must hold cm.mu lock
func (cm *cacheManager) search(ctx context.Context, idx string) ([]RefMetadata, error) {
sis, err := cm.MetadataStore.Search(idx)
sis, err := cm.MetadataStore.Search(ctx, idx)
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions cache/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package metadata

import (
"bytes"
"context"
"encoding/json"
"strings"
"sync"

"github.com/moby/buildkit/util/bklog"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
bolt "go.etcd.io/bbolt"
)

Expand Down Expand Up @@ -80,7 +81,7 @@ func (s *Store) Probe(index string) (bool, error) {
return exists, errors.WithStack(err)
}

func (s *Store) Search(index string) ([]*StorageItem, error) {
func (s *Store) Search(ctx context.Context, index string) ([]*StorageItem, error) {
var out []*StorageItem
err := s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(indexBucket))
Expand All @@ -100,7 +101,7 @@ func (s *Store) Search(index string) ([]*StorageItem, error) {
k, _ = c.Next()
b := main.Bucket([]byte(itemID))
if b == nil {
logrus.Errorf("index pointing to missing record %s", itemID)
bklog.G(ctx).Errorf("index pointing to missing record %s", itemID)
continue
}
si, err := newStorageItem(itemID, b, s)
Expand Down
8 changes: 5 additions & 3 deletions cache/metadata/metadata_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package metadata

import (
"context"
"path/filepath"
"testing"

Expand Down Expand Up @@ -140,14 +141,15 @@ func TestIndexes(t *testing.T) {
require.NoError(t, err)
}

sis, err := s.Search("tag:baz")
ctx := context.Background()
sis, err := s.Search(ctx, "tag:baz")
require.NoError(t, err)
require.Equal(t, 2, len(sis))

require.Equal(t, sis[0].ID(), "foo1")
require.Equal(t, sis[1].ID(), "foo3")

sis, err = s.Search("tag:bax")
sis, err = s.Search(ctx, "tag:bax")
require.NoError(t, err)
require.Equal(t, 1, len(sis))

Expand All @@ -156,7 +158,7 @@ func TestIndexes(t *testing.T) {
err = s.Clear("foo1")
require.NoError(t, err)

sis, err = s.Search("tag:baz")
sis, err = s.Search(ctx, "tag:baz")
require.NoError(t, err)
require.Equal(t, 1, len(sis))

Expand Down
7 changes: 3 additions & 4 deletions cache/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -993,7 +992,7 @@ func (sr *immutableRef) withRemoteSnapshotLabelsStargzMode(ctx context.Context,
info.Labels[k] = "" // Remove labels appended in this call
}
if _, err := r.cm.Snapshotter.Update(ctx, info, flds...); err != nil {
logrus.Warn(errors.Wrapf(err, "failed to remove tmp remote labels"))
bklog.G(ctx).Warn(errors.Wrapf(err, "failed to remove tmp remote labels"))
}
}()

Expand Down Expand Up @@ -1055,7 +1054,7 @@ func (sr *immutableRef) prepareRemoteSnapshotsStargzMode(ctx context.Context, s
info.Labels[k] = ""
}
if _, err := r.cm.Snapshotter.Update(ctx, info, tmpFields...); err != nil {
logrus.Warn(errors.Wrapf(err,
bklog.G(ctx).Warn(errors.Wrapf(err,
"failed to remove tmp remote labels after prepare"))
}
}()
Expand Down Expand Up @@ -1363,7 +1362,7 @@ func (cr *cacheRecord) finalize(ctx context.Context) error {
cr.cm.mu.Lock()
defer cr.cm.mu.Unlock()
if err := mutable.remove(context.TODO(), true); err != nil {
logrus.Error(err)
bklog.G(ctx).Error(err)
}
}()

Expand Down
4 changes: 2 additions & 2 deletions cache/remotecache/azblob/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
v1 "github.com/moby/buildkit/cache/remotecache/v1"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/solver"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/compression"
"github.com/moby/buildkit/util/progress"
digest "github.com/opencontainers/go-digest"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// ResolveCacheExporterFunc for "azblob" cache exporter.
Expand Down Expand Up @@ -89,7 +89,7 @@ func (ce *exporter) Finalize(ctx context.Context) (map[string]string, error) {
return nil, err
}

logrus.Debugf("layers %s exists = %t", key, exists)
bklog.G(ctx).Debugf("layers %s exists = %t", key, exists)

if !exists {
layerDone := progress.OneOff(ctx, fmt.Sprintf("writing layer %s", l.Blob))
Expand Down
8 changes: 4 additions & 4 deletions cache/remotecache/azblob/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (
v1 "github.com/moby/buildkit/cache/remotecache/v1"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/solver"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/contentutil"
"github.com/moby/buildkit/util/progress"
"github.com/moby/buildkit/worker"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -92,7 +92,7 @@ func (ci *importer) loadManifest(ctx context.Context, name string) (*v1.CacheCha
return nil, err
}

logrus.Debugf("name %s cache with key %s exists = %v", name, key, exists)
bklog.G(ctx).Debugf("name %s cache with key %s exists = %v", name, key, exists)

if !exists {
return v1.NewCacheChains(), nil
Expand All @@ -113,7 +113,7 @@ func (ci *importer) loadManifest(ctx context.Context, name string) (*v1.CacheCha
return nil, errors.WithStack(err)
}

logrus.Debugf("imported config: %s", string(bytes))
bklog.G(ctx).Debugf("imported config: %s", string(bytes))

var config v1.CacheConfig
if err := json.Unmarshal(bytes, &config); err != nil {
Expand Down Expand Up @@ -188,7 +188,7 @@ func (f *fetcher) Fetch(ctx context.Context, desc ocispecs.Descriptor) (io.ReadC
return nil, errors.Errorf("blob %s not found", desc.Digest)
}

logrus.Debugf("reading layer from cache: %s", key)
bklog.G(ctx).Debugf("reading layer from cache: %s", key)

blobClient, err := f.containerClient.NewBlockBlobClient(key)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cache/remotecache/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (ce *contentCacheExporter) Finalize(ctx context.Context) (map[string]string
mfst.Manifests = append(mfst.Manifests, dgstPair.Descriptor)
}

mfst.Manifests = compression.ConvertAllLayerMediaTypes(ce.oci, mfst.Manifests...)
mfst.Manifests = compression.ConvertAllLayerMediaTypes(ctx, ce.oci, mfst.Manifests...)

dt, err := json.Marshal(config)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cache/remotecache/gha/gha.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ import (
v1 "github.com/moby/buildkit/cache/remotecache/v1"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/solver"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/compression"
"github.com/moby/buildkit/util/progress"
"github.com/moby/buildkit/util/tracing"
"github.com/moby/buildkit/worker"
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
actionscache "github.com/tonistiigi/go-actions-cache"
"golang.org/x/sync/errgroup"
)

func init() {
actionscache.Log = logrus.Debugf
actionscache.Log = bklog.L.Debugf
}

const (
Expand Down
4 changes: 2 additions & 2 deletions cache/remotecache/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
v1 "github.com/moby/buildkit/cache/remotecache/v1"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/solver"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/imageutil"
"github.com/moby/buildkit/worker"
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -162,7 +162,7 @@ func (ci *contentCacheImporter) importInlineCache(ctx context.Context, dt []byte
}

if len(img.Rootfs.DiffIDs) != len(m.Layers) {
logrus.Warnf("invalid image with mismatching manifest and config")
bklog.G(ctx).Warnf("invalid image with mismatching manifest and config")
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions cache/remotecache/inline/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
v1 "github.com/moby/buildkit/cache/remotecache/v1"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/solver"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/compression"
digest "github.com/opencontainers/go-digest"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

func ResolveCacheExporterFunc() remotecache.ResolveCacheExporterFunc {
Expand Down Expand Up @@ -85,7 +85,7 @@ func (ce *exporter) ExportForLayers(ctx context.Context, layers []digest.Digest)
}

if len(cfg.Layers) == 0 {
logrus.Warn("failed to match any cache with layers")
bklog.G(ctx).Warn("failed to match any cache with layers")
return nil, nil
}

Expand Down
6 changes: 3 additions & 3 deletions cache/remotecache/v1/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *CacheChains) Visited(v interface{}) bool {
return ok
}

func (c *CacheChains) normalize() error {
func (c *CacheChains) normalize(ctx context.Context) error {
st := &normalizeState{
added: map[*item]*item{},
links: map[*item]map[nlink]map[digest.Digest]struct{}{},
Expand All @@ -66,7 +66,7 @@ func (c *CacheChains) normalize() error {
}
}

st.removeLoops()
st.removeLoops(ctx)

items := make([]*item, 0, len(st.byKey))
for _, it := range st.byKey {
Expand All @@ -77,7 +77,7 @@ func (c *CacheChains) normalize() error {
}

func (c *CacheChains) Marshal(ctx context.Context) (*CacheConfig, DescriptorProvider, error) {
if err := c.normalize(); err != nil {
if err := c.normalize(ctx); err != nil {
return nil, nil, err
}

Expand Down
12 changes: 6 additions & 6 deletions cache/remotecache/v1/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"sort"

"github.com/moby/buildkit/solver"
"github.com/moby/buildkit/util/bklog"
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// sortConfig sorts the config structure to make sure it is deterministic
Expand Down Expand Up @@ -128,7 +128,7 @@ type normalizeState struct {
next int
}

func (s *normalizeState) removeLoops() {
func (s *normalizeState) removeLoops(ctx context.Context) {
roots := []digest.Digest{}
for dgst, it := range s.byKey {
if len(it.links) == 0 {
Expand All @@ -139,11 +139,11 @@ func (s *normalizeState) removeLoops() {
visited := map[digest.Digest]struct{}{}

for _, d := range roots {
s.checkLoops(d, visited)
s.checkLoops(ctx, d, visited)
}
}

func (s *normalizeState) checkLoops(d digest.Digest, visited map[digest.Digest]struct{}) {
func (s *normalizeState) checkLoops(ctx context.Context, d digest.Digest, visited map[digest.Digest]struct{}) {
it, ok := s.byKey[d]
if !ok {
return
Expand All @@ -165,11 +165,11 @@ func (s *normalizeState) checkLoops(d digest.Digest, visited map[digest.Digest]s
continue
}
if !it2.removeLink(it) {
logrus.Warnf("failed to remove looping cache key %s %s", d, id)
bklog.G(ctx).Warnf("failed to remove looping cache key %s %s", d, id)
}
delete(links[l], id)
} else {
s.checkLoops(id, visited)
s.checkLoops(ctx, id, visited)
}
}
}
Expand Down
Loading

0 comments on commit 589dba3

Please sign in to comment.