mirror of
https://github.com/containers/podman.git
synced 2025-12-06 13:56:22 +08:00
podman-remote port
add the port command to the remote client. this allows users to displa port information about their host system from the remote client Signed-off-by: baude <bbaude@redhat.com>
This commit is contained in:
@@ -17,7 +17,6 @@ func getMainCommands() []*cobra.Command {
|
|||||||
_loginCommand,
|
_loginCommand,
|
||||||
_logoutCommand,
|
_logoutCommand,
|
||||||
_mountCommand,
|
_mountCommand,
|
||||||
_portCommand,
|
|
||||||
_refreshCommand,
|
_refreshCommand,
|
||||||
_searchCommand,
|
_searchCommand,
|
||||||
_statsCommand,
|
_statsCommand,
|
||||||
@@ -45,7 +44,6 @@ func getContainerSubCommands() []*cobra.Command {
|
|||||||
_commitCommand,
|
_commitCommand,
|
||||||
_execCommand,
|
_execCommand,
|
||||||
_mountCommand,
|
_mountCommand,
|
||||||
_portCommand,
|
|
||||||
_refreshCommand,
|
_refreshCommand,
|
||||||
_restoreCommand,
|
_restoreCommand,
|
||||||
_runlabelCommand,
|
_runlabelCommand,
|
||||||
|
|||||||
@@ -60,8 +60,9 @@ var (
|
|||||||
_listSubCommand,
|
_listSubCommand,
|
||||||
_logsCommand,
|
_logsCommand,
|
||||||
_pauseCommand,
|
_pauseCommand,
|
||||||
_restartCommand,
|
_portCommand,
|
||||||
_pruneContainersCommand,
|
_pruneContainersCommand,
|
||||||
|
_restartCommand,
|
||||||
_runCommand,
|
_runCommand,
|
||||||
_rmCommand,
|
_rmCommand,
|
||||||
_startCommand,
|
_startCommand,
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ var mainCommands = []*cobra.Command{
|
|||||||
_logsCommand,
|
_logsCommand,
|
||||||
_pauseCommand,
|
_pauseCommand,
|
||||||
podCommand.Command,
|
podCommand.Command,
|
||||||
|
_portCommand,
|
||||||
&_psCommand,
|
&_psCommand,
|
||||||
_pullCommand,
|
_pullCommand,
|
||||||
_pushCommand,
|
_pushCommand,
|
||||||
|
|||||||
@@ -6,8 +6,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/containers/libpod/cmd/podman/cliconfig"
|
"github.com/containers/libpod/cmd/podman/cliconfig"
|
||||||
"github.com/containers/libpod/cmd/podman/libpodruntime"
|
"github.com/containers/libpod/pkg/adapter"
|
||||||
"github.com/containers/libpod/libpod"
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@@ -51,10 +50,7 @@ func portCmd(c *cliconfig.PortValues) error {
|
|||||||
var (
|
var (
|
||||||
userProto, containerName string
|
userProto, containerName string
|
||||||
userPort int
|
userPort int
|
||||||
container *libpod.Container
|
|
||||||
containers []*libpod.Container
|
|
||||||
)
|
)
|
||||||
|
|
||||||
args := c.InputArgs
|
args := c.InputArgs
|
||||||
|
|
||||||
if c.Latest && c.All {
|
if c.Latest && c.All {
|
||||||
@@ -66,9 +62,6 @@ func portCmd(c *cliconfig.PortValues) error {
|
|||||||
if len(args) == 0 && !c.Latest && !c.All {
|
if len(args) == 0 && !c.Latest && !c.All {
|
||||||
return errors.Errorf("you must supply a running container name or id")
|
return errors.Errorf("you must supply a running container name or id")
|
||||||
}
|
}
|
||||||
if !c.Latest && !c.All {
|
|
||||||
containerName = args[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
port := ""
|
port := ""
|
||||||
if len(args) > 1 && !c.Latest {
|
if len(args) > 1 && !c.Latest {
|
||||||
@@ -98,36 +91,14 @@ func portCmd(c *cliconfig.PortValues) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
runtime, err := libpodruntime.GetRuntime(getContext(), &c.PodmanCommand)
|
runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "could not get runtime")
|
return errors.Wrapf(err, "could not get runtime")
|
||||||
}
|
}
|
||||||
defer runtime.Shutdown(false)
|
defer runtime.Shutdown(false)
|
||||||
|
|
||||||
if !c.Latest && !c.All {
|
containers, err := runtime.Port(c)
|
||||||
container, err = runtime.LookupContainer(containerName)
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrapf(err, "unable to find container %s", containerName)
|
|
||||||
}
|
|
||||||
containers = append(containers, container)
|
|
||||||
} else if c.Latest {
|
|
||||||
container, err = runtime.GetLatestContainer()
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrapf(err, "unable to get last created container")
|
|
||||||
}
|
|
||||||
containers = append(containers, container)
|
|
||||||
} else {
|
|
||||||
containers, err = runtime.GetRunningContainers()
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrapf(err, "unable to get all containers")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, con := range containers {
|
for _, con := range containers {
|
||||||
if state, _ := con.State(); state != libpod.ContainerStateRunning {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
portmappings, err := con.PortMappings()
|
portmappings, err := con.PortMappings()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -876,3 +876,30 @@ func cleanupContainer(ctx context.Context, ctr *libpod.Container, runtime *Local
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Port displays port information about existing containers
|
||||||
|
func (r *LocalRuntime) Port(c *cliconfig.PortValues) ([]*Container, error) {
|
||||||
|
var (
|
||||||
|
portContainers []*Container
|
||||||
|
containers []*libpod.Container
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
if !c.All {
|
||||||
|
containers, err = shortcuts.GetContainersByContext(false, c.Latest, c.InputArgs, r.Runtime)
|
||||||
|
} else {
|
||||||
|
containers, err = r.Runtime.GetRunningContainers()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Convert libpod containers to adapter Containers
|
||||||
|
for _, con := range containers {
|
||||||
|
if state, _ := con.State(); state != libpod.ContainerStateRunning {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
portContainers = append(portContainers, &Container{con})
|
||||||
|
}
|
||||||
|
return portContainers, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
"github.com/containers/libpod/libpod"
|
"github.com/containers/libpod/libpod"
|
||||||
"github.com/containers/libpod/pkg/inspect"
|
"github.com/containers/libpod/pkg/inspect"
|
||||||
"github.com/containers/libpod/pkg/varlinkapi/virtwriter"
|
"github.com/containers/libpod/pkg/varlinkapi/virtwriter"
|
||||||
|
"github.com/cri-o/ocicni/pkg/ocicni"
|
||||||
"github.com/docker/docker/pkg/term"
|
"github.com/docker/docker/pkg/term"
|
||||||
"github.com/opencontainers/runtime-spec/specs-go"
|
"github.com/opencontainers/runtime-spec/specs-go"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@@ -63,6 +64,19 @@ func (c *Container) Unpause() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Container) PortMappings() ([]ocicni.PortMapping, error) {
|
||||||
|
// First check if the container belongs to a network namespace (like a pod)
|
||||||
|
// Taken from libpod portmappings()
|
||||||
|
if len(c.config.NetNsCtr) > 0 {
|
||||||
|
netNsCtr, err := c.Runtime.LookupContainer(c.config.NetNsCtr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "unable to lookup network namespace for container %s", c.ID())
|
||||||
|
}
|
||||||
|
return netNsCtr.PortMappings()
|
||||||
|
}
|
||||||
|
return c.config.PortMappings, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Config returns a container config
|
// Config returns a container config
|
||||||
func (r *LocalRuntime) Config(name string) *libpod.ContainerConfig {
|
func (r *LocalRuntime) Config(name string) *libpod.ContainerConfig {
|
||||||
// TODO the Spec being returned is not populated. Matt and I could not figure out why. Will defer
|
// TODO the Spec being returned is not populated. Matt and I could not figure out why. Will defer
|
||||||
@@ -888,3 +902,23 @@ func (r *LocalRuntime) Prune(ctx context.Context, maxWorkers int, force bool) ([
|
|||||||
func (r *LocalRuntime) CleanupContainers(ctx context.Context, cli *cliconfig.CleanupValues) ([]string, map[string]error, error) {
|
func (r *LocalRuntime) CleanupContainers(ctx context.Context, cli *cliconfig.CleanupValues) ([]string, map[string]error, error) {
|
||||||
return nil, nil, errors.New("container cleanup not supported for remote clients")
|
return nil, nil, errors.New("container cleanup not supported for remote clients")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Port displays port information about existing containers
|
||||||
|
func (r *LocalRuntime) Port(c *cliconfig.PortValues) ([]*Container, error) {
|
||||||
|
var (
|
||||||
|
containers []*Container
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
// This one is a bit odd because when all is used, we only use running containers.
|
||||||
|
if !c.All {
|
||||||
|
containers, err = r.GetContainersByContext(false, c.Latest, c.InputArgs)
|
||||||
|
} else {
|
||||||
|
// we need to only use running containers if all
|
||||||
|
filters := []string{libpod.ContainerStateRunning.String()}
|
||||||
|
containers, err = r.LookupContainersWithStatus(filters)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return containers, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -889,3 +889,20 @@ func (r *LocalRuntime) GenerateKube(c *cliconfig.GenerateKubeValues) (*v1.Pod, *
|
|||||||
err = json.Unmarshal([]byte(reply.Service), &service)
|
err = json.Unmarshal([]byte(reply.Service), &service)
|
||||||
return &pod, &service, err
|
return &pod, &service, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetContainersByContext looks up containers based on the cli input of all, latest, or a list
|
||||||
|
func (r *LocalRuntime) GetContainersByContext(all bool, latest bool, namesOrIDs []string) ([]*Container, error) {
|
||||||
|
var containers []*Container
|
||||||
|
cids, err := iopodman.GetContainersByContext().Call(r.Conn, all, latest, namesOrIDs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, cid := range cids {
|
||||||
|
ctr, err := r.LookupContainer(cid)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
containers = append(containers, ctr)
|
||||||
|
}
|
||||||
|
return containers, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user