Skip to content

Commit 4a1d25e

Browse files
committed
Adjust retry logic to address 5xx and tests for same.
Signed-off-by: Pushpalanka Jayawardhana <[email protected]>
1 parent da19868 commit 4a1d25e

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

filters/openpolicyagent/openpolicyagent.go

+18-12
Original file line numberDiff line numberDiff line change
@@ -523,12 +523,6 @@ func (opa *OpenPolicyAgentInstance) Start(ctx context.Context, timeout time.Dura
523523
}
524524

525525
select {
526-
case <-done:
527-
return nil
528-
case err := <-failed:
529-
opa.Close(ctx)
530-
531-
return err
532526
case <-ctx.Done():
533527
timeoutErr := ctx.Err()
534528

@@ -546,6 +540,13 @@ func (opa *OpenPolicyAgentInstance) Start(ctx context.Context, timeout time.Dura
546540
return fmt.Errorf("one or more open policy agent plugins failed to start in %v with error: %w", timeout, timeoutErr)
547541
}
548542
return fmt.Errorf("one or more open policy agent plugins failed to start in %v", timeout)
543+
544+
case <-done:
545+
return nil
546+
case err := <-failed:
547+
opa.Close(ctx)
548+
549+
return err
549550
}
550551
}
551552

@@ -822,7 +823,7 @@ func (l *QuietLogger) Warn(fmt string, a ...interface{}) {
822823
l.target.Warn(fmt, a)
823824
}
824825

825-
var temporaryErrorHTTPCodes = []int64{
826+
var temporaryClientErrorHTTPCodes = []int64{
826827
429, // too many requests
827828
408, // request timeout
828829
}
@@ -838,10 +839,15 @@ func handleStatusErrors(
838839
return
839840
}
840841
code, err := status.HTTPCode.Int64()
841-
if err == nil && code >= 400 && code < 500 && !isTemporaryError(code) {
842-
//Fail for error codes in the range 400-500 excluding temporary errors
843-
failed <- formatStatusError(prefix, status)
844-
return
842+
if err == nil {
843+
if code >= 400 && code < 500 && !isTemporaryError(code) {
844+
// Fail for error codes in the range 400-500 excluding temporary errors
845+
failed <- formatStatusError(prefix, status)
846+
return
847+
} else if code >= 500 {
848+
// Do not fail for 5xx errors and keep retrying
849+
return
850+
}
845851
}
846852
if err != nil {
847853
failed <- formatStatusError(prefix, status)
@@ -851,7 +857,7 @@ func handleStatusErrors(
851857
}
852858

853859
func isTemporaryError(code int64) bool {
854-
for _, tempCode := range temporaryErrorHTTPCodes {
860+
for _, tempCode := range temporaryClientErrorHTTPCodes {
855861
if code == tempCode {
856862
return true
857863
}

filters/openpolicyagent/openpolicyagent_test.go

+18-10
Original file line numberDiff line numberDiff line change
@@ -712,31 +712,39 @@ func TestOpaActivationFailureWithInvalidDiscovery(t *testing.T) {
712712
}
713713

714714
func TestDiscoveryRetryTemporaryError429(t *testing.T) {
715-
testDiscoveryBundleTemporaryError(t, http.StatusTooManyRequests, "one or more open policy agent plugins failed to start in 1s with error: context deadline exceeded")
715+
testDiscoveryBundleError(t, http.StatusTooManyRequests, "one or more open policy agent plugins failed to start in 1s with error: context deadline exceeded")
716716
}
717717

718718
func TestDiscoveryRetryTemporaryError408(t *testing.T) {
719-
testDiscoveryBundleTemporaryError(t, http.StatusRequestTimeout, "one or more open policy agent plugins failed to start in 1s with error: context deadline exceeded")
719+
testDiscoveryBundleError(t, http.StatusRequestTimeout, "one or more open policy agent plugins failed to start in 1s with error: context deadline exceeded")
720720
}
721721

722-
func TestDiscoveryFailure503BundleError(t *testing.T) {
723-
testDiscoveryBundleTemporaryError(t, http.StatusServiceUnavailable, "discovery plugin failed: Name: bundle, Code: bundle_error, Message: server replied with Service Unavailable, HTTPCode: 503")
722+
func TestDiscoveryRetry5xx(t *testing.T) {
723+
testDiscoveryBundleError(t, http.StatusInternalServerError, "one or more open policy agent plugins failed to start in 1s with error: context deadline exceeded")
724+
}
725+
726+
func TestDiscoveryFailure403BundleError(t *testing.T) {
727+
testDiscoveryBundleError(t, http.StatusForbidden, "discovery plugin failed: Name: discovery, Code: bundle_error, Message: server replied with Forbidden, HTTPCode: 403")
724728
}
725729

726730
func TestResourceBundleRetryTemporaryError429(t *testing.T) {
727-
testResourceBundleTemporaryError(t, http.StatusTooManyRequests, "one or more open policy agent plugins failed to start in 1s with error: context deadline exceeded")
731+
testResourceBundleError(t, http.StatusTooManyRequests, "one or more open policy agent plugins failed to start in 1s with error: context deadline exceeded")
728732
}
729733

730734
func TestResourceBundleRetryTemporaryError408(t *testing.T) {
731-
testResourceBundleTemporaryError(t, http.StatusRequestTimeout, "one or more open policy agent plugins failed to start in 1s with error: context deadline exceeded")
735+
testResourceBundleError(t, http.StatusRequestTimeout, "one or more open policy agent plugins failed to start in 1s with error: context deadline exceeded")
736+
}
737+
738+
func TestResourceBundleRetry5xx(t *testing.T) {
739+
testResourceBundleError(t, http.StatusInternalServerError, "one or more open policy agent plugins failed to start in 1s with error: context deadline exceeded")
732740
}
733741

734-
func TestResourceBundleFailure503BundleError(t *testing.T) {
735-
testResourceBundleTemporaryError(t, http.StatusServiceUnavailable, "bundle plugin failed: Name: bundle, Code: bundle_error, Message: server replied with Service Unavailable, HTTPCode: 503")
742+
func TestResourceBundleFailure403BundleError(t *testing.T) {
743+
testResourceBundleError(t, http.StatusForbidden, "bundle plugin failed: Name: test, Code: bundle_error, Message: server replied with Forbidden, HTTPCode: 403")
736744
}
737745

738746
// Helper function to test discovery bundle temporary errors
739-
func testDiscoveryBundleTemporaryError(t *testing.T, statusCode int, expectedError string) {
747+
func testDiscoveryBundleError(t *testing.T, statusCode int, expectedError string) {
740748
mockDiscoveryBundleServer := mockBundleServerWithStatus("/bundles/discovery", statusCode)
741749
defer mockDiscoveryBundleServer.Close()
742750

@@ -768,7 +776,7 @@ func testDiscoveryBundleTemporaryError(t *testing.T, statusCode int, expectedErr
768776
}
769777

770778
// Helper function to test resource bundle temporary errors
771-
func testResourceBundleTemporaryError(t *testing.T, statusCode int, expectedError string) {
779+
func testResourceBundleError(t *testing.T, statusCode int, expectedError string) {
772780
mockServer := mockBundleServerWithStatus("/bundles/test.tgz", statusCode)
773781
defer mockServer.Close()
774782

0 commit comments

Comments
 (0)