Skip to content

Commit

Permalink
feat(scaler.docker): handle scale down logic
Browse files Browse the repository at this point in the history
  • Loading branch information
scriptnull committed Mar 5, 2023
1 parent 5c39c96 commit 55e584d
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion internal/scaler/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ func (s *Scaler) Register(ctx context.Context) error {

currentCount := len(containers)
if currentCount < s.count {
fmt.Printf("scaler.%s current count: %d, desired count: %d \n", s.id, currentCount, s.count)
fmt.Printf("scaler.%s :: current count: %d, desired count: %d \n", s.id, currentCount, s.count)
remainingCount := s.count - currentCount
fmt.Printf("scaler.%s :: scaling up by creating %d container(s) \n", s.id, remainingCount)

for i := 0; i < remainingCount; i++ {
c, err := cli.ContainerCreate(ctx, &container.Config{Image: imageFullName}, nil, nil, nil, "")
Expand All @@ -77,6 +78,19 @@ func (s *Scaler) Register(ctx context.Context) error {
}
fmt.Printf("scaler.%s.container.started: %s \n", s.id, c.ID)
}
} else if currentCount > s.count {
fmt.Printf("scaler.%s :: current count: %d, desired count: %d \n", s.id, currentCount, s.count)
deletionCount := currentCount - s.count
fmt.Printf("scaler.%s :: scaling down by removing %d container(s) \n", s.id, deletionCount)

for i := 0; i < deletionCount; i++ {
err = cli.ContainerRemove(ctx, containers[i].ID, types.ContainerRemoveOptions{Force: true})
if err != nil {
fmt.Printf("scaler.%s.error: %s \n", s.id, err)
return
}
fmt.Printf("scaler.%s.container.removed: %s \n", s.id, containers[i].ID)
}
}

fmt.Printf("scaler.%s.done \n", s.id)
Expand Down

0 comments on commit 55e584d

Please sign in to comment.