-
Notifications
You must be signed in to change notification settings - Fork 944
feature: add pouch port cmd #2895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
allencloud
merged 1 commit into
AliyunContainerService:master
from
yeya24:feature/add-port-cmd
Jun 10, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/docker/go-connections/nat" | ||
| "github.com/pkg/errors" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // portDescription is used to describe port command in detail and auto generate command doc. | ||
| var portDescription = "Return port binding information on Pouch container" | ||
|
|
||
| // PortCommand is used to implement 'port' command. | ||
| type PortCommand struct { | ||
| baseCommand | ||
| container string | ||
| port string | ||
| } | ||
|
|
||
| // Init initializes PortCommand command. | ||
| func (p *PortCommand) Init(c *Cli) { | ||
| p.cli = c | ||
| p.cmd = &cobra.Command{ | ||
| Use: "port CONTAINER [PRIVATE_PORT[/PROTO]]", | ||
| Short: "List port mappings or a specific mapping for the container", | ||
| Long: portDescription, | ||
| Args: cobra.RangeArgs(1, 2), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| p.container = args[0] | ||
| if len(args) > 1 { | ||
| p.port = args[1] | ||
| } | ||
| return p.runPort() | ||
| }, | ||
| Example: portExample(), | ||
| } | ||
| } | ||
|
|
||
| // runPort is the entry of PortCommand command. | ||
| func (p *PortCommand) runPort() error { | ||
| ctx := context.Background() | ||
| apiClient := p.cli.Client() | ||
|
|
||
| c, err := apiClient.ContainerGet(ctx, p.container) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if p.port != "" { | ||
| port := p.port | ||
| proto := "tcp" | ||
| parts := strings.SplitN(port, "/", 2) | ||
|
|
||
| if len(parts) == 2 && len(parts[1]) != 0 { | ||
| port = parts[0] | ||
| proto = parts[1] | ||
| } | ||
| natPort := port + "/" + proto | ||
| newP, err := nat.NewPort(proto, port) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if portBindings, exists := c.NetworkSettings.Ports[string(newP)]; exists && portBindings != nil { | ||
| for _, pb := range portBindings { | ||
| fmt.Fprintf(os.Stdout, "%s:%s\n", pb.HostIP, pb.HostPort) | ||
| } | ||
| return nil | ||
| } | ||
| return errors.Errorf("No public port '%s' published for %s", natPort, p.container) | ||
| } | ||
|
|
||
| for from, portBindings := range c.NetworkSettings.Ports { | ||
| for _, pb := range portBindings { | ||
| fmt.Fprintf(os.Stdout, "%s -> %s:%s\n", from, pb.HostIP, pb.HostPort) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // portExample shows examples in port command, and is used in auto-generated cli docs. | ||
| func portExample() string { | ||
| return `$ pouch run -d -p 6379:6379 -p 6380:6380/udp redis:latest | ||
| 179eba2c29fb27a000bcda75cb2be271d1833ab140d1133799d0d4d865abc44e | ||
| $ pouch port 179 | ||
| 6379/tcp -> 0.0.0.0:6379 | ||
| 6380/udp -> 0.0.0.0:6380 | ||
| $ pouch port 179 6379 | ||
| 0.0.0.0:6379 | ||
| $ pouch port 179 6380/udp | ||
| 0.0.0.0:6380 | ||
| ` | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "github.com/alibaba/pouch/test/command" | ||
| "github.com/alibaba/pouch/test/environment" | ||
|
|
||
| "github.com/go-check/check" | ||
ZYecho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "github.com/gotestyourself/gotestyourself/icmd" | ||
| ) | ||
|
|
||
| // PouchContainerPortSuite is the test suite for container port CLI. | ||
| type PouchContainerPortSuite struct{} | ||
|
|
||
| func init() { | ||
| check.Suite(&PouchContainerPortSuite{}) | ||
| } | ||
|
|
||
| // SetUpSuite does common setup in the beginning of each test suite. | ||
| func (suite *PouchContainerPortSuite) SetUpSuite(c *check.C) { | ||
| SkipIfFalse(c, environment.IsLinux) | ||
| PullImage(c, busyboxImage) | ||
| } | ||
|
|
||
| // Test pouch port | ||
| func (suite *PouchContainerPortSuite) TestPouchPort(c *check.C) { | ||
| testcase1 := "8000:8000" | ||
| testcase2 := "10000:10000/udp" | ||
| testcase3 := "127.0.0.1:5000:5000" | ||
|
|
||
| name := "TestPouchPort" | ||
| command.PouchRun("run", | ||
| "--name", name, "-d", | ||
| "-p", testcase1, | ||
| "-p", testcase2, | ||
| "-p", testcase3, | ||
| busyboxImage, | ||
| "sh", "-c", "sleep 10000").Assert(c, icmd.Success) | ||
| defer DelContainerForceMultyTime(c, name) | ||
|
|
||
| ret := command.PouchRun("port", | ||
ZYecho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| name, "5000").Assert(c, icmd.Success) | ||
| c.Assert(ret.Stdout(), check.Equals, "127.0.0.1:5000\n") | ||
|
|
||
| ret = command.PouchRun("port", | ||
| name, "8000").Assert(c, icmd.Success) | ||
| c.Assert(ret.Stdout(), check.Equals, "0.0.0.0:8000\n") | ||
|
|
||
| ret = command.PouchRun("port", | ||
| name, "10000/udp").Assert(c, icmd.Success) | ||
| c.Assert(ret.Stdout(), check.Equals, "0.0.0.0:10000\n") | ||
|
|
||
| portBindingMap := map[string]string{ | ||
| "10000/udp": "0.0.0.0:10000", | ||
| "5000/tcp": "127.0.0.1:5000", | ||
| "8000/tcp": "0.0.0.0:8000", | ||
| } | ||
| // Test for only one arg. | ||
| ret = command.PouchRun("port", name).Assert(c, icmd.Success) | ||
| lines := strings.Split(ret.Stdout(), "\n") | ||
| for _, l := range lines { | ||
| outputs := strings.Split(l, "->") | ||
| // filter out the last line | ||
| if len(outputs) < 2 { | ||
| continue | ||
| } | ||
| port := strings.TrimSpace(outputs[0]) | ||
| actualPortBinding := strings.TrimSpace(outputs[1]) | ||
| if expected, exist := portBindingMap[port]; exist { | ||
| c.Assert(expected, check.Equals, actualPortBinding) | ||
| } else { | ||
| c.Errorf("Port %s not exists for container %s", port, name) | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.