This repository has been archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #652 from gnawux/portmapping
pod level port-mapping support
- Loading branch information
Showing
36 changed files
with
2,863 additions
and
476 deletions.
There are no files selected for viewing
This file contains 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 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 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,62 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/hyperhq/hyperd/types" | ||
) | ||
|
||
type PortMappingList struct { | ||
PortMappings []*types.PortMapping `json:"portMappings"` | ||
} | ||
|
||
func (c *Client) ListPortMappings(podId string) ([]*types.PortMapping, error) { | ||
path := fmt.Sprintf("/pod/%s/portmappings", podId) | ||
|
||
body, code, err := readBody(c.call("GET", path, nil, nil)) | ||
if code == http.StatusNotFound { | ||
return nil, fmt.Errorf("pod %s not found", podId) | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
|
||
var pms PortMappingList | ||
err = json.Unmarshal(body, &pms) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return pms.PortMappings, nil | ||
} | ||
|
||
func (c *Client) AddPortMappings(podId string, pms []*types.PortMapping) error { | ||
path := fmt.Sprintf("/pod/%s/portmappings/add", podId) | ||
r, code, err := readBody(c.call("PUT", path, pms, nil)) | ||
|
||
if code == http.StatusNoContent || code == http.StatusOK { | ||
return nil | ||
} else if code == http.StatusNotFound { | ||
return fmt.Errorf("pod %s not found", podId) | ||
} else if err != nil { | ||
return err | ||
} else { | ||
return fmt.Errorf("unexpect response code %d: %s", code, string(r)) | ||
} | ||
} | ||
|
||
func (c *Client) DeletePortMappings(podId string, pms []*types.PortMapping) error { | ||
path := fmt.Sprintf("/pod/%s/portmappings/delete", podId) | ||
r, code, err := readBody(c.call("PUT", path, pms, nil)) | ||
|
||
if code == http.StatusNoContent || code == http.StatusOK { | ||
return nil | ||
} else if code == http.StatusNotFound { | ||
return fmt.Errorf("pod %s not found", podId) | ||
} else if err != nil { | ||
return err | ||
} else { | ||
return fmt.Errorf("unexpect response code %d: %s", code, string(r)) | ||
} | ||
} |
This file contains 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 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 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 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,83 @@ | ||
package client | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
"text/tabwriter" | ||
|
||
"github.com/hyperhq/hyperd/types" | ||
gflag "github.com/jessevdk/go-flags" | ||
) | ||
|
||
func (cli *HyperClient) HyperCmdPorts(args ...string) error { | ||
var opts struct { | ||
Portmap []string `short:"p" long:"publish" value-name:"[]" default-mask:"-" description:"Publish a container's port to the host, format: -p|--publish [tcp/udp:]hostPort:containerPort (only valid for add and delete)"` | ||
} | ||
var parser = gflag.NewParser(&opts, gflag.Default|gflag.IgnoreUnknown|gflag.PassAfterNonOption) | ||
parser.Usage = "ports ls|add|delete [OPTIONS] POD\n\nList or modify port mapping rules of a Pod\n" | ||
|
||
if len(args) == 0 { | ||
parser.WriteHelp(cli.err) | ||
return nil | ||
} | ||
cmd := args[0] | ||
|
||
args, err := parser.ParseArgs(args[1:]) | ||
if err != nil { | ||
if !strings.Contains(err.Error(), "Usage") { | ||
return err | ||
} else { | ||
return nil | ||
} | ||
} | ||
|
||
var modFunc func(string, []*types.PortMapping) error | ||
|
||
switch cmd { | ||
case "ls": | ||
if len(args) != 1 { | ||
return errors.New("need a Pod Id as command parameter") | ||
} | ||
pms, err := cli.client.ListPortMappings(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0) | ||
fmt.Fprintln(w, "Protocol\tHost Ports\tContainer Ports") | ||
for _, pm := range pms { | ||
fmt.Fprintf(w, "%s\t%s\t%s\n", pm.Protocol, pm.HostPort, pm.ContainerPort) | ||
} | ||
w.Flush() | ||
return nil | ||
case "add": | ||
modFunc = cli.client.AddPortMappings | ||
case "delete": | ||
modFunc = cli.client.DeletePortMappings | ||
default: | ||
parser.WriteHelp(cli.err) | ||
return nil | ||
} | ||
|
||
if len(args) != 1 { | ||
return errors.New("need a Pod Id as command parameter") | ||
} | ||
if len(opts.Portmap) == 0 { | ||
return errors.New("no rules to be add or delete") | ||
} | ||
|
||
pms := make([]*types.PortMapping, 0, len(opts.Portmap)) | ||
for _, o := range opts.Portmap { | ||
pm, err := parsePortMapping(o) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse rule %s: %v", o, err) | ||
} | ||
pms = append(pms, pm) | ||
} | ||
|
||
err = modFunc(args[0], pms) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains 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 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
Oops, something went wrong.