Skip to content

Commit 2d2c090

Browse files
committed
use errors.Is instead of comparison with errors.Cause
Signed-off-by: Matthieu MOREL <[email protected]>
1 parent 99a2cd2 commit 2d2c090

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+70
-3248
lines changed

agent/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package agent
22

33
import (
4+
"errors"
5+
46
"github.com/docker/go-events"
57
"github.com/moby/swarmkit/v2/agent/exec"
68
"github.com/moby/swarmkit/v2/api"
79
"github.com/moby/swarmkit/v2/connectionbroker"
8-
"github.com/pkg/errors"
910
bolt "go.etcd.io/bbolt"
1011
"google.golang.org/grpc/credentials"
1112
)

agent/exec/controller.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package exec
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"time"
78

89
"github.com/moby/swarmkit/v2/api"
910
"github.com/moby/swarmkit/v2/api/equality"
1011
"github.com/moby/swarmkit/v2/log"
1112
"github.com/moby/swarmkit/v2/protobuf/ptypes"
12-
"github.com/pkg/errors"
1313
)
1414

1515
// Controller controls execution of a task.
@@ -197,7 +197,7 @@ func Do(ctx context.Context, task *api.Task, ctlr Controller) (*api.TaskStatus,
197197
exitCode = ec.ExitCode()
198198
}
199199

200-
if cause := errors.Cause(err); cause == context.DeadlineExceeded || cause == context.Canceled {
200+
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
201201
return retry()
202202
}
203203

@@ -355,6 +355,5 @@ func logStateChange(ctx context.Context, desired, previous, next api.TaskState)
355355
}
356356

357357
func contextDoneError(err error) bool {
358-
cause := errors.Cause(err)
359-
return cause == context.Canceled || cause == context.DeadlineExceeded
358+
return errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded)
360359
}

agent/exec/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package exec
22

3-
import "github.com/pkg/errors"
3+
import "errors"
44

55
var (
66
// ErrRuntimeUnsupported encountered when a task requires a runtime

ca/certificates.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"crypto/x509"
1313
"encoding/asn1"
1414
"encoding/pem"
15+
"errors"
1516
"fmt"
1617
"io"
1718
"os"
@@ -30,7 +31,6 @@ import (
3031
"github.com/moby/swarmkit/v2/connectionbroker"
3132
"github.com/moby/swarmkit/v2/ioutils"
3233
"github.com/opencontainers/go-digest"
33-
"github.com/pkg/errors"
3434
"google.golang.org/grpc"
3535
"google.golang.org/grpc/codes"
3636
"google.golang.org/grpc/credentials"

ca/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
cryptorand "crypto/rand"
66
"crypto/tls"
77
"crypto/x509"
8+
"errors"
89
"fmt"
910
"math/big"
1011
"math/rand"
@@ -16,7 +17,6 @@ import (
1617
cfconfig "github.com/cloudflare/cfssl/config"
1718
events "github.com/docker/go-events"
1819
"github.com/opencontainers/go-digest"
19-
"github.com/pkg/errors"
2020
"google.golang.org/grpc/credentials"
2121

2222
"github.com/moby/swarmkit/v2/api"
@@ -513,8 +513,8 @@ func (rootCA RootCA) CreateSecurityConfig(ctx context.Context, krw *KeyReadWrite
513513

514514
proposedRole := ManagerRole
515515
tlsKeyPair, issuerInfo, err := rootCA.IssueAndSaveNewCertificates(krw, cn, proposedRole, org)
516-
switch errors.Cause(err) {
517-
case ErrNoValidSigner:
516+
switch {
517+
case errors.Is(err, ErrNoValidSigner):
518518
config.RetryInterval = GetCertRetryInterval
519519
// Request certificate issuance from a remote CA.
520520
// Last argument is nil because at this point we don't have any valid TLS creds
@@ -523,7 +523,7 @@ func (rootCA RootCA) CreateSecurityConfig(ctx context.Context, krw *KeyReadWrite
523523
log.G(ctx).WithError(err).Error("failed to request and save new certificate")
524524
return nil, nil, err
525525
}
526-
case nil:
526+
case err == nil:
527527
log.G(ctx).WithFields(log.Fields{
528528
"node.id": cn,
529529
"node.role": proposedRole,

ca/config_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"crypto/tls"
77
"crypto/x509"
8+
"errors"
89
"net"
910
"os"
1011
"path/filepath"
@@ -25,7 +26,6 @@ import (
2526
"github.com/moby/swarmkit/v2/manager/state"
2627
"github.com/moby/swarmkit/v2/manager/state/store"
2728
"github.com/moby/swarmkit/v2/testutils"
28-
"github.com/pkg/errors"
2929
"github.com/stretchr/testify/assert"
3030
"github.com/stretchr/testify/require"
3131
)
@@ -236,19 +236,22 @@ func TestLoadSecurityConfigExpiredCert(t *testing.T) {
236236

237237
_, _, err = ca.LoadSecurityConfig(tc.Context, tc.RootCA, krw, false)
238238
require.Error(t, err)
239-
require.IsType(t, x509.CertificateInvalidError{}, errors.Cause(err))
239+
var cie1 x509.CertificateInvalidError
240+
require.ErrorAs(t, err, &cie1)
240241

241242
_, _, err = ca.LoadSecurityConfig(tc.Context, tc.RootCA, krw, true)
242243
require.Error(t, err)
243-
require.IsType(t, x509.CertificateInvalidError{}, errors.Cause(err))
244+
var cie2 x509.CertificateInvalidError
245+
require.ErrorAs(t, err, &cie2)
244246

245247
// a cert that is expired is not valid if expiry is not allowed
246248
invalidCert = cautils.ReDateCert(t, certBytes, tc.RootCA.Certs, s.Key, now.Add(-2*time.Minute), now.Add(-1*time.Minute))
247249
require.NoError(t, os.WriteFile(tc.Paths.Node.Cert, invalidCert, 0o700))
248250

249251
_, _, err = ca.LoadSecurityConfig(tc.Context, tc.RootCA, krw, false)
250252
require.Error(t, err)
251-
require.IsType(t, x509.CertificateInvalidError{}, errors.Cause(err))
253+
var cie3 x509.CertificateInvalidError
254+
require.ErrorAs(t, err, &cie3)
252255

253256
// but it is valid if expiry is allowed
254257
_, cancel, err := ca.LoadSecurityConfig(tc.Context, tc.RootCA, krw, true)
@@ -798,7 +801,8 @@ func TestRenewTLSConfigUpdatesRootNonUnknownAuthError(t *testing.T) {
798801

799802
err = ca.RenewTLSConfigNow(tc.Context, secConfig, fakeCAServer.getConnBroker(), tc.Paths.RootCA)
800803
require.Error(t, err)
801-
require.IsType(t, x509.CertificateInvalidError{}, errors.Cause(err))
804+
var cie x509.CertificateInvalidError
805+
require.ErrorAs(t, err, &cie)
802806
require.NoError(t, <-signErr)
803807
}
804808

ca/external.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"encoding/hex"
1010
"encoding/json"
1111
"encoding/pem"
12+
"errors"
1213
"fmt"
1314
"io"
1415
"net/http"
@@ -20,7 +21,6 @@ import (
2021
"github.com/cloudflare/cfssl/csr"
2122
"github.com/cloudflare/cfssl/signer"
2223
"github.com/moby/swarmkit/v2/log"
23-
"github.com/pkg/errors"
2424
"golang.org/x/net/context/ctxhttp"
2525
)
2626

ca/keyreadwriter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ca
33
import (
44
"crypto/x509"
55
"encoding/pem"
6+
"errors"
67
"fmt"
78
"os"
89
"path/filepath"
@@ -15,7 +16,6 @@ import (
1516
"github.com/moby/swarmkit/v2/ca/keyutils"
1617
"github.com/moby/swarmkit/v2/ca/pkcs8"
1718
"github.com/moby/swarmkit/v2/ioutils"
18-
"github.com/pkg/errors"
1919
)
2020

2121
const (

ca/reconciler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ca
33
import (
44
"bytes"
55
"context"
6+
"errors"
67
"fmt"
78
"reflect"
89
"sync"
@@ -13,7 +14,6 @@ import (
1314
"github.com/moby/swarmkit/v2/api/equality"
1415
"github.com/moby/swarmkit/v2/log"
1516
"github.com/moby/swarmkit/v2/manager/state/store"
16-
"github.com/pkg/errors"
1717
)
1818

1919
// IssuanceStateRotateMaxBatchSize is the maximum number of nodes we'll tell to rotate their certificates in any given update

ca/renewer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package ca
22

33
import (
44
"context"
5+
"errors"
56
"sync"
67
"time"
78

89
"github.com/docker/go-events"
910
"github.com/moby/swarmkit/v2/connectionbroker"
1011
"github.com/moby/swarmkit/v2/log"
11-
"github.com/pkg/errors"
1212
)
1313

1414
// RenewTLSExponentialBackoff sets the exponential backoff when trying to renew TLS certificates that have expired

0 commit comments

Comments
 (0)