Skip to content
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
7 changes: 5 additions & 2 deletions provider/webhook/api/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ import (
const (
MediaTypeFormatAndVersion = "application/external.dns.webhook+json;version=1"
ContentTypeHeader = "Content-Type"
UrlAdjustEndpoints = "/adjustendpoints"
UrlApplyChanges = "/applychanges"
UrlRecords = "/records"
)

type WebhookServer struct {
Expand Down Expand Up @@ -121,8 +124,8 @@ func StartHTTPApi(provider provider.Provider, startedChan chan struct{}, readTim

m := http.NewServeMux()
m.HandleFunc("/", p.NegotiateHandler)
m.HandleFunc("/records", p.RecordsHandler)
m.HandleFunc("/adjustendpoints", p.AdjustEndpointsHandler)
m.HandleFunc(UrlRecords, p.RecordsHandler)
m.HandleFunc(UrlAdjustEndpoints, p.AdjustEndpointsHandler)

s := &http.Server{
Addr: providerPort,
Expand Down
16 changes: 8 additions & 8 deletions provider/webhook/api/httpapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestMain(m *testing.M) {
}

func TestRecordsHandlerRecords(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/records", nil)
req := httptest.NewRequest(http.MethodGet, UrlRecords, nil)
w := httptest.NewRecorder()

providerAPIServer := &WebhookServer{
Expand All @@ -99,7 +99,7 @@ func TestRecordsHandlerRecords(t *testing.T) {
}

func TestRecordsHandlerRecordsWithErrors(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/records", nil)
req := httptest.NewRequest(http.MethodGet, UrlRecords, nil)
w := httptest.NewRecorder()

providerAPIServer := &WebhookServer{
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestRecordsHandlerApplyChangesWithValidRequest(t *testing.T) {

reader := bytes.NewReader(j)

req := httptest.NewRequest(http.MethodPost, "/applychanges", reader)
req := httptest.NewRequest(http.MethodPost, UrlApplyChanges, reader)
w := httptest.NewRecorder()

providerAPIServer := &WebhookServer{
Expand All @@ -165,7 +165,7 @@ func TestRecordsHandlerApplyChangesWithErrors(t *testing.T) {

reader := bytes.NewReader(j)

req := httptest.NewRequest(http.MethodPost, "/applychanges", reader)
req := httptest.NewRequest(http.MethodPost, UrlApplyChanges, reader)
w := httptest.NewRecorder()

providerAPIServer := &WebhookServer{
Expand All @@ -191,7 +191,7 @@ func TestRecordsHandlerWithWrongHTTPMethod(t *testing.T) {
}

func TestAdjustEndpointsHandlerWithInvalidRequest(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/adjustendpoints", nil)
req := httptest.NewRequest(http.MethodPost, UrlAdjustEndpoints, nil)
w := httptest.NewRecorder()

providerAPIServer := &WebhookServer{
Expand All @@ -201,7 +201,7 @@ func TestAdjustEndpointsHandlerWithInvalidRequest(t *testing.T) {
res := w.Result()
require.Equal(t, http.StatusBadRequest, res.StatusCode)

req = httptest.NewRequest(http.MethodGet, "/adjustendpoints", nil)
req = httptest.NewRequest(http.MethodGet, UrlAdjustEndpoints, nil)

providerAPIServer.AdjustEndpointsHandler(w, req)
res = w.Result()
Expand All @@ -222,7 +222,7 @@ func TestAdjustEndpointsHandlerWithValidRequest(t *testing.T) {
require.NoError(t, err)

reader := bytes.NewReader(j)
req := httptest.NewRequest(http.MethodPost, "/adjustendpoints", reader)
req := httptest.NewRequest(http.MethodPost, UrlAdjustEndpoints, reader)
w := httptest.NewRecorder()

providerAPIServer := &WebhookServer{
Expand All @@ -248,7 +248,7 @@ func TestAdjustEndpointsHandlerWithError(t *testing.T) {
require.NoError(t, err)

reader := bytes.NewReader(j)
req := httptest.NewRequest(http.MethodPost, "/adjustendpoints", reader)
req := httptest.NewRequest(http.MethodPost, UrlAdjustEndpoints, reader)
w := httptest.NewRecorder()

providerAPIServer := &WebhookServer{
Expand Down
23 changes: 12 additions & 11 deletions provider/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func NewWebhookProvider(u string) (*WebhookProvider, error) {
}

// negotiate API information
req, err := http.NewRequest("GET", u, nil)
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, err
}
Expand All @@ -128,8 +128,8 @@ func NewWebhookProvider(u string) (*WebhookProvider, error) {
return err
}
// we currently only use 200 as success, but considering okay all 2XX for future usage
if resp.StatusCode >= 300 && resp.StatusCode < 500 {
return backoff.Permanent(fmt.Errorf("status code < 500"))
if resp.StatusCode >= http.StatusMultipleChoices && resp.StatusCode < http.StatusInternalServerError {
return backoff.Permanent(fmt.Errorf("status code < %d", http.StatusInternalServerError))
}
return nil
}, backoff.WithMaxRetries(backoff.NewExponentialBackOff(), maxRetries))
Expand Down Expand Up @@ -164,7 +164,7 @@ func (p WebhookProvider) Records(ctx context.Context) ([]*endpoint.Endpoint, err
recordsRequestsGauge.Gauge.Inc()
u := p.remoteServerURL.JoinPath("records").String()

req, err := http.NewRequest("GET", u, nil)
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
recordsErrorsGauge.Gauge.Inc()
log.Debugf("Failed to create request: %s", err.Error())
Expand All @@ -189,7 +189,7 @@ func (p WebhookProvider) Records(ctx context.Context) ([]*endpoint.Endpoint, err
return nil, err
}

endpoints := []*endpoint.Endpoint{}
var endpoints []*endpoint.Endpoint
if err := json.NewDecoder(resp.Body).Decode(&endpoints); err != nil {
recordsErrorsGauge.Gauge.Inc()
log.Debugf("Failed to decode response body: %s", err.Error())
Expand All @@ -201,7 +201,7 @@ func (p WebhookProvider) Records(ctx context.Context) ([]*endpoint.Endpoint, err
// ApplyChanges will make a POST to remoteServerURL/records with the changes
func (p WebhookProvider) ApplyChanges(ctx context.Context, changes *plan.Changes) error {
applyChangesRequestsGauge.Gauge.Inc()
u := p.remoteServerURL.JoinPath("records").String()
u := p.remoteServerURL.JoinPath(webhookapi.UrlRecords).String()

b := new(bytes.Buffer)
if err := json.NewEncoder(b).Encode(changes); err != nil {
Expand All @@ -210,7 +210,7 @@ func (p WebhookProvider) ApplyChanges(ctx context.Context, changes *plan.Changes
return err
}

req, err := http.NewRequest("POST", u, b)
req, err := http.NewRequest(http.MethodPost, u, b)
if err != nil {
applyChangesErrorsGauge.Gauge.Inc()
log.Debugf("Failed to create request: %s", err.Error())
Expand All @@ -225,6 +225,7 @@ func (p WebhookProvider) ApplyChanges(ctx context.Context, changes *plan.Changes
log.Debugf("Failed to perform request: %s", err.Error())
return err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusNoContent {
Expand All @@ -240,12 +241,12 @@ func (p WebhookProvider) ApplyChanges(ctx context.Context, changes *plan.Changes
}

// AdjustEndpoints will call the provider doing a POST on `/adjustendpoints` which will return a list of modified endpoints
// based on a provider specific requirement.
// based on a provider-specific requirement.
// This method returns an empty slice in case there is a technical error on the provider's side so that no endpoints will be considered.
func (p WebhookProvider) AdjustEndpoints(e []*endpoint.Endpoint) ([]*endpoint.Endpoint, error) {
adjustEndpointsRequestsGauge.Gauge.Inc()
endpoints := []*endpoint.Endpoint{}
u, err := url.JoinPath(p.remoteServerURL.String(), "adjustendpoints")
var endpoints []*endpoint.Endpoint
u, err := url.JoinPath(p.remoteServerURL.String(), webhookapi.UrlAdjustEndpoints)
if err != nil {
adjustEndpointsErrorsGauge.Gauge.Inc()
log.Debugf("Failed to join path, %s", err)
Expand All @@ -259,7 +260,7 @@ func (p WebhookProvider) AdjustEndpoints(e []*endpoint.Endpoint) ([]*endpoint.En
return nil, err
}

req, err := http.NewRequest("POST", u, b)
req, err := http.NewRequest(http.MethodPost, u, b)
if err != nil {
adjustEndpointsErrorsGauge.Gauge.Inc()
log.Debugf("Failed to create new HTTP request, %s", err)
Expand Down
Loading
Loading