Skip to content

Commit

Permalink
fix: skip alias flag not catching all instances
Browse files Browse the repository at this point in the history
* test: added test for lambda aliases

* chore: added test script

* fix: fixed alias pagination error

* ci: fixed version issue

* test: added more test for codecoverage

* ci: updated docker dependencies
  • Loading branch information
karl-cardenas-coding authored Feb 3, 2024
1 parent 4a002bb commit 07c6875
Show file tree
Hide file tree
Showing 11 changed files with 570 additions and 161 deletions.
44 changes: 22 additions & 22 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
steps:

- name: "Check out code into the Go module directory"
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Node.js environment
uses: actions/setup-node@v3
Expand Down Expand Up @@ -49,7 +49,7 @@ jobs:


- name: "Check out code into the Go module directory"
uses: actions/checkout@v3
uses: actions/checkout@v4

- id: go
name: "Set up Go 1.21"
Expand All @@ -61,10 +61,10 @@ jobs:
- name: create
run: |
aws ec2 describe-regions --all-regions --region us-east-1 --query "Regions[].RegionName" --output text >> cmd/aws-regions.txt
go get && go build -ldflags="-X 'github.com/karl-cardenas-coding/go-lambda-cleanup/cmd.VersionString=${{needs.next-version.outputs.version}}'" -o=glc -v && zip go-lambda-cleanup-v${{needs.next-version.outputs.version}}-${{env.GOOS}}-${{env.GOARCH}}.zip glc
go get && go build -ldflags="-X 'github.com/karl-cardenas-coding/go-lambda-cleanup/v2/cmd.VersionString=${{needs.next-version.outputs.version}}'" -o=glc -v && zip go-lambda-cleanup-v${{needs.next-version.outputs.version}}-${{env.GOOS}}-${{env.GOARCH}}.zip glc
- uses: actions/upload-artifact@v1
- uses: actions/upload-artifact@v4
with:
name: go-lambda-cleanup-v${{ needs.next-version.outputs.version }}-${{env.GOOS}}-${{env.GOARCH}}.zip
path: go-lambda-cleanup-v${{ needs.next-version.outputs.version }}-${{env.GOOS}}-${{env.GOARCH}}.zip
Expand Down Expand Up @@ -93,8 +93,8 @@ jobs:
name: create
run: |
aws ec2 describe-regions --all-regions --region us-east-1 --query "Regions[].RegionName" --output text >> cmd/aws-regions.txt
go get && go build -ldflags="-X 'github.com/karl-cardenas-coding/go-lambda-cleanup/cmd.VersionString=${{needs.next-version.outputs.version}}'" -o=glc -v && zip go-lambda-cleanup-v${{needs.next-version.outputs.version}}-${{env.GOOS}}-${{env.GOARCH}}.zip glc
- uses: actions/upload-artifact@v1
go get && go build -ldflags="-X 'github.com/karl-cardenas-coding/go-lambda-cleanup/v2/cmd.VersionString=${{needs.next-version.outputs.version}}'" -o=glc -v && zip go-lambda-cleanup-v${{needs.next-version.outputs.version}}-${{env.GOOS}}-${{env.GOARCH}}.zip glc
- uses: actions/upload-artifact@v4
with:
name: go-lambda-cleanup-v${{ needs.next-version.outputs.version }}-${{env.GOOS}}-${{env.GOARCH}}.zip
path: go-lambda-cleanup-v${{ needs.next-version.outputs.version }}-${{env.GOOS}}-${{env.GOARCH}}.zip
Expand All @@ -110,7 +110,7 @@ jobs:
GOOS: linux
steps:
- name: "Check out code into the Go module directory"
uses: actions/checkout@v3
uses: actions/checkout@v4


