Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add webhook spec to Terminator
  • Loading branch information
cjerad committed May 17, 2022
commit 853839c082a498883224f003bf2b3b95fa812cc6
19 changes: 19 additions & 0 deletions src/api/v1alpha1/terminator_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,22 @@ func (e EventsSpec) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("stateChange", e.StateChange)
return nil
}

func (w WebhookSpec) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("url", w.URL)
enc.AddString("proxyURL", w.ProxyURL)

enc.AddArray("headers", zapcore.ArrayMarshalerFunc(func(enc zapcore.ArrayEncoder) error {
for _, header := range w.Headers {
enc.AppendObject(zapcore.ObjectMarshalerFunc(func(enc zapcore.ObjectEncoder) error {
enc.AddString("name", header.Name)
enc.AddString("value", header.Value)
return nil
}))
}
return nil
}))

enc.AddString("template", w.Template)
return nil
}
15 changes: 15 additions & 0 deletions src/api/v1alpha1/terminator_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type TerminatorSpec struct {
SQS SQSSpec `json:"sqs,omitempty"`
Drain DrainSpec `json:"drain,omitempty"`
Events EventsSpec `json:"events,omitempty"`
Webhook WebhookSpec `json:"webhook,omitempty"`
}

// SQSSpec defines inputs to SQS "receive messages" requests.
Expand Down Expand Up @@ -73,6 +74,20 @@ type EventsSpec struct {
StateChange Action `json:"stateChange,omitempty"`
}

// HeaderSpec defines the HTTP headers to include with webhook notifications.
type HeaderSpec struct {
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
}

// WebhookSpec defines the configuration of webhook notifications to send when events are handled.
type WebhookSpec struct {
URL string `json:"url,omitempty"`
ProxyURL string `json:"proxyURL,omitempty"`
Headers []HeaderSpec `json:"headers,omitempty"`
Template string `json:"template,omitempty"`
}

// TerminatorStatus defines the observed state of Terminator
type TerminatorStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
Expand Down
27 changes: 27 additions & 0 deletions src/api/v1alpha1/terminator_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"net/url"
"text/template"

"k8s.io/apimachinery/pkg/util/sets"

Expand All @@ -44,6 +45,7 @@ func (t *TerminatorSpec) validate() (errs *apis.FieldError) {
t.validateMatchLabels().ViaField("matchLabels"),
t.SQS.validate().ViaField("sqs"),
t.Events.validate().ViaField("events"),
t.Webhook.validate().ViaField("webhook"),
)
}

Expand Down Expand Up @@ -78,3 +80,28 @@ func (e *EventsSpec) validate() (errs *apis.FieldError) {
}
return errs
}

func (w *WebhookSpec) validate() (errs *apis.FieldError) {
if _, err := url.Parse(w.URL); err != nil {
errs = errs.Also(apis.ErrInvalidValue(w.URL, "url", err.Error()))
}

if _, err := url.Parse(w.ProxyURL); err != nil && w.ProxyURL != "" {
errs = errs.Also(apis.ErrInvalidValue(w.ProxyURL, "proxyURL", "must be a valid URL"))
}

for i, h := range w.Headers {
if h.Name != "" {
continue
}
errs = errs.Also(apis.ErrInvalidValue(h.Name, "name", "must not be empty").ViaFieldIndex("headers", i))
}

if w.Template == "" {
errs = errs.Also(apis.ErrInvalidValue(w.Template, "template", "must not be empty"))
} else if _, err := template.New("Validate").Parse(w.Template); err != nil {
errs = errs.Also(apis.ErrInvalidValue(w.Template, "template", err.Error()))
}

return errs
}