Merge pull request #2413 from baude/remotepodstop

Enable more podman-remote pod commands
This commit is contained in:
OpenShift Merge Robot
2019-02-24 03:13:25 +01:00
committed by GitHub
44 changed files with 301 additions and 171 deletions

View File

@ -9,7 +9,7 @@ import (
"github.com/containers/buildah/imagebuildah"
buildahcli "github.com/containers/buildah/pkg/cli"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/pkg/adapter"
"github.com/docker/go-units"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"

View File

@ -94,13 +94,10 @@ func getContainerSubCommands() []*cobra.Command {
func getPodSubCommands() []*cobra.Command {
return []*cobra.Command{
_podCreateCommand,
_podKillCommand,
_podPauseCommand,
_podPsCommand,
_podRestartCommand,
_podStartCommand,
_podStatsCommand,
_podStopCommand,
_podTopCommand,
_podUnpauseCommand,
}

View File

@ -6,7 +6,7 @@ import (
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/cmd/podman/shared"
"github.com/containers/libpod/libpod"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/pkg/adapter"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

View File

@ -6,8 +6,8 @@ import (
"os"
"github.com/containers/libpod/libpod"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/libpod/image"
"github.com/containers/libpod/pkg/adapter"
"github.com/pkg/errors"
)

View File

@ -4,7 +4,7 @@ import (
"os"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/pkg/adapter"
"github.com/containers/libpod/pkg/rootless"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"

View File

@ -8,8 +8,8 @@ import (
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/cmd/podman/formats"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/libpod/image"
"github.com/containers/libpod/pkg/adapter"
"github.com/docker/go-units"
"github.com/pkg/errors"
"github.com/spf13/cobra"

View File

@ -5,7 +5,7 @@ import (
"strings"
"time"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/pkg/adapter"
"github.com/containers/libpod/pkg/inspect"
)

View File

@ -11,8 +11,8 @@ import (
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/cmd/podman/formats"
"github.com/containers/libpod/cmd/podman/imagefilters"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/libpod/image"
"github.com/containers/libpod/pkg/adapter"
"github.com/docker/go-units"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"

View File

@ -4,7 +4,7 @@ import (
"fmt"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/pkg/adapter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

View File

@ -4,7 +4,7 @@ import (
"fmt"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/pkg/adapter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

View File

@ -7,7 +7,7 @@ import (
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/cmd/podman/formats"
"github.com/containers/libpod/libpod"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/pkg/adapter"
"github.com/containers/libpod/version"
"github.com/pkg/errors"
"github.com/spf13/cobra"

View File

@ -8,7 +8,7 @@ import (
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/cmd/podman/formats"
"github.com/containers/libpod/cmd/podman/shared"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/pkg/adapter"
cc "github.com/containers/libpod/pkg/spec"
"github.com/containers/libpod/pkg/util"
"github.com/pkg/errors"

View File

@ -7,7 +7,7 @@ import (
"os"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/pkg/adapter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

View File

@ -22,7 +22,10 @@ var podCommand = cliconfig.PodmanCommand{
var podSubCommands = []*cobra.Command{
_podExistsCommand,
_podInspectCommand,
_podKillCommand,
_podRmCommand,
_podStartCommand,
_podStopCommand,
}
func init() {

View File

@ -5,7 +5,7 @@ import (
"fmt"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/pkg/adapter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

View File

@ -5,7 +5,7 @@ import (
"syscall"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/cmd/podman/libpodruntime"
"github.com/containers/libpod/pkg/adapter"
"github.com/docker/docker/pkg/signal"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
@ -45,7 +45,7 @@ func init() {
// podKillCmd kills one or more pods with a signal
func podKillCmd(c *cliconfig.PodKillValues) error {
runtime, err := libpodruntime.GetRuntime(&c.PodmanCommand)
runtime, err := adapter.GetRuntime(&c.PodmanCommand)
if err != nil {
return errors.Wrapf(err, "could not get runtime")
}
@ -63,30 +63,20 @@ func podKillCmd(c *cliconfig.PodKillValues) error {
killSignal = uint(sysSignal)
}
// getPodsFromContext returns an error when a requested pod
// isn't found. The only fatal error scenerio is when there are no pods
// in which case the following loop will be skipped.
pods, lastError := getPodsFromContext(&c.PodmanCommand, runtime)
podKillIds, podKillErrors := runtime.KillPods(getContext(), c, killSignal)
for _, p := range podKillIds {
fmt.Println(p)
}
if len(podKillErrors) == 0 {
return nil
}
// Grab the last error
lastError := podKillErrors[len(podKillErrors)-1]
// Remove the last error from the error slice
podKillErrors = podKillErrors[:len(podKillErrors)-1]
for _, pod := range pods {
ctr_errs, err := pod.Kill(killSignal)
if ctr_errs != nil {
for ctr, err := range ctr_errs {
if lastError != nil {
logrus.Errorf("%q", lastError)
}
lastError = errors.Wrapf(err, "unable to kill container %q in pod %q", ctr, pod.ID())
}
continue
}
if err != nil {
if lastError != nil {
logrus.Errorf("%q", lastError)
}
lastError = errors.Wrapf(err, "unable to kill pod %q", pod.ID())
continue
}
fmt.Println(pod.ID())
for _, err := range podKillErrors {
logrus.Errorf("%q", err)
}
return lastError
}

View File

@ -2,9 +2,9 @@ package main
import (
"fmt"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/pkg/adapter"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

View File

@ -4,7 +4,7 @@ import (
"fmt"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/cmd/podman/libpodruntime"
"github.com/containers/libpod/pkg/adapter"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -45,38 +45,26 @@ func init() {
}
func podStartCmd(c *cliconfig.PodStartValues) error {
runtime, err := libpodruntime.GetRuntime(&c.PodmanCommand)
runtime, err := adapter.GetRuntime(&c.PodmanCommand)
if err != nil {
return errors.Wrapf(err, "could not get runtime")
}
defer runtime.Shutdown(false)
// getPodsFromContext returns an error when a requested pod
// isn't found. The only fatal error scenerio is when there are no pods
// in which case the following loop will be skipped.
pods, lastError := getPodsFromContext(&c.PodmanCommand, runtime)
ctx := getContext()
for _, pod := range pods {
ctr_errs, err := pod.Start(ctx)
if ctr_errs != nil {
for ctr, err := range ctr_errs {
if lastError != nil {
logrus.Errorf("%q", lastError)
}
lastError = errors.Wrapf(err, "unable to start container %q on pod %q", ctr, pod.ID())
}
continue
}
if err != nil {
if lastError != nil {
logrus.Errorf("%q", lastError)
}
lastError = errors.Wrapf(err, "unable to start pod %q", pod.ID())
continue
}
fmt.Println(pod.ID())
podStartIDs, podStartErrors := runtime.StartPods(getContext(), c)
for _, p := range podStartIDs {
fmt.Println(p)
}
if len(podStartErrors) == 0 {
return nil
}
// Grab the last error
lastError := podStartErrors[len(podStartErrors)-1]
// Remove the last error from the error slice
podStartErrors = podStartErrors[:len(podStartErrors)-1]
for _, err := range podStartErrors {
logrus.Errorf("%q", err)
}
return lastError
}

View File

@ -4,7 +4,7 @@ import (
"fmt"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/cmd/podman/libpodruntime"
"github.com/containers/libpod/pkg/adapter"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -47,43 +47,26 @@ func init() {
}
func podStopCmd(c *cliconfig.PodStopValues) error {
timeout := -1
runtime, err := libpodruntime.GetRuntime(&c.PodmanCommand)
runtime, err := adapter.GetRuntime(&c.PodmanCommand)
if err != nil {
return errors.Wrapf(err, "could not get runtime")
}
defer runtime.Shutdown(false)
// getPodsFromContext returns an error when a requested pod
// isn't found. The only fatal error scenerio is when there are no pods
// in which case the following loop will be skipped.
pods, lastError := getPodsFromContext(&c.PodmanCommand, runtime)
ctx := getContext()
if c.Flag("timeout").Changed {
timeout = int(c.Timeout)
podStopIds, podStopErrors := runtime.StopPods(getContext(), c)
for _, p := range podStopIds {
fmt.Println(p)
}
for _, pod := range pods {
// set cleanup to true to clean mounts and namespaces
ctr_errs, err := pod.StopWithTimeout(ctx, true, timeout)
if ctr_errs != nil {
for ctr, err := range ctr_errs {
if lastError != nil {
logrus.Errorf("%q", lastError)
}
lastError = errors.Wrapf(err, "unable to stop container %q on pod %q", ctr, pod.ID())
}
continue
}
if err != nil {
if lastError != nil {
logrus.Errorf("%q", lastError)
}
lastError = errors.Wrapf(err, "unable to stop pod %q", pod.ID())
continue
}
fmt.Println(pod.ID())
if len(podStopErrors) == 0 {
return nil
}
// Grab the last error
lastError := podStopErrors[len(podStopErrors)-1]
// Remove the last error from the error slice
podStopErrors = podStopErrors[:len(podStopErrors)-1]
for _, err := range podStopErrors {
logrus.Errorf("%q", err)
}
return lastError
}

View File

@ -11,9 +11,9 @@ import (
"github.com/containers/image/transports/alltransports"
"github.com/containers/image/types"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/libpod/common"
image2 "github.com/containers/libpod/libpod/image"
"github.com/containers/libpod/pkg/adapter"
"github.com/containers/libpod/pkg/util"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"

View File

@ -10,8 +10,8 @@ import (
"github.com/containers/image/manifest"
"github.com/containers/image/types"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/libpod/image"
"github.com/containers/libpod/pkg/adapter"
"github.com/containers/libpod/pkg/util"
imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"

View File

@ -5,7 +5,7 @@ import (
"os"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/pkg/adapter"
"github.com/containers/storage"
"github.com/pkg/errors"
"github.com/spf13/cobra"

View File

@ -1,14 +1,15 @@
package main
import (
"os"
"strings"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/pkg/adapter"
"github.com/containers/libpod/pkg/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
"strings"
)
const (

View File

@ -8,7 +8,7 @@ import (
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/cmd/podman/shared"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/pkg/adapter"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

View File

@ -2,7 +2,7 @@ package main
import (
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/pkg/adapter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

View File

@ -4,7 +4,7 @@ import (
"fmt"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/pkg/adapter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

View File

@ -2,7 +2,7 @@ package main
import (
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/pkg/adapter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

View File

@ -6,7 +6,7 @@ import (
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/cmd/podman/formats"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/pkg/adapter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

View File

@ -8,7 +8,7 @@ import (
"strings"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/pkg/adapter"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

View File

@ -4,7 +4,7 @@ import (
"fmt"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod/adapter"
"github.com/containers/libpod/pkg/adapter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

View File

@ -1,54 +0,0 @@
// +build !remoteclient
package adapter
import (
"context"
"github.com/containers/libpod/libpod/adapter/shortcuts"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod"
)
// Pod ...
type Pod struct {
*libpod.Pod
}
// RemovePods ...
func (r *LocalRuntime) RemovePods(ctx context.Context, cli *cliconfig.PodRmValues) ([]string, []error) {
var (
errs []error
podids []string
)
pods, err := shortcuts.GetPodsByContext(cli.All, cli.Latest, cli.InputArgs, r.Runtime)
if err != nil {
errs = append(errs, err)
return nil, errs
}
for _, p := range pods {
if err := r.RemovePod(ctx, p, cli.Force, cli.Force); err != nil {
errs = append(errs, err)
} else {
podids = append(podids, p.ID())
}
}
return podids, errs
}
// GetLatestPod gets the latest pod and wraps it in an adapter pod
func (r *LocalRuntime) GetLatestPod() (*Pod, error) {
pod := Pod{}
p, err := r.Runtime.GetLatestPod()
pod.Pod = p
return &pod, err
}
// LookupPod gets a pod by name or id and wraps it in an adapter pod
func (r *LocalRuntime) LookupPod(nameOrID string) (*Pod, error) {
pod := Pod{}
p, err := r.Runtime.LookupPod(nameOrID)
pod.Pod = p
return &pod, err
}

152
pkg/adapter/pods.go Normal file
View File

@ -0,0 +1,152 @@
// +build !remoteclient
package adapter
import (
"context"
"github.com/containers/libpod/pkg/adapter/shortcuts"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod"
)
// Pod ...
type Pod struct {
*libpod.Pod
}
// RemovePods ...
func (r *LocalRuntime) RemovePods(ctx context.Context, cli *cliconfig.PodRmValues) ([]string, []error) {
var (
errs []error
podids []string
)
pods, err := shortcuts.GetPodsByContext(cli.All, cli.Latest, cli.InputArgs, r.Runtime)
if err != nil {
errs = append(errs, err)
return nil, errs
}
for _, p := range pods {
if err := r.RemovePod(ctx, p, cli.Force, cli.Force); err != nil {
errs = append(errs, err)
} else {
podids = append(podids, p.ID())
}
}
return podids, errs
}
// GetLatestPod gets the latest pod and wraps it in an adapter pod
func (r *LocalRuntime) GetLatestPod() (*Pod, error) {
pod := Pod{}
p, err := r.Runtime.GetLatestPod()
pod.Pod = p
return &pod, err
}
// LookupPod gets a pod by name or id and wraps it in an adapter pod
func (r *LocalRuntime) LookupPod(nameOrID string) (*Pod, error) {
pod := Pod{}
p, err := r.Runtime.LookupPod(nameOrID)
pod.Pod = p
return &pod, err
}
// StopPods is a wrapper to libpod to stop pods based on a cli context
func (r *LocalRuntime) StopPods(ctx context.Context, cli *cliconfig.PodStopValues) ([]string, []error) {
timeout := -1
if cli.Flags().Changed("timeout") {
timeout = int(cli.Timeout)
}
var (
errs []error
podids []string
)
pods, err := shortcuts.GetPodsByContext(cli.All, cli.Latest, cli.InputArgs, r.Runtime)
if err != nil {
errs = append(errs, err)
return nil, errs
}
for _, p := range pods {
stopped := true
conErrs, stopErr := p.StopWithTimeout(ctx, true, int(timeout))
if stopErr != nil {
errs = append(errs, stopErr)
stopped = false
}
if conErrs != nil {
stopped = false
for _, err := range conErrs {
errs = append(errs, err)
}
}
if stopped {
podids = append(podids, p.ID())
}
}
return podids, errs
}
// KillPods is a wrapper to libpod to start pods based on the cli context
func (r *LocalRuntime) KillPods(ctx context.Context, cli *cliconfig.PodKillValues, signal uint) ([]string, []error) {
var (
errs []error
podids []string
)
pods, err := shortcuts.GetPodsByContext(cli.All, cli.Latest, cli.InputArgs, r.Runtime)
if err != nil {
errs = append(errs, err)
return nil, errs
}
for _, p := range pods {
killed := true
conErrs, killErr := p.Kill(signal)
if killErr != nil {
errs = append(errs, killErr)
killed = false
}
if conErrs != nil {
killed = false
for _, err := range conErrs {
errs = append(errs, err)
}
}
if killed {
podids = append(podids, p.ID())
}
}
return podids, errs
}
// StartPods is a wrapper to start pods based on the cli context
func (r *LocalRuntime) StartPods(ctx context.Context, cli *cliconfig.PodStartValues) ([]string, []error) {
var (
errs []error
podids []string
)
pods, err := shortcuts.GetPodsByContext(cli.All, cli.Latest, cli.InputArgs, r.Runtime)
if err != nil {
errs = append(errs, err)
return nil, errs
}
for _, p := range pods {
started := true
conErrs, startErr := p.Start(ctx)
if startErr != nil {
errs = append(errs, startErr)
started = false
}
if conErrs != nil {
started = false
for _, err := range conErrs {
errs = append(errs, err)
}
}
if started {
podids = append(podids, p.ID())
}
}
return podids, errs
}

View File

@ -96,3 +96,75 @@ func (p *Pod) Inspect() (*libpod.PodInspect, error) {
}
return &inspectData, nil
}
// StopPods stops pods based on the cli context from the remote client.
func (r *LocalRuntime) StopPods(ctx context.Context, cli *cliconfig.PodStopValues) ([]string, []error) {
var (
stopErrs []error
stopPods []string
)
var timeout int64 = -1
if cli.Flags().Changed("timeout") {
timeout = int64(cli.Timeout)
}
podIDs, err := iopodman.GetPodsByContext().Call(r.Conn, cli.All, cli.Latest, cli.InputArgs)
if err != nil {
return nil, []error{err}
}
for _, p := range podIDs {
podID, err := iopodman.StopPod().Call(r.Conn, p, timeout)
if err != nil {
stopErrs = append(stopErrs, err)
} else {
stopPods = append(stopPods, podID)
}
}
return stopPods, stopErrs
}
// KillPods kills pods over varlink for the remoteclient
func (r *LocalRuntime) KillPods(ctx context.Context, cli *cliconfig.PodKillValues, signal uint) ([]string, []error) {
var (
killErrs []error
killPods []string
)
podIDs, err := iopodman.GetPodsByContext().Call(r.Conn, cli.All, cli.Latest, cli.InputArgs)
if err != nil {
return nil, []error{err}
}
for _, p := range podIDs {
podID, err := iopodman.KillPod().Call(r.Conn, p, int64(signal))
if err != nil {
killErrs = append(killErrs, err)
} else {
killPods = append(killPods, podID)
}
}
return killPods, killErrs
}
// StartPods starts pods for the remote client over varlink
func (r *LocalRuntime) StartPods(ctx context.Context, cli *cliconfig.PodStartValues) ([]string, []error) {
var (
startErrs []error
startPods []string
)
podIDs, err := iopodman.GetPodsByContext().Call(r.Conn, cli.All, cli.Latest, cli.InputArgs)
if err != nil {
return nil, []error{err}
}
for _, p := range podIDs {
podID, err := iopodman.StartPod().Call(r.Conn, p)
if err != nil {
startErrs = append(startErrs, err)
} else {
startPods = append(startPods, podID)
}
}
return startPods, startErrs
}

View File

@ -2,7 +2,7 @@ package varlinkapi
import (
"encoding/json"
"github.com/containers/libpod/libpod/adapter/shortcuts"
"github.com/containers/libpod/pkg/adapter/shortcuts"
"github.com/containers/libpod/pkg/rootless"
"syscall"

View File

@ -136,6 +136,5 @@ var _ = Describe("Podman pod start", func() {
session = podmanTest.Podman([]string{"pod", "start", podid, "doesnotexist"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(125))
Expect(podmanTest.NumberOfContainersRunning()).To(Equal(1))
})
})

View File

@ -137,6 +137,5 @@ var _ = Describe("Podman pod stop", func() {
session = podmanTest.Podman([]string{"pod", "stop", podid1, "doesnotexist"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(125))
Expect(podmanTest.NumberOfContainersRunning()).To(Equal(0))
})
})