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 client
  • Loading branch information
cjerad committed May 17, 2022
commit 59fcef09a49f07c29563e58594ae514224e0d03e
79 changes: 79 additions & 0 deletions src/pkg/webhook/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2022 Amazon.com, Inc. or its affiliates. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package webhook

import (
"fmt"
"net/http"
"net/url"
urlpkg "net/url"
templatepkg "text/template"
"time"
)

type (
HttpSendFunc = func(*http.Request) (*http.Response, error)
ProxyFunc = func(*http.Request) (*url.URL, error)

ClientBuilder func(ProxyFunc) HttpSendFunc
)

func NewHttpClientDo(proxy ProxyFunc) HttpSendFunc {
c := &http.Client{
Timeout: 5 * time.Second,
Transport: &http.Transport{
IdleConnTimeout: 1 * time.Second,
Proxy: proxy,
},
}
return c.Do
}

func (b ClientBuilder) NewClient(url, proxyURL, template string, headers http.Header) (Client, error) {
c := Client{}

if url == "" {
return c, nil
}

proxy := noopProxy
if proxyURL != "" {
proxyURL, err := urlpkg.Parse(proxyURL)
if err != nil {
return c, fmt.Errorf("failed to parse proxy URL: %w", err)
}

proxy = func(_ *http.Request) (*urlpkg.URL, error) {
return proxyURL, nil
}
}

tmpl, err := templatepkg.New("webhook").Parse(template)
if err != nil {
return c, fmt.Errorf("failed to parse template: %w", err)
}

c.url = url
c.headers = headers
c.sendFunc = b(proxy)
c.template = tmpl
return c, nil
}

func noopProxy(_ *http.Request) (*url.URL, error) {
return nil, nil
}
76 changes: 76 additions & 0 deletions src/pkg/webhook/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2022 Amazon.com, Inc. or its affiliates. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package webhook

import (
"bytes"
"context"
"fmt"
"net/http"
templatepkg "text/template"

"github.com/aws/aws-node-termination-handler/pkg/logging"
)

type Client struct {
url string
headers http.Header
sendFunc HttpSendFunc
template *templatepkg.Template
}

func (c Client) NewRequest() Request {
return Request{sendFunc: c.send}
}

func (c Client) send(ctx context.Context, n Notification) error {
ctx = logging.WithLogger(ctx, logging.FromContext(ctx).Named("webhook"))

if c.url == "" {
return nil
}

var buf bytes.Buffer
if err := c.template.Execute(&buf, n); err != nil {
msg := "failed to populate template"
logging.FromContext(ctx).With("error", err).Error(msg)
return fmt.Errorf("%s: %w", msg, err)
}

req, err := http.NewRequest(http.MethodPost, c.url, &buf)
if err != nil {
msg := "failed to create request"
logging.FromContext(ctx).With("error", err).Error(msg)
return fmt.Errorf("%s: %w", msg, err)
}

req.Header = c.headers

resp, err := c.sendFunc(req)
if err != nil {
msg := "request failed"
logging.FromContext(ctx).With("error", err).Error(msg)
return fmt.Errorf("%s: %w", msg, err)
}
if resp != nil {
logging.FromContext(ctx).
With("status", resp.StatusCode).
Info("response status")
}

return nil
}
27 changes: 27 additions & 0 deletions src/pkg/webhook/notification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2022 Amazon.com, Inc. or its affiliates. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package webhook

import "time"

type Notification struct {
EventID string
InstanceID string
Kind string
NodeName string
StartTime time.Time
}
50 changes: 50 additions & 0 deletions src/pkg/webhook/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2022 Amazon.com, Inc. or its affiliates. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package webhook

import (
"context"
"time"
)

type (
sendFuncType func(context.Context, Notification) error

Event interface {
EventID() string
Kind() string
StartTime() time.Time
}

Request struct {
sendFunc sendFuncType

Event Event
InstanceID string
NodeName string
}
)

func (r Request) Send(ctx context.Context) error {
return r.sendFunc(ctx, Notification{
EventID: r.Event.EventID(),
InstanceID: r.InstanceID,
Kind: r.Event.Kind(),
NodeName: r.NodeName,
StartTime: r.Event.StartTime(),
})
}