Skip to content

Commit

Permalink
webhook: Move httpapi into own package
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Rüger <manuel@rueg.eu>
  • Loading branch information
mrueg committed Nov 30, 2023
1 parent 832bcb5 commit bf8bab7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ import (
"sigs.k8s.io/external-dns/provider/ultradns"
"sigs.k8s.io/external-dns/provider/vinyldns"
"sigs.k8s.io/external-dns/provider/vultr"
"sigs.k8s.io/external-dns/provider/webhook"
"sigs.k8s.io/external-dns/provider/webhook"
webhookapi "sigs.k8s.io/external-dns/provider/webhook/api"
"sigs.k8s.io/external-dns/registry"
"sigs.k8s.io/external-dns/source"
)
Expand Down Expand Up @@ -413,7 +414,7 @@ func main() {
}

if cfg.WebhookServer {
webhook.StartHTTPApi(p, nil, cfg.WebhookProviderReadTimeout, cfg.WebhookProviderWriteTimeout, "127.0.0.1:8888")
webhookapi.StartHTTPApi(p, nil, cfg.WebhookProviderReadTimeout, cfg.WebhookProviderWriteTimeout, "127.0.0.1:8888")
os.Exit(0)
}

Expand Down
15 changes: 10 additions & 5 deletions provider/webhook/httpapi.go → provider/webhook/api/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package webhook
package api

