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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(WebhookProvider): Let WebhookProvider return SoftError on response status codes >= 500 #4319

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
24 changes: 21 additions & 3 deletions provider/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/plan"
"sigs.k8s.io/external-dns/provider"
webhookapi "sigs.k8s.io/external-dns/provider/webhook/api"

backoff "github.com/cenkalti/backoff/v4"
Expand Down Expand Up @@ -180,7 +181,11 @@ func (p WebhookProvider) Records(ctx context.Context) ([]*endpoint.Endpoint, err
if resp.StatusCode != http.StatusOK {
recordsErrorsGauge.Inc()
log.Debugf("Failed to get records with code %d", resp.StatusCode)
return nil, fmt.Errorf("failed to get records with code %d", resp.StatusCode)
err := fmt.Errorf("failed to get records with code %d", resp.StatusCode)
if isRetryableError(resp.StatusCode) {
return nil, provider.NewSoftError(err)
}
return nil, err
}

endpoints := []*endpoint.Endpoint{}
Expand Down Expand Up @@ -224,7 +229,11 @@ func (p WebhookProvider) ApplyChanges(ctx context.Context, changes *plan.Changes
if resp.StatusCode != http.StatusNoContent {
applyChangesErrorsGauge.Inc()
log.Debugf("Failed to apply changes with code %d", resp.StatusCode)
return fmt.Errorf("failed to apply changes with code %d", resp.StatusCode)
err := fmt.Errorf("failed to apply changes with code %d", resp.StatusCode)
if isRetryableError(resp.StatusCode) {
return provider.NewSoftError(err)
}
return err
}
return nil
}
Expand Down Expand Up @@ -270,7 +279,11 @@ func (p WebhookProvider) AdjustEndpoints(e []*endpoint.Endpoint) ([]*endpoint.En
if resp.StatusCode != http.StatusOK {
adjustEndpointsErrorsGauge.Inc()
log.Debugf("Failed to AdjustEndpoints with code %d", resp.StatusCode)
return nil, fmt.Errorf("failed to AdjustEndpoints with code %d", resp.StatusCode)
err := fmt.Errorf("failed to AdjustEndpoints with code %d", resp.StatusCode)
if isRetryableError(resp.StatusCode) {
return nil, provider.NewSoftError(err)
}
return nil, err
}

if err := json.NewDecoder(resp.Body).Decode(&endpoints); err != nil {
Expand All @@ -286,3 +299,8 @@ func (p WebhookProvider) AdjustEndpoints(e []*endpoint.Endpoint) ([]*endpoint.En
func (p WebhookProvider) GetDomainFilter() endpoint.DomainFilter {
return p.DomainFilter
}

// isRetryableError returns true for HTTP status codes between 500 and 510 (inclusive)
func isRetryableError(statusCode int) bool {
return statusCode >= http.StatusInternalServerError && statusCode <= http.StatusNotExtended
}
18 changes: 11 additions & 7 deletions provider/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/stretchr/testify/require"
"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/provider"
webhookapi "sigs.k8s.io/external-dns/provider/webhook/api"
)

Expand Down Expand Up @@ -99,10 +100,11 @@ func TestRecordsWithErrors(t *testing.T) {
}))
defer svr.Close()

provider, err := NewWebhookProvider(svr.URL)
p, err := NewWebhookProvider(svr.URL)
require.NoError(t, err)
_, err = provider.Records(context.Background())
_, err = p.Records(context.Background())
require.NotNil(t, err)
require.ErrorIs(t, err, provider.SoftError)
}

func TestApplyChanges(t *testing.T) {
Expand All @@ -122,15 +124,16 @@ func TestApplyChanges(t *testing.T) {
}))
defer svr.Close()

provider, err := NewWebhookProvider(svr.URL)
p, err := NewWebhookProvider(svr.URL)
require.NoError(t, err)
err = provider.ApplyChanges(context.TODO(), nil)
err = p.ApplyChanges(context.TODO(), nil)
require.NoError(t, err)

successfulApplyChanges = false

err = provider.ApplyChanges(context.TODO(), nil)
err = p.ApplyChanges(context.TODO(), nil)
require.NotNil(t, err)
require.ErrorIs(t, err, provider.SoftError)
}

func TestAdjustEndpoints(t *testing.T) {
Expand Down Expand Up @@ -198,7 +201,7 @@ func TestAdjustendpointsWithError(t *testing.T) {
}))
defer svr.Close()

provider, err := NewWebhookProvider(svr.URL)
p, err := NewWebhookProvider(svr.URL)
require.NoError(t, err)
endpoints := []*endpoint.Endpoint{
{
Expand All @@ -210,6 +213,7 @@ func TestAdjustendpointsWithError(t *testing.T) {
},
},
}
_, err = provider.AdjustEndpoints(endpoints)
_, err = p.AdjustEndpoints(endpoints)
require.Error(t, err)
require.ErrorIs(t, err, provider.SoftError)
}
Loading