- id: go
Expand All @@ -123,8 +123,8 @@ jobs:
name: create
run: |
aws ec2 describe-regions --all-regions --region us-east-1 --query "Regions[].RegionName" --output text >> cmd/aws-regions.txt
go get && go build -ldflags="-X 'github.com/karl-cardenas-coding/go-lambda-cleanup/cmd.VersionString=${{needs.next-version.outputs.version}}'" -o=glc -v && zip go-lambda-cleanup-v${{needs.next-version.outputs.version}}-${{env.GOOS}}-${{env.GOARCH}}.zip glc
- uses: actions/upload-artifact@v1
go get && go build -ldflags="-X 'github.com/karl-cardenas-coding/go-lambda-cleanup/v2/cmd.VersionString=${{needs.next-version.outputs.version}}'" -o=glc -v && zip go-lambda-cleanup-v${{needs.next-version.outputs.version}}-${{env.GOOS}}-${{env.GOARCH}}.zip glc
- uses: actions/upload-artifact@v4
with:
name: go-lambda-cleanup-v${{ needs.next-version.outputs.version }}-${{env.GOOS}}-${{env.GOARCH}}.zip
path: go-lambda-cleanup-v${{ needs.next-version.outputs.version }}-${{env.GOOS}}-${{env.GOARCH}}.zip
Expand All @@ -139,7 +139,7 @@ jobs:
GOOS: linux
steps:
- name: "Check out code into the Go module directory"
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Node.js environment
uses: actions/setup-node@v3
Expand All @@ -157,8 +157,8 @@ jobs:
name: create
run: |
aws ec2 describe-regions --all-regions --region us-east-1 --query "Regions[].RegionName" --output text >> cmd/aws-regions.txt
go get && go build -ldflags="-X 'github.com/karl-cardenas-coding/go-lambda-cleanup/cmd.VersionString=v${{needs.next-version.outputs.version}}'" -o=glc -v && zip go-lambda-cleanup-v${{needs.next-version.outputs.version}}-${{env.GOOS}}-${{env.GOARCH}}.zip glc
- uses: actions/upload-artifact@v1
go get && go build -ldflags="-X 'github.com/karl-cardenas-coding/go-lambda-cleanup/v2/cmd.VersionString=v${{needs.next-version.outputs.version}}'" -o=glc -v && zip go-lambda-cleanup-v${{needs.next-version.outputs.version}}-${{env.GOOS}}-${{env.GOARCH}}.zip glc
- uses: actions/upload-artifact@v4
with:
name: go-lambda-cleanup-v${{needs.next-version.outputs.version}}-${{env.GOOS}}-${{env.GOARCH}}.zip
path: go-lambda-cleanup-v${{needs.next-version.outputs.version}}-${{env.GOOS}}-${{env.GOARCH}}.zip
Expand All @@ -176,7 +176,7 @@ jobs:
GOOS: windows
steps:
- name: "Check out code into the Go module directory"
uses: actions/checkout@v3
uses: actions/checkout@v4

- id: go
name: "Set up Go 1.21"
Expand All @@ -187,8 +187,8 @@ jobs:
- name: create
run: |
aws ec2 describe-regions --all-regions --region us-east-1 --query "Regions[].RegionName" --output text >> cmd/aws-regions.txt
go get && go build -ldflags="-X 'github.com/karl-cardenas-coding/go-lambda-cleanup/cmd.VersionString=${{needs.next-version.outputs.version}}'" -o=glc.exe -v && zip go-lambda-cleanup-v${{needs.next-version.outputs.version}}-${{env.GOOS}}-${{env.GOARCH}}.zip glc.exe
- uses: actions/upload-artifact@v1
go get && go build -ldflags="-X 'github.com/karl-cardenas-coding/go-lambda-cleanup/v2/cmd.VersionString=${{needs.next-version.outputs.version}}'" -o=glc.exe -v && zip go-lambda-cleanup-v${{needs.next-version.outputs.version}}-${{env.GOOS}}-${{env.GOARCH}}.zip glc.exe
- uses: actions/upload-artifact@v4
with:
name: go-lambda-cleanup-v${{needs.next-version.outputs.version}}-${{env.GOOS}}-${{env.GOARCH}}.zip
path: go-lambda-cleanup-v${{needs.next-version.outputs.version}}-${{env.GOOS}}-${{env.GOARCH}}.zip
Expand All @@ -211,7 +211,7 @@ jobs:


- name: Check out code into the Go module directory
uses: actions/checkout@v3
uses: actions/checkout@v4


- name: Generate AWS region file
Expand Down Expand Up @@ -254,7 +254,7 @@ jobs:
VERSION: ${{ needs.next-version.outputs.version != ''}}
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v3
uses: actions/checkout@v4


- name: Setup Node.js environment
Expand All @@ -263,29 +263,29 @@ jobs:
node-version: '18'
cache: 'npm'

- uses: actions/download-artifact@v1
- uses: actions/download-artifact@v4
with:
name: go-lambda-cleanup-v${{ needs.next-version.outputs.version }}-darwin-amd64.zip
path: ./

- uses: actions/download-artifact@v1
- uses: actions/download-artifact@v4
with:
name: go-lambda-cleanup-v${{ needs.next-version.outputs.version }}-linux-386.zip
path: ./

- uses: actions/download-artifact@v1
- uses: actions/download-artifact@v4
with:
name: go-lambda-cleanup-v${{ needs.next-version.outputs.version }}-darwin-arm64.zip
path: ./


- uses: actions/download-artifact@v1
- uses: actions/download-artifact@v4
with:
name: go-lambda-cleanup-v${{ needs.next-version.outputs.version }}-linux-amd64.zip
path: ./


- uses: actions/download-artifact@v1
- uses: actions/download-artifact@v4
with:
name: go-lambda-cleanup-v${{ needs.next-version.outputs.version }}-windows-amd64.zip
path: ./
Expand Down
9 changes: 2 additions & 7 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
branches: [ main ]
env:
GITHUB_TOKEN: ${{ github.token }}
LINT_VERSION: "1.25.1"

permissions:
contents: read
Expand All @@ -28,8 +25,6 @@ jobs:

test:
needs: [run-ci]
outputs:
VERSION: ${{ steps.go.outputs.version }}
name: Test
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -79,7 +74,7 @@ jobs:
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
run: |
echo "--------------------------------------------------"
go build -ldflags="-X 'github.com/karl-cardenas-coding/go-lambda-cleanup/cmd.VersionString=0.0.0'" -o=glc -v
go build -ldflags="-X 'github.com/karl-cardenas-coding/go-lambda-cleanup/v2/cmd.VersionString=0.0.0'" -o=glc -v
./glc clean -r us-west-2
Expand All @@ -102,7 +97,7 @@ jobs:
uses: docker/build-push-action@v4
with:
context: .
build-args: VERSION=${{steps.dependencies.outputs.VERSION}}
build-args: VERSION="99.99.99"
platforms: linux/amd64,linux/arm64
push: true
tags: ghcr.io/${{ github.repository }}:test
7 changes: 2 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ name: Branch Tests
on:
push:
branches-ignore: [ main ]
env:
GITHUB_TOKEN: ${{ github.token }}
LINT_VERSION: "1.25.1"


jobs:
Expand All @@ -22,7 +19,7 @@ jobs:
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Generate AWS region file
env:
Expand Down Expand Up @@ -53,5 +50,5 @@ jobs:
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
run: |
echo "--------------------------------------------------"
go build -ldflags="-X 'github.com/karl-cardenas-coding/go-lambda-cleanup/cmd.VersionString=0.0.0'" -o=glc -v
go build -ldflags="-X 'github.com/karl-cardenas-coding/go-lambda-cleanup/v2/cmd.VersionString=0.0.0'" -o=glc -v
./glc clean -r us-west-2
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) karl-cardenas-coding
# SPDX-License-Identifier: MIT

FROM golang:1.21.4-alpine3.18 as builder
FROM golang:1.21.6-alpine3.19 as builder

LABEL org.opencontainers.image.source="http://github.com/karl-cardenas-coding/go-lambda-cleanup"
LABEL org.opencontainers.image.description "A solution for removing previous versions of AWS Lambdas"
Expand All @@ -11,7 +11,7 @@ ARG VERSION
ADD ./ /source
RUN cd /source && \
adduser -H -u 1002 -D appuser appuser && \
go build -ldflags="-X 'github.com/karl-cardenas-coding/go-lambda-cleanup/cmd.VersionString=${VERSION}'" -o glc -v
go build -ldflags="-X 'github.com/karl-cardenas-coding/go-lambda-cleanup/v2/cmd.VersionString=${VERSION}'" -o glc -v

FROM alpine:latest

Expand Down
42 changes: 26 additions & 16 deletions cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,17 @@ func executeClean(ctx context.Context, config *cliConfig, svc *lambda.Client, cu
updatedCounter = updatedCounter + v
}

log.Info("Total space freed up: ", (calculateFileSize(uint64(counter-updatedCounter), config)))
log.Info("Post clean-up storage size: ", calculateFileSize(uint64(updatedCounter), config))
log.Info("*********************************************")
}
if len(lambdaList) == 0 {
log.Info("No lambdas found in ", *config.RegionFlag)
} else {

log.Info("Total versions removed: ", countDeleteVersions(globalLambdaDeleteInputStructs))
log.Info("Total space freed up: ", (calculateFileSize(uint64(counter-updatedCounter), config)))
log.Info("Post clean-up storage size: ", calculateFileSize(uint64(updatedCounter), config))
log.Info("*********************************************")

}

if len(lambdaList) == 0 {
log.Info("No lambdas found in ", *config.RegionFlag)
}

displayDuration(startTime)
Expand Down Expand Up @@ -381,7 +385,7 @@ func deleteLambdaVersion(ctx context.Context, svc *lambda.Client, deleteList ...
defer wg.Done()
_, err := svc.DeleteFunction(ctx, &version)
if err != nil {
log.Error(err)
err = errors.New("Failed to delete version " + *version.Qualifier + " of " + *version.FunctionName + ". \n Additional details: " + err.Error())
returnError = err
}
}()
Expand Down Expand Up @@ -493,25 +497,31 @@ func getAllLambdaVersion(

if *flags.SkipAliases {
// fetch the list of aliases for this function
aliasesOut, err := svc.ListAliases(ctx, &lambda.ListAliasesInput{

pg := lambda.NewListAliasesPaginator(svc, &lambda.ListAliasesInput{
FunctionName: aws.String(*item.FunctionArn),
MaxItems: aws.Int32(maxItems),
})
if err != nil {
log.Error(err)
return lambdasLisOutput, err

var aliasesOut []types.AliasConfiguration
for pg.HasMorePages() {
page, err := pg.NextPage(ctx)
if err != nil {
log.Error(err)
return lambdasLisOutput, err
}
aliasesOut = append(aliasesOut, page.Aliases...)
}

log.Debug(fmt.Sprintf("Lamba function %s has %d aliases \n", *item.FunctionName, len(aliasesOut)))

// produce a new slice that includes only versions for which there is no alias
var result []types.FunctionConfiguration

// iterate over the list of versions and check if there is an alias for each
// if there is no alias, add the version to the result
// if there is an alias, skip the version
// this is done to avoid deleting versions that are in use by an alias
for _, funConf := range lambdasLisOutput {
isAlias := false

for _, alias := range aliasesOut.Aliases {
for _, alias := range aliasesOut {
if alias.FunctionVersion != nil && *alias.FunctionVersion == *funConf.Version {
isAlias = true
break
Expand Down
Loading

0 comments on commit 07c6875

Please sign in to comment.