diff options
Diffstat (limited to 'libpod/pod.go')
-rw-r--r-- | libpod/pod.go | 78 |
1 files changed, 69 insertions, 9 deletions
diff --git a/libpod/pod.go b/libpod/pod.go index a5b87f8b5..6e568f2b7 100644 --- a/libpod/pod.go +++ b/libpod/pod.go @@ -212,15 +212,15 @@ func (p *Pod) Start(ctx context.Context) (map[string]error, error) { // Traverse the graph beginning at nodes with no dependencies for _, node := range graph.noDepNodes { - startNode(ctx, node, false, ctrErrors, ctrsVisited) + startNode(ctx, node, false, ctrErrors, ctrsVisited, false) } return ctrErrors, nil } // Visit a node on a container graph and start the container, or set an error if -// a dependency failed to start -func startNode(ctx context.Context, node *containerNode, setError bool, ctrErrors map[string]error, ctrsVisited map[string]bool) { +// a dependency failed to start. if restart is true, startNode will restart the node instead of starting it. +func startNode(ctx context.Context, node *containerNode, setError bool, ctrErrors map[string]error, ctrsVisited map[string]bool, restart bool) { // First, check if we have already visited the node if ctrsVisited[node.id] { return @@ -235,7 +235,7 @@ func startNode(ctx context.Context, node *containerNode, setError bool, ctrError // Hit anyone who depends on us, and set errors on them too for _, successor := range node.dependedOn { - startNode(ctx, successor, true, ctrErrors, ctrsVisited) + startNode(ctx, successor, true, ctrErrors, ctrsVisited, restart) } return @@ -287,10 +287,18 @@ func startNode(ctx context.Context, node *containerNode, setError bool, ctrError } // Start the container (only if it is not running) - if !ctrErrored && node.container.state.State != ContainerStateRunning { - if err := node.container.initAndStart(ctx); err != nil { - ctrErrored = true - ctrErrors[node.id] = err + if !ctrErrored { + if !restart && node.container.state.State != ContainerStateRunning { + if err := node.container.initAndStart(ctx); err != nil { + ctrErrored = true + ctrErrors[node.id] = err + } + } + if restart && node.container.state.State != ContainerStatePaused && node.container.state.State != ContainerStateUnknown { + if err := node.container.restartWithTimeout(ctx, node.container.config.StopTimeout); err != nil { + ctrErrored = true + ctrErrors[node.id] = err + } } } @@ -298,7 +306,7 @@ func startNode(ctx context.Context, node *containerNode, setError bool, ctrError // Recurse to anyone who depends on us and start them for _, successor := range node.dependedOn { - startNode(ctx, successor, ctrErrored, ctrErrors, ctrsVisited) + startNode(ctx, successor, ctrErrored, ctrErrors, ctrsVisited, restart) } return @@ -375,6 +383,58 @@ func (p *Pod) Stop(cleanup bool) (map[string]error, error) { return nil, nil } +// Restart restarts all containers within a pod that are not paused or in an error state. +// It combines the effects of Stop() and Start() on a container +// Each container will use its own stop timeout. +// All containers are started independently, in order dictated by their +// dependencies. An error restarting one container +// will not prevent other containers being restarted. +// An error and a map[string]error are returned +// If the error is not nil and the map is nil, an error was encountered before +// any containers were restarted +// If map is not nil, an error was encountered when restarting one or more +// containers. The container ID is mapped to the error encountered. The error is +// set to ErrCtrExists +// If both error and the map are nil, all containers were restarted without error +func (p *Pod) Restart(ctx context.Context) (map[string]error, error) { + p.lock.Lock() + defer p.lock.Unlock() + + if !p.valid { + return nil, ErrPodRemoved + } + allCtrs, err := p.runtime.state.PodContainers(p) + if err != nil { + return nil, err + } + + // Build a dependency graph of containers in the pod + graph, err := buildContainerGraph(allCtrs) + if err != nil { + return nil, errors.Wrapf(err, "error generating dependency graph for pod %s", p.ID()) + } + + ctrErrors := make(map[string]error) + ctrsVisited := make(map[string]bool) + + // If there are no containers without dependencies, we can't start + // Error out + if len(graph.noDepNodes) == 0 { + return nil, errors.Wrapf(ErrNoSuchCtr, "no containers in pod %s have no dependencies, cannot start pod", p.ID()) + } + + // Traverse the graph beginning at nodes with no dependencies + for _, node := range graph.noDepNodes { + startNode(ctx, node, false, ctrErrors, ctrsVisited, true) + } + + if len(ctrErrors) > 0 { + return ctrErrors, errors.Wrapf(ErrCtrExists, "error stopping some containers") + } + + return nil, nil +} + // Kill sends a signal to all running containers within a pod // Signals will only be sent to running containers. Containers that are not // running will be ignored. All signals are sent independently, and sending will |