本文整理汇总了Golang中github.com/docker/go-connections/nat.Port函数的典型用法代码示例。如果您正苦于以下问题:Golang Port函数的具体用法?Golang Port怎么用?Golang Port使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Port函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ports
func ports(c *config.ServiceConfig) (map[nat.Port]struct{}, nat.PortMap, error) {
ports, binding, err := nat.ParsePortSpecs(c.Ports)
if err != nil {
return nil, nil, err
}
exPorts, _, err := nat.ParsePortSpecs(c.Expose)
if err != nil {
return nil, nil, err
}
for k, v := range exPorts {
ports[k] = v
}
exposedPorts := map[nat.Port]struct{}{}
for k, v := range ports {
exposedPorts[nat.Port(k)] = v
}
portBindings := nat.PortMap{}
for k, bv := range binding {
dcbs := make([]nat.PortBinding, len(bv))
for k, v := range bv {
dcbs[k] = nat.PortBinding{HostIP: v.HostIP, HostPort: v.HostPort}
}
portBindings[nat.Port(k)] = dcbs
}
return exposedPorts, portBindings, nil
}
示例2: TestPortFilterForHostMode
func TestPortFilterForHostMode(t *testing.T) {
var (
p = PortFilter{}
nodes = []*node.Node{
{
ID: "node-1-id",
Name: "node-1-name",
Addr: "node-1",
},
{
ID: "node-2-id",
Name: "node-2-name",
Addr: "node-2",
},
{
ID: "node-3-id",
Name: "node-3-name",
Addr: "node-3",
},
}
result []*node.Node
err error
)
// Add a container taking away port 80 in the host mode to nodes[0].
container := &cluster.Container{
Container: types.Container{ID: "c1"},
Info: types.ContainerJSON{
Config: &containertypes.Config{
ExposedPorts: map[nat.Port]struct{}{nat.Port("80"): {}},
},
ContainerJSONBase: &types.ContainerJSONBase{
HostConfig: &containertypes.HostConfig{
NetworkMode: containertypes.NetworkMode("host"),
},
},
}}
assert.NoError(t, nodes[0].AddContainer(container))
// Request port 80 in the host mode
config := &cluster.ContainerConfig{Config: containertypes.Config{
ExposedPorts: map[nat.Port]struct{}{nat.Port("80"): {}},
}, HostConfig: containertypes.HostConfig{
NetworkMode: containertypes.NetworkMode("host"),
}, NetworkingConfig: networktypes.NetworkingConfig{}}
// nodes[0] should be excluded since port 80 is taken away
result, err = p.Filter(config, nodes, true)
assert.NoError(t, err)
assert.Equal(t, 2, len(result))
assert.NotContains(t, result, nodes[0])
}
示例3: makeBinding
func makeBinding(ip, port string) nat.PortMap {
binding := nat.PortBinding{
HostIP: ip,
HostPort: port,
}
bindingMap := map[nat.Port][]nat.PortBinding{nat.Port(fmt.Sprintf("%s/tcp", port)): {binding}}
return nat.PortMap(bindingMap)
}
示例4: tranformPorts
func tranformPorts(in map[docker.Port]struct{}) map[nat.Port]struct{} {
result := make(map[nat.Port]struct{})
for k, v := range in {
result[nat.Port(k)] = v
}
return result
}
示例5: Port
// Port returns the host port the specified port is mapped on.
func (c *Container) Port(ctx context.Context, port string) (string, error) {
if bindings, ok := c.container.NetworkSettings.Ports[nat.Port(port)]; ok {
result := []string{}
for _, binding := range bindings {
result = append(result, binding.HostIP+":"+binding.HostPort)
}
return strings.Join(result, "\n"), nil
}
return "", nil
}
示例6: Port
// Port returns the host port the specified port is mapped on.
func (c *Container) Port(port string) (string, error) {
info, err := c.findInfo()
if err != nil {
return "", err
}
if bindings, ok := info.NetworkSettings.Ports[nat.Port(port)]; ok {
result := []string{}
for _, binding := range bindings {
result = append(result, binding.HostIP+":"+binding.HostPort)
}
return strings.Join(result, "\n"), nil
}
return "", nil
}
示例7: makePortsAndBindings
func makePortsAndBindings(pm []*runtimeApi.PortMapping) (map[dockernat.Port]struct{}, map[dockernat.Port][]dockernat.PortBinding) {
exposedPorts := map[dockernat.Port]struct{}{}
portBindings := map[dockernat.Port][]dockernat.PortBinding{}
for _, port := range pm {
exteriorPort := port.GetHostPort()
if exteriorPort == 0 {
// No need to do port binding when HostPort is not specified
continue
}
interiorPort := port.GetContainerPort()
// Some of this port stuff is under-documented voodoo.
// See http://stackoverflow.com/questions/20428302/binding-a-port-to-a-host-interface-using-the-rest-api
var protocol string
switch strings.ToUpper(string(port.GetProtocol())) {
case "UDP":
protocol = "/udp"
case "TCP":
protocol = "/tcp"
default:
glog.Warningf("Unknown protocol %q: defaulting to TCP", port.Protocol)
protocol = "/tcp"
}
dockerPort := dockernat.Port(strconv.Itoa(int(interiorPort)) + protocol)
exposedPorts[dockerPort] = struct{}{}
hostBinding := dockernat.PortBinding{
HostPort: strconv.Itoa(int(exteriorPort)),
HostIP: port.GetHostIp(),
}
// Allow multiple host ports bind to same docker port
if existedBindings, ok := portBindings[dockerPort]; ok {
// If a docker port already map to a host port, just append the host ports
portBindings[dockerPort] = append(existedBindings, hostBinding)
} else {
// Otherwise, it's fresh new port binding
portBindings[dockerPort] = []dockernat.PortBinding{
hostBinding,
}
}
}
return exposedPorts, portBindings
}
示例8: TestPortFilterRandomAssignment
func TestPortFilterRandomAssignment(t *testing.T) {
var (
p = PortFilter{}
nodes = []*node.Node{
{
ID: "node-0-id",
Name: "node-0-name",
Addr: "node-0",
},
{
ID: "node-1-id",
Name: "node-1-name",
Addr: "node-1",
},
{
ID: "node-2-id",
Name: "node-2-name",
Addr: "node-2",
},
}
result []*node.Node
err error
)
// Simulate a container that requested to map 80 to a random port.
// In this case, HostConfig.PortBindings should contain a binding with no
// HostPort defined and NetworkSettings.Ports should contain the actual
// mapped port.
container := &cluster.Container{
Container: types.Container{ID: "c1"},
Info: types.ContainerJSON{
ContainerJSONBase: &types.ContainerJSONBase{
HostConfig: &containertypes.HostConfig{
PortBindings: nat.PortMap(
map[nat.Port][]nat.PortBinding{
nat.Port("80/tcp"): {
{
HostIP: "",
HostPort: "",
},
},
},
),
}}}}
container.Info.NetworkSettings = &types.NetworkSettings{
NetworkSettingsBase: types.NetworkSettingsBase{
Ports: nat.PortMap(
map[nat.Port][]nat.PortBinding{
nat.Port("80/tcp"): {
{
HostIP: "127.0.0.1",
HostPort: "1234",
},
},
},
),
}}
assert.NoError(t, nodes[0].AddContainer(container))
// Request port 80.
config := &cluster.ContainerConfig{Config: containertypes.Config{}, HostConfig: containertypes.HostConfig{
PortBindings: makeBinding("", "80"),
}, NetworkingConfig: networktypes.NetworkingConfig{}}
// Since port "80" has been mapped to "1234", we should be able to request "80".
result, err = p.Filter(config, nodes, true)
assert.NoError(t, err)
assert.Equal(t, result, nodes)
// However, we should not be able to request "1234" since it has been used for a random assignment.
config = &cluster.ContainerConfig{Config: containertypes.Config{}, HostConfig: containertypes.HostConfig{
PortBindings: makeBinding("", "1234"),
}, NetworkingConfig: networktypes.NetworkingConfig{}}
result, err = p.Filter(config, nodes, true)
assert.NoError(t, err)
assert.NotContains(t, result, nodes[0])
}
示例9: Addr
// Addr returns the host and port from the node in the format HOST:PORT.
func (l *LocalCluster) Addr(i int, port string) string {
return l.Nodes[i].Addr(nat.Port(port + "/tcp")).String()
}
示例10: portSpec
func portSpec(port uint32, protocol api.PortConfig_Protocol) nat.Port {
return nat.Port(fmt.Sprintf("%d/%s", port, strings.ToLower(protocol.String())))
}