Skip to content

Commit

Permalink
feat: Add support for multiple webhooks (#6)
Browse files Browse the repository at this point in the history
* feat: Add support for multiple webhooks \n\n BREAKING CHANGE: Change data structure of repo_mapping attribute

* ci: Update trigger branch

* chore(release): v1.1.0-beta.1

* docs: Add support for multiple webhooks

* chore(release): v1.1.0-beta.2

---------

Co-authored-by: semantic-release-bot <semantic-release-bot@martynus.net>
  • Loading branch information
fabidick22 and semantic-release-bot authored Jul 3, 2023
1 parent f96cd07 commit 1054bb3
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/semver.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
push:
branches:
- main
- beta
- alpha

jobs:
release:
Expand Down
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# [1.1.0-beta.2](https://github.com/fabidick22/flux2-ecr-webhook/compare/v1.1.0-beta.1...v1.1.0-beta.2) (2023-07-03)

### Documentation

- Add support for multiple webhooks ([c922986](https://github.com/fabidick22/flux2-ecr-webhook/commit/c9229863292034106c43915a4592c2b1f534d8b2))

# [1.1.0-beta.1](https://github.com/fabidick22/flux2-ecr-webhook/compare/v1.0.2...v1.1.0-beta.1) (2023-06-07)

### Continuous Integration

- Fix string format (#5) ([f96cd07](https://github.com/fabidick22/flux2-ecr-webhook/commit/f96cd07cd6105b4bf53cfaaebed9854a28489a50)), closes [#5](https://github.com/fabidick22/flux2-ecr-webhook/issues/5)
- Refactor semver workflow (#4) ([37134da](https://github.com/fabidick22/flux2-ecr-webhook/commit/37134daa58edc20c5db6749be79674aac4fa3018)), closes [#4](https://github.com/fabidick22/flux2-ecr-webhook/issues/4)
- Update trigger branch ([6dc64a6](https://github.com/fabidick22/flux2-ecr-webhook/commit/6dc64a66f54b1de44c6e3068246df02324818664))

### Features

- Add support for multiple webhooks \n\n BREAKING CHANGE: Change data structure of repo_mapping attribute ([e28764a](https://github.com/fabidick22/flux2-ecr-webhook/commit/e28764af57c6514347688e44081900ad60aa9723))

## [1.0.2](https://github.com/fabidick22/flux2-ecr-webhook/compare/v1.0.1...v1.0.2) (2023-05-10)

### Documentation
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module "flux2-ecr-webhook" {
...
repo_mapping = {
my-ecr-repo = {
webhook = "https://custom.domain.com/hook/11111111"
webhook = ["https://custom.domain.com/hook/11111111", "https://custom.domain.com/hook/2222222"]
}
}
...
Expand Down Expand Up @@ -102,7 +102,7 @@ module "flux2-ecr-webhook" {
|------|-------------|------|---------|:--------:|
| <a name="input_app_name"></a> [app\_name](#input\_app\_name) | Name used for resources to create. | `string` | `"flux2-ecr-webhook"` | no |
| <a name="input_cw_logs_retention"></a> [cw\_logs\_retention](#input\_cw\_logs\_retention) | Specifies the number of days you want to retain log events in the specified log group. | `number` | `14` | no |
| <a name="input_repo_mapping"></a> [repo\_mapping](#input\_repo\_mapping) | Object with repository mapping, if this variable is set `repo_mapping_file` will be ignored.<br>**Example:**<pre>{<br> ecr-repo-name = {<br> webhook = "https://gitops.domain.com/hook/111111 "<br> }<br> test/ecr-repo-name = {<br> webhook = "https://gitops.domain.com/hook/111111 "<br> token = "webhook-token "<br> }<br>}</pre> | `any` | `null` | no |
| <a name="input_repo_mapping"></a> [repo\_mapping](#input\_repo\_mapping) | Object with repository mapping, if this variable is set `repo_mapping_file` will be ignored.<br>**Example:**<pre>{<br> ecr-repo-name = {<br> webhook = ["https://gitops.domain.com/hook/111111" ]<br> }<br> test/ecr-repo-name = {<br> webhook = ["https://gitops.domain.com/hook/111111", "https://gitops.domain.com/hook/222222" ]<br> token = "webhook-token "<br> }<br>}</pre> | `any` | `null` | no |
| <a name="input_repo_mapping_file"></a> [repo\_mapping\_file](#input\_repo\_mapping\_file) | YAML file path with repository mapping. | `string` | `""` | no |
| <a name="input_webhook_token"></a> [webhook\_token](#input\_webhook\_token) | Webhook default token used to call the Flux receiver. If it doesn't find a `token` attribute in the repository mapping use this token for the webhooks | `string` | `null` | no |

Expand Down
3 changes: 1 addition & 2 deletions docs/tf-docs/header.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ graph LR
- Add unit tests
- Add support for [generic-hmac](https://fluxcd.io/flux/components/notification/receiver/#generic-hmac)
- Add support to lambda with VPC (for internal webhook)
- Add support to multiple webhooks

## Usage
To use this Terraform module, you must first have created webhooks for each [ImageRepository](https://fluxcd.io/flux/components/image/imagerepositories/) resource in your cluster.
Expand Down Expand Up @@ -50,7 +49,7 @@ module "flux2-ecr-webhook" {
...
repo_mapping = {
my-ecr-repo = {
webhook = "https://custom.domain.com/hook/11111111"
webhook = ["https://custom.domain.com/hook/11111111", "https://custom.domain.com/hook/2222222"]
}
}
...
Expand Down
31 changes: 18 additions & 13 deletions src/call_receiver/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,9 @@ def process_ecr_push_event(detail):
}))


def call_flux_webhook(repository):
# Retrieve the map of values from Secrets Manager
webhook_map = get_webhook_map()

# Find the webhook endpoint corresponding to the event repository
webhook_url = None
token = None
if repository in webhook_map:
repo_data = webhook_map[repository]
webhook_url = repo_data.get('webhook')
token = repo_data.get('token', get_global_token())

def make_requests(webhook_url, repository, headers):
# Call the Flux webhook with the token and corresponding endpoint
if webhook_url:
headers = {'Authorization': f'Bearer {token}'}
response = session.post(webhook_url, headers=headers)
print(json.dumps({
'message': 'Webhook response',
Expand All @@ -66,6 +54,23 @@ def call_flux_webhook(repository):
'repository': repository
}))

def call_flux_webhook(repository):
# Retrieve the map of values from Secrets Manager
webhook_map = get_webhook_map()

# Find the webhook endpoint corresponding to the event repository
webhook_url = None
token = None
if repository in webhook_map:
repo_data = webhook_map[repository]
webhook_urls = repo_data.get('webhook')
token = repo_data.get('token', get_global_token())
for webhook in webhook_urls:
headers = {'Authorization': f'Bearer {token}'}
make_requests(webhook, repository, headers)




def lambda_handler(event, context):
# Extract event information
Expand Down
4 changes: 2 additions & 2 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ Object with repository mapping, if this variable is set `repo_mapping_file` will
```
{
ecr-repo-name = {
webhook = "https://gitops.domain.com/hook/111111 "
webhook = ["https://gitops.domain.com/hook/111111" ]
}
test/ecr-repo-name = {
webhook = "https://gitops.domain.com/hook/111111 "
webhook = ["https://gitops.domain.com/hook/111111", "https://gitops.domain.com/hook/222222" ]
token = "webhook-token "
}
}
Expand Down

0 comments on commit 1054bb3

Please sign in to comment.