add custom metadata in event webhook

Signed-off-by: Prajith Ndz <prajithpalakkuda@gmail.com>
This commit is contained in:
Prajith Ndz
2021-01-27 20:58:48 +05:30
parent 4f54901d08
commit c876f879de
3 changed files with 26 additions and 8 deletions
+6 -2
View File
@@ -52,7 +52,7 @@ func (c *Controller) sendEventToWebhook(r *flaggerv1.Canary, eventType, template
for _, canaryWebhook := range r.GetAnalysis().Webhooks {
if canaryWebhook.Type == flaggerv1.EventHook {
webhookOverride = true
err := CallEventWebhook(r, canaryWebhook.URL, fmt.Sprintf(template, args...), eventType)
err := CallEventWebhook(r, canaryWebhook, fmt.Sprintf(template, args...), eventType)
if err != nil {
c.logger.With("canary", fmt.Sprintf("%s.%s", r.Name, r.Namespace)).Errorf("error sending event to webhook: %s", err)
}
@@ -60,7 +60,11 @@ func (c *Controller) sendEventToWebhook(r *flaggerv1.Canary, eventType, template
}
if c.eventWebhook != "" && !webhookOverride {
err := CallEventWebhook(r, c.eventWebhook, fmt.Sprintf(template, args...), eventType)
hook := flaggerv1.CanaryWebhook{
Name: "events",
URL: c.eventWebhook,
}
err := CallEventWebhook(r, hook, fmt.Sprintf(template, args...), eventType)
if err != nil {
c.logger.With("canary", fmt.Sprintf("%s.%s", r.Name, r.Namespace)).Errorf("error sending event to webhook: %s", err)
}
+10 -2
View File
@@ -99,7 +99,7 @@ func CallWebhook(name string, namespace string, phase flaggerv1.CanaryPhase, w f
return callWebhook(w.URL, payload, w.Timeout)
}
func CallEventWebhook(r *flaggerv1.Canary, webhook, message, eventtype string) error {
func CallEventWebhook(r *flaggerv1.Canary, w flaggerv1.CanaryWebhook, message, eventtype string) error {
t := time.Now()
payload := flaggerv1.CanaryWebhookPayload{
@@ -113,5 +113,13 @@ func CallEventWebhook(r *flaggerv1.Canary, webhook, message, eventtype string) e
},
}
return callWebhook(webhook, payload, "5s")
if w.Metadata != nil {
for key, value := range *w.Metadata {
if _, ok := payload.Metadata[key]; ok {
continue
}
payload.Metadata[key] = value
}
}
return callWebhook(w.URL, payload, "5s")
}
+10 -4
View File
@@ -101,7 +101,10 @@ func TestCallEventWebhook(t *testing.T) {
w.WriteHeader(http.StatusAccepted)
}))
defer ts.Close()
hook := flaggerv1.CanaryWebhook{
Name: "event",
URL: ts.URL,
}
canary := &flaggerv1.Canary{
ObjectMeta: v1.ObjectMeta{
Name: canaryName,
@@ -112,7 +115,7 @@ func TestCallEventWebhook(t *testing.T) {
},
}
err := CallEventWebhook(canary, ts.URL, canaryMessage, canaryEventType)
err := CallEventWebhook(canary, hook, canaryMessage, canaryEventType)
require.NoError(t, err)
}
@@ -126,7 +129,10 @@ func TestCallEventWebhookStatusCode(t *testing.T) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer ts.Close()
hook := flaggerv1.CanaryWebhook{
Name: "event",
URL: ts.URL,
}
canary := &flaggerv1.Canary{
ObjectMeta: v1.ObjectMeta{
Name: canaryName,
@@ -137,6 +143,6 @@ func TestCallEventWebhookStatusCode(t *testing.T) {
},
}
err := CallEventWebhook(canary, ts.URL, canaryMessage, canaryEventType)
err := CallEventWebhook(canary, hook, canaryMessage, canaryEventType)
assert.Error(t, err)
}