diff --git a/args.go b/args.go index 83bfb46..e01ab4f 100644 --- a/args.go +++ b/args.go @@ -26,7 +26,6 @@ import ( "github.com/aws/shim-loggers-for-containerd/logger/splunk" "github.com/docker/go-units" - "github.com/pkg/errors" "github.com/spf13/pflag" "github.com/spf13/viper" ) @@ -54,7 +53,7 @@ func getGlobalArgs() (*logger.GlobalArgs, error) { } mode, maxBufferSize, err := getModeAndMaxBufferSize() if err != nil { - return nil, errors.Wrapf(err, "unable to get value of flag %s and %s", modeKey, maxBufferSizeKey) + return nil, fmt.Errorf("unable to get value of flag %s and %s: %w", modeKey, maxBufferSizeKey, err) } cleanupTime, err := getCleanupTime() if err != nil { @@ -226,8 +225,7 @@ func getSplunkArgs() (*splunk.Args, error) { func getRequiredValue(flag string) (string, error) { isSet := viper.IsSet(flag) if !isSet { - err := errors.Errorf("%s is required", flag) - return "", err + return "", fmt.Errorf("%s is required", flag) } val := viper.GetString(flag) @@ -250,10 +248,10 @@ func getModeAndMaxBufferSize() (string, int, error) { case nonBlockingMode: maxBufSize, err = getMaxBufferSize() if err != nil { - return "", 0, errors.Wrap(err, "unable to get max buffer size") + return "", 0, fmt.Errorf("unable to get max buffer size: %w", err) } default: - return "", 0, errors.Errorf("unknown mode type: %s", mode) + return "", 0, fmt.Errorf("unknown mode type: %s", mode) } return mode, maxBufSize, nil @@ -273,7 +271,7 @@ func getMaxBufferSize() (int, error) { } if err != nil { - return 0, errors.Wrap(err, "unable to parse buffer size to bytes") + return 0, fmt.Errorf("unable to parse buffer size to bytes: %w", err) } return int(size), nil @@ -287,10 +285,10 @@ func getCleanupTime() (*time.Duration, error) { } duration, err := time.ParseDuration(cleanupTime) if err != nil { - return nil, errors.Wrap(err, "failed to parse clean up time") + return nil, fmt.Errorf("failed to parse clean up time: %w", err) } if duration > time.Duration(12*time.Second) { - return nil, errors.Errorf("invalid time %s, maximum timeout is 12 seconds.", duration.String()) + return nil, fmt.Errorf("invalid time %s, maximum timeout is 12 seconds", duration.String()) } return &duration, nil diff --git a/debug/debug_linux.go b/debug/debug_linux.go index c0b8de6..356334e 100644 --- a/debug/debug_linux.go +++ b/debug/debug_linux.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. @@ -16,6 +17,7 @@ package debug import ( + "errors" "fmt" "os" "os/signal" @@ -24,15 +26,14 @@ import ( "time" "github.com/coreos/go-systemd/journal" - "github.com/pkg/errors" ) var ( // journalPriority is a map that maps strings to journal priority values journalPriority = map[string]journal.Priority{ - ERROR : journal.PriErr, - INFO : journal.PriInfo, - DEBUG : journal.PriDebug, + ERROR: journal.PriErr, + INFO: journal.PriInfo, + DEBUG: journal.PriDebug, } ) @@ -76,6 +77,6 @@ func sendEventsToJournal(syslogIdentifier string, msg string, msgType journal.Pr // SetLogFilePath only supported on Windows // For non-Windows logs will be written to journald -func SetLogFilePath(logFlag, cId string) error{ +func SetLogFilePath(logFlag, cId string) error { return errors.New("debugging to file not supported, debug logs will be written with journald") } diff --git a/go.mod b/go.mod index 314cd73..9764e3a 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ require ( github.com/docker/docker v20.10.13+incompatible github.com/docker/go-units v0.4.0 github.com/golang/mock v1.4.1 - github.com/pkg/errors v0.9.1 github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.4.0 github.com/stretchr/testify v1.7.0 @@ -45,6 +44,7 @@ require ( github.com/opencontainers/image-spec v1.0.2 // indirect github.com/pelletier/go-toml v1.8.1 // indirect github.com/philhofer/fwd v1.0.0 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_golang v1.7.1 // indirect github.com/prometheus/client_model v0.2.0 // indirect diff --git a/logger/awslogs/logger.go b/logger/awslogs/logger.go index 21aa2dc..70fcb0e 100644 --- a/logger/awslogs/logger.go +++ b/logger/awslogs/logger.go @@ -15,13 +15,13 @@ package awslogs import ( "context" + "fmt" "github.com/aws/shim-loggers-for-containerd/debug" "github.com/aws/shim-loggers-for-containerd/logger" "github.com/containerd/containerd/runtime/v2/logging" dockerawslogs "github.com/docker/docker/daemon/logger/awslogs" - "github.com/pkg/errors" ) const ( @@ -89,7 +89,7 @@ func (la *LoggerArgs) RunLogDriver(ctx context.Context, config *logging.Config, ) stream, err := dockerawslogs.New(*info) if err != nil { - debug.LoggerErr = errors.Wrap(err, "unable to create stream") + debug.LoggerErr = fmt.Errorf("unable to create stream: %w", err) return debug.LoggerErr } @@ -101,7 +101,7 @@ func (la *LoggerArgs) RunLogDriver(ctx context.Context, config *logging.Config, logger.WithBufferSizeInBytes(maximumBytesPerEvent), ) if err != nil { - debug.LoggerErr = errors.Wrap(err, "unable to create awslogs driver") + debug.LoggerErr = fmt.Errorf("unable to create awslogs driver: %w", err) return debug.LoggerErr } @@ -115,7 +115,7 @@ func (la *LoggerArgs) RunLogDriver(ctx context.Context, config *logging.Config, debug.SendEventsToLog(logger.DaemonName, "Starting log streaming for awslogs driver", debug.INFO, 0) err = l.Start(ctx, la.globalArgs.UID, la.globalArgs.GID, la.globalArgs.CleanupTime, ready) if err != nil { - debug.LoggerErr = errors.Wrap(err, "failed to run awslogs driver") + debug.LoggerErr = fmt.Errorf("failed to run awslogs driver: %w", err) // Do not return error if log driver has issue sending logs to destination, because if error // returned here, containerd will identify this error and kill shim process, which will kill // the container process accordingly. diff --git a/logger/buffered_logger.go b/logger/buffered_logger.go index 117e0c9..f4a4734 100644 --- a/logger/buffered_logger.go +++ b/logger/buffered_logger.go @@ -22,10 +22,9 @@ import ( "github.com/aws/shim-loggers-for-containerd/debug" - dockerlogger "github.com/docker/docker/daemon/logger" types "github.com/docker/docker/api/types/backend" + dockerlogger "github.com/docker/docker/daemon/logger" - "github.com/pkg/errors" "golang.org/x/sync/errgroup" ) @@ -39,12 +38,12 @@ const ( // bufferedLogger is a wrapper of underlying log driver and an intermediate ring // buffer between container pipes and underlying log driver. type bufferedLogger struct { - l LogDriver - buffer *ringBuffer + l LogDriver + buffer *ringBuffer // bufReadSizeInBytes determines how many bytes to read at a time from the source input when // sending data to the ringBuffer. bufReadSizeInBytes int - containerID string + containerID string } // Adopted from https://github.com/moby/moby/blob/master/daemon/logger/ring.go#L128 @@ -74,10 +73,10 @@ type ringBuffer struct { // and stderr pipes are closed. func NewBufferedLogger(l LogDriver, bufferReadSize int, maxBufferSize int, containerID string) LogDriver { return &bufferedLogger{ - l: l, - buffer: newLoggerBuffer(maxBufferSize), - bufReadSizeInBytes: bufferReadSize, - containerID: containerID, + l: l, + buffer: newLoggerBuffer(maxBufferSize), + bufReadSizeInBytes: bufferReadSize, + containerID: containerID, } } @@ -125,7 +124,7 @@ func (bl *bufferedLogger) Start( errGroup.Go(func() error { logErr := bl.saveLogMessagesToRingBuffer(ctx, pipe, source, uid, gid) if logErr != nil { - err := errors.Wrapf(logErr, "failed to send logs from pipe %s", source) + err := fmt.Errorf("failed to send logs from pipe %s: %w", source, logErr) debug.SendEventsToLog(DaemonName, err.Error(), debug.ERROR, 1) return err } @@ -135,7 +134,7 @@ func (bl *bufferedLogger) Start( // Signal that the container is ready to be started if err := ready(); err != nil { - return errors.Wrap(err, "failed to check container ready status") + return fmt.Errorf("failed to check container ready status: %w", err) } // Wait() will return the first error it receives. @@ -150,7 +149,7 @@ func (bl *bufferedLogger) saveLogMessagesToRingBuffer( uid int, gid int, ) error { if err := bl.Read(ctx, f, source, bl.bufReadSizeInBytes, bl.saveSingleLogMessageToRingBuffer); err != nil { - err := errors.Wrapf(err, "failed to read logs from %s pipe", source) + err := fmt.Errorf("failed to read logs from %s pipe: %w", source, err) debug.SendEventsToLog(DaemonName, err.Error(), debug.ERROR, 1) return err } @@ -199,8 +198,7 @@ func (bl *bufferedLogger) saveSingleLogMessageToRingBuffer( } err := bl.buffer.Enqueue(message) if err != nil { - err := errors.Wrap(err, "failed to save logs to buffer") - return err + return fmt.Errorf("failed to save logs to buffer: %w", err) } return nil @@ -241,12 +239,12 @@ func (bl *bufferedLogger) sendLogMessageToDestination() error { return nil } if err != nil { - return errors.Wrap(err, "failed to read logs from buffer") + return fmt.Errorf("failed to read logs from buffer: %w", err) } err = bl.Log(msg) if err != nil { - return errors.Wrap(err, "failed to send logs to destination") + return fmt.Errorf("failed to send logs to destination: %w", err) } return nil @@ -259,7 +257,7 @@ func (bl *bufferedLogger) flushMessages() error { for _, msg := range messages { err := bl.Log(msg) if err != nil { - return errors.Wrap(err, "unable to flush the remaining messages to destination") + return fmt.Errorf("unable to flush the remaining messages to destination: %w", err) } } diff --git a/logger/common.go b/logger/common.go index 7811a79..6aa9d18 100644 --- a/logger/common.go +++ b/logger/common.go @@ -18,6 +18,7 @@ import ( "context" "crypto/rand" "encoding/hex" + "errors" "fmt" "io" "math" @@ -28,7 +29,6 @@ import ( dockerlogger "github.com/docker/docker/daemon/logger" types "github.com/docker/docker/api/types/backend" - "github.com/pkg/errors" "golang.org/x/sync/errgroup" ) @@ -93,8 +93,8 @@ type Logger struct { // WindowsArgs struct for Windows configuration type WindowsArgs struct { - ProxyEnvVar string - LogFileDir string + ProxyEnvVar string + LogFileDir string } // Client is a wrapper for docker logger's Log method, which is mostly used for testing @@ -182,7 +182,7 @@ func (l *Logger) Start( errGroup.Go(func() error { logErr := l.sendLogs(ctx, pipe, source, uid, gid, cleanupTime) if logErr != nil { - err := errors.Wrapf(logErr, "failed to send logs from pipe %s", source) + err := fmt.Errorf("failed to send logs from pipe %s: %w", source, err) debug.SendEventsToLog(DaemonName, err.Error(), debug.ERROR, 1) return err } @@ -192,7 +192,7 @@ func (l *Logger) Start( // Signal that the container is ready to be started if err := ready(); err != nil { - return errors.Wrap(err, "failed to check container ready status") + return fmt.Errorf("failed to check container ready status: %w", err) } // Wait() will return the first error it receives. @@ -208,7 +208,7 @@ func (l *Logger) sendLogs( cleanupTime *time.Duration, ) error { if err := l.Read(ctx, f, source, l.bufferSizeInBytes, l.sendLogMsgToDest); err != nil { - err := errors.Wrapf(err, "failed to read logs from %s pipe", source) + err := fmt.Errorf("failed to read logs from %s pipe: %w", source, err) debug.SendEventsToLog(DaemonName, err.Error(), debug.ERROR, 1) return err } @@ -245,10 +245,10 @@ func (l *Logger) Read( sendLogMsgToDest sendLogToDestFunc, ) error { var ( - msgTimestamp time.Time - bytesInBuffer int - err error - eof bool + msgTimestamp time.Time + bytesInBuffer int + err error + eof bool ) // Initiate an in-memory buffer to hold bytes read from container pipe. buf := make([]byte, bufferSizeInBytes) @@ -290,7 +290,7 @@ func (l *Logger) Read( // If this is the end of a partial message // use the existing timestamp, so that all // partials split from the same message have the same timestamp - // If not, new timestamp. + // If not, new timestamp. if isPartialMsg { isLastPartial = true } else { @@ -315,7 +315,7 @@ func (l *Logger) Read( isPartialMsg = false isLastPartial = false partialID = "" - partialOrdinal = 1; + partialOrdinal = 1 // Update the index of head of next line message. head += lenOfLine + 1 @@ -383,12 +383,12 @@ func (l *Logger) Read( // generateRandomID is based on Docker // GenerateRandomID: https://github.com/moby/moby/blob/bca8d9f2ce0d63e1490692917cde6273bc288bad/pkg/stringid/stringid.go#L40 -// with the simplification that we don't need to worry about guaranteeing the string isn't all 0 - 9 -// Consequently ^ we have our own function instead of importing from Docker. +// with the simplification that we don't need to worry about guaranteeing the string isn't all 0 - 9 +// Consequently ^ we have our own function instead of importing from Docker. func generateRandomID() (string, error) { b := make([]byte, 32) if _, err := rand.Read(b); err != nil { - return "", err + return "", err } id := hex.EncodeToString(b) return id, nil @@ -407,7 +407,7 @@ func readFromContainerPipe(pipe io.Reader, buf []byte, bytesInBuffer, maxReadByt readBytesFromPipe, err := pipe.Read(buf[bytesInBuffer:readBytesUpto]) if err != nil { if err != io.EOF { - return false, bytesInBuffer, errors.Wrap(err, "failed to read log stream from container pipe") + return false, bytesInBuffer, fmt.Errorf("failed to read log stream from container pipe: %w", err) } // Pipe is closed, set flag to true. eof = true @@ -444,7 +444,7 @@ func (l *Logger) sendLogMsgToDest( } err := l.Log(message) if err != nil { - return errors.Wrapf(err, "failed to log msg for container %s", l.Info.ContainerName) + return fmt.Errorf("failed to log msg for container %s: %w", l.Info.ContainerName, err) } return nil diff --git a/logger/common_linux.go b/logger/common_linux.go index 527b597..1e0586f 100644 --- a/logger/common_linux.go +++ b/logger/common_linux.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. @@ -20,8 +21,6 @@ import ( "syscall" "github.com/aws/shim-loggers-for-containerd/debug" - - "github.com/pkg/errors" ) // setUID sets UID of current goroutine/process. @@ -31,13 +30,13 @@ import ( func setUID(id int) error { err := syscall.Setuid(id) if err != nil { - return errors.Wrap(err, "unable to set uid") + return fmt.Errorf("unable to set uid: %w", err) } // Check if uid set correctly u := syscall.Getuid() if u != id { - return errors.New(fmt.Sprintf("want uid %d, but get uid %d", id, u)) + return fmt.Errorf("want uid %d, but get uid %d", id, u) } debug.SendEventsToLog(DaemonName, fmt.Sprintf("Set uid: %d", u), @@ -53,13 +52,13 @@ func setUID(id int) error { func setGID(id int) error { err := syscall.Setgid(id) if err != nil { - return errors.Wrap(err, "unable to set gid") + return fmt.Errorf("unable to set gid: %w", err) } // Check if gid set correctly g := syscall.Getgid() if g != id { - return errors.New(fmt.Sprintf("want gid %d, but get gid %d", id, g)) + return fmt.Errorf("want gid %d, but get gid %d", id, g) } debug.SendEventsToLog(DaemonName, fmt.Sprintf("Set gid %d", g), diff --git a/logger/common_test.go b/logger/common_test.go index 45c6fbc..78a8f6f 100644 --- a/logger/common_test.go +++ b/logger/common_test.go @@ -21,12 +21,12 @@ import ( "bytes" "context" "encoding/json" + "fmt" "os" "testing" "time" dockerlogger "github.com/docker/docker/daemon/logger" - "github.com/pkg/errors" "github.com/stretchr/testify/require" "golang.org/x/sync/errgroup" ) @@ -66,8 +66,7 @@ func (d *dummyClient) Log(msg *dockerlogger.Message) error { } f, err := os.OpenFile(logDestinationFileName, os.O_APPEND|os.O_RDWR, 0644) if err != nil { - return errors.Wrapf(err, - "unable to open file %s to record log message", logDestinationFileName) + return fmt.Errorf("unable to open file %s to record log message: %w", logDestinationFileName, err) } defer f.Close() b, err = json.Marshal(msg) diff --git a/logger/fluentd/logger.go b/logger/fluentd/logger.go index 584fa35..8ba269b 100644 --- a/logger/fluentd/logger.go +++ b/logger/fluentd/logger.go @@ -15,13 +15,13 @@ package fluentd import ( "context" + "fmt" "github.com/aws/shim-loggers-for-containerd/debug" "github.com/aws/shim-loggers-for-containerd/logger" "github.com/containerd/containerd/runtime/v2/logging" dockerfluentd "github.com/docker/docker/daemon/logger/fluentd" - "github.com/pkg/errors" ) const ( @@ -71,7 +71,7 @@ func (la *LoggerArgs) RunLogDriver(ctx context.Context, config *logging.Config, ) stream, err := dockerfluentd.New(*info) if err != nil { - debug.LoggerErr = errors.Wrap(err, "unable to create stream") + debug.LoggerErr = fmt.Errorf("unable to create stream: %w", err) return debug.LoggerErr } @@ -82,7 +82,7 @@ func (la *LoggerArgs) RunLogDriver(ctx context.Context, config *logging.Config, logger.WithStream(stream), ) if err != nil { - debug.LoggerErr = errors.Wrap(err, "unable to create fluentd driver") + debug.LoggerErr = fmt.Errorf("unable to create fluentd driver: %w", err) return debug.LoggerErr } @@ -95,7 +95,7 @@ func (la *LoggerArgs) RunLogDriver(ctx context.Context, config *logging.Config, debug.SendEventsToLog(logger.DaemonName, "Starting fluentd driver", debug.INFO, 0) err = l.Start(ctx, la.globalArgs.UID, la.globalArgs.GID, la.globalArgs.CleanupTime, ready) if err != nil { - debug.LoggerErr = errors.Wrap(err, "failed to run fluentd driver") + debug.LoggerErr = fmt.Errorf("failed to run fluentd driver: %w", err) // Do not return error if log driver has issue sending logs to destination, because if error // returned here, containerd will identify this error and kill shim process, which will kill // the container process accordingly. diff --git a/logger/splunk/logger.go b/logger/splunk/logger.go index 8cdb536..bd24419 100644 --- a/logger/splunk/logger.go +++ b/logger/splunk/logger.go @@ -15,10 +15,10 @@ package splunk import ( "context" + "fmt" "github.com/containerd/containerd/runtime/v2/logging" dockersplunk "github.com/docker/docker/daemon/logger/splunk" - "github.com/pkg/errors" "github.com/aws/shim-loggers-for-containerd/debug" "github.com/aws/shim-loggers-for-containerd/logger" @@ -105,7 +105,7 @@ func (la *LoggerArgs) RunLogDriver(ctx context.Context, config *logging.Config, stream, err := dockersplunk.New(*info) if err != nil { - debug.LoggerErr = errors.Wrap(err, "unable to create stream") + debug.LoggerErr = fmt.Errorf("unable to create stream: %w", err) return debug.LoggerErr } @@ -116,7 +116,7 @@ func (la *LoggerArgs) RunLogDriver(ctx context.Context, config *logging.Config, logger.WithStream(stream), ) if err != nil { - debug.LoggerErr = errors.Wrap(err, "unable to create splunk log driver") + debug.LoggerErr = fmt.Errorf("unable to create splunk log driver: %w", err) return debug.LoggerErr } @@ -129,7 +129,7 @@ func (la *LoggerArgs) RunLogDriver(ctx context.Context, config *logging.Config, debug.SendEventsToLog(logger.DaemonName, "Starting splunk driver", debug.INFO, 0) err = l.Start(ctx, la.globalArgs.UID, la.globalArgs.GID, la.globalArgs.CleanupTime, ready) if err != nil { - debug.LoggerErr = errors.Wrap(err, "failed to run splunk driver") + debug.LoggerErr = fmt.Errorf("failed to run splunk driver: %w", err) // Do not return error if log driver has issue sending logs to destination, because if error // returned here, containerd will identify this error and kill shim process, which will kill // the container process accordingly. diff --git a/main.go b/main.go index e44ebdb..9d96c0d 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ package main import ( + "fmt" "os" "runtime" @@ -24,7 +25,6 @@ import ( "github.com/aws/shim-loggers-for-containerd/logger/splunk" "github.com/containerd/containerd/runtime/v2/logging" - "github.com/pkg/errors" "github.com/spf13/pflag" "github.com/spf13/viper" ) @@ -48,12 +48,12 @@ func main() { func run() error { if err := viper.BindPFlags(pflag.CommandLine); err != nil { - return errors.Wrap(err, "unable to bind command line flags") + return fmt.Errorf("unable to bind command line flags: %w", err) } globalArgs, err := getGlobalArgs() if err != nil { - return errors.Wrap(err, "unable to get global arguments") + return fmt.Errorf("unable to get global arguments: %w", err) } // Read the Windows specific options and set the environment up accordingly @@ -61,7 +61,7 @@ func run() error { windowsArgs := getWindowsArgs() err = setWindowsEnv(windowsArgs.LogFileDir, globalArgs.ContainerName, windowsArgs.ProxyEnvVar) if err != nil { - return errors.Wrap(err, "failed to set up Windows env with options") + return fmt.Errorf("failed to set up Windows env with options: %w", err) } defer cleanWindowsEnv(windowsArgs.ProxyEnvVar) } @@ -87,16 +87,16 @@ func run() error { switch logDriver { case awslogsDriverName: if err := runAWSLogsDriver(globalArgs); err != nil { - return errors.Wrap(err, "unable to run awslogs driver") + return fmt.Errorf("unable to run awslogs driver: %w", err) } case fluentdDriverName: runFluentdDriver(globalArgs) case splunkDriverName: if err := runSplunkDriver(globalArgs); err != nil { - return errors.Wrap(err, "unable to run splunk driver") + return fmt.Errorf("unable to run splunk driver: %w", err) } default: - return errors.Errorf("unknown log driver: %s", logDriver) + return fmt.Errorf("unknown log driver: %s", logDriver) } return nil @@ -105,7 +105,7 @@ func run() error { func runAWSLogsDriver(globalArgs *logger.GlobalArgs) error { args, err := getAWSLogsArgs() if err != nil { - return errors.Wrap(err, "unable to get awslogs specified arguments") + return fmt.Errorf("unable to get awslogs specified arguments: %w", err) } loggerArgs := awslogs.InitLogger(globalArgs, args) logging.Run(loggerArgs.RunLogDriver) @@ -122,12 +122,12 @@ func runFluentdDriver(globalArgs *logger.GlobalArgs) { func runSplunkDriver(globalArgs *logger.GlobalArgs) error { dockerConfigs, err := getDockerConfigs() if err != nil { - return errors.Wrap(err, "unable to get docker config arguments") + return fmt.Errorf("unable to get docker config arguments: %w", err) } args, err := getSplunkArgs() if err != nil { - return errors.Wrap(err, "unable to get splunk specified arguments") + return fmt.Errorf("unable to get splunk specified arguments: %w", err) } loggerArgs := splunk.InitLogger(globalArgs, dockerConfigs, args)