Skip to content

Commit f14472b

Browse files
committed
obsolete "keep_aliase". All aliased versions are always kept.
1 parent cb501b7 commit f14472b

3 files changed

Lines changed: 56 additions & 8 deletions

File tree

config.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (r *RepositoryConfig) Validate() error {
110110

111111
if len(r.KeepTagPatterns) == 0 {
112112
log.Printf(
113-
"[warn] keep_tag_patterns are not defind. set default keep_tag_patterns to %v",
113+
"[warn] keep_tag_patterns are not defined. set default keep_tag_patterns to %v",
114114
DefaultKeepTagPatterns,
115115
)
116116
r.KeepTagPatterns = DefaultKeepTagPatterns
@@ -193,7 +193,7 @@ type LambdaConfig struct {
193193
Name string `yaml:"name,omitempty"`
194194
NamePattern string `yaml:"name_pattern,omitempty"`
195195
KeepCount int64 `yaml:"keep_count,omitempty"`
196-
KeepAliase bool `yaml:"keep_aliase,omitempty"`
196+
KeepAliase *bool `yaml:"keep_aliase,omitempty"` // for backward compatibility
197197
}
198198

199199
func (c *LambdaConfig) Validate() error {
@@ -209,6 +209,11 @@ func (c *LambdaConfig) Validate() error {
209209
)
210210
c.KeepCount = int64(DefaultKeepCount)
211211
}
212+
if c.KeepAliase != nil {
213+
log.Printf(
214+
"[warn] \"keep_aliase\" is obsoleted. All aliased versions are always kept. Please remove it from the lambda_functions section.",
215+
)
216+
}
212217
return nil
213218
}
214219

generate.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ func (g *Generator) generateLambdaConfig(ctx context.Context, config *Config) er
149149
}
150150
for _, name := range lambdaNames.members() {
151151
cfg := LambdaConfig{
152-
KeepCount: int64(DefaultKeepCount),
153-
KeepAliase: true,
152+
KeepCount: int64(DefaultKeepCount),
154153
}
155154
if strings.Contains(name, "*") {
156155
cfg.NamePattern = name

lambda.go

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ecrm
22

33
import (
44
"context"
5+
"fmt"
56
"log"
67
"math"
78
"sort"
@@ -33,6 +34,10 @@ func (s *Scanner) scanLambdaFunctions(ctx context.Context, lcs []*LambdaConfig)
3334
continue
3435
}
3536
log.Printf("[debug] Checking Lambda function %s latest %d versions", name, keepCount)
37+
aliases, err := s.getLambdaAliases(ctx, name)
38+
if err != nil {
39+
return fmt.Errorf("failed to get lambda aliases: %w", err)
40+
}
3641
p := lambda.NewListVersionsByFunctionPaginator(
3742
s.lambda,
3843
&lambda.ListVersionsByFunctionInput{
@@ -51,9 +56,7 @@ func (s *Scanner) scanLambdaFunctions(ctx context.Context, lcs []*LambdaConfig)
5156
sort.SliceStable(versions, func(i, j int) bool {
5257
return lambdaVersionInt64(*versions[j].Version) < lambdaVersionInt64(*versions[i].Version)
5358
})
54-
if len(versions) > int(keepCount) {
55-
versions = versions[:int(keepCount)]
56-
}
59+
var kept int64
5760
for _, v := range versions {
5861
log.Println("[debug] Getting Lambda function ", *v.FunctionArn)
5962
f, err := s.lambda.GetFunction(ctx, &lambda.GetFunctionInput{
@@ -67,20 +70,61 @@ func (s *Scanner) scanLambdaFunctions(ctx context.Context, lcs []*LambdaConfig)
6770
continue
6871
}
6972
log.Println("[debug] ImageUri", u)
73+
if a, ok := aliases[*v.Version]; ok { // check if the version is aliased
74+
if s.Images.Add(u, aws.ToString(v.FunctionArn)) {
75+
log.Printf("[info] %s is in use by Lambda function %s:%s (alias=%v)", u.String(), *v.FunctionName, *v.Version, a)
76+
}
77+
continue
78+
}
79+
if kept >= keepCount {
80+
continue
81+
}
7082
if s.Images.Add(u, aws.ToString(v.FunctionArn)) {
7183
log.Printf("[info] %s is in use by Lambda function %s:%s", u.String(), *v.FunctionName, *v.Version)
84+
kept++
7285
}
7386
}
7487
}
7588
return nil
7689
}
7790

91+
func (s *Scanner) getLambdaAliases(ctx context.Context, name string) (map[string][]string, error) {
92+
aliases := make(map[string][]string)
93+
var nextAliasMarker *string
94+
for {
95+
res, err := s.lambda.ListAliases(ctx, &lambda.ListAliasesInput{
96+
FunctionName: &name,
97+
Marker: nextAliasMarker,
98+
})
99+
if err != nil {
100+
return nil, fmt.Errorf("failed to list aliases: %w", err)
101+
}
102+
for _, alias := range res.Aliases {
103+
aliases[*alias.FunctionVersion] = append(aliases[*alias.FunctionVersion], *alias.Name)
104+
if alias.RoutingConfig == nil || alias.RoutingConfig.AdditionalVersionWeights == nil {
105+
continue
106+
}
107+
for v := range alias.RoutingConfig.AdditionalVersionWeights {
108+
aliases[v] = append(aliases[v], *alias.Name)
109+
}
110+
}
111+
if nextAliasMarker = res.NextMarker; nextAliasMarker == nil {
112+
break
113+
}
114+
}
115+
return aliases, nil
116+
}
117+
78118
func lambdaVersionInt64(v string) int64 {
79119
var vi int64
80120
if v == "$LATEST" {
81121
vi = math.MaxInt64
82122
} else {
83-
vi, _ = strconv.ParseInt(v, 10, 64)
123+
var err error
124+
vi, err = strconv.ParseInt(v, 10, 64)
125+
if err != nil {
126+
panic(fmt.Sprintf("invalid version number:%s %s", v, err.Error()))
127+
}
84128
}
85129
return vi
86130
}

0 commit comments

Comments
 (0)