generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 277
add webhook config to Terminator #641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add webhook client
- Loading branch information
commit 59fcef09a49f07c29563e58594ae514224e0d03e
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(), | ||
| }) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.