Skip to content

Commit

Permalink
Merge pull request #598 from thaJeztah/switch_to_containerd_log
Browse files Browse the repository at this point in the history
go.mod: github.com/containerd/containerd v1.7.7, containerd/nri v0.4.0, switch to github.com/containerd/log module
  • Loading branch information
imeoer authored Jun 26, 2024
2 parents e9d1bb7 + 1eee7eb commit 71ab64a
Show file tree
Hide file tree
Showing 62 changed files with 147 additions and 141 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
38 changes: 20 additions & 18 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,18 +122,20 @@ type plugin struct {

var (
cfg PluginConfig
log *logrus.Logger
logWriter *syslog.Writer
_ = stub.ConfigureInterface(&plugin{})
globalFanotifyServer = make(map[string]*fanotify.Server)

_ = stub.ConfigureInterface(&plugin{})
_ = stub.StartContainerInterface(&plugin{})
_ = stub.StopContainerInterface(&plugin{})
)

const (
imageNameLabel = "io.kubernetes.cri.image-name"
)

func (p *plugin) Configure(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 @@ -151,12 +153,12 @@ func (p *plugin) Configure(config, runtime, version string) (stub.EventMask, err
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
}

func (p *plugin) StartContainer(_ *api.PodSandbox, container *api.Container) error {
func (p *plugin) StartContainer(_ context.Context, _ *api.PodSandbox, container *api.Container) error {
dir, imageName, err := GetImageName(container.Annotations)
if err != nil {
return err
Expand All @@ -183,7 +185,7 @@ func (p *plugin) StartContainer(_ *api.PodSandbox, container *api.Container) err
return nil
}

func (p *plugin) StopContainer(_ *api.PodSandbox, container *api.Container) ([]*api.ContainerUpdate, error) {
func (p *plugin) StopContainer(_ context.Context, _ *api.PodSandbox, container *api.Container) ([]*api.ContainerUpdate, error) {
var update = []*api.ContainerUpdate{}
_, imageName, err := GetImageName(container.Annotations)
if err != nil {
Expand Down Expand Up @@ -237,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 @@ -256,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 @@ -275,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")
}
}
}
33 changes: 15 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,8 +83,9 @@ type plugin struct {

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

_ = stub.RunPodInterface(&plugin{})
)

// sendDataOverHTTP sends the prefetch data to the specified endpoint over HTTP using a Unix socket.
Expand Down Expand Up @@ -115,23 +116,22 @@ func sendDataOverHTTP(data string, endpoint, sock string) error {
return nil
}

func (p *plugin) RunPodSandbox(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 @@ -146,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 @@ -187,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 @@ -202,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
30 changes: 15 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@ module github.com/containerd/nydus-snapshotter
go 1.19

require (
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230106234847-43070de90fa1
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24
github.com/KarpelesLab/reflink v1.0.1
github.com/aliyun/aliyun-oss-go-sdk v2.2.6+incompatible
github.com/aws/aws-sdk-go-v2 v1.17.6
github.com/aws/aws-sdk-go-v2/config v1.18.16
github.com/aws/aws-sdk-go-v2/credentials v1.13.16
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.56
github.com/containerd/cgroups v1.1.0
github.com/containerd/cgroups/v3 v3.0.1
github.com/containerd/containerd v1.7.0
github.com/containerd/continuity v0.3.0
github.com/containerd/cgroups/v3 v3.0.2
github.com/containerd/containerd v1.7.7
github.com/containerd/continuity v0.4.2
github.com/containerd/fifo v1.1.0
github.com/containerd/nri v0.3.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
github.com/containers/ocicrypt v1.1.7
github.com/distribution/reference v0.5.0
github.com/docker/cli v23.0.1+incompatible
github.com/docker/cli v23.0.3+incompatible
github.com/freddierice/go-losetup v0.0.0-20220711213114-2a14873012db
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da
github.com/google/go-containerregistry v0.13.0
github.com/google/go-containerregistry v0.14.0
github.com/gorilla/mux v1.8.0
github.com/hashicorp/go-retryablehttp v0.7.2
github.com/imdario/mergo v0.3.13
Expand All @@ -37,8 +38,8 @@ require (
github.com/prometheus/client_golang v1.14.0
github.com/prometheus/client_model v0.3.0
github.com/rs/xid v1.4.0
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.3
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
github.com/urfave/cli/v2 v2.25.0
go.etcd.io/bbolt v1.3.7
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
Expand All @@ -56,9 +57,9 @@ require (
)

require (
github.com/AdamKorcz/go-118-fuzz-build v0.0.0-20221215162035-5330a85ea652 // indirect
github.com/Microsoft/go-winio v0.6.0 // indirect
github.com/Microsoft/hcsshim v0.10.0-rc.7 // indirect
github.com/AdamKorcz/go-118-fuzz-build v0.0.0-20230306123547-8075edf89bb0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Microsoft/hcsshim v0.11.1 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.10 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.24 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.30 // indirect
Expand All @@ -77,11 +78,10 @@ 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/ttrpc v1.2.1 // indirect
github.com/containerd/typeurl/v2 v2.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
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v23.0.3+incompatible // indirect
Expand Down
Loading

0 comments on commit 71ab64a

Please sign in to comment.