import (
"context"
Expand All @@ -30,6 +30,11 @@ import (
log "github.com/sirupsen/logrus"
)

const (
MediaTypeFormatAndVersion = "application/external.dns.webhook+json;version=1"
ContentTypeHeader = "Content-Type"
)

type WebhookServer struct {
Provider provider.Provider
}
Expand All @@ -43,7 +48,7 @@ func (p *WebhookServer) RecordsHandler(w http.ResponseWriter, req *http.Request)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set(contentTypeHeader, mediaTypeFormatAndVersion)
w.Header().Set(ContentTypeHeader, MediaTypeFormatAndVersion)
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(records); err != nil {
log.Errorf("Failed to encode records: %v", err)
Expand All @@ -58,7 +63,7 @@ func (p *WebhookServer) RecordsHandler(w http.ResponseWriter, req *http.Request)
}
err := p.Provider.ApplyChanges(context.Background(), &changes)
if err != nil {
log.Errorf("Failed to Apply Changes: %v", err)
log.Errorf("Failed to apply changes: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand All @@ -83,7 +88,7 @@ func (p *WebhookServer) AdjustEndpointsHandler(w http.ResponseWriter, req *http.
w.WriteHeader(http.StatusBadRequest)
return
}
w.Header().Set(contentTypeHeader, mediaTypeFormatAndVersion)
w.Header().Set(ContentTypeHeader, MediaTypeFormatAndVersion)
pve, err := p.Provider.AdjustEndpoints(pve)
if err != nil {
log.Errorf("Failed to call adjust endpoints: %v", err)
Expand All @@ -97,7 +102,7 @@ func (p *WebhookServer) AdjustEndpointsHandler(w http.ResponseWriter, req *http.
}

func (p *WebhookServer) NegotiateHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set(contentTypeHeader, mediaTypeFormatAndVersion)
w.Header().Set(ContentTypeHeader, MediaTypeFormatAndVersion)
json.NewEncoder(w).Encode(p.Provider.GetDomainFilter())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package webhook
package api

import (
"bytes"
Expand Down
21 changes: 10 additions & 11 deletions provider/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@ import (

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

backoff "github.com/cenkalti/backoff/v4"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)

const (
mediaTypeFormatAndVersion = "application/external.dns.webhook+json;version=1"
contentTypeHeader = "Content-Type"
acceptHeader = "Accept"
maxRetries = 5
acceptHeader = "Accept"
maxRetries = 5
)

var (
Expand Down Expand Up @@ -89,7 +88,7 @@ func NewWebhookProvider(u string) (*WebhookProvider, error) {
if err != nil {
return nil, err
}
req.Header.Set(acceptHeader, mediaTypeFormatAndVersion)
req.Header.Set(acceptHeader, webhookapi.MediaTypeFormatAndVersion)

client := &http.Client{}
var resp *http.Response
Expand All @@ -110,7 +109,7 @@ func NewWebhookProvider(u string) (*WebhookProvider, error) {
return nil, fmt.Errorf("failed to connect to plugin api: %v", err)
}

contentType := resp.Header.Get(contentTypeHeader)
contentType := resp.Header.Get(webhookapi.ContentTypeHeader)

// read the serialized DomainFilter from the response body and set it in the webhook provider struct
defer resp.Body.Close()
Expand All @@ -120,7 +119,7 @@ func NewWebhookProvider(u string) (*WebhookProvider, error) {
return nil, fmt.Errorf("failed to unmarshal response body of DomainFilter: %v", err)
}

if contentType != mediaTypeFormatAndVersion {
if contentType != webhookapi.MediaTypeFormatAndVersion {
return nil, fmt.Errorf("wrong content type returned from server: %s", contentType)
}

Expand All @@ -140,7 +139,7 @@ func (p WebhookProvider) Records(ctx context.Context) ([]*endpoint.Endpoint, err
log.Debugf("Failed to create request: %s", err.Error())
return nil, err
}
req.Header.Set(acceptHeader, mediaTypeFormatAndVersion)
req.Header.Set(acceptHeader, webhookapi.MediaTypeFormatAndVersion)
resp, err := p.client.Do(req)
if err != nil {
recordsErrorsGauge.Inc()
Expand Down Expand Up @@ -182,7 +181,7 @@ func (p WebhookProvider) ApplyChanges(ctx context.Context, changes *plan.Changes
return err
}

req.Header.Set(contentTypeHeader, mediaTypeFormatAndVersion)
req.Header.Set(webhookapi.ContentTypeHeader, webhookapi.MediaTypeFormatAndVersion)

resp, err := p.client.Do(req)
if err != nil {
Expand Down Expand Up @@ -226,8 +225,8 @@ func (p WebhookProvider) AdjustEndpoints(e []*endpoint.Endpoint) ([]*endpoint.En
return nil, err
}

req.Header.Set(contentTypeHeader, mediaTypeFormatAndVersion)
req.Header.Set(acceptHeader, mediaTypeFormatAndVersion)
req.Header.Set(webhookapi.ContentTypeHeader, webhookapi.MediaTypeFormatAndVersion)
req.Header.Set(acceptHeader, webhookapi.MediaTypeFormatAndVersion)

resp, err := p.client.Do(req)
if err != nil {
Expand Down
15 changes: 8 additions & 7 deletions provider/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ import (

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

func TestInvalidDomainFilter(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
w.Header().Set(contentTypeHeader, mediaTypeFormatAndVersion)
w.Header().Set(webhookapi.ContentTypeHeader, webhookapi.MediaTypeFormatAndVersion)
w.WriteHeader(200)
return
}
Expand All @@ -50,7 +51,7 @@ func TestValidDomainfilter(t *testing.T) {
domainFilter := endpoint.NewDomainFilter([]string{"example.com"})
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
w.Header().Set(contentTypeHeader, mediaTypeFormatAndVersion)
w.Header().Set(webhookapi.ContentTypeHeader, webhookapi.MediaTypeFormatAndVersion)
json.NewEncoder(w).Encode(domainFilter)
return
}
Expand All @@ -65,7 +66,7 @@ func TestValidDomainfilter(t *testing.T) {
func TestRecords(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
w.Header().Set(contentTypeHeader, mediaTypeFormatAndVersion)
w.Header().Set(webhookapi.ContentTypeHeader, webhookapi.MediaTypeFormatAndVersion)
w.Write([]byte(`{}`))
return
}
Expand All @@ -89,7 +90,7 @@ func TestRecords(t *testing.T) {
func TestRecordsWithErrors(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
w.Header().Set(contentTypeHeader, mediaTypeFormatAndVersion)
w.Header().Set(webhookapi.ContentTypeHeader, webhookapi.MediaTypeFormatAndVersion)
w.Write([]byte(`{}`))
return
}
Expand All @@ -108,7 +109,7 @@ func TestApplyChanges(t *testing.T) {
successfulApplyChanges := true
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
w.Header().Set(contentTypeHeader, mediaTypeFormatAndVersion)
w.Header().Set(webhookapi.ContentTypeHeader, webhookapi.MediaTypeFormatAndVersion)
w.Write([]byte(`{}`))
return
}
Expand All @@ -135,7 +136,7 @@ func TestApplyChanges(t *testing.T) {
func TestAdjustEndpoints(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
w.Header().Set(contentTypeHeader, mediaTypeFormatAndVersion)
w.Header().Set(webhookapi.ContentTypeHeader, webhookapi.MediaTypeFormatAndVersion)
w.Write([]byte(`{}`))
return
}
Expand Down Expand Up @@ -188,7 +189,7 @@ func TestAdjustEndpoints(t *testing.T) {
func TestAdjustendpointsWithError(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
w.Header().Set(contentTypeHeader, mediaTypeFormatAndVersion)
w.Header().Set(webhookapi.ContentTypeHeader, webhookapi.MediaTypeFormatAndVersion)
w.Write([]byte(`{}`))
return
}
Expand Down

0 comments on commit bf8bab7

Please sign in to comment.