forked from AliyunContainerService/pouch
/
portbindings.go
49 lines (40 loc) · 1.24 KB
/
portbindings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package opts
import (
"fmt"
"github.com/alibaba/pouch/apis/types"
"github.com/docker/go-connections/nat"
)
// ParsePortBinding parse string list to PortMap
// FIXME(ziren): add examples
func ParsePortBinding(ports []string) (types.PortMap, error) {
// parse port binding
_, tmpPortBindings, err := nat.ParsePortSpecs(ports)
if err != nil {
return nil, err
}
portBindings := make(types.PortMap)
for n, pbs := range tmpPortBindings {
portBindings[string(n)] = []types.PortBinding{}
for _, tmpPb := range pbs {
pb := types.PortBinding{HostIP: tmpPb.HostIP, HostPort: tmpPb.HostPort}
portBindings[string(n)] = append(portBindings[string(n)], pb)
}
}
return portBindings, nil
}
// ValidatePortBinding verify PortMap struct correctness.
func ValidatePortBinding(portBindings types.PortMap) error {
for port := range portBindings {
_, portStr := nat.SplitProtoPort(string(port))
if _, err := nat.ParsePort(portStr); err != nil {
return fmt.Errorf("invalid port specification: %q, err: %v", portStr, err)
}
for _, pb := range portBindings[port] {
_, err := nat.NewPort(nat.SplitProtoPort(pb.HostPort))
if err != nil {
return fmt.Errorf("invalud port specification: %q, err: %v", pb.HostPort, err)
}
}
}
return nil
}