mirror of
https://github.com/kubernetes-sigs/external-dns.git
synced 2025-08-06 01:26:59 +02:00
feat: make webhook httpapi reusable
Signed-off-by: Manuel Rüger <manuel@rueg.eu>
This commit is contained in:
parent
70eb2b73e8
commit
8281e3894d
@ -31,13 +31,13 @@ import (
|
||||
)
|
||||
|
||||
type WebhookServer struct {
|
||||
provider provider.Provider
|
||||
Provider provider.Provider
|
||||
}
|
||||
|
||||
func (p *WebhookServer) recordsHandler(w http.ResponseWriter, req *http.Request) {
|
||||
func (p *WebhookServer) RecordsHandler(w http.ResponseWriter, req *http.Request) {
|
||||
switch req.Method {
|
||||
case http.MethodGet:
|
||||
records, err := p.provider.Records(context.Background())
|
||||
records, err := p.Provider.Records(context.Background())
|
||||
if err != nil {
|
||||
log.Errorf("Failed to get Records: %v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
@ -56,7 +56,7 @@ func (p *WebhookServer) recordsHandler(w http.ResponseWriter, req *http.Request)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
err := p.provider.ApplyChanges(context.Background(), &changes)
|
||||
err := p.Provider.ApplyChanges(context.Background(), &changes)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to Apply Changes: %v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
@ -70,7 +70,7 @@ func (p *WebhookServer) recordsHandler(w http.ResponseWriter, req *http.Request)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *WebhookServer) adjustEndpointsHandler(w http.ResponseWriter, req *http.Request) {
|
||||
func (p *WebhookServer) AdjustEndpointsHandler(w http.ResponseWriter, req *http.Request) {
|
||||
if req.Method != http.MethodPost {
|
||||
log.Errorf("Unsupported method %s", req.Method)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
@ -84,7 +84,7 @@ func (p *WebhookServer) adjustEndpointsHandler(w http.ResponseWriter, req *http.
|
||||
return
|
||||
}
|
||||
w.Header().Set(contentTypeHeader, mediaTypeFormatAndVersion)
|
||||
pve, err := p.provider.AdjustEndpoints(pve)
|
||||
pve, err := p.Provider.AdjustEndpoints(pve)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to call adjust endpoints: %v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
@ -96,9 +96,9 @@ func (p *WebhookServer) adjustEndpointsHandler(w http.ResponseWriter, req *http.
|
||||
}
|
||||
}
|
||||
|
||||
func (p *WebhookServer) negotiateHandler(w http.ResponseWriter, req *http.Request) {
|
||||
func (p *WebhookServer) NegotiateHandler(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set(contentTypeHeader, mediaTypeFormatAndVersion)
|
||||
json.NewEncoder(w).Encode(p.provider.GetDomainFilter())
|
||||
json.NewEncoder(w).Encode(p.Provider.GetDomainFilter())
|
||||
}
|
||||
|
||||
// StartHTTPApi starts a HTTP server given any provider.
|
||||
@ -111,13 +111,13 @@ func (p *WebhookServer) negotiateHandler(w http.ResponseWriter, req *http.Reques
|
||||
// - /adjustendpoints (POST): executes the AdjustEndpoints method
|
||||
func StartHTTPApi(provider provider.Provider, startedChan chan struct{}, readTimeout, writeTimeout time.Duration, providerPort string) {
|
||||
p := WebhookServer{
|
||||
provider: provider,
|
||||
Provider: provider,
|
||||
}
|
||||
|
||||
m := http.NewServeMux()
|
||||
m.HandleFunc("/", p.negotiateHandler)
|
||||
m.HandleFunc("/records", p.recordsHandler)
|
||||
m.HandleFunc("/adjustendpoints", p.adjustEndpointsHandler)
|
||||
m.HandleFunc("/", p.NegotiateHandler)
|
||||
m.HandleFunc("/records", p.RecordsHandler)
|
||||
m.HandleFunc("/adjustendpoints", p.AdjustEndpointsHandler)
|
||||
|
||||
s := &http.Server{
|
||||
Addr: providerPort,
|
||||
|
@ -81,11 +81,11 @@ func TestRecordsHandlerRecords(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
providerAPIServer := &WebhookServer{
|
||||
provider: &FakeWebhookProvider{
|
||||
Provider: &FakeWebhookProvider{
|
||||
domainFilter: endpoint.NewDomainFilter([]string{"foo.bar.com"}),
|
||||
},
|
||||
}
|
||||
providerAPIServer.recordsHandler(w, req)
|
||||
providerAPIServer.RecordsHandler(w, req)
|
||||
res := w.Result()
|
||||
require.Equal(t, http.StatusOK, res.StatusCode)
|
||||
// require that the res has the same endpoints as the records slice
|
||||
@ -103,11 +103,11 @@ func TestRecordsHandlerRecordsWithErrors(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
providerAPIServer := &WebhookServer{
|
||||
provider: &FakeWebhookProvider{
|
||||
Provider: &FakeWebhookProvider{
|
||||
err: fmt.Errorf("error"),
|
||||
},
|
||||
}
|
||||
providerAPIServer.recordsHandler(w, req)
|
||||
providerAPIServer.RecordsHandler(w, req)
|
||||
res := w.Result()
|
||||
require.Equal(t, http.StatusInternalServerError, res.StatusCode)
|
||||
}
|
||||
@ -117,9 +117,9 @@ func TestRecordsHandlerApplyChangesWithBadRequest(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
providerAPIServer := &WebhookServer{
|
||||
provider: &FakeWebhookProvider{},
|
||||
Provider: &FakeWebhookProvider{},
|
||||
}
|
||||
providerAPIServer.recordsHandler(w, req)
|
||||
providerAPIServer.RecordsHandler(w, req)
|
||||
res := w.Result()
|
||||
require.Equal(t, http.StatusBadRequest, res.StatusCode)
|
||||
}
|
||||
@ -143,9 +143,9 @@ func TestRecordsHandlerApplyChangesWithValidRequest(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
providerAPIServer := &WebhookServer{
|
||||
provider: &FakeWebhookProvider{},
|
||||
Provider: &FakeWebhookProvider{},
|
||||
}
|
||||
providerAPIServer.recordsHandler(w, req)
|
||||
providerAPIServer.RecordsHandler(w, req)
|
||||
res := w.Result()
|
||||
require.Equal(t, http.StatusNoContent, res.StatusCode)
|
||||
}
|
||||
@ -169,11 +169,11 @@ func TestRecordsHandlerApplyChangesWithErrors(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
providerAPIServer := &WebhookServer{
|
||||
provider: &FakeWebhookProvider{
|
||||
Provider: &FakeWebhookProvider{
|
||||
err: fmt.Errorf("error"),
|
||||
},
|
||||
}
|
||||
providerAPIServer.recordsHandler(w, req)
|
||||
providerAPIServer.RecordsHandler(w, req)
|
||||
res := w.Result()
|
||||
require.Equal(t, http.StatusInternalServerError, res.StatusCode)
|
||||
}
|
||||
@ -183,9 +183,9 @@ func TestRecordsHandlerWithWrongHTTPMethod(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
providerAPIServer := &WebhookServer{
|
||||
provider: &FakeWebhookProvider{},
|
||||
Provider: &FakeWebhookProvider{},
|
||||
}
|
||||
providerAPIServer.recordsHandler(w, req)
|
||||
providerAPIServer.RecordsHandler(w, req)
|
||||
res := w.Result()
|
||||
require.Equal(t, http.StatusBadRequest, res.StatusCode)
|
||||
}
|
||||
@ -195,15 +195,15 @@ func TestAdjustEndpointsHandlerWithInvalidRequest(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
providerAPIServer := &WebhookServer{
|
||||
provider: &FakeWebhookProvider{},
|
||||
Provider: &FakeWebhookProvider{},
|
||||
}
|
||||
providerAPIServer.adjustEndpointsHandler(w, req)
|
||||
providerAPIServer.AdjustEndpointsHandler(w, req)
|
||||
res := w.Result()
|
||||
require.Equal(t, http.StatusBadRequest, res.StatusCode)
|
||||
|
||||
req = httptest.NewRequest(http.MethodGet, "/adjustendpoints", nil)
|
||||
|
||||
providerAPIServer.adjustEndpointsHandler(w, req)
|
||||
providerAPIServer.AdjustEndpointsHandler(w, req)
|
||||
res = w.Result()
|
||||
require.Equal(t, http.StatusBadRequest, res.StatusCode)
|
||||
}
|
||||
@ -226,9 +226,9 @@ func TestAdjustEndpointsHandlerWithValidRequest(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
providerAPIServer := &WebhookServer{
|
||||
provider: &FakeWebhookProvider{},
|
||||
Provider: &FakeWebhookProvider{},
|
||||
}
|
||||
providerAPIServer.adjustEndpointsHandler(w, req)
|
||||
providerAPIServer.AdjustEndpointsHandler(w, req)
|
||||
res := w.Result()
|
||||
require.Equal(t, http.StatusOK, res.StatusCode)
|
||||
require.NotNil(t, res.Body)
|
||||
@ -252,11 +252,11 @@ func TestAdjustEndpointsHandlerWithError(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
providerAPIServer := &WebhookServer{
|
||||
provider: &FakeWebhookProvider{
|
||||
Provider: &FakeWebhookProvider{
|
||||
err: fmt.Errorf("error"),
|
||||
},
|
||||
}
|
||||
providerAPIServer.adjustEndpointsHandler(w, req)
|
||||
providerAPIServer.AdjustEndpointsHandler(w, req)
|
||||
res := w.Result()
|
||||
require.Equal(t, http.StatusInternalServerError, res.StatusCode)
|
||||
require.NotNil(t, res.Body)
|
||||
|
Loading…
Reference in New Issue
Block a user