diff --git a/go.mod b/go.mod index e1f59430b5..c3304ea584 100644 --- a/go.mod +++ b/go.mod @@ -33,7 +33,7 @@ require ( github.com/jpillora/backoff v1.0.0 github.com/json-iterator/go v1.1.12 github.com/kanisterio/errkit v0.0.2 - github.com/kanisterio/safecli v0.0.8 + github.com/kanisterio/safecli v0.0.9-0.20241121134252-2ffd9801dcad github.com/kopia/kopia v0.17.1-0.20240927044625-1bceb7155ede github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0 github.com/lib/pq v1.10.9 @@ -42,7 +42,7 @@ require ( //pinned openshift to release-4.5 branch github.com/openshift/api v0.0.0-20231222123017-053aee22b4b4 github.com/openshift/client-go v0.0.0-20231221125933-2aa81c72f992 - github.com/pkg/errors v0.9.1 + github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.20.5 github.com/prometheus/client_model v0.6.1 github.com/sirupsen/logrus v1.9.3 diff --git a/go.sum b/go.sum index 7512c046b2..8bd11209a6 100644 --- a/go.sum +++ b/go.sum @@ -375,8 +375,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1 github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/kanisterio/errkit v0.0.2 h1:3v3HGz9lHIbZR6Jr9qIJpRjaqUX0rsJSLMEQGsMHiUk= github.com/kanisterio/errkit v0.0.2/go.mod h1:ViQ6kPJ2gTJDEvRytmwde7pzG9/sndObF9BPZoEZixc= -github.com/kanisterio/safecli v0.0.8 h1:flvTiGksy/a0+zvqjaBSJwxESu/nFcG65yttmR0XwiA= -github.com/kanisterio/safecli v0.0.8/go.mod h1:KBraqj8mdv2cwAr9wecknGUb8jztTzUik0r7uE6yRA8= +github.com/kanisterio/safecli v0.0.9-0.20241121134252-2ffd9801dcad h1:JaC21jpch32i3hfUE3m1So06CvPoi+sdQ2KKIRCRKCo= +github.com/kanisterio/safecli v0.0.9-0.20241121134252-2ffd9801dcad/go.mod h1:y1oYVoT2eqsmySCIS5yOzrxaYVywlMSEdWEKfLhBjgs= github.com/kastenhq/check v0.0.0-20180626002341-0264cfcea734 h1:qulsCaCv+O2y9/sQ9nd5KChnAgFOWakTHQ9ZADjs6DQ= github.com/kastenhq/check v0.0.0-20180626002341-0264cfcea734/go.mod h1:rdqSnvOJuKCPFW/h2rVLuXOAkRnHHdp9PZcKx4HCoDM= github.com/kastenhq/stow v0.2.6-kasten.1.0.20231101232131-9321daa23aae h1:2cl4yuAJpdmLCx7G8eIsfNlQBLEfw0JDj6mTTyqc5qg= diff --git a/pkg/kanx/server.go b/pkg/kanx/server.go index 50883ff938..eab13e1f22 100644 --- a/pkg/kanx/server.go +++ b/pkg/kanx/server.go @@ -3,7 +3,6 @@ package kanx import ( "bytes" "context" - "fmt" "io" "net" "os" @@ -12,7 +11,7 @@ import ( "syscall" "time" - "github.com/pkg/errors" + "github.com/kanisterio/errkit" "google.golang.org/grpc" "github.com/kanisterio/kanister/pkg/field" @@ -117,12 +116,12 @@ func (s *processServiceServer) ListProcesses(lpr *ListProcessesRequest, lps Proc return nil } -var errProcessNotFound = fmt.Errorf("Process not found") +var errProcessNotFound = errkit.NewSentinelErr("Process not found") func (s *processServiceServer) Stdout(por *ProcessOutputRequest, ss ProcessService_StdoutServer) error { p, ok := s.processes[por.Pid] if !ok { - return errors.WithStack(errProcessNotFound) + return errkit.WithStack(errProcessNotFound) } fh, err := os.Open(p.stdout.Name()) if err != nil { @@ -134,7 +133,7 @@ func (s *processServiceServer) Stdout(por *ProcessOutputRequest, ss ProcessServi func (s *processServiceServer) Stderr(por *ProcessOutputRequest, ss ProcessService_StderrServer) error { p, ok := s.processes[por.Pid] if !ok { - return errors.WithStack(errProcessNotFound) + return errkit.WithStack(errProcessNotFound) } fh, err := os.Open(p.stderr.Name()) if err != nil { diff --git a/pkg/kopia/cli/internal/test/command_suite.go b/pkg/kopia/cli/internal/test/command_suite.go index 57a8c2f96d..660909dc6d 100644 --- a/pkg/kopia/cli/internal/test/command_suite.go +++ b/pkg/kopia/cli/internal/test/command_suite.go @@ -15,9 +15,9 @@ package test import ( + "github.com/kanisterio/errkit" "github.com/kanisterio/safecli" "github.com/kanisterio/safecli/test" - "github.com/pkg/errors" "gopkg.in/check.v1" ) @@ -62,8 +62,7 @@ func (t *CommandTest) assertNoError(c *check.C, err error) { // assertError checks the error against ExpectedErr. func (t *CommandTest) assertError(c *check.C, err error) { - actualErr := errors.Cause(err) - c.Assert(actualErr, check.Equals, t.ExpectedErr) + c.Assert(errkit.Is(err, t.ExpectedErr), check.Equals, true) } // assertCLI asserts the builder's CLI output against ExpectedCLI. diff --git a/pkg/ksprig/fipsonly_sprig_test.go b/pkg/ksprig/fipsonly_sprig_test.go index 7a2ea4968c..e45409c72e 100644 --- a/pkg/ksprig/fipsonly_sprig_test.go +++ b/pkg/ksprig/fipsonly_sprig_test.go @@ -15,11 +15,11 @@ package ksprig_test import ( - "errors" "strings" "testing" "text/template" + "github.com/kanisterio/errkit" "gopkg.in/check.v1" "github.com/kanisterio/kanister/pkg/ksprig" @@ -74,7 +74,7 @@ func (f *FipsOnlySprigSuite) TestUnsupportedTxtFuncMapUsage(c *check.C) { err = temp.Execute(nil, "") var sprigErr ksprig.UnsupportedSprigUsageErr - c.Assert(errors.As(err, &sprigErr), check.Equals, true) + c.Assert(errkit.As(err, &sprigErr), check.Equals, true) c.Assert(sprigErr.Usage, check.Equals, tc.usageErr) } } diff --git a/pkg/kube/exec_test.go b/pkg/kube/exec_test.go index 9242abb4ec..2475a54fad 100644 --- a/pkg/kube/exec_test.go +++ b/pkg/kube/exec_test.go @@ -20,10 +20,10 @@ package kube import ( "bytes" "context" - "errors" "strings" "time" + "github.com/kanisterio/errkit" "gopkg.in/check.v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -191,7 +191,7 @@ func (s *ExecSuite) TestErrorInExecWithOptions(c *check.C) { c.Assert(err1, check.Not(check.IsNil)) var ee1 *ExecError - ok := errors.As(err1, &ee1) + ok := errkit.As(err1, &ee1) c.Assert(ok, check.Equals, true) c.Assert(ee1.Stdout(), check.Not(check.Equals), testCase.expectedOut) c.Assert(ee1.Stderr(), check.Not(check.Equals), testCase.expectedErr) @@ -208,7 +208,7 @@ func (s *ExecSuite) TestErrorInExecWithOptions(c *check.C) { c.Assert(err2, check.Not(check.IsNil)) var ee2 *ExecError - ok = errors.As(err2, &ee2) + ok = errkit.As(err2, &ee2) c.Assert(ok, check.Equals, true) // When error happens, stdout/stderr buffers should contain all lines produced by an app diff --git a/pkg/kube/pod.go b/pkg/kube/pod.go index 5fcb0bd162..09be10c94c 100644 --- a/pkg/kube/pod.go +++ b/pkg/kube/pod.go @@ -450,7 +450,7 @@ func checkPVCAndPVStatus(ctx context.Context, vol corev1.Volume, p *corev1.Pod, pvcName := vol.VolumeSource.PersistentVolumeClaim.ClaimName pvc, err := cli.CoreV1().PersistentVolumeClaims(namespace).Get(ctx, pvcName, metav1.GetOptions{}) if err != nil { - if apierrors.IsNotFound(errors.Cause(err)) { + if apierrors.IsNotFound(err) { // Do not return err, wait for timeout, since sometimes in case of statefulsets, they trigger creation of a volume return nil } @@ -469,7 +469,7 @@ func checkPVCAndPVStatus(ctx context.Context, vol corev1.Volume, p *corev1.Pod, } pv, err := cli.CoreV1().PersistentVolumes().Get(ctx, pvName, metav1.GetOptions{}) if err != nil { - if apierrors.IsNotFound(errors.Cause(err)) { + if apierrors.IsNotFound(err) { // wait for timeout return nil } diff --git a/pkg/kube/pod_command_executor_test.go b/pkg/kube/pod_command_executor_test.go index bc037dad47..064e6b35c0 100644 --- a/pkg/kube/pod_command_executor_test.go +++ b/pkg/kube/pod_command_executor_test.go @@ -17,11 +17,11 @@ package kube import ( "bytes" "context" - "errors" "os" "sync" "time" + "github.com/kanisterio/errkit" "gopkg.in/check.v1" "k8s.io/client-go/kubernetes/fake" ) @@ -129,7 +129,7 @@ func (s *PodCommandExecutorTestSuite) TestPodRunnerExec(c *check.C) { prp.execWithOptionsSyncEnd.Sync() c.Assert(err, check.Not(check.IsNil)) - c.Assert(errors.Is(err, context.DeadlineExceeded), check.Equals, true) + c.Assert(errkit.Is(err, context.DeadlineExceeded), check.Equals, true) }, "Cancelled": func(ctx context.Context, pr PodCommandExecutor, prp *fakePodCommandExecutorProcessor) { var err error @@ -151,7 +151,7 @@ func (s *PodCommandExecutorTestSuite) TestPodRunnerExec(c *check.C) { prp.execWithOptionsSyncEnd.Sync() // Release ExecWithOptions c.Assert(err, check.Not(check.IsNil)) - c.Assert(errors.Is(err, context.Canceled), check.Equals, true) + c.Assert(errkit.Is(err, context.Canceled), check.Equals, true) }, "Successful execution": func(ctx context.Context, pr PodCommandExecutor, prp *fakePodCommandExecutorProcessor) { var err error diff --git a/pkg/kube/pod_controller_test.go b/pkg/kube/pod_controller_test.go index 2b01d5122f..1555070569 100644 --- a/pkg/kube/pod_controller_test.go +++ b/pkg/kube/pod_controller_test.go @@ -16,7 +16,6 @@ package kube import ( "context" - "errors" "fmt" "os" "time" @@ -53,7 +52,7 @@ func (s *PodControllerTestSuite) TestPodControllerStartPod(c *check.C) { pcp.CreatePodErr = simulatedError err := pc.StartPod(ctx) c.Assert(err, check.Not(check.IsNil)) - c.Assert(errors.Is(err, simulatedError), check.Equals, true) + c.Assert(errkit.Is(err, simulatedError), check.Equals, true) c.Assert(pcp.InCreatePodOptions, check.DeepEquals, &PodOptions{ Namespace: podControllerNS, Name: podControllerPodName, @@ -85,7 +84,7 @@ func (s *PodControllerTestSuite) TestPodControllerStartPod(c *check.C) { err = pr.StartPod(ctx) c.Assert(err, check.Not(check.IsNil)) - c.Assert(errors.Is(err, ErrPodControllerPodAlreadyStarted), check.Equals, true) + c.Assert(errkit.Is(err, ErrPodControllerPodAlreadyStarted), check.Equals, true) c.Assert(prp.InCreatePodOptions, check.IsNil) }, } @@ -114,7 +113,7 @@ func (s *PodControllerTestSuite) TestPodControllerWaitPod(c *check.C) { "Waiting failed because pod not started yet": func(pcp *FakePodControllerProcessor, pc PodController) { err := pc.WaitForPodReady(ctx) c.Assert(err, check.Not(check.IsNil)) - c.Assert(errors.Is(err, ErrPodControllerPodNotStarted), check.Equals, true) + c.Assert(errkit.Is(err, ErrPodControllerPodNotStarted), check.Equals, true) c.Assert(pcp.InCreatePodOptions, check.IsNil) }, "Waiting failed due to timeout": func(pcp *FakePodControllerProcessor, pc PodController) { @@ -132,7 +131,7 @@ func (s *PodControllerTestSuite) TestPodControllerWaitPod(c *check.C) { c.Assert(err, check.Not(check.IsNil)) c.Assert(pcp.InWaitForPodReadyPodName, check.Equals, podControllerPodName) c.Assert(pcp.InWaitForPodReadyNamespace, check.Equals, podControllerNS) - c.Assert(errors.Is(err, pcp.WaitForPodReadyErr), check.Equals, true) + c.Assert(errkit.Is(err, pcp.WaitForPodReadyErr), check.Equals, true) c.Assert(err.Error(), check.Equals, fmt.Sprintf("Pod failed to become ready in time: %s", simulatedError.Error())) // Check that POD deletion was also invoked with expected arguments @@ -175,7 +174,7 @@ func (s *PodControllerTestSuite) TestPodControllerStopPod(c *check.C) { "Pod not started yet": func(pcp *FakePodControllerProcessor, pc PodController) { err := pc.StopPod(ctx, 30*time.Second, int64(0)) c.Assert(err, check.Not(check.IsNil)) - c.Assert(errors.Is(err, ErrPodControllerPodNotStarted), check.Equals, true) + c.Assert(errkit.Is(err, ErrPodControllerPodNotStarted), check.Equals, true) c.Assert(pcp.InDeletePodPodName, check.Equals, untouchedStr) c.Assert(pcp.InDeletePodNamespace, check.Equals, untouchedStr) }, @@ -192,7 +191,7 @@ func (s *PodControllerTestSuite) TestPodControllerStopPod(c *check.C) { pcp.DeletePodErr = simulatedError err = pc.StopPod(ctx, 30*time.Second, int64(0)) c.Assert(err, check.Not(check.IsNil)) - c.Assert(errors.Is(err, simulatedError), check.Equals, true) + c.Assert(errkit.Is(err, simulatedError), check.Equals, true) }, "Pod successfully deleted": func(pcp *FakePodControllerProcessor, pc PodController) { pcp.CreatePodRet = &corev1.Pod{ @@ -239,12 +238,12 @@ func (s *PodControllerTestSuite) TestPodControllerGetCommandExecutorAndFileWrite pce, err := pc.GetCommandExecutor() c.Assert(pce, check.IsNil) c.Assert(err, check.Not(check.IsNil)) - c.Assert(errors.Is(err, ErrPodControllerPodNotStarted), check.Equals, true) + c.Assert(errkit.Is(err, ErrPodControllerPodNotStarted), check.Equals, true) pfw, err := pc.GetFileWriter() c.Assert(pfw, check.IsNil) c.Assert(err, check.Not(check.IsNil)) - c.Assert(errors.Is(err, ErrPodControllerPodNotStarted), check.Equals, true) + c.Assert(errkit.Is(err, ErrPodControllerPodNotStarted), check.Equals, true) }, "Pod not ready yet": func(pcp *FakePodControllerProcessor, pc PodController) { pcp.CreatePodRet = &corev1.Pod{ @@ -258,12 +257,12 @@ func (s *PodControllerTestSuite) TestPodControllerGetCommandExecutorAndFileWrite pce, err := pc.GetCommandExecutor() c.Assert(pce, check.IsNil) c.Assert(err, check.Not(check.IsNil)) - c.Assert(errors.Is(err, ErrPodControllerPodNotReady), check.Equals, true) + c.Assert(errkit.Is(err, ErrPodControllerPodNotReady), check.Equals, true) pfw, err := pc.GetFileWriter() c.Assert(pfw, check.IsNil) c.Assert(err, check.Not(check.IsNil)) - c.Assert(errors.Is(err, ErrPodControllerPodNotReady), check.Equals, true) + c.Assert(errkit.Is(err, ErrPodControllerPodNotReady), check.Equals, true) }, "CommandExecutor successfully returned": func(pcp *FakePodControllerProcessor, pc PodController) { pcp.CreatePodRet = &corev1.Pod{ diff --git a/pkg/kube/pod_file_writer_test.go b/pkg/kube/pod_file_writer_test.go index c9c45636b1..85e6a95e4b 100644 --- a/pkg/kube/pod_file_writer_test.go +++ b/pkg/kube/pod_file_writer_test.go @@ -17,7 +17,6 @@ package kube import ( "bytes" "context" - "errors" "io" "os" @@ -90,7 +89,7 @@ func (s *PodFileWriterTestSuite) TestPodRunnerWriteFile(c *check.C) { buf := bytes.NewBuffer([]byte("some file content")) remover, err := pfw.Write(ctx, "/path/to/file", buf) c.Assert(err, check.Not(check.IsNil)) - c.Assert(errors.Is(err, simulatedError), check.Equals, true) + c.Assert(errkit.Is(err, simulatedError), check.Equals, true) c.Assert(remover, check.IsNil) c.Assert(pfwp.podWriter.inWriteNamespace, check.Equals, podFileWriterNS) @@ -130,7 +129,7 @@ func (s *PodFileWriterTestSuite) TestPodRunnerWriteFile(c *check.C) { err = remover.Remove(ctx) c.Assert(err, check.Not(check.IsNil)) - c.Assert(errors.Is(err, simulatedError), check.Equals, true) + c.Assert(errkit.Is(err, simulatedError), check.Equals, true) c.Assert(pfwp.podWriter.inRemoveNamespace, check.Equals, podFileWriterNS) c.Assert(pfwp.podWriter.inRemovePodName, check.Equals, podFileWriterPodName) c.Assert(pfwp.podWriter.inRemoveContainerName, check.Equals, podFileWriterContainerName) diff --git a/pkg/kube/pod_test.go b/pkg/kube/pod_test.go index 191e074b3a..e88467d780 100644 --- a/pkg/kube/pod_test.go +++ b/pkg/kube/pod_test.go @@ -19,14 +19,15 @@ package kube import ( "context" - "errors" "fmt" "os" "strings" "time" + "github.com/kanisterio/errkit" "gopkg.in/check.v1" corev1 "k8s.io/api/core/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -1244,3 +1245,18 @@ func (s *PodSuite) TestAddAnnotations(c *check.C) { c.Assert(tc.podOptions, check.DeepEquals, tc.expectedPodOptions) } } + +func (s *PodSuite) TestSomething(c *check.C) { + // Create the fake client + fakeClient := fake.NewSimpleClientset() + + // Add a reactor to simulate an error when trying to get a PVC + fakeClient.PrependReactor("get", "persistentvolumeclaims", func(action testing.Action) (handled bool, ret runtime.Object, err error) { + return true, nil, apierrors.NewNotFound(action.GetResource().GroupResource(), action.GetSubresource()) + }) + + _, err := fakeClient.CoreV1().PersistentVolumeClaims("abc").Get(context.TODO(), "def", metav1.GetOptions{}) + if err != nil { + c.Assert(apierrors.IsNotFound(err), check.Equals, true) + } +} diff --git a/pkg/log/log_test.go b/pkg/log/log_test.go index 96e59a03b6..2cb20b1e76 100644 --- a/pkg/log/log_test.go +++ b/pkg/log/log_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "errors" "fmt" "net/url" "os" @@ -12,6 +11,7 @@ import ( "testing" "time" + "github.com/kanisterio/errkit" "github.com/sirupsen/logrus" "gopkg.in/check.v1" diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index ba884f2f5b..011d1debf3 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -15,9 +15,9 @@ package metrics import ( - "errors" "fmt" + "github.com/kanisterio/errkit" "github.com/prometheus/client_golang/prometheus" "gonum.org/v1/gonum/stat/combin" @@ -241,7 +241,7 @@ func registerGauge(r prometheus.Registerer, g prometheus.Gauge) prometheus.Gauge func registerMetricOrDie(r prometheus.Registerer, c prometheus.Collector) prometheus.Collector { if err := r.Register(c); err != nil { are := prometheus.AlreadyRegisteredError{} - if !errors.As(err, &are) { + if !errkit.As(err, &are) { panic(fmt.Sprintf("failed to register metric. error: %v", err)) } // Use already registered metric diff --git a/pkg/objectstore/aws.go b/pkg/objectstore/aws.go index 08317d55bd..f0e4d106b6 100644 --- a/pkg/objectstore/aws.go +++ b/pkg/objectstore/aws.go @@ -23,7 +23,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/s3" - "github.com/pkg/errors" + "github.com/kanisterio/errkit" kaws "github.com/kanisterio/kanister/pkg/aws" ) @@ -38,7 +38,8 @@ func IsBucketNotFoundError(err error) bool { if err == nil { return false } - if awsErr, ok := errors.Cause(err).(awserr.Error); ok { + var awsErr awserr.Error + if errkit.As(err, &awsErr) { code := awsErr.Code() return code == bucketNotFound || code == noSuchBucket } @@ -55,7 +56,7 @@ func awsConfig(ctx context.Context, pc ProviderConfig, s SecretAws) (*aws.Config } cfg, r, err := kaws.GetConfig(ctx, c) if err != nil { - return nil, "", errors.Wrap(err, "failed to create aws config") + return nil, "", errkit.Wrap(err, "failed to create aws config") } cfg = cfg.WithRegion(r) if pc.Endpoint != "" { diff --git a/pkg/poll/poll_test.go b/pkg/poll/poll_test.go index 90b297058e..d8be71a3ae 100644 --- a/pkg/poll/poll_test.go +++ b/pkg/poll/poll_test.go @@ -16,13 +16,13 @@ package poll import ( "context" - "errors" "fmt" "runtime" "testing" "time" "github.com/jpillora/backoff" + "github.com/kanisterio/errkit" "gopkg.in/check.v1" ) diff --git a/pkg/testing/integration_test.go b/pkg/testing/integration_test.go index 514a07c4d0..baf3ca8fc7 100644 --- a/pkg/testing/integration_test.go +++ b/pkg/testing/integration_test.go @@ -18,7 +18,8 @@ package testing import ( - context "context" + "context" + "fmt" "os" test "testing" "time"