Skip to content

Commit 0d39a50

Browse files
authored
Merge pull request #10 from fabidick22/alpha
BREAKING CHANGE: Refactor the repo_mapping variable
2 parents b101b29 + 7eac91c commit 0d39a50

File tree

6 files changed

+103
-50
lines changed

6 files changed

+103
-50
lines changed

CHANGELOG.md

+30
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
# [1.2.0-alpha.3](https://github.com/fabidick22/flux2-ecr-webhook/compare/v1.2.0-alpha.2...v1.2.0-alpha.3) (2023-07-12)
2+
3+
### Documentation
4+
5+
- Update docs about refactoring the repo_mapping variable ([ffc4f5c](https://github.com/fabidick22/flux2-ecr-webhook/commit/ffc4f5cf4a1b4be4b593b9a34742e0c9d863b2e2))
6+
7+
# [1.2.0-alpha.2](https://github.com/fabidick22/flux2-ecr-webhook/compare/v1.2.0-alpha.1...v1.2.0-alpha.2) (2023-07-06)
8+
9+
### Code Refactoring
10+
11+
- Add info logs ([2602f32](https://github.com/fabidick22/flux2-ecr-webhook/commit/2602f3275c3ba3fa7b057c38cb7ea625777f47a3))
12+
13+
# [1.2.0-alpha.1](https://github.com/fabidick22/flux2-ecr-webhook/compare/v1.1.0...v1.2.0-alpha.1) (2023-07-05)
14+
15+
### Chores
16+
17+
- **release:** v1.1.1 ([b101b29](https://github.com/fabidick22/flux2-ecr-webhook/commit/b101b29a74b192f0aeb2347fd761ff7b26b78ef5))
18+
19+
### Continuous Integration
20+
21+
- Update trigger branchs (#8) ([8d2b843](https://github.com/fabidick22/flux2-ecr-webhook/commit/8d2b843603409d66f5aff1549997d89063f88c46)), closes [#8](https://github.com/fabidick22/flux2-ecr-webhook/issues/8)
22+
23+
### Documentation
24+
25+
- Update TODO list (#9) ([1df3d43](https://github.com/fabidick22/flux2-ecr-webhook/commit/1df3d4380852c25ceb4648ea2eeddafe105f4df7)), closes [#9](https://github.com/fabidick22/flux2-ecr-webhook/issues/9)
26+
27+
### Features
28+
29+
- Add support for a regex parameter \n\n BREAKING CHANGE: Update repo_mapping variable to support the regex attribute ([4f165d0](https://github.com/fabidick22/flux2-ecr-webhook/commit/4f165d041333ca8e2f9b0df49adaa3bf7ba0dc1f))
30+
131
## [1.1.1](https://github.com/fabidick22/flux2-ecr-webhook/compare/v1.1.0...v1.1.1) (2023-07-04)
232

333
### Continuous Integration

README.md

+17-6
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,28 @@ spec:
4242
name: my-ecr-repo-ir
4343
```
4444
The webhook created by the `Receiver` resource has to be configured in the module, for example:
45+
> **Note**: Let's assume that our ECR repository is called `my-ecr-repo`.
46+
4547
```hcl
4648
module "flux2-ecr-webhook" {
47-
source = "github.com/fabidick22/flux2-ecr-webhook?ref=v1.0.2"
49+
source = "github.com/fabidick22/flux2-ecr-webhook?ref=v1.2.0"
50+
51+
app_name = "flux-ecr-webhook"
4852
49-
...
5053
repo_mapping = {
51-
my-ecr-repo = {
52-
webhook = ["https://custom.domain.com/hook/11111111", "https://custom.domain.com/hook/2222222"]
54+
my-ecr-repo = { # ECR resource name
55+
prod = {
56+
webhook = ["https://domain.com/hook/1111111"] # URL created by the Receiver
57+
regex = "prod-(?P<version>.*)" # Regex for ECR image tag
58+
}
59+
stg = {
60+
webhook = ["https://domain.com/hook/2222222"] # URL created by the Receiver
61+
regex = "stg-(?P<version>.*)" # Regex for ECR image tag
62+
}
5363
}
5464
}
55-
...
65+
66+
webhook_token = "var.webhook_token"
5667
}
5768
```
5869
## Example
@@ -101,7 +112,7 @@ module "flux2-ecr-webhook" {
101112
|------|-------------|------|---------|:--------:|
102113
| <a name="input_app_name"></a> [app\_name](#input\_app\_name) | Name used for resources to create. | `string` | `"flux2-ecr-webhook"` | no |
103114
| <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 |
104-
| <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 |
115+
| <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><br>**Available Attributes:**<br>- `<ECR>`: ECR resource name.<br>- `<ECR>.<ID>`: Unique name for webhooks.<br>- `<ECR>.<ID>.webhook`: Webhook list.<br>- `<ECR>.<ID>.token` (Optional): Token used for webhooks, if set, then "webhook\_token" will be ignored.<br>- `<ECR>.<ID>.regex` (Optional): Regular expression that is applied to the image tag | `any` | `null` | no |
105116
| <a name="input_repo_mapping_file"></a> [repo\_mapping\_file](#input\_repo\_mapping\_file) | YAML file path with repository mapping. | `string` | `""` | no |
106117
| <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 |
107118

docs/tf-docs/header.md

+16-5
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,28 @@ spec:
4242
name: my-ecr-repo-ir
4343
```
4444
The webhook created by the `Receiver` resource has to be configured in the module, for example:
45+
> **Note**: Let's assume that our ECR repository is called `my-ecr-repo`.
46+
4547
```hcl
4648
module "flux2-ecr-webhook" {
47-
source = "github.com/fabidick22/flux2-ecr-webhook?ref=v1.0.2"
49+
source = "github.com/fabidick22/flux2-ecr-webhook?ref=v1.2.0"
50+
51+
app_name = "flux-ecr-webhook"
4852
49-
...
5053
repo_mapping = {
51-
my-ecr-repo = {
52-
webhook = ["https://custom.domain.com/hook/11111111", "https://custom.domain.com/hook/2222222"]
54+
my-ecr-repo = { # ECR resource name
55+
prod = {
56+
webhook = ["https://domain.com/hook/1111111"] # URL created by the Receiver
57+
regex = "prod-(?P<version>.*)" # Regex for ECR image tag
58+
}
59+
stg = {
60+
webhook = ["https://domain.com/hook/2222222"] # URL created by the Receiver
61+
regex = "stg-(?P<version>.*)" # Regex for ECR image tag
62+
}
5363
}
5464
}
55-
...
65+
66+
webhook_token = "var.webhook_token"
5667
}
5768
```
5869
## Example

examples/complete/main.tf

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11

2-
module "flux2-ecr-webhook" {
3-
source = "../../"
4-
5-
app_name = "flux2-ecr-webhook"
6-
repo_mapping_file = "repos.yml" # Deprecated
7-
webhook_token = "WEBHOOK-TOKEN" # Keep this token safe, you can use sops (mozilla/sops).
8-
cw_logs_retention = 7
9-
}
10-
112
module "flux2-ecr-webhook2" {
123
source = "../../"
134

145
app_name = "flux2-ecr-webhook2"
156
repo_mapping = {
16-
test/my-ecr-repo = {
17-
webhook = "https://gitops.domain.com/hook/11111111111"
7+
"test/my-ecr-repo" = {
8+
production = {
9+
webhook = ["https://gitops.domain.com/hook/11111111111"]
10+
}
1811
}
1912
}
2013
webhook_token = "WEBHOOK-TOKEN" # Keep this token safe, you can use sops (mozilla/sops).
@@ -26,15 +19,22 @@ module "flux2-ecr-webhook3" {
2619
app_name = "flux2-ecr-webhook3"
2720
repo_mapping = {
2821
my-ecr-repo = {
29-
webhook = "https://gitops.domain.com/hook/11111111111"
30-
token = "WEBHOOK-TOKEN" # Keep this token safe, you can use sops (mozilla/sops).
22+
prod = {
23+
webhook = ["https://gitops.domain.com/hook/11111111111"]
24+
}
3125
}
3226
my-ecr-repo2 = {
33-
webhook = "https://gitops.domain.com/hook/11111111111"
27+
prod = {
28+
webhook = ["https://gitops.domain.com/hook/11111111111"]
29+
regex = "prod-(?P<version>.*)" # Regex for ECR image tag
30+
}
3431
}
3532
my-ecr-repo3 = {
36-
webhook = "https://gitops.domain.com/hook/11111111111"
33+
prod = {
34+
webhook = ["https://gitops.domain.com/hook/11111111111"]
35+
token = "WEBHOOK-TOKEN" # Custom token (you can use mozilla/sops).
36+
}
3737
}
3838
}
39-
webhook_token = "WEBHOOK-TOKEN" # Keep this token safe, you can use sops (mozilla/sops).
40-
}
39+
webhook_token = "WEBHOOK-TOKEN" # Webhook token (you can use mozilla/sops).
40+
}

src/call_receiver/app.py

+17-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import boto3
33
import requests
44
import os
5+
import re
56

67
secretsmanager = boto3.client('secretsmanager')
78
TOKEN_SECRET_NAME = os.environ['FLUX2_WEBHOOK_TOKEN_SECRET_NAME']
@@ -54,7 +55,8 @@ def make_requests(webhook_url, repository, headers):
5455
'repository': repository
5556
}))
5657

57-
def call_flux_webhook(repository):
58+
59+
def call_flux_webhook(repository, image_tag):
5860
# Retrieve the map of values from Secrets Manager
5961
webhook_map = get_webhook_map()
6062

@@ -63,13 +65,19 @@ def call_flux_webhook(repository):
6365
token = None
6466
if repository in webhook_map:
6567
repo_data = webhook_map[repository]
66-
webhook_urls = repo_data.get('webhook')
67-
token = repo_data.get('token', get_global_token())
68-
for webhook in webhook_urls:
69-
headers = {'Authorization': f'Bearer {token}'}
70-
make_requests(webhook, repository, headers)
71-
72-
68+
for key, data in repo_data.items():
69+
webhook_urls = data.get('webhook')
70+
token = data.get('token', get_global_token())
71+
regex = data.get('regex', '.*')
72+
for webhook in webhook_urls:
73+
headers = {'Authorization': f'Bearer {token}'}
74+
if regex and re.match(regex, image_tag):
75+
make_requests(webhook, repository, headers)
76+
else:
77+
print(json.dumps({
78+
'message': f'The {image_tag} tag does not match the regular expresion ({regex})',
79+
'repository': repository
80+
}))
7381

7482

7583
def lambda_handler(event, context):
@@ -82,7 +90,7 @@ def lambda_handler(event, context):
8290
process_ecr_push_event(detail)
8391

8492
# Call the Flux webhook with the event repository
85-
call_flux_webhook(detail['repository-name'])
93+
call_flux_webhook(detail['repository-name'], detail['image-tag'])
8694

8795
return {
8896
'statusCode': 200,

variables.tf

+6-13
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,15 @@ variable "repo_mapping" {
1414
type = any
1515
default = null
1616
sensitive = true
17-
#description = "Object with repository mapping, if this variable is set `repo_mapping_file` will be ignored."
1817
description = <<EOT
1918
Object with repository mapping, if this variable is set `repo_mapping_file` will be ignored.
20-
**Example:**
2119
22-
```
23-
{
24-
ecr-repo-name = {
25-
webhook = ["https://gitops.domain.com/hook/111111" ]
26-
}
27-
test/ecr-repo-name = {
28-
webhook = ["https://gitops.domain.com/hook/111111", "https://gitops.domain.com/hook/222222" ]
29-
token = "webhook-token "
30-
}
31-
}
32-
```
20+
**Available Attributes:**
21+
- `<ECR>`: ECR resource name.
22+
- `<ECR>.<ID>`: Unique name for webhooks.
23+
- `<ECR>.<ID>.webhook`: Webhook list.
24+
- `<ECR>.<ID>.token` (Optional): Token used for webhooks, if set, then "webhook_token" will be ignored.
25+
- `<ECR>.<ID>.regex` (Optional): Regular expression that is applied to the image tag
3326
3427
EOT
3528
}

0 commit comments

Comments
 (0)