Skip to content

Commit

Permalink
Merge pull request #7 from martijnvdp/refactor-and-remove-csv-output
Browse files Browse the repository at this point in the history
refactor input images to input repository arns and lookup config from…
  • Loading branch information
martijnvdp authored Mar 5, 2023
2 parents 8424620 + 573fa0a commit 37b47a5
Show file tree
Hide file tree
Showing 16 changed files with 420 additions and 573 deletions.
29 changes: 5 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Lambda-ecr-image-sync
![docker build](https://github.com/martijnvdp/lambda-ecr-image-sync/actions/workflows/release-docker-slim.yml/badge.svg)

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.

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.
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.

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.
This function is compatible with most container registries. For more information, please refer to the container lib at https://github.com/containers/image.

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

Image names format:
(registry hostname)/imagename/name
(registry hostname)/Source/name

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

```hcl
{
"ecr_repo_prefix":"base/images" // optional global_ecr_repo_prefix
"repository_arn":"arn:aws:ecr:us-east-1:123456789012:repository/base/infra/datadog/datadog-operator" //optional to sync a single repository
"images": [ // optional images payload to sync
{
"constraint": "~>2.0"
"exclude_rls": ["beta","rc"] \\ excluded pre-releases matches the text after eg: 1.1-beta beta
"exclude_tags": [],
"image_name": "docker.io/hashicorp/tfc-agent",
"include_tags": [],
"include_rls": ["linux","debian","cee"] \\ included pre-releases matches the text after eg: 1.1-beta beta
"max_results": 10
"repo_prefix": "ecr/cm"
},
{
"exclude_tags": [],
"image_name": "docker.io/datadog/agent",
"include_tags": ["latest","6.27.0-rc.6"],
"repo_prefix": "ecr/cm"
}
]
"repositories": [ // optional if not specified it wil syn call repos that are configured with tags
"arn:aws:ecr:us-east-1:123456789012:repository/dev/datadog/datadog-operator","arn:aws:ecr:us-east-1:123456789012:repository/dev/datadog/datadog"]
"check_digest": true // check digest of existing tags on ecr and only add tags if the digest is not the same
"max_results": 5
"slack_channel_id":"CDDF324"
Expand Down
2 changes: 1 addition & 1 deletion pkg/lambda/check_digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func Test_checkDigest(t *testing.T) {
"docker.io/nginx:1.23.3": {
name: "nginx",
tag: "1.23.3",
hash: "sha256:7f797701ded5055676d656f11071f84e2888548a2e7ed12a4977c28ef6114b17",
hash: "sha256:942ae2dfd73088b54d7151a3c3fd5af038a51c50029bfcfd21f1e650d9579967",
},
},
},
Expand Down
52 changes: 26 additions & 26 deletions pkg/lambda/check_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,43 +45,43 @@ func comparePreReleases(v *version.Version, releases *[]string) bool {
return false
}

func (i *inputImage) checkExcConstraints(v *version.Version, c *version.Constraints) bool {
return comparePreReleases(v, &i.ExcludeRLS)
func (i *inputRepository) checkExcConstraints(v *version.Version, c *version.Constraints) bool {
return comparePreReleases(v, &i.excludeRLS)
}

func (i *inputImage) checkExcTags(t string) bool {
return compareIncExclTags(&t, &i.ExcludeTags)
func (i *inputRepository) checkExcTags(t string) bool {
return compareIncExclTags(&t, &i.excludeTags)
}

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

func (i *inputImage) checkIncConstraints(v *version.Version, c *version.Constraints) bool {
return comparePreReleases(v, &i.IncludeRLS)
func (i *inputRepository) checkIncConstraints(v *version.Version, c *version.Constraints) bool {
return comparePreReleases(v, &i.includeRLS)
}

func (i *inputImage) checkIncTags(t string) bool {
return compareIncExclTags(&t, &i.IncludeTags)
func (i *inputRepository) checkIncTags(t string) bool {
return compareIncExclTags(&t, &i.includeTags)
}

func (i *inputImage) checkNonVersionTags(tag string) bool {
func (i *inputRepository) checkNonVersionTags(tag string) bool {
switch {
case len(i.IncludeTags) > 0 && i.checkIncTags(tag):
case len(i.includeTags) > 0 && i.checkIncTags(tag):
return true
case len(i.ExcludeTags) > 0 && !i.checkExcTags(tag):
case len(i.excludeTags) > 0 && !i.checkExcTags(tag):
return true
}
return false
}

func (i *inputImage) checkVersionTags(v *version.Version, c *version.Constraints) bool {
func (i *inputRepository) checkVersionTags(v *version.Version, c *version.Constraints) bool {
switch {
case len(i.IncludeTags) > 0 && i.checkIncTags(v.Original()):
case len(i.includeTags) > 0 && i.checkIncTags(v.Original()):
return true
case len(i.ExcludeTags) > 0:
case len(i.excludeTags) > 0:
return !i.checkExcTags(v.Original())
case checkRelease(v, c) && !(i.ReleaseOnly):
case checkRelease(v, c) && !(i.releaseOnly):
return true
case i.checkExcConstraints(v, c):
return false
Expand All @@ -92,22 +92,22 @@ func (i *inputImage) checkVersionTags(v *version.Version, c *version.Constraints
return false
}

func (i *inputImage) createConstraint() (constraints version.Constraints, err error) {
if i.Constraint != "" {
return version.NewConstraint(i.Constraint)
func (i *inputRepository) createConstraint() (constraints version.Constraints, err error) {
if i.constraint != "" {
return version.NewConstraint(i.constraint)
}
return version.NewConstraint(noConstraint)
}

func (i *inputImage) maxResults(globalMaxResults int) (maxResults int) {
maxResults = maxInt(globalMaxResults, i.MaxResults)
func (i *inputRepository) getMaxResults(globalMaxResults int) (maxResults int) {
maxResults = maxInt(globalMaxResults, i.maxResults)

if !(maxResults > 0) {
maxResults = -1
}

if len(i.IncludeTags) > 0 && i.Constraint == "" {
return len(i.IncludeTags)
if len(i.includeTags) > 0 && i.constraint == "" {
return len(i.includeTags)
}
return maxResults
}
Expand Down Expand Up @@ -146,8 +146,8 @@ func sortVersions(rawTags *[]string) (sortedTags []*version.Version, err error)
return sortedTags, err
}

func (i *inputImage) checkTagsFromPublicRepo(inputTags *[]string, maxResults int) (result []string, err error) {
maxResults = i.maxResults(maxResults)
func (i *inputRepository) checkTagsFromPublicRepo(inputTags *[]string, maxResults int) (result []string, err error) {
maxResults = i.getMaxResults(maxResults)
noFilter := i.checkFilter()
versionTags, nonVersionTags := parseVersions(inputTags)
sortedTags, err := sortVersions(&versionTags)
Expand Down
Loading

0 comments on commit 37b47a5

Please sign in to comment.