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 1d7ff0d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 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"

Check failure on line 83 in main.go

View workflow job for this annotation

GitHub Actions / Build

could not import 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
10 changes: 7 additions & 3 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 @@ -23,11 +23,15 @@ import (
"net/http"
"time"

log "github.com/sirupsen/logrus"
"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/plan"
"sigs.k8s.io/external-dns/provider"
)

log "github.com/sirupsen/logrus"
const (
mediaTypeFormatAndVersion = "application/external.dns.webhook+json;version=1"
contentTypeHeader = "Content-Type"
)

type WebhookServer struct {
Expand Down Expand Up @@ -58,7 +62,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 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
20 changes: 9 additions & 11 deletions provider/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ import (
)

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 +87,7 @@ func NewWebhookProvider(u string) (*WebhookProvider, error) {
if err != nil {
return nil, err
}
req.Header.Set(acceptHeader, mediaTypeFormatAndVersion)
req.Header.Set(acceptHeader, webhookapi.mediaTypeFormatAndVersion)

Check failure on line 90 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

Check failure on line 90 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

Check failure on line 90 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

undefined: webhookapi

Check failure on line 90 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

Check failure on line 90 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

Check failure on line 90 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

client := &http.Client{}
var resp *http.Response
Expand All @@ -110,7 +108,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)

Check failure on line 111 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

Check failure on line 111 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

Check failure on line 111 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

undefined: webhookapi

Check failure on line 111 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

Check failure on line 111 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

Check failure on line 111 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

// read the serialized DomainFilter from the response body and set it in the webhook provider struct
defer resp.Body.Close()
Expand All @@ -120,7 +118,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 {

Check failure on line 121 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

Check failure on line 121 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

undefined: webhookapi

Check failure on line 121 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

Check failure on line 121 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

Check failure on line 121 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi
return nil, fmt.Errorf("wrong content type returned from server: %s", contentType)
}

Expand All @@ -140,7 +138,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)

Check failure on line 141 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

Check failure on line 141 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

undefined: webhookapi

Check failure on line 141 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

Check failure on line 141 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi
resp, err := p.client.Do(req)
if err != nil {
recordsErrorsGauge.Inc()
Expand Down Expand Up @@ -182,7 +180,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)

Check failure on line 183 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

Check failure on line 183 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

undefined: webhookapi

Check failure on line 183 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

Check failure on line 183 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

resp, err := p.client.Do(req)
if err != nil {
Expand Down Expand Up @@ -226,8 +224,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)

Check failure on line 227 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

Check failure on line 227 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

undefined: webhookapi

Check failure on line 227 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

Check failure on line 227 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi
req.Header.Set(acceptHeader, webhookapi.mediaTypeFormatAndVersion)

Check failure on line 228 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi) (typecheck)

Check failure on line 228 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

undefined: webhookapi

Check failure on line 228 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

Check failure on line 228 in provider/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / Build

undefined: webhookapi

resp, err := p.client.Do(req)
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions provider/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
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 +50,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 +65,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 +89,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 +108,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 +135,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 +188,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 1d7ff0d

Please sign in to comment.