Skip to content

Commit

Permalink
ignore STOPPED task
Browse files Browse the repository at this point in the history
task def in used by STOPPED task is removable.
  • Loading branch information
fujiwara committed Oct 8, 2021
1 parent 58ecb96 commit 89958ed
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions deregister.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,11 @@ func (d *App) Deregister(opt DeregisterOption) error {
ctx, cancel := d.Start()
defer cancel()
d.Log("Starting deregister task definition", opt.DryRunString())
inUse := make(map[string]string)

tasks, err := d.listTasks(ctx, nil)
inUse, err := d.inUseRevisions(ctx)
if err != nil {
return err
}
for _, task := range tasks {
name, _ := taskDefinitionToName(*task.TaskDefinitionArn)
inUse[name] = fmt.Sprintf("%s task", *task.LastStatus)
d.DebugLog(fmt.Sprintf("%s is in use by tasks", name))
}

if d.config.Service != "" {
sv, err := d.DescribeService(ctx)
if err != nil {
return err
}
for _, dp := range sv.Deployments {
name, _ := taskDefinitionToName(*dp.TaskDefinition)
inUse[name] = fmt.Sprintf("%s deployment", *dp.Status)
d.DebugLog(fmt.Sprintf("%s is in use by deployments", name))
}
}

if aws.Int64Value(opt.Revision) > 0 {
return d.deregiserRevision(ctx, opt, inUse)
Expand Down Expand Up @@ -166,6 +148,39 @@ func (d *App) deregisterKeeps(ctx context.Context, opt DeregisterOption, inUse m
return nil
}

func (d *App) inUseRevisions(ctx context.Context) (map[string]string, error) {
inUse := make(map[string]string)
tasks, err := d.listTasks(ctx, nil)
if err != nil {
return nil, err
}
for _, task := range tasks {
name, _ := taskDefinitionToName(*task.TaskDefinitionArn)
st := aws.StringValue(task.LastStatus)
if st == "" {
st = aws.StringValue(task.DesiredStatus)
}
if st != "STOPPED" {
// ignore STOPPED tasks for in use
inUse[name] = st + " task"
}
d.DebugLog(fmt.Sprintf("%s is in use by tasks", name))
}

if d.config.Service != "" {
sv, err := d.DescribeService(ctx)
if err != nil {
return nil, err
}
for _, dp := range sv.Deployments {
name, _ := taskDefinitionToName(*dp.TaskDefinition)
inUse[name] = fmt.Sprintf("%s deployment", *dp.Status)
d.DebugLog(fmt.Sprintf("%s is in use by deployments", name))
}
}
return inUse, nil
}

func taskDefinitionToName(a string) (string, error) {
an, err := arn.Parse(a)
if err != nil {
Expand Down

0 comments on commit 89958ed

Please sign in to comment.