-
Notifications
You must be signed in to change notification settings - Fork 0
/
port_mapping.go
190 lines (171 loc) · 4.82 KB
/
port_mapping.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) 2016 Pulcy.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package jobs
import (
"fmt"
"strconv"
"strings"
"github.com/juju/errgo"
)
type PortMapping string
// Parse parses a given port mapping.
func (p PortMapping) Parse() (ParsedPortMapping, error) {
result, err := parsePortMapping(string(p))
if err != nil {
return ParsedPortMapping{}, maskAny(err)
}
if err := result.Validate(); err != nil {
return ParsedPortMapping{}, maskAny(err)
}
return result, nil
}
func (p PortMapping) String() string {
return string(p)
}
type ParsedPortMapping struct {
HostIP string
HostPort int
ContainerPort int
Protocol string
}
func (p ParsedPortMapping) HasHostIP() bool {
return p.HostIP != ""
}
func (p ParsedPortMapping) HasHostPort() bool {
return p.HostPort != 0
}
func (p ParsedPortMapping) IsTCP() bool {
return p.Protocol == "" || p.Protocol == protocolTCP
}
func (p ParsedPortMapping) IsUDP() bool {
return p.Protocol == protocolUDP
}
func (p ParsedPortMapping) ProtocolString() string {
if p.IsTCP() {
return "TCP"
}
if p.IsUDP() {
return "UDP"
}
return p.Protocol
}
func (p ParsedPortMapping) String() string {
containerPort := strconv.Itoa(p.ContainerPort)
if p.Protocol == protocolUDP {
containerPort = containerPort + "/" + p.Protocol
}
hostPort := ""
if p.HostPort != 0 {
hostPort = strconv.Itoa(p.HostPort)
}
if p.HostIP == "" {
return hostPort + ":" + containerPort
}
return p.HostIP + ":" + hostPort + ":" + containerPort
}
// Validate checks the port mapping for errors
func (p ParsedPortMapping) Validate() error {
if p.HostPort < 0 || p.HostPort > 65535 {
return maskAny(errgo.WithCausef(nil, ValidationError, "HostPort must be a number between 0 and 65535, got '%s'", p.HostPort))
}
if p.ContainerPort < 1 || p.ContainerPort > 65535 {
return maskAny(errgo.WithCausef(nil, ValidationError, "ContainerPort must be a number between 1 and 65535, got '%s'", p.ContainerPort))
}
switch p.Protocol {
case "":
return maskAny(errgo.WithCausef(nil, ValidationError, "Protocol must not be empty."))
case protocolUDP, protocolTCP:
// OK
default:
return maskAny(errgo.WithCausef(nil, ValidationError, "Unknown protocol: '%s'", p.Protocol))
}
return nil
}
func parsePortMapping(input string) (ParsedPortMapping, error) {
parts := strings.Split(input, ":")
hostIP := ""
containerPort := 0
protocol := ""
hostPort := 0
var err error
switch len(parts) {
case 1:
containerPort, protocol, err = parseContainerPort(parts[0])
if err != nil {
return ParsedPortMapping{}, maskAny(err)
}
case 2:
if parts[0] != "" {
hostPort, err = strconv.Atoi(parts[0])
if err != nil {
return ParsedPortMapping{}, maskAny(err)
}
}
containerPort, protocol, err = parseContainerPort(parts[1])
if err != nil {
return ParsedPortMapping{}, maskAny(err)
}
case 3:
if parts[0] != "" {
hostIP = parts[0]
}
if parts[1] != "" {
hostPort, err = strconv.Atoi(parts[1])
if err != nil {
return ParsedPortMapping{}, maskAny(err)
}
}
containerPort, protocol, err = parseContainerPort(parts[2])
if err != nil {
return ParsedPortMapping{}, maskAny(err)
}
default:
return ParsedPortMapping{}, maskAny(fmt.Errorf("Unknown port format '%s'", input))
}
return ParsedPortMapping{
HostIP: hostIP,
HostPort: hostPort,
ContainerPort: containerPort,
Protocol: protocol,
}, nil
}
// parseContainerPort parses input like "port" or "port/protocol".
func parseContainerPort(input string) (int, string, error) {
s := strings.Split(input, "/")
var portStr, protocol string
switch len(s) {
case 1:
portStr = s[0]
protocol = protocolTCP
case 2:
portStr = s[0]
protocol = strings.ToLower(s[1])
default:
return 0, "", maskAny(fmt.Errorf("Invalid format, must be either <port> or <port>/<prot>, got '%s'", input))
}
port, err := strconv.Atoi(portStr)
if err != nil {
return 0, "", maskAny(fmt.Errorf("Port must be a number, got '%s'", portStr))
} else if port < 1 || port > 65535 {
return 0, "", maskAny(fmt.Errorf("Port must be a number between 1 and 65535, got '%s'", portStr))
}
switch protocol {
case "":
return 0, "", maskAny(fmt.Errorf("Protocol must not be empty."))
case protocolUDP, protocolTCP:
return port, protocol, nil
default:
return 0, "", maskAny(fmt.Errorf("Unknown protocol: '%s' in '%s'", protocol, input))
}
}