Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove direct dependency on github.com/pkg/errors #33

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
11 changes: 6 additions & 5 deletions debug/debug_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Expand All @@ -16,6 +17,7 @@
package debug

import (
"errors"
"fmt"
"os"
"os/signal"
Expand All @@ -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,
}
)

Expand Down Expand Up @@ -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")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions logger/awslogs/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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.
Expand Down
32 changes: 15 additions & 17 deletions logger/buffered_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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.
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}

Expand Down
34 changes: 17 additions & 17 deletions logger/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"io"
"math"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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.
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading