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

chore: Errkit migration 8.3 - migrate remaining piece of pkg/kube to errkit #3256

Merged
merged 3 commits into from
Nov 29, 2024
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
6 changes: 3 additions & 3 deletions pkg/kube/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/kube/log_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package kube
import (
"bytes"
"context"
"errors"
"io"

"github.com/kanisterio/errkit"
"gopkg.in/check.v1"
"k8s.io/client-go/rest"
)
Expand Down Expand Up @@ -39,7 +39,7 @@ func (frw *fakeResponseWrapper) Stream(context.Context) (io.ReadCloser, error) {
}

func (s *LogReaderSuite) TestLogReader(c *check.C) {
err := errors.New("TEST")
err := errkit.New("TEST")
for _, tc := range []struct {
rw *fakeResponseWrapper
err error
Expand Down
5 changes: 2 additions & 3 deletions pkg/kube/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/gofrs/uuid"
json "github.com/json-iterator/go"
"github.com/kanisterio/errkit"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -451,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
}
Expand All @@ -470,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
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/kube/pod_command_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
29 changes: 14 additions & 15 deletions pkg/kube/pod_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package kube

import (
"context"
"errors"
"fmt"
"os"
"time"
Expand Down Expand Up @@ -46,14 +45,14 @@ func (s *PodControllerTestSuite) TestPodControllerStartPod(c *check.C) {
ctx := context.Background()
cli := fake.NewSimpleClientset()

simulatedError := errors.New("SimulatedError")
simulatedError := errkit.New("SimulatedError")

cases := map[string]func(prp *FakePodControllerProcessor, pr PodController){
"Pod creation failure": func(pcp *FakePodControllerProcessor, pc PodController) {
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,
Expand Down Expand Up @@ -81,11 +80,11 @@ func (s *PodControllerTestSuite) TestPodControllerStartPod(c *check.C) {

prp.InCreatePodOptions = nil
prp.CreatePodRet = nil
prp.CreatePodErr = errors.New("CreatePod should not be invoked")
prp.CreatePodErr = errkit.New("CreatePod should not be invoked")

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)
},
}
Expand All @@ -108,13 +107,13 @@ func (s *PodControllerTestSuite) TestPodControllerWaitPod(c *check.C) {
ctx := context.Background()
cli := fake.NewSimpleClientset()

simulatedError := errkit.Wrap(errors.New("SimulatedError"), "Wrapped")
simulatedError := errkit.Wrap(errkit.New("SimulatedError"), "Wrapped")

cases := map[string]func(pcp *FakePodControllerProcessor, pc PodController){
"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) {
Expand All @@ -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
Expand Down Expand Up @@ -169,13 +168,13 @@ func (s *PodControllerTestSuite) TestPodControllerStopPod(c *check.C) {
cli := fake.NewSimpleClientset()

untouchedStr := "DEADBEEF"
simulatedError := errors.New("SimulatedError")
simulatedError := errkit.New("SimulatedError")

cases := map[string]func(pcp *FakePodControllerProcessor, pc PodController){
"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)
},
Expand All @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand Down
5 changes: 2 additions & 3 deletions pkg/kube/pod_file_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package kube
import (
"bytes"
"context"
"errors"
"io"
"os"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions pkg/kube/pod_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"os"
"path"

"github.com/pkg/errors"
"github.com/kanisterio/errkit"
"gopkg.in/check.v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -191,10 +191,10 @@ func afterPodRunTestKeyPresentFunc(labelKey, expectedLabelValue string, isLabelE
<-ch
labelValue, found := pc.Pod().Labels[labelKey]
if found != isLabelExpected {
return nil, errors.New("Got different label than expected")
return nil, errkit.New("Got different label than expected")
}
if isLabelExpected && labelValue != expectedLabelValue {
return nil, errors.New("Found label doesn't match with expected label")
return nil, errkit.New("Found label doesn't match with expected label")
}
return nil, nil
}
Expand Down
25 changes: 21 additions & 4 deletions pkg/kube/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -315,7 +316,7 @@ func (s *PodSuite) TestPodWithFilesystemModeVolumes(c *check.C) {
ca := action.(testing.CreateAction)
p = ca.GetObject().(*corev1.Pod)
if len(p.Spec.Volumes[0].Name) > 63 {
return true, nil, errors.New("spec.volumes[0].name must be no more than 63 characters")
return true, nil, errkit.New("spec.volumes[0].name must be no more than 63 characters")
}
return false, nil, nil
})
Expand Down Expand Up @@ -364,7 +365,7 @@ func (s *PodSuite) TestPodWithFilesystemModeReadOnlyVolumes(c *check.C) {
ca := action.(testing.CreateAction)
p = ca.GetObject().(*corev1.Pod)
if len(p.Spec.Volumes[0].Name) > 63 {
return true, nil, errors.New("spec.volumes[0].name must be no more than 63 characters")
return true, nil, errkit.New("spec.volumes[0].name must be no more than 63 characters")
}
return false, nil, nil
})
Expand Down Expand Up @@ -415,7 +416,7 @@ func (s *PodSuite) TestPodWithBlockModeVolumes(c *check.C) {
ca := action.(testing.CreateAction)
p = ca.GetObject().(*corev1.Pod)
if len(p.Spec.Volumes[0].Name) > 63 {
return true, nil, errors.New("spec.volumes[0].name must be no more than 63 characters")
return true, nil, errkit.New("spec.volumes[0].name must be no more than 63 characters")
}
return false, nil, nil
})
Expand Down Expand Up @@ -1244,3 +1245,19 @@ func (s *PodSuite) TestAddAnnotations(c *check.C) {
c.Assert(tc.podOptions, check.DeepEquals, tc.expectedPodOptions)
}
}

// TestErrkitApiErrorsWrapping verifies that apierrors wrapped with errkit.Wrap are still matchable using apierrors matchers
func (s *PodSuite) TestErrkitApiErrorsWrapping(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, errkit.Wrap(apierrors.NewNotFound(action.GetResource().GroupResource(), action.GetSubresource()), "Some context")
})

_, err := fakeClient.CoreV1().PersistentVolumeClaims("abc").Get(context.TODO(), "def", metav1.GetOptions{})
if err != nil {
c.Assert(apierrors.IsNotFound(err), check.Equals, true)
}
}