Skip to content

Commit

Permalink
Let the WebhookProvider return SoftError on status codes >= 500
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonKienzler committed Mar 14, 2024
1 parent 573cb2d commit 701e753
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
21 changes: 17 additions & 4 deletions provider/webhook/webhook.go
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 resp.StatusCode >= http.StatusInternalServerError {
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 resp.StatusCode >= http.StatusInternalServerError {
return provider.NewSoftError(err)
}
return err
}
return nil
}
Expand Down Expand Up @@ -270,11 +279,15 @@ 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 resp.StatusCode >= http.StatusInternalServerError {
return nil, provider.NewSoftError(err)
}
return nil, err
}

if err := json.NewDecoder(resp.Body).Decode(&endpoints); err != nil {
recordsErrorsGauge.Inc()
adjustEndpointsErrorsGauge.Inc()
log.Debugf("Failed to decode response body: %s", err.Error())
return nil, err
}
Expand Down
18 changes: 11 additions & 7 deletions provider/webhook/webhook_test.go
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)
}

0 comments on commit 701e753

Please sign in to comment.