From f14472b7cf0f12ecf6fe36da4d7f1ed812c55db6 Mon Sep 17 00:00:00 2001 From: fujiwara Date: Fri, 1 Nov 2024 23:41:23 +0900 Subject: [PATCH] obsolete "keep_aliase". All aliased versions are always kept. --- config.go | 9 +++++++-- generate.go | 3 +-- lambda.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 56 insertions(+), 8 deletions(-) diff --git a/config.go b/config.go index ad42684..d3f33cd 100644 --- a/config.go +++ b/config.go @@ -110,7 +110,7 @@ func (r *RepositoryConfig) Validate() error { if len(r.KeepTagPatterns) == 0 { log.Printf( - "[warn] keep_tag_patterns are not defind. set default keep_tag_patterns to %v", + "[warn] keep_tag_patterns are not defined. set default keep_tag_patterns to %v", DefaultKeepTagPatterns, ) r.KeepTagPatterns = DefaultKeepTagPatterns @@ -193,7 +193,7 @@ type LambdaConfig struct { Name string `yaml:"name,omitempty"` NamePattern string `yaml:"name_pattern,omitempty"` KeepCount int64 `yaml:"keep_count,omitempty"` - KeepAliase bool `yaml:"keep_aliase,omitempty"` + KeepAliase *bool `yaml:"keep_aliase,omitempty"` // for backward compatibility } func (c *LambdaConfig) Validate() error { @@ -209,6 +209,11 @@ func (c *LambdaConfig) Validate() error { ) c.KeepCount = int64(DefaultKeepCount) } + if c.KeepAliase != nil { + log.Printf( + "[warn] \"keep_aliase\" is obsoleted. All aliased versions are always kept. Please remove it from the lambda_functions section.", + ) + } return nil } diff --git a/generate.go b/generate.go index e7053bc..271f5cf 100644 --- a/generate.go +++ b/generate.go @@ -149,8 +149,7 @@ func (g *Generator) generateLambdaConfig(ctx context.Context, config *Config) er } for _, name := range lambdaNames.members() { cfg := LambdaConfig{ - KeepCount: int64(DefaultKeepCount), - KeepAliase: true, + KeepCount: int64(DefaultKeepCount), } if strings.Contains(name, "*") { cfg.NamePattern = name diff --git a/lambda.go b/lambda.go index fc7fd3d..668f216 100644 --- a/lambda.go +++ b/lambda.go @@ -2,6 +2,7 @@ package ecrm import ( "context" + "fmt" "log" "math" "sort" @@ -33,6 +34,10 @@ func (s *Scanner) scanLambdaFunctions(ctx context.Context, lcs []*LambdaConfig) continue } log.Printf("[debug] Checking Lambda function %s latest %d versions", name, keepCount) + aliases, err := s.getLambdaAliases(ctx, name) + if err != nil { + return fmt.Errorf("failed to get lambda aliases: %w", err) + } p := lambda.NewListVersionsByFunctionPaginator( s.lambda, &lambda.ListVersionsByFunctionInput{ @@ -51,9 +56,7 @@ func (s *Scanner) scanLambdaFunctions(ctx context.Context, lcs []*LambdaConfig) sort.SliceStable(versions, func(i, j int) bool { return lambdaVersionInt64(*versions[j].Version) < lambdaVersionInt64(*versions[i].Version) }) - if len(versions) > int(keepCount) { - versions = versions[:int(keepCount)] - } + var kept int64 for _, v := range versions { log.Println("[debug] Getting Lambda function ", *v.FunctionArn) f, err := s.lambda.GetFunction(ctx, &lambda.GetFunctionInput{ @@ -67,20 +70,61 @@ func (s *Scanner) scanLambdaFunctions(ctx context.Context, lcs []*LambdaConfig) continue } log.Println("[debug] ImageUri", u) + if a, ok := aliases[*v.Version]; ok { // check if the version is aliased + if s.Images.Add(u, aws.ToString(v.FunctionArn)) { + log.Printf("[info] %s is in use by Lambda function %s:%s (alias=%v)", u.String(), *v.FunctionName, *v.Version, a) + } + continue + } + if kept >= keepCount { + continue + } if s.Images.Add(u, aws.ToString(v.FunctionArn)) { log.Printf("[info] %s is in use by Lambda function %s:%s", u.String(), *v.FunctionName, *v.Version) + kept++ } } } return nil } +func (s *Scanner) getLambdaAliases(ctx context.Context, name string) (map[string][]string, error) { + aliases := make(map[string][]string) + var nextAliasMarker *string + for { + res, err := s.lambda.ListAliases(ctx, &lambda.ListAliasesInput{ + FunctionName: &name, + Marker: nextAliasMarker, + }) + if err != nil { + return nil, fmt.Errorf("failed to list aliases: %w", err) + } + for _, alias := range res.Aliases { + aliases[*alias.FunctionVersion] = append(aliases[*alias.FunctionVersion], *alias.Name) + if alias.RoutingConfig == nil || alias.RoutingConfig.AdditionalVersionWeights == nil { + continue + } + for v := range alias.RoutingConfig.AdditionalVersionWeights { + aliases[v] = append(aliases[v], *alias.Name) + } + } + if nextAliasMarker = res.NextMarker; nextAliasMarker == nil { + break + } + } + return aliases, nil +} + func lambdaVersionInt64(v string) int64 { var vi int64 if v == "$LATEST" { vi = math.MaxInt64 } else { - vi, _ = strconv.ParseInt(v, 10, 64) + var err error + vi, err = strconv.ParseInt(v, 10, 64) + if err != nil { + panic(fmt.Sprintf("invalid version number:%s %s", v, err.Error())) + } } return vi }