forked from juju/juju
/
ports.go
173 lines (156 loc) · 4.4 KB
/
ports.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
// Copyright 2012-2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package context
import (
"strings"
"github.com/juju/errors"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/network"
)
// PortRangeInfo contains information about a pending open- or
// close-port operation for a port range. This is only exported for
// testing.
type PortRangeInfo struct {
ShouldOpen bool
RelationTag names.RelationTag
}
// PortRange contains a port range and a relation id. Used as key to
// pendingRelations and is only exported for testing.
type PortRange struct {
Ports network.PortRange
RelationId int
}
func validatePortRange(protocol string, fromPort, toPort int) (network.PortRange, error) {
// Validate the given range.
newRange := network.PortRange{
Protocol: strings.ToLower(protocol),
FromPort: fromPort,
ToPort: toPort,
}
if err := newRange.Validate(); err != nil {
return network.PortRange{}, err
}
return newRange, nil
}
func tryOpenPorts(
protocol string,
fromPort, toPort int,
unitTag names.UnitTag,
machinePorts map[network.PortRange]params.RelationUnit,
pendingPorts map[PortRange]PortRangeInfo,
) error {
// TODO(dimitern) Once port ranges are linked to relations in
// addition to networks, refactor this functions and test it
// better to ensure it handles relations properly.
relationId := -1
//Validate the given range.
newRange, err := validatePortRange(protocol, fromPort, toPort)
if err != nil {
return err
}
rangeKey := PortRange{
Ports: newRange,
RelationId: relationId,
}
rangeInfo, isKnown := pendingPorts[rangeKey]
if isKnown {
if !rangeInfo.ShouldOpen {
// If the same range is already pending to be closed, just
// mark is pending to be opened.
rangeInfo.ShouldOpen = true
pendingPorts[rangeKey] = rangeInfo
}
return nil
}
// Ensure there are no conflicts with existing ports on the
// machine.
for portRange, relUnit := range machinePorts {
relUnitTag, err := names.ParseUnitTag(relUnit.Unit)
if err != nil {
return errors.Annotatef(
err,
"machine ports %v contain invalid unit tag",
portRange,
)
}
if newRange.ConflictsWith(portRange) {
if portRange == newRange && relUnitTag == unitTag {
// The same unit trying to open the same range is just
// ignored.
return nil
}
return errors.Errorf(
"cannot open %v (unit %q): conflicts with existing %v (unit %q)",
newRange, unitTag.Id(), portRange, relUnitTag.Id(),
)
}
}
// Ensure other pending port ranges do not conflict with this one.
for rangeKey, rangeInfo := range pendingPorts {
if newRange.ConflictsWith(rangeKey.Ports) && rangeInfo.ShouldOpen {
return errors.Errorf(
"cannot open %v (unit %q): conflicts with %v requested earlier",
newRange, unitTag.Id(), rangeKey.Ports,
)
}
}
rangeInfo = pendingPorts[rangeKey]
rangeInfo.ShouldOpen = true
pendingPorts[rangeKey] = rangeInfo
return nil
}
func tryClosePorts(
protocol string,
fromPort, toPort int,
unitTag names.UnitTag,
machinePorts map[network.PortRange]params.RelationUnit,
pendingPorts map[PortRange]PortRangeInfo,
) error {
// TODO(dimitern) Once port ranges are linked to relations in
// addition to networks, refactor this functions and test it
// better to ensure it handles relations properly.
relationId := -1
// Validate the given range.
newRange, err := validatePortRange(protocol, fromPort, toPort)
if err != nil {
return err
}
rangeKey := PortRange{
Ports: newRange,
RelationId: relationId,
}
rangeInfo, isKnown := pendingPorts[rangeKey]
if isKnown {
if rangeInfo.ShouldOpen {
// If the same range is already pending to be opened, just
// remove it from pending.
delete(pendingPorts, rangeKey)
}
return nil
}
// Ensure the range we're trying to close is opened on the
// machine.
relUnit, found := machinePorts[newRange]
if !found {
// Trying to close a range which is not open is ignored.
return nil
} else if relUnit.Unit != unitTag.String() {
relUnitTag, err := names.ParseUnitTag(relUnit.Unit)
if err != nil {
return errors.Annotatef(
err,
"machine ports %v contain invalid unit tag",
newRange,
)
}
return errors.Errorf(
"cannot close %v (opened by %q) from %q",
newRange, relUnitTag.Id(), unitTag.Id(),
)
}
rangeInfo = pendingPorts[rangeKey]
rangeInfo.ShouldOpen = false
pendingPorts[rangeKey] = rangeInfo
return nil
}