Skip to content

Commit 573fa0a

Browse files
committed
refactor input images to input repository arns and lookup config from tags
1 parent 8424620 commit 573fa0a

16 files changed

+420
-573
lines changed

README.md

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# Lambda-ecr-image-sync
22
![docker build](https://github.com/martijnvdp/lambda-ecr-image-sync/actions/workflows/release-docker-slim.yml/badge.svg)
33

4-
This is a Golang Lambda function that compares images between ECR and public repositories such as DockerHub, Quay.io, and GCR.io. It has the capability to sync the images directly to the target ECR on AWS or output a zipped CSV file with the missing images/tags to an S3 bucket. Another script can then pick up the CSV file to sync the missing images.
5-
6-
The function compares the provided images and tags between ECR and the public registry using the Crane library to login and copy the missing images to the ECR on AWS. If the Action: s3 is set in the Lambda event, the function will only place the missing images in a CSV file in an S3 bucket. This CSV file can be used by other tools, such as CodePipeline, to synchronize the missing images mentioned in the CSV.
4+
This is a Golang Lambda function that compares images between ECR and public repositories such as DockerHub, Quay.io, and GCR.io and synces/copies the missing images to the ECR. It has the capability to sync the images directly to the target ECR on AWS or output a zipped CSV file with the missing images/tags to an S3 bucket.
75

6+
The function compares the provided images and tags between ECR and the public registry using the Crane library to login and copy the missing images to the ECR on AWS.
87
This function is compatible with most container registries. For more information, please refer to the container lib at https://github.com/containers/image.
98

109
## Docker images
@@ -18,7 +17,7 @@ Set environment variables in the lambda configuration section. \
1817
https://github.com/martijnvdp/terraform-ecr-image-sync
1918

2019
Image names format:
21-
(registry hostname)/imagename/name
20+
(registry hostname)/Source/name
2221

2322
```hcl
2423
docker.io/datadog/agent
@@ -41,26 +40,8 @@ Lambda event data:
4140

4241
```hcl
4342
{
44-
"ecr_repo_prefix":"base/images" // optional global_ecr_repo_prefix
45-
"repository_arn":"arn:aws:ecr:us-east-1:123456789012:repository/base/infra/datadog/datadog-operator" //optional to sync a single repository
46-
"images": [ // optional images payload to sync
47-
{
48-
"constraint": "~>2.0"
49-
"exclude_rls": ["beta","rc"] \\ excluded pre-releases matches the text after eg: 1.1-beta beta
50-
"exclude_tags": [],
51-
"image_name": "docker.io/hashicorp/tfc-agent",
52-
"include_tags": [],
53-
"include_rls": ["linux","debian","cee"] \\ included pre-releases matches the text after eg: 1.1-beta beta
54-
"max_results": 10
55-
"repo_prefix": "ecr/cm"
56-
},
57-
{
58-
"exclude_tags": [],
59-
"image_name": "docker.io/datadog/agent",
60-
"include_tags": ["latest","6.27.0-rc.6"],
61-
"repo_prefix": "ecr/cm"
62-
}
63-
]
43+
"repositories": [ // optional if not specified it wil syn call repos that are configured with tags
44+
"arn:aws:ecr:us-east-1:123456789012:repository/dev/datadog/datadog-operator","arn:aws:ecr:us-east-1:123456789012:repository/dev/datadog/datadog"]
6445
"check_digest": true // check digest of existing tags on ecr and only add tags if the digest is not the same
6546
"max_results": 5
6647
"slack_channel_id":"CDDF324"

pkg/lambda/check_digest_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func Test_checkDigest(t *testing.T) {
5757
"docker.io/nginx:1.23.3": {
5858
name: "nginx",
5959
tag: "1.23.3",
60-
hash: "sha256:7f797701ded5055676d656f11071f84e2888548a2e7ed12a4977c28ef6114b17",
60+
hash: "sha256:942ae2dfd73088b54d7151a3c3fd5af038a51c50029bfcfd21f1e650d9579967",
6161
},
6262
},
6363
},

pkg/lambda/check_tags.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,43 +45,43 @@ func comparePreReleases(v *version.Version, releases *[]string) bool {
4545
return false
4646
}
4747

48-
func (i *inputImage) checkExcConstraints(v *version.Version, c *version.Constraints) bool {
49-
return comparePreReleases(v, &i.ExcludeRLS)
48+
func (i *inputRepository) checkExcConstraints(v *version.Version, c *version.Constraints) bool {
49+
return comparePreReleases(v, &i.excludeRLS)
5050
}
5151

52-
func (i *inputImage) checkExcTags(t string) bool {
53-
return compareIncExclTags(&t, &i.ExcludeTags)
52+
func (i *inputRepository) checkExcTags(t string) bool {
53+
return compareIncExclTags(&t, &i.excludeTags)
5454
}
5555

56-
func (i *inputImage) checkFilter() bool {
57-
return len(i.IncludeTags) == 0 && len(i.ExcludeTags) == 0 && !(len(i.IncludeRLS) > 0) && !(len(i.ExcludeRLS) > 0) && i.Constraint == ""
56+
func (i *inputRepository) checkFilter() bool {
57+
return len(i.includeTags) == 0 && len(i.excludeTags) == 0 && !(len(i.includeRLS) > 0) && !(len(i.excludeRLS) > 0) && i.constraint == ""
5858
}
5959

60-
func (i *inputImage) checkIncConstraints(v *version.Version, c *version.Constraints) bool {
61-
return comparePreReleases(v, &i.IncludeRLS)
60+
func (i *inputRepository) checkIncConstraints(v *version.Version, c *version.Constraints) bool {
61+
return comparePreReleases(v, &i.includeRLS)
6262
}
6363

64-
func (i *inputImage) checkIncTags(t string) bool {
65-
return compareIncExclTags(&t, &i.IncludeTags)
64+
func (i *inputRepository) checkIncTags(t string) bool {
65+
return compareIncExclTags(&t, &i.includeTags)
6666
}
6767

68-
func (i *inputImage) checkNonVersionTags(tag string) bool {
68+
func (i *inputRepository) checkNonVersionTags(tag string) bool {
6969
switch {
70-
case len(i.IncludeTags) > 0 && i.checkIncTags(tag):
70+
case len(i.includeTags) > 0 && i.checkIncTags(tag):
7171
return true
72-
case len(i.ExcludeTags) > 0 && !i.checkExcTags(tag):
72+
case len(i.excludeTags) > 0 && !i.checkExcTags(tag):
7373
return true
7474
}
7575
return false
7676
}
7777

78-
func (i *inputImage) checkVersionTags(v *version.Version, c *version.Constraints) bool {
78+
func (i *inputRepository) checkVersionTags(v *version.Version, c *version.Constraints) bool {
7979
switch {
80-
case len(i.IncludeTags) > 0 && i.checkIncTags(v.Original()):
80+
case len(i.includeTags) > 0 && i.checkIncTags(v.Original()):
8181
return true
82-
case len(i.ExcludeTags) > 0:
82+
case len(i.excludeTags) > 0:
8383
return !i.checkExcTags(v.Original())
84-
case checkRelease(v, c) && !(i.ReleaseOnly):
84+
case checkRelease(v, c) && !(i.releaseOnly):
8585
return true
8686
case i.checkExcConstraints(v, c):
8787
return false
@@ -92,22 +92,22 @@ func (i *inputImage) checkVersionTags(v *version.Version, c *version.Constraints
9292
return false
9393
}
9494

95-
func (i *inputImage) createConstraint() (constraints version.Constraints, err error) {
96-
if i.Constraint != "" {
97-
return version.NewConstraint(i.Constraint)
95+
func (i *inputRepository) createConstraint() (constraints version.Constraints, err error) {
96+
if i.constraint != "" {
97+
return version.NewConstraint(i.constraint)
9898
}
9999
return version.NewConstraint(noConstraint)
100100
}
101101

102-
func (i *inputImage) maxResults(globalMaxResults int) (maxResults int) {
103-
maxResults = maxInt(globalMaxResults, i.MaxResults)
102+
func (i *inputRepository) getMaxResults(globalMaxResults int) (maxResults int) {
103+
maxResults = maxInt(globalMaxResults, i.maxResults)
104104

105105
if !(maxResults > 0) {
106106
maxResults = -1
107107
}
108108

109-
if len(i.IncludeTags) > 0 && i.Constraint == "" {
110-
return len(i.IncludeTags)
109+
if len(i.includeTags) > 0 && i.constraint == "" {
110+
return len(i.includeTags)
111111
}
112112
return maxResults
113113
}
@@ -146,8 +146,8 @@ func sortVersions(rawTags *[]string) (sortedTags []*version.Version, err error)
146146
return sortedTags, err
147147
}
148148

149-
func (i *inputImage) checkTagsFromPublicRepo(inputTags *[]string, maxResults int) (result []string, err error) {
150-
maxResults = i.maxResults(maxResults)
149+
func (i *inputRepository) checkTagsFromPublicRepo(inputTags *[]string, maxResults int) (result []string, err error) {
150+
maxResults = i.getMaxResults(maxResults)
151151
noFilter := i.checkFilter()
152152
versionTags, nonVersionTags := parseVersions(inputTags)
153153
sortedTags, err := sortVersions(&versionTags)

0 commit comments

Comments
 (0)