Skip to content

Commit

Permalink
switch to github.com/containerd/log module
Browse files Browse the repository at this point in the history
containerd 1.7.7 and up alias the log package to the new module,
and deprecate the package.

This patch:

- replaces existing uses of the log package to use the module
- replaces some direct uses of logrus to use the log module
- enables the depguard linter to prevent accidental use of
  the deprecated packages.

There is one change in behavior in the nri-plugins, which now
use the default format as used by the containerd log module.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed Jun 21, 2024
1 parent 04cf1d7 commit 1eee7eb
Show file tree
Hide file tree
Showing 61 changed files with 95 additions and 95 deletions.
7 changes: 7 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ run:
- pkg/remote/remotes

linters-settings:
depguard:
rules:
main:
deny:
- pkg: "github.com/containerd/containerd/log"
desc: The containerd log package was migrated to a separate module. Use github.com/containerd/log instead.
# govet:
# check-shadowing: true
# enable:
Expand All @@ -34,6 +40,7 @@ linters-settings:

linters:
enable:
- depguard # Checks for imports that shouldn't be used.
- staticcheck
- unconvert
- gofmt
Expand Down
2 changes: 1 addition & 1 deletion cmd/containerd-nydus-grpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"fmt"
"os"

"github.com/containerd/containerd/log"
"github.com/containerd/log"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"

Expand Down
2 changes: 1 addition & 1 deletion cmd/containerd-nydus-grpc/snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (

api "github.com/containerd/containerd/api/services/snapshots/v1"
"github.com/containerd/containerd/contrib/snapshotservice"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/snapshots"
"github.com/containerd/log"
"google.golang.org/grpc"
)

Expand Down
29 changes: 14 additions & 15 deletions cmd/optimizer-nri-plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"strings"
"time"

"github.com/containerd/log"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"

"github.com/containerd/containerd/reference/docker"
Expand Down Expand Up @@ -122,7 +122,6 @@ type plugin struct {

var (
cfg PluginConfig
log *logrus.Logger
logWriter *syslog.Writer
globalFanotifyServer = make(map[string]*fanotify.Server)

Expand All @@ -135,8 +134,8 @@ const (
imageNameLabel = "io.kubernetes.cri.image-name"
)

func (p *plugin) Configure(_ context.Context, config, runtime, version string) (stub.EventMask, error) {
log.Infof("got configuration data: %q from runtime %s %s", config, runtime, version)
func (p *plugin) Configure(ctx context.Context, config, runtime, version string) (stub.EventMask, error) {
log.G(ctx).Infof("got configuration data: %q from runtime %s %s", config, runtime, version)
if config == "" {
return p.mask, nil
}
Expand All @@ -154,7 +153,7 @@ func (p *plugin) Configure(_ context.Context, config, runtime, version string) (
return 0, errors.Wrap(err, "parse events in configuration")
}

log.Infof("configuration: %#v", cfg)
log.G(ctx).Infof("configuration: %#v", cfg)

return p.mask, nil
}
Expand Down Expand Up @@ -240,13 +239,13 @@ func main() {

cfg = flags.Args.Config

log = logrus.StandardLogger()
log.SetFormatter(&logrus.TextFormatter{
PadLevelText: true,
})
// FIXME(thaJeztah): ucontainerd's log does not set "PadLevelText: true"
_ = log.SetFormat(log.TextFormat)
ctx := log.WithLogger(context.Background(), log.L)

logWriter, err = syslog.New(syslog.LOG_INFO, "optimizer-nri-plugin")
if err == nil {
log.SetOutput(io.MultiWriter(os.Stdout, logWriter))
log.G(ctx).Logger.SetOutput(io.MultiWriter(os.Stdout, logWriter))
}

if flags.Args.PluginName != "" {
Expand All @@ -259,17 +258,17 @@ func main() {
p := &plugin{}

if p.mask, err = api.ParseEventMask(flags.Args.PluginEvents); err != nil {
log.Fatalf("failed to parse events: %v", err)
log.G(ctx).Fatalf("failed to parse events: %v", err)
}
cfg.Events = strings.Split(flags.Args.PluginEvents, ",")

if p.stub, err = stub.New(p, append(opts, stub.WithOnClose(p.onClose))...); err != nil {
log.Fatalf("failed to create plugin stub: %v", err)
log.G(ctx).Fatalf("failed to create plugin stub: %v", err)
}

err = p.stub.Run(context.Background())
if err != nil {
log.Errorf("plugin exited with error %v", err)
log.G(ctx).Errorf("plugin exited with error %v", err)
os.Exit(1)
}

Expand All @@ -278,9 +277,9 @@ func main() {
}
if err := app.Run(os.Args); err != nil {
if errdefs.IsConnectionClosed(err) {
log.Info("optimizer NRI plugin exited")
log.L.Info("optimizer NRI plugin exited")
} else {
log.WithError(err).Fatal("failed to start optimizer NRI plugin")
log.L.WithError(err).Fatal("failed to start optimizer NRI plugin")
}
}
}
31 changes: 13 additions & 18 deletions cmd/prefetchfiles-nri-plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (
"path/filepath"
"strings"

"github.com/containerd/log"
"github.com/containerd/nri/pkg/api"
"github.com/containerd/nri/pkg/stub"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"

"github.com/containerd/nydus-snapshotter/pkg/errdefs"
Expand Down Expand Up @@ -83,7 +83,6 @@ type plugin struct {

var (
globalSocket string
log *logrus.Logger
logWriter *syslog.Writer

_ = stub.RunPodInterface(&plugin{})
Expand Down Expand Up @@ -117,23 +116,22 @@ func sendDataOverHTTP(data string, endpoint, sock string) error {
return nil
}

func (p *plugin) RunPodSandbox(_ context.Context, pod *api.PodSandbox) error {
func (p *plugin) RunPodSandbox(ctx context.Context, pod *api.PodSandbox) error {
prefetchList, ok := pod.Annotations[nydusPrefetchAnnotation]
if !ok {
return nil
}

err := sendDataOverHTTP(prefetchList, endpointPrefetch, globalSocket)
if err != nil {
log.Errorf("failed to send data: %v", err)
log.G(ctx).Errorf("failed to send data: %v", err)
return err
}

return nil
}

func main() {

flags := NewPluginFlags()

app := &cli.App{
Expand All @@ -148,35 +146,33 @@ func main() {
err error
)

log = logrus.StandardLogger()
// FIXME(thaJeztah): ucontainerd's log does not set "PadLevelText: true"
_ = log.SetFormat(log.TextFormat)
ctx := log.WithLogger(context.Background(), log.L)

configFileName := "prefetchConfig.toml"
configDir := defaultPrefetchConfigDir
configFilePath := filepath.Join(configDir, configFileName)

config, err := toml.LoadFile(configFilePath)
if err != nil {
log.Warnf("failed to read config file: %v", err)
log.G(ctx).Warnf("failed to read config file: %v", err)
}

configSocketAddrRaw := config.Get("file_prefetch.socket_address")
if configSocketAddrRaw != nil {
if configSocketAddr, ok := configSocketAddrRaw.(string); ok {
globalSocket = configSocketAddr
} else {
log.Warnf("failed to read config: 'file_prefetch.socket_address' is not a string")
log.G(ctx).Warnf("failed to read config: 'file_prefetch.socket_address' is not a string")
}
} else {
globalSocket = flags.Args.SocketAddress
}

log.SetFormatter(&logrus.TextFormatter{
PadLevelText: true,
})
logWriter, err = syslog.New(syslog.LOG_INFO, "prefetch-nri-plugin")

if err == nil {
log.SetOutput(io.MultiWriter(os.Stdout, logWriter))
log.G(ctx).Logger.SetOutput(io.MultiWriter(os.Stdout, logWriter))
}

if flags.Args.PluginName != "" {
Expand All @@ -189,11 +185,11 @@ func main() {
p := &plugin{}

if p.mask, err = api.ParseEventMask(defaultEvents); err != nil {
log.Fatalf("failed to parse events: %v", err)
log.G(ctx).Fatalf("failed to parse events: %v", err)
}

if p.stub, err = stub.New(p, opts...); err != nil {
log.Fatalf("failed to create plugin stub: %v", err)
log.G(ctx).Fatalf("failed to create plugin stub: %v", err)
}

err = p.stub.Run(context.Background())
Expand All @@ -204,11 +200,10 @@ func main() {
},
}
if err := app.Run(os.Args); err != nil {

if errdefs.IsConnectionClosed(err) {
log.Info("prefetch NRI plugin exited")
log.L.Info("prefetch NRI plugin exited")
} else {
log.WithError(err).Fatal("failed to start prefetch NRI plugin")
log.L.WithError(err).Fatal("failed to start prefetch NRI plugin")
}
}
}
2 changes: 1 addition & 1 deletion config/daemonconfig/fscache.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"os"
"path"

"github.com/containerd/containerd/log"
"github.com/containerd/log"
"github.com/containerd/nydus-snapshotter/pkg/auth"
"github.com/containerd/nydus-snapshotter/pkg/utils/erofs"

Expand Down
2 changes: 1 addition & 1 deletion config/daemonconfig/mirrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"sort"
"strings"

"github.com/containerd/containerd/log"
"github.com/containerd/log"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
)
Expand Down
2 changes: 1 addition & 1 deletion config/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"path/filepath"
"time"

"github.com/containerd/containerd/log"
"github.com/containerd/log"
"github.com/pkg/errors"

"github.com/containerd/nydus-snapshotter/internal/logging"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/containerd/containerd v1.7.7
github.com/containerd/continuity v0.4.2
github.com/containerd/fifo v1.1.0
github.com/containerd/log v0.1.0
github.com/containerd/nri v0.4.0
github.com/containerd/stargz-snapshotter v0.14.3
github.com/containerd/stargz-snapshotter/estargz v0.14.3
Expand Down Expand Up @@ -77,7 +78,6 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cilium/ebpf v0.9.1 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/ttrpc v1.2.2 // indirect
github.com/containerd/typeurl/v2 v2.1.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion internal/logging/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"os"
"path/filepath"

"github.com/containerd/containerd/log"
"github.com/containerd/log"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
Expand Down
2 changes: 1 addition & 1 deletion internal/logging/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"testing"
"time"

"github.com/containerd/containerd/log"
"github.com/containerd/log"
"github.com/sirupsen/logrus"
"gotest.tools/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/auth/image_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
"time"

"github.com/containerd/containerd/defaults"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/pkg/dialer"
"github.com/containerd/containerd/reference"
distribution "github.com/containerd/containerd/reference/docker"
runtime_alpha "github.com/containerd/containerd/third_party/k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
"github.com/containerd/log"
"github.com/containerd/stargz-snapshotter/service/keychain/cri"
"github.com/containerd/stargz-snapshotter/service/keychain/crialpha"
"github.com/containerd/stargz-snapshotter/service/resolver"
Expand Down
2 changes: 1 addition & 1 deletion pkg/cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (

"github.com/pkg/errors"

"github.com/containerd/containerd/log"
"github.com/containerd/containerd/snapshots"
"github.com/containerd/continuity/fs"
"github.com/containerd/log"
"github.com/containerd/nydus-snapshotter/pkg/store"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/cgroup/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package cgroup

import (
"github.com/containerd/containerd/log"
"github.com/containerd/log"
)

type Manager struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cgroup/v1/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package v1

import (
"github.com/containerd/cgroups/v3/cgroup1"
"github.com/containerd/containerd/log"
"github.com/containerd/log"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cgroup/v2/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"strings"

"github.com/containerd/cgroups/v3/cgroup2"
"github.com/containerd/containerd/log"
"github.com/containerd/log"
"golang.org/x/exp/slices"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/daemon/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"github.com/pkg/errors"

"github.com/containerd/containerd/log"
"github.com/containerd/log"
"github.com/containerd/nydus-snapshotter/pkg/daemon/types"
"github.com/containerd/nydus-snapshotter/pkg/metrics/tool"
"github.com/containerd/nydus-snapshotter/pkg/utils/retry"
Expand Down
2 changes: 1 addition & 1 deletion pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

"github.com/pkg/errors"

"github.com/containerd/containerd/log"
"github.com/containerd/log"

"github.com/containerd/nydus-snapshotter/config"
"github.com/containerd/nydus-snapshotter/config/daemonconfig"
Expand Down
2 changes: 1 addition & 1 deletion pkg/filesystem/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"

"github.com/containerd/containerd/log"
"github.com/containerd/containerd/snapshots"
"github.com/containerd/containerd/snapshots/storage"
"github.com/containerd/log"

"github.com/containerd/nydus-snapshotter/config"
"github.com/containerd/nydus-snapshotter/config/daemonconfig"
Expand Down
2 changes: 1 addition & 1 deletion pkg/filesystem/stargz_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"time"

"github.com/KarpelesLab/reflink"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/snapshots/storage"
"github.com/containerd/log"
"github.com/containerd/nydus-snapshotter/config"
"github.com/containerd/nydus-snapshotter/pkg/auth"
"github.com/containerd/nydus-snapshotter/pkg/label"
Expand Down
Loading

0 comments on commit 1eee7eb

Please sign in to comment.