diff --git a/cmd/containerd-shim-runhcs-v1/delete.go b/cmd/containerd-shim-runhcs-v1/delete.go index 5c8f8313e4..1313b3e5ad 100644 --- a/cmd/containerd-shim-runhcs-v1/delete.go +++ b/cmd/containerd-shim-runhcs-v1/delete.go @@ -9,8 +9,9 @@ import ( "path/filepath" "time" + "errors" + task "github.com/containerd/containerd/api/runtime/task/v2" - "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/urfave/cli" "google.golang.org/protobuf/proto" @@ -27,7 +28,7 @@ import ( func limitedRead(filePath string, readLimitBytes int64) ([]byte, error) { f, err := os.Open(filePath) if err != nil { - return nil, errors.Wrapf(err, "limited read failed to open file: %s", filePath) + return nil, fmt.Errorf("limited read failed to open file: %s: %w", filePath, err) } defer f.Close() if fi, err := f.Stat(); err == nil { @@ -37,11 +38,11 @@ func limitedRead(filePath string, readLimitBytes int64) ([]byte, error) { buf := make([]byte, readLimitBytes) _, err := f.Read(buf) if err != nil { - return []byte{}, errors.Wrapf(err, "limited read failed during file read: %s", filePath) + return []byte{}, fmt.Errorf("limited read failed during file read: %s: %w", filePath, err) } return buf, nil } - return []byte{}, errors.Wrapf(err, "limited read failed during file stat: %s", filePath) + return []byte{}, fmt.Errorf("limited read failed during file stat: %s: %w", filePath, err) } var deleteCommand = cli.Command{ diff --git a/cmd/ncproxy/computeagent_cache.go b/cmd/ncproxy/computeagent_cache.go index bc2200783f..18a3635fe0 100644 --- a/cmd/ncproxy/computeagent_cache.go +++ b/cmd/ncproxy/computeagent_cache.go @@ -5,7 +5,7 @@ package main import ( "sync" - "github.com/pkg/errors" + "errors" ) var errNilCache = errors.New("cannot access a nil cache") diff --git a/cmd/runhcs/vm.go b/cmd/runhcs/vm.go index 0b1374bfcf..f8aa6e04e1 100644 --- a/cmd/runhcs/vm.go +++ b/cmd/runhcs/vm.go @@ -11,12 +11,13 @@ import ( "os" "syscall" + "errors" + "github.com/Microsoft/go-winio" "github.com/Microsoft/hcsshim/internal/appargs" "github.com/Microsoft/hcsshim/internal/logfields" "github.com/Microsoft/hcsshim/internal/runhcs" "github.com/Microsoft/hcsshim/internal/uvm" - "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/urfave/cli" ) diff --git a/internal/bridgeutils/gcserr/errors.go b/internal/bridgeutils/gcserr/errors.go index e282c62a9b..bf2b68976c 100644 --- a/internal/bridgeutils/gcserr/errors.go +++ b/internal/bridgeutils/gcserr/errors.go @@ -1,19 +1,16 @@ package gcserr import ( + "errors" "fmt" "io" - - "github.com/pkg/errors" ) // Hresult is a type corresponding to the HRESULT error type used on Windows. type Hresult int32 -// ! Must match error values in internal\hcs\errors.go -// from -// - https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/705fb797-2175-4a90-b5a3-3918024b10b8 -// - https://docs.microsoft.com/en-us/virtualization/api/hcs/reference/hcshresult +// HRESULT constants matching internal\hcs\errors.go +// from MS-ERREF and HCS docs const ( // HrNotImpl is the HRESULT for a not implemented function. HrNotImpl = Hresult(-2147467263) // 0x80004001 @@ -55,41 +52,12 @@ const ( HrVmcomputeSystemAlreadyStopped = Hresult(-1070137072) // 0xC0370110 ) -// TODO: update implementation to use go1.13 style errors with `errors.As` and co. - -// StackTracer is an interface originating (but not exported) from the -// github.com/pkg/errors package. It defines something which can return a stack -// trace. -type StackTracer interface { - StackTrace() errors.StackTrace -} - -// BaseStackTrace gets the earliest errors.StackTrace in the given error's cause -// stack. This will be the stack trace which reaches closest to the error's -// actual origin. It returns nil if no stack trace is found in the cause stack. -func BaseStackTrace(e error) errors.StackTrace { - type causer interface { - Cause() error - } - cause := e - var tracer StackTracer - for cause != nil { - serr, ok := cause.(StackTracer) - if ok { - tracer = serr - } - cerr, ok := cause.(causer) - if !ok { - break - } - cause = cerr.Cause() - } - if tracer == nil { - return nil - } - return tracer.StackTrace() +// Hresulter interface for errors with Hresult(). +type Hresulter interface { + Hresult() Hresult } +// baseHresultError is a basic HRESULT error. type baseHresultError struct { hresult Hresult } @@ -97,86 +65,71 @@ type baseHresultError struct { func (e *baseHresultError) Error() string { return fmt.Sprintf("HRESULT: 0x%x", uint32(e.Hresult())) } + func (e *baseHresultError) Hresult() Hresult { return e.hresult } +func (e *baseHresultError) Unwrap() error { + return nil +} + +// wrappingHresultError wraps another error with an HRESULT. type wrappingHresultError struct { cause error hresult Hresult } func (e *wrappingHresultError) Error() string { - return fmt.Sprintf("HRESULT 0x%x", uint32(e.Hresult())) + ": " + e.Cause().Error() + return fmt.Sprintf("HRESULT 0x%x: %s", uint32(e.Hresult()), e.cause.Error()) } + func (e *wrappingHresultError) Hresult() Hresult { return e.hresult } + func (e *wrappingHresultError) Cause() error { return e.cause } + +func (e *wrappingHresultError) Unwrap() error { + return e.cause +} + func (e *wrappingHresultError) Format(s fmt.State, verb rune) { switch verb { case 'v': if s.Flag('+') { - fmt.Fprintf(s, "%+v\n", e.Cause()) + fmt.Fprintf(s, "%+v", e.Unwrap()) return } fallthrough - case 's': + case 's', 'q': _, _ = io.WriteString(s, e.Error()) - case 'q': - fmt.Fprintf(s, "%q", e.Error()) } } -func (e *wrappingHresultError) StackTrace() errors.StackTrace { - type stackTracer interface { - StackTrace() errors.StackTrace - } - serr, ok := e.Cause().(stackTracer) - if !ok { - return nil - } - return serr.StackTrace() -} // NewHresultError produces a new error with the given HRESULT. func NewHresultError(hresult Hresult) error { return &baseHresultError{hresult: hresult} } -// WrapHresult produces a new error with the given HRESULT and wrapping the -// given error. -func WrapHresult(e error, hresult Hresult) error { - return &wrappingHresultError{ - cause: e, - hresult: hresult, - } +// WrapHresult produces a new error with the given HRESULT wrapping the given error. +func WrapHresult(cause error, hresult Hresult) error { + return &wrappingHresultError{cause: cause, hresult: hresult} } -// GetHresult iterates through the error's cause stack (similar to how the -// Cause function in github.com/pkg/errors operates). At the first error it -// encounters which implements the Hresult() method, it return's that error's -// HRESULT. This allows errors higher up in the cause stack to shadow the -// HRESULTs of errors lower down. +// GetHresult finds the first Hresult in the error chain using errors.As compatible loop. func GetHresult(e error) (Hresult, error) { - type hresulter interface { - Hresult() Hresult - } - type causer interface { - Cause() error - } - cause := e - for cause != nil { - herr, ok := cause.(hresulter) - if ok { + for e != nil { + if herr, ok := e.(Hresulter); ok { return herr.Hresult(), nil } - cerr, ok := cause.(causer) - if !ok { - break - } - cause = cerr.Cause() + e = errors.Unwrap(e) } - return -1, errors.Errorf("no HRESULT found in cause stack for error %s", e) + return 0, fmt.Errorf("no HRESULT found in error chain: %w", e) } + +// BaseStackTrace is removed as pkg/errors.StackTrace is deprecated. +// Stack traces can be obtained using %+v formatting on wrapped errors. +// TODO: if runtime/callers stack needed, add custom stack capturer. diff --git a/internal/gcs/guestconnection.go b/internal/gcs/guestconnection.go index 35e6709d15..4225bad0d2 100644 --- a/internal/gcs/guestconnection.go +++ b/internal/gcs/guestconnection.go @@ -13,6 +13,8 @@ import ( "strings" "sync" + "errors" + "github.com/Microsoft/go-winio" "github.com/Microsoft/go-winio/pkg/guid" "github.com/Microsoft/hcsshim/internal/cow" @@ -21,7 +23,6 @@ import ( "github.com/Microsoft/hcsshim/internal/log" "github.com/Microsoft/hcsshim/internal/logfields" "github.com/Microsoft/hcsshim/internal/oc" - "github.com/pkg/errors" "github.com/sirupsen/logrus" "go.opencensus.io/trace" ) diff --git a/internal/guest/storage/crypt/crypt_test.go b/internal/guest/storage/crypt/crypt_test.go index b3db41d8a0..1b199dbd47 100644 --- a/internal/guest/storage/crypt/crypt_test.go +++ b/internal/guest/storage/crypt/crypt_test.go @@ -7,7 +7,7 @@ import ( "context" "testing" - "github.com/pkg/errors" + "errors" ) const tempDir = "/tmp/dir/" diff --git a/internal/jobcontainers/env.go b/internal/jobcontainers/env.go index 25f1bab64d..43e1cc83f4 100644 --- a/internal/jobcontainers/env.go +++ b/internal/jobcontainers/env.go @@ -6,7 +6,8 @@ import ( "unicode/utf16" "unsafe" - "github.com/pkg/errors" + "errors" + "golang.org/x/sys/windows" ) diff --git a/internal/layers/lcow.go b/internal/layers/lcow.go index b1385fa4ac..d2262bc1f4 100644 --- a/internal/layers/lcow.go +++ b/internal/layers/lcow.go @@ -10,9 +10,10 @@ import ( "path/filepath" "strings" + "errors" + "github.com/Microsoft/go-winio/pkg/fs" "github.com/containerd/containerd/api/types" - "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/Microsoft/hcsshim/internal/guestpath" diff --git a/internal/memory/pool.go b/internal/memory/pool.go index 6d39ca3bf9..a44388f4bd 100644 --- a/internal/memory/pool.go +++ b/internal/memory/pool.go @@ -1,7 +1,7 @@ package memory import ( - "github.com/pkg/errors" + "errors" ) const ( diff --git a/internal/regopolicyinterpreter/regopolicyinterpreter.go b/internal/regopolicyinterpreter/regopolicyinterpreter.go index 66f62c5114..07bcc2dbf8 100644 --- a/internal/regopolicyinterpreter/regopolicyinterpreter.go +++ b/internal/regopolicyinterpreter/regopolicyinterpreter.go @@ -10,11 +10,12 @@ import ( "os" "sync" + "errors" + "github.com/open-policy-agent/opa/ast" "github.com/open-policy-agent/opa/rego" "github.com/open-policy-agent/opa/storage/inmem" "github.com/open-policy-agent/opa/topdown" - "github.com/pkg/errors" ) type LogLevel int diff --git a/internal/shim/util_windows.go b/internal/shim/util_windows.go index 268744a284..f8f47eff5f 100644 --- a/internal/shim/util_windows.go +++ b/internal/shim/util_windows.go @@ -27,8 +27,9 @@ import ( "syscall" "time" + "errors" + winio "github.com/Microsoft/go-winio" - "github.com/pkg/errors" ) const shimBinaryFormat = "containerd-shim-%s-%s.exe" diff --git a/internal/verity/verity.go b/internal/verity/verity.go index 795a6427e8..8a57e74ed9 100644 --- a/internal/verity/verity.go +++ b/internal/verity/verity.go @@ -9,7 +9,6 @@ import ( "github.com/Microsoft/hcsshim/ext4/tar2ext4" "github.com/Microsoft/hcsshim/internal/log" "github.com/Microsoft/hcsshim/internal/protocol/guestresource" - "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -36,7 +35,7 @@ func ReadVeritySuperBlock(ctx context.Context, layerPath string) (*guestresource dmvsb, err := dmverity.ReadDMVerityInfo(layerPath, ext4SizeInBytes) if err != nil { - return nil, errors.Wrap(err, "failed to read dm-verity super block") + return nil, fmt.Errorf("failed to read dm-verity super block: %w", err) } log.G(ctx).WithFields(logrus.Fields{ "layerPath": layerPath, diff --git a/internal/vm/vmutils/gcs_logs.go b/internal/vm/vmutils/gcs_logs.go index c680dfcd76..285ffb382b 100644 --- a/internal/vm/vmutils/gcs_logs.go +++ b/internal/vm/vmutils/gcs_logs.go @@ -12,7 +12,8 @@ import ( "github.com/Microsoft/hcsshim/internal/log" "github.com/Microsoft/hcsshim/internal/logfields" - "github.com/pkg/errors" + "errors" + "github.com/sirupsen/logrus" "golang.org/x/sys/windows" ) diff --git a/pkg/securitypolicy/rego_utils_test.go b/pkg/securitypolicy/rego_utils_test.go index 5ac12a5a0a..d8c961a810 100644 --- a/pkg/securitypolicy/rego_utils_test.go +++ b/pkg/securitypolicy/rego_utils_test.go @@ -21,13 +21,14 @@ import ( "testing" "time" + "errors" + "github.com/Microsoft/hcsshim/internal/guestpath" "github.com/Microsoft/hcsshim/internal/protocol/guestrequest" rpi "github.com/Microsoft/hcsshim/internal/regopolicyinterpreter" "github.com/blang/semver/v4" "github.com/open-policy-agent/opa/rego" oci "github.com/opencontainers/runtime-spec/specs-go" - "github.com/pkg/errors" ) const ( diff --git a/pkg/securitypolicy/securitypolicy_options.go b/pkg/securitypolicy/securitypolicy_options.go index 35dd755367..ec5da0262e 100644 --- a/pkg/securitypolicy/securitypolicy_options.go +++ b/pkg/securitypolicy/securitypolicy_options.go @@ -11,6 +11,8 @@ import ( "sync" "time" + "errors" + "github.com/Microsoft/cosesign1go/pkg/cosesign1" didx509resolver "github.com/Microsoft/didx509go/pkg/did-x509-resolver" "github.com/Microsoft/hcsshim/internal/log" @@ -18,7 +20,6 @@ import ( "github.com/Microsoft/hcsshim/pkg/amdsevsnp" "github.com/Microsoft/hcsshim/pkg/annotations" "github.com/opencontainers/runtime-spec/specs-go" - "github.com/pkg/errors" "github.com/sirupsen/logrus" ) diff --git a/pkg/securitypolicy/securitypolicyenforcer.go b/pkg/securitypolicy/securitypolicyenforcer.go index 6cae97118d..d826c4f8d9 100644 --- a/pkg/securitypolicy/securitypolicyenforcer.go +++ b/pkg/securitypolicy/securitypolicyenforcer.go @@ -5,9 +5,10 @@ import ( "fmt" "syscall" + "errors" + "github.com/Microsoft/hcsshim/internal/protocol/guestrequest" oci "github.com/opencontainers/runtime-spec/specs-go" - "github.com/pkg/errors" ) type createEnforcerFunc func(base64EncodedPolicy string, criMounts, criPrivilegedMounts []oci.Mount, maxErrorMessageLength int) (SecurityPolicyEnforcer, error) diff --git a/pkg/securitypolicy/securitypolicyenforcer_rego.go b/pkg/securitypolicy/securitypolicyenforcer_rego.go index 1da5d78a0e..ea7883c893 100644 --- a/pkg/securitypolicy/securitypolicyenforcer_rego.go +++ b/pkg/securitypolicy/securitypolicyenforcer_rego.go @@ -14,12 +14,13 @@ import ( "strings" "syscall" + "errors" + "github.com/Microsoft/hcsshim/internal/guestpath" hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2" "github.com/Microsoft/hcsshim/internal/log" rpi "github.com/Microsoft/hcsshim/internal/regopolicyinterpreter" oci "github.com/opencontainers/runtime-spec/specs-go" - "github.com/pkg/errors" ) const regoEnforcerName = "rego" diff --git a/test/runhcs/e2e_matrix_test.go b/test/runhcs/e2e_matrix_test.go index f566441f54..8c5a3a78b2 100644 --- a/test/runhcs/e2e_matrix_test.go +++ b/test/runhcs/e2e_matrix_test.go @@ -15,6 +15,8 @@ import ( "syscall" "testing" + "errors" + "github.com/Microsoft/go-winio/vhd" "github.com/Microsoft/hcsshim/osversion" runhcs "github.com/Microsoft/hcsshim/pkg/go-runhcs" @@ -23,7 +25,6 @@ import ( "github.com/Microsoft/hcsshim/test/pkg/require" runc "github.com/containerd/go-runc" "github.com/opencontainers/runtime-tools/generate" - "github.com/pkg/errors" "golang.org/x/sync/errgroup" )