-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(controller): Always retry when
IsTransientErr
to tolerate tran…
- Loading branch information
Showing
14 changed files
with
634 additions
and
589 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package errors | ||
|
||
import ( | ||
"net" | ||
"net/url" | ||
"strings" | ||
|
||
apierr "k8s.io/apimachinery/pkg/api/errors" | ||
|
||
argoerrs "github.com/argoproj/argo/errors" | ||
) | ||
|
||
func IsTransientErr(err error) bool { | ||
if err == nil { | ||
return false | ||
} | ||
err = argoerrs.Cause(err) | ||
return isExceededQuotaErr(err) || apierr.IsTooManyRequests(err) || isResourceQuotaConflictErr(err) || isTransientNetworkErr(err) | ||
} | ||
|
||
func isExceededQuotaErr(err error) bool { | ||
return apierr.IsForbidden(err) && strings.Contains(err.Error(), "exceeded quota") | ||
} | ||
|
||
func isResourceQuotaConflictErr(err error) bool { | ||
return apierr.IsConflict(err) && strings.Contains(err.Error(), "Operation cannot be fulfilled on resourcequota") | ||
} | ||
|
||
func isTransientNetworkErr(err error) bool { | ||
switch err.(type) { | ||
case net.Error: | ||
switch err.(type) { | ||
case *net.DNSError, *net.OpError, net.UnknownNetworkError: | ||
return true | ||
case *url.Error: | ||
// For a URL error, where it replies back "connection closed" | ||
// retry again. | ||
return strings.Contains(err.Error(), "Connection closed by foreign host") | ||
default: | ||
if strings.Contains(err.Error(), "net/http: TLS handshake timeout") { | ||
// If error is - tlsHandshakeTimeoutError, retry. | ||
return true | ||
} else if strings.Contains(err.Error(), "i/o timeout") { | ||
// If error is - tcp timeoutError, retry. | ||
return true | ||
} else if strings.Contains(err.Error(), "connection timed out") { | ||
// If err is a net.Dial timeout, retry. | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package errors | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
apierr "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
type netError string | ||
|
||
func (n netError) Error() string { return string(n) } | ||
func (n netError) Timeout() bool { return false } | ||
func (n netError) Temporary() bool { return false } | ||
|
||
var tlsHandshakeTimeoutErr net.Error = netError("net/http: TLS handshake timeout") | ||
var ioTimeoutErr net.Error = netError("i/o timeout") | ||
var connectionTimedout net.Error = netError("connection timed out") | ||
|
||
func TestIsTransientErr(t *testing.T) { | ||
t.Run("Nil", func(t *testing.T) { | ||
assert.False(t, IsTransientErr(nil)) | ||
}) | ||
t.Run("ResourceQuotaConflictErr", func(t *testing.T) { | ||
assert.False(t, IsTransientErr(apierr.NewConflict(schema.GroupResource{}, "", nil))) | ||
assert.True(t, IsTransientErr(apierr.NewConflict(schema.GroupResource{Group: "v1", Resource: "resourcequotas"}, "", nil))) | ||
}) | ||
t.Run("ExceededQuotaErr", func(t *testing.T) { | ||
assert.False(t, IsTransientErr(apierr.NewForbidden(schema.GroupResource{}, "", nil))) | ||
assert.True(t, IsTransientErr(apierr.NewForbidden(schema.GroupResource{Group: "v1", Resource: "pods"}, "", errors.New("exceeded quota")))) | ||
}) | ||
t.Run("TooManyRequestsDNS", func(t *testing.T) { | ||
assert.True(t, IsTransientErr(apierr.NewTooManyRequests("", 0))) | ||
}) | ||
t.Run("DNSError", func(t *testing.T) { | ||
assert.True(t, IsTransientErr(&net.DNSError{})) | ||
}) | ||
t.Run("OpError", func(t *testing.T) { | ||
assert.True(t, IsTransientErr(&net.OpError{})) | ||
}) | ||
t.Run("UnknownNetworkError", func(t *testing.T) { | ||
assert.True(t, IsTransientErr(net.UnknownNetworkError(""))) | ||
}) | ||
t.Run("ConnectionClosedErr", func(t *testing.T) { | ||
assert.False(t, IsTransientErr(&url.Error{Err: errors.New("")})) | ||
assert.True(t, IsTransientErr(&url.Error{Err: errors.New("Connection closed by foreign host")})) | ||
}) | ||
t.Run("TLSHandshakeTimeout", func(t *testing.T) { | ||
assert.True(t, IsTransientErr(tlsHandshakeTimeoutErr)) | ||
}) | ||
t.Run("IOHandshakeTimeout", func(t *testing.T) { | ||
assert.True(t, IsTransientErr(ioTimeoutErr)) | ||
}) | ||
t.Run("ConnectionTimeout", func(t *testing.T) { | ||
assert.True(t, IsTransientErr(connectionTimedout)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,14 @@ | ||
package retry | ||
|
||
import ( | ||
"net" | ||
"net/url" | ||
"strings" | ||
"time" | ||
|
||
apierr "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
|
||
argoerrs "github.com/argoproj/argo/errors" | ||
) | ||
|
||
// DefaultRetry is a default retry backoff settings when retrying API calls | ||
var DefaultRetry = wait.Backoff{ | ||
Steps: 5, | ||
Duration: 10 * time.Millisecond, | ||
Factor: 1.0, | ||
Jitter: 0.1, | ||
} | ||
|
||
// IsRetryableKubeAPIError returns if the error is a retryable kubernetes error | ||
func IsRetryableKubeAPIError(err error) bool { | ||
// get original error if it was wrapped | ||
err = argoerrs.Cause(err) | ||
if apierr.IsNotFound(err) || apierr.IsForbidden(err) || apierr.IsTooManyRequests(err) || IsResourceQuotaConflictErr(err) || apierr.IsInvalid(err) || apierr.IsMethodNotSupported(err) { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
// It is possible to create a pod and it be prevented by a conflict updating the resource quota, | ||
// this func identifies those errors and allows us to retry muck like `Forbidden` or `TooManyRequests` errors. | ||
func IsResourceQuotaConflictErr(err error) bool { | ||
return apierr.IsConflict(err) && strings.Contains(err.Error(), "Operation cannot be fulfilled on resourcequota") | ||
} | ||
|
||
// IsRetryableNetworkError returns whether or not the error is a retryable network error | ||
func IsRetryableNetworkError(err error) bool { | ||
if err == nil { | ||
return false | ||
} | ||
// get original error if it was wrapped | ||
err = argoerrs.Cause(err) | ||
errStr := err.Error() | ||
|
||
switch err.(type) { | ||
case net.Error: | ||
switch err.(type) { | ||
case *net.DNSError, *net.OpError, net.UnknownNetworkError: | ||
return true | ||
case *url.Error: | ||
// For a URL error, where it replies back "connection closed" | ||
// retry again. | ||
if strings.Contains(errStr, "Connection closed by foreign host") { | ||
return true | ||
} | ||
default: | ||
if strings.Contains(errStr, "net/http: TLS handshake timeout") { | ||
// If error is - tlsHandshakeTimeoutError, retry. | ||
return true | ||
} else if strings.Contains(errStr, "i/o timeout") { | ||
// If error is - tcp timeoutError, retry. | ||
return true | ||
} else if strings.Contains(errStr, "connection timed out") { | ||
// If err is a net.Dial timeout, retry. | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.