Make webhook publisher use relative urls

This commit is contained in:
Soam Vasani
2016-12-20 12:19:51 -08:00
parent 8704761fd0
commit c5425180e8
2 changed files with 12 additions and 9 deletions
+1 -1
View File
@@ -49,7 +49,7 @@ func Start(controllerUrl string, routerUrl string) error {
if err != nil { if err != nil {
return err return err
} }
poster := MakeWebhookPublisher() poster := MakeWebhookPublisher(routerUrl)
kubeWatch := MakeKubeWatcher(kubeClient, poster) kubeWatch := MakeKubeWatcher(kubeClient, poster)
client := client.MakeClient(controllerUrl) client := client.MakeClient(controllerUrl)
+11 -8
View File
@@ -22,6 +22,7 @@ import (
"log" "log"
"net/http" "net/http"
"reflect" "reflect"
"strings"
"time" "time"
"k8s.io/client-go/1.5/pkg/watch" "k8s.io/client-go/1.5/pkg/watch"
@@ -34,6 +35,8 @@ type (
maxRetries int maxRetries int
retryDelay time.Duration retryDelay time.Duration
baseUrl string
} }
publishRequest struct { publishRequest struct {
url string url string
@@ -43,11 +46,10 @@ type (
} }
) )
// The caller must make one of these per URL func MakeWebhookPublisher(baseUrl string) *WebhookPublisher {
func MakeWebhookPublisher() *WebhookPublisher {
p := &WebhookPublisher{ p := &WebhookPublisher{
baseUrl: baseUrl,
requestChannel: make(chan *publishRequest, 32), // buffered channel requestChannel: make(chan *publishRequest, 32), // buffered channel
// TODO make this configurable // TODO make this configurable
maxRetries: 10, maxRetries: 10,
retryDelay: 500 * time.Millisecond, retryDelay: 500 * time.Millisecond,
@@ -74,7 +76,8 @@ func (p *WebhookPublisher) svc() {
func (p *WebhookPublisher) makeHttpRequest(r *publishRequest) { func (p *WebhookPublisher) makeHttpRequest(r *publishRequest) {
log.Printf("Making HTTP request to %v", r.url) url := p.baseUrl + "/" + strings.TrimPrefix(r.url, "/")
log.Printf("Making HTTP request to %v", url)
// Serialize the object // Serialize the object
var buf bytes.Buffer var buf bytes.Buffer
@@ -85,9 +88,9 @@ func (p *WebhookPublisher) makeHttpRequest(r *publishRequest) {
} }
// Create request // Create request
req, err := http.NewRequest("POST", r.url, &buf) req, err := http.NewRequest("POST", url, &buf)
if err != nil { if err != nil {
log.Printf("Failed to create request to %v", r.url) log.Printf("Failed to create request to %v", url)
// can't do anything more, drop the event. // can't do anything more, drop the event.
return return
} }
@@ -113,7 +116,7 @@ func (p *WebhookPublisher) makeHttpRequest(r *publishRequest) {
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close() resp.Body.Close()
if err == nil { if err == nil {
log.Printf("request error: %v", body) log.Printf("request error: %v", string(body))
} }
} }
@@ -125,7 +128,7 @@ func (p *WebhookPublisher) makeHttpRequest(r *publishRequest) {
p.requestChannel <- r p.requestChannel <- r
}) })
} else { } else {
log.Printf("Final retry failed, giving up on %v", r.url) log.Printf("Final retry failed, giving up on %v", url)
// Event dropped // Event dropped
} }
} }