Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ffried committed Apr 4, 2023
1 parent b63a173 commit c6bc229
Show file tree
Hide file tree
Showing 11 changed files with 1,122 additions and 1 deletion.
25 changes: 25 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: 2

updates:
- package-ecosystem: "github-actions"
directory: "/"
open-pull-requests-limit: 10
schedule:
interval: "daily"
time: "07:00"
timezone: "Europe/Berlin"
assignees:
- ffried
reviewers:
- ffried
- package-ecosystem: "gomod"
directory: "/"
open-pull-requests-limit: 10
schedule:
interval: "daily"
time: "07:00"
timezone: "Europe/Berlin"
assignees:
- ffried
reviewers:
- ffried
25 changes: 25 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Build

on:
push:
branches:
- main

jobs:
build:
strategy:
matrix:
os: [linux, darwin, windows]
arch: [amd64, arm64]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version-file: go.mod
check-latest: true
- name: Build
env:
GOOS: ${{ matrix.os }}
GOARCH: ${{ matrix.arch }}
run: go build -C './cmd/cleanup-ecr'
40 changes: 40 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Publish Release Artifacts

on:
release:
types:
- published
- edited

jobs:
build-and-publish:
strategy:
matrix:
os: [linux, darwin, windows]
arch: [amd64, arm64]
env:
TOOL_NAME: cleanup-ecr
OUTPUT_FOLDER: dist
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version-file: go.mod
check-latest: true
- name: Build
env:
GOOS: ${{ matrix.os }}
GOARCH: ${{ matrix.arch }}
run: |
mkdir -p "./${OUTPUT_FOLDER}"
go build -C "./cmd/${TOOL_NAME}" -o "./${OUTPUT_FOLDER}"
- name: Upload Release Artifacts
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAG_NAME: ${{ github.event.release.tag_name }}
BUILT_OS: ${{ matrix.os }}
BUILT_ARCH: ${{ matrix.arch }}
run: |
gh release upload --clobber "${TAG_NAME}" \
"./${OUTPUT_FOLDER}/${TOOL_NAME}#${TOOL_NAME}-${BUILT_OS}-${BUILT_ARCH}"
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# ecr-cleanup
# ECR Cleanup

Cleans Amazon ECR docker repositories by deleting unused images.
71 changes: 71 additions & 0 deletions cmd/cleanup-ecr/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"context"
"flag"
"github.com/sersoft-gmbh/ecr-cleanup/internal/awshelpers"
"github.com/sersoft-gmbh/ecr-cleanup/internal/kubehelpers"
"strings"
)

type AWSDockerImg struct {
registry string
name string
tag string
}

func (img AWSDockerImg) GetRegistry() string {
return img.registry
}

func (img AWSDockerImg) GetName() string {
return img.name
}

func (img AWSDockerImg) GetTag() string {
return img.tag
}

func main() {
kubeConfigPath := flag.String("kubeconfig", kubehelpers.KubeConfigDefaultPath(), "The path to the kubeconfig file")
namespace := flag.String("namespace", "", "The namespace in Kubernetes")
awsAccountId := flag.String("awsAccountId", "", "The AWS Account ID")
repoRegex := flag.String("repositoryRegex", "", "Only matching ECR repositories will be cleaned up")
dryRun := flag.Bool("dryRun", false, "If true, no images will be deleted")
flag.Parse()

k8sHelpersConfig := kubehelpers.Config{
KubeConfigPath: *kubeConfigPath,
Namespace: *namespace,
}
awsHelpersConfig := awshelpers.Config{
AccountId: *awsAccountId,
RepositoryRegex: *repoRegex,
DryRun: *dryRun,
}

ctx := context.Background()

imagesInUse, err := kubehelpers.FindImagesInUse(ctx, k8sHelpersConfig)
if err != nil {
panic(err.Error())
}

awsImagesInUse := make([]awshelpers.AWSDockerImage, 0, len(imagesInUse))
for _, img := range imagesInUse {
// nil means it's a dockerhub image, not an AWS image
// we only want to clean up AWS images, so also check for ECR in the registry
if img.Registry != nil && strings.Contains(*img.Registry, ".ecr.") {
awsImagesInUse = append(awsImagesInUse, AWSDockerImg{
registry: *img.Registry,
name: img.Name,
tag: img.Tag,
})
}
}

err = awshelpers.CleanRepositories(ctx, awsHelpersConfig, awsImagesInUse)
if err != nil {
panic(err.Error())
}
}
62 changes: 62 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
module github.com/sersoft-gmbh/ecr-cleanup

go 1.20

require (
github.com/aws/aws-sdk-go-v2/config v1.18.19
github.com/aws/aws-sdk-go-v2/service/ecr v1.18.7
k8s.io/api v0.26.3
k8s.io/apimachinery v0.26.3
k8s.io/client-go v0.26.3
k8s.io/utils v0.0.0-20221107191617-1a15be271d1d
)

require (
github.com/aws/aws-sdk-go-v2 v1.17.7 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.13.18 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.1 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.31 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.25 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.32 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.25 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.12.6 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.6 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.18.7 // indirect
github.com/aws/smithy-go v1.13.5 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading

0 comments on commit c6bc229

Please sign in to comment.