From 86863f4b6c4d0ac7d015f215662d450689b54901 Mon Sep 17 00:00:00 2001 From: Frederik Ring Date: Sat, 27 Jan 2024 19:48:50 +0100 Subject: [PATCH] Inline handling of in-swarm container level restart --- cmd/backup/docker.go | 44 +++++++++++++++----------------------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/cmd/backup/docker.go b/cmd/backup/docker.go index 9caaad45..029a3aa6 100644 --- a/cmd/backup/docker.go +++ b/cmd/backup/docker.go @@ -229,46 +229,32 @@ func (s *script) stopContainersAndServices() (func() error, error) { } return func() error { - servicesRequiringForceUpdate := map[string]struct{}{} - var restartErrors []error for _, container := range stoppedContainers { - // in case a container was part of a swarm service, teh service requires to - // be force updated instead of restarting the container as it would otherwise - // remain in a "completed" state - if swarmServiceName, ok := container.Labels["com.docker.swarm.service.name"]; ok { - servicesRequiringForceUpdate[swarmServiceName] = struct{}{} - continue - } - if err := s.cli.ContainerStart(context.Background(), container.ID, types.ContainerStartOptions{}); err != nil { - restartErrors = append(restartErrors, err) - } - } - - if len(servicesRequiringForceUpdate) != 0 { - services, _ := s.cli.ServiceList(context.Background(), types.ServiceListOptions{}) - for serviceName := range servicesRequiringForceUpdate { - var serviceMatch swarm.Service - for _, service := range services { - if service.Spec.Name == serviceName { - serviceMatch = service - break - } - } - if serviceMatch.ID == "" { + if swarmServiceID, ok := container.Labels["com.docker.swarm.service.id"]; ok && isDockerSwarm { + // in case a container was part of a swarm service, the service requires to + // be force updated instead of restarting the container as it would otherwise + // remain in a "completed" state + service, _, err := s.cli.ServiceInspectWithRaw(context.Background(), swarmServiceID, types.ServiceInspectOptions{}) + if err != nil { restartErrors = append( restartErrors, - fmt.Errorf("(*script).stopContainersAndServices: couldn't find service with name %s", serviceName), + fmt.Errorf("(*script).stopContainersAndServices: error looking up parent service: %w", err), ) continue } - serviceMatch.Spec.TaskTemplate.ForceUpdate += 1 + service.Spec.TaskTemplate.ForceUpdate += 1 if _, err := s.cli.ServiceUpdate( - context.Background(), serviceMatch.ID, - serviceMatch.Version, serviceMatch.Spec, types.ServiceUpdateOptions{}, + context.Background(), service.ID, + service.Version, service.Spec, types.ServiceUpdateOptions{}, ); err != nil { restartErrors = append(restartErrors, err) } + continue + } + + if err := s.cli.ContainerStart(context.Background(), container.ID, types.ContainerStartOptions{}); err != nil { + restartErrors = append(restartErrors, err) } }