Try scaling down services

This commit is contained in:
Frederik Ring 2024-01-25 19:56:49 +01:00
parent 4b79a05971
commit 27017e69d0
2 changed files with 58 additions and 15 deletions

View File

@ -326,6 +326,12 @@ func (s *script) stopContainersAndServices() (func() error, error) {
return noop, nil return noop, nil
} }
dockerInfo, err := s.cli.Info(context.Background())
if err != nil {
return noop, fmt.Errorf("stopContainers: error getting docker info: %w", err)
}
isDockerSwarm := dockerInfo.Swarm.LocalNodeState != "inactive"
matchLabel := fmt.Sprintf( matchLabel := fmt.Sprintf(
"docker-volume-backup.stop-during-backup=%s", "docker-volume-backup.stop-during-backup=%s",
s.c.BackupStopContainerLabel, s.c.BackupStopContainerLabel,
@ -345,11 +351,14 @@ func (s *script) stopContainersAndServices() (func() error, error) {
return noop, fmt.Errorf("stopContainers: error querying for containers to stop: %w", err) return noop, fmt.Errorf("stopContainers: error querying for containers to stop: %w", err)
} }
allServices, err := s.cli.ServiceList(context.Background(), types.ServiceListOptions{}) var allServices []swarm.Service
var servicesToScaleDown []swarm.Service
if isDockerSwarm {
allServices, err = s.cli.ServiceList(context.Background(), types.ServiceListOptions{})
if err != nil { if err != nil {
return noop, fmt.Errorf("stopContainers: error querying for services: %w", err) return noop, fmt.Errorf("stopContainers: error querying for services: %w", err)
} }
servicesToScaleDown, err := s.cli.ServiceList(context.Background(), types.ServiceListOptions{ servicesToScaleDown, err = s.cli.ServiceList(context.Background(), types.ServiceListOptions{
Filters: filters.NewArgs(filters.KeyValuePair{ Filters: filters.NewArgs(filters.KeyValuePair{
Key: "label", Key: "label",
Value: matchLabel, Value: matchLabel,
@ -358,6 +367,7 @@ func (s *script) stopContainersAndServices() (func() error, error) {
if err != nil { if err != nil {
return noop, fmt.Errorf("stopContainers: error querying for services to scale down: %w", err) return noop, fmt.Errorf("stopContainers: error querying for services to scale down: %w", err)
} }
}
if len(containersToStop) == 0 && len(servicesToScaleDown) == 0 { if len(containersToStop) == 0 && len(servicesToScaleDown) == 0 {
return noop, nil return noop, nil
@ -393,10 +403,33 @@ func (s *script) stopContainersAndServices() (func() error, error) {
) )
} }
var scaledDownServices []swarm.Service
var scaleDownErrors []error
if isDockerSwarm {
for _, service := range servicesToScaleDown {
var zero uint64
service.Spec.Mode.Replicated.Replicas = &zero
service.Spec.TaskTemplate.ForceUpdate += 1
if _, err := s.cli.ServiceUpdate(context.Background(), service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{}); err != nil {
scaleDownErrors = append(scaleDownErrors, err)
} else {
scaledDownServices = append(scaledDownServices, service)
}
}
}
s.stats.Containers = ContainersStats{ s.stats.Containers = ContainersStats{
All: uint(len(allContainers)), All: uint(len(allContainers)),
ToStop: uint(len(containersToStop)), ToStop: uint(len(containersToStop)),
Stopped: uint(len(stoppedContainers)), Stopped: uint(len(stoppedContainers)),
StopErrors: uint(len(stopErrors)),
}
s.stats.Services = ServicesStats{
All: uint(len(allServices)),
ToScaleDown: uint(len(servicesToScaleDown)),
ScaledDown: uint(len(scaledDownServices)),
ScaleDownErrors: uint(len(scaleDownErrors)),
} }
return func() error { return func() error {

View File

@ -17,6 +17,15 @@ type ContainersStats struct {
StopErrors uint StopErrors uint
} }
// ServicesStats contains info about Swarm services that have been
// operated upon
type ServicesStats struct {
All uint
ToScaleDown uint
ScaledDown uint
ScaleDownErrors uint
}
// BackupFileStats stats about the created backup file // BackupFileStats stats about the created backup file
type BackupFileStats struct { type BackupFileStats struct {
Name string Name string
@ -40,6 +49,7 @@ type Stats struct {
LockedTime time.Duration LockedTime time.Duration
LogOutput *bytes.Buffer LogOutput *bytes.Buffer
Containers ContainersStats Containers ContainersStats
Services ServicesStats
BackupFile BackupFileStats BackupFile BackupFileStats
Storages map[string]StorageStats Storages map[string]StorageStats
} }