Skip to content

Commit b116b5c

Browse files
committed
Merge pull request #303 from icecrime/cherry-picks-1.7.0
Cherry picks 1.7.0
2 parents e578e95 + 78fef19 commit b116b5c

File tree

3 files changed

+17
-21
lines changed

3 files changed

+17
-21
lines changed

drivers/bridge/bridge.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/Sirupsen/logrus"
1111
"github.com/docker/libnetwork/driverapi"
1212
"github.com/docker/libnetwork/ipallocator"
13+
"github.com/docker/libnetwork/iptables"
1314
"github.com/docker/libnetwork/netlabel"
1415
"github.com/docker/libnetwork/netutils"
1516
"github.com/docker/libnetwork/options"
@@ -109,6 +110,9 @@ func Init(dc driverapi.DriverCallback) error {
109110
if out, err := exec.Command("modprobe", "-va", "bridge", "nf_nat", "br_netfilter").Output(); err != nil {
110111
logrus.Warnf("Running modprobe bridge nf_nat failed with message: %s, error: %v", out, err)
111112
}
113+
if err := iptables.RemoveExistingChain(DockerChain, iptables.Nat); err != nil {
114+
logrus.Warnf("Failed to remove existing iptables entries in %s : %v", DockerChain, err)
115+
}
112116

113117
return dc.RegisterDriver(networkType, newDriver())
114118
}

iptables/iptables.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,17 @@ func NewChain(name, bridge string, table Table, hairpinMode bool) (*Chain, error
9999
case Nat:
100100
preroute := []string{
101101
"-m", "addrtype",
102-
"--dst-type", "LOCAL"}
102+
"--dst-type", "LOCAL",
103+
"-j", c.Name}
103104
if !Exists(Nat, "PREROUTING", preroute...) {
104105
if err := c.Prerouting(Append, preroute...); err != nil {
105106
return nil, fmt.Errorf("Failed to inject docker in PREROUTING chain: %s", err)
106107
}
107108
}
108109
output := []string{
109110
"-m", "addrtype",
110-
"--dst-type", "LOCAL"}
111+
"--dst-type", "LOCAL",
112+
"-j", c.Name}
111113
if !hairpinMode {
112114
output = append(output, "!", "--dst", "127.0.0.0/8")
113115
}
@@ -228,7 +230,7 @@ func (c *Chain) Prerouting(action Action, args ...string) error {
228230
if len(args) > 0 {
229231
a = append(a, args...)
230232
}
231-
if output, err := Raw(append(a, "-j", c.Name)...); err != nil {
233+
if output, err := Raw(a...); err != nil {
232234
return err
233235
} else if len(output) != 0 {
234236
return ChainError{Chain: "PREROUTING", Output: output}
@@ -242,7 +244,7 @@ func (c *Chain) Output(action Action, args ...string) error {
242244
if len(args) > 0 {
243245
a = append(a, args...)
244246
}
245-
if output, err := Raw(append(a, "-j", c.Name)...); err != nil {
247+
if output, err := Raw(a...); err != nil {
246248
return err
247249
} else if len(output) != 0 {
248250
return ChainError{Chain: "OUTPUT", Output: output}
@@ -254,9 +256,9 @@ func (c *Chain) Output(action Action, args ...string) error {
254256
func (c *Chain) Remove() error {
255257
// Ignore errors - This could mean the chains were never set up
256258
if c.Table == Nat {
257-
c.Prerouting(Delete, "-m", "addrtype", "--dst-type", "LOCAL")
258-
c.Output(Delete, "-m", "addrtype", "--dst-type", "LOCAL", "!", "--dst", "127.0.0.0/8")
259-
c.Output(Delete, "-m", "addrtype", "--dst-type", "LOCAL") // Created in versions <= 0.1.6
259+
c.Prerouting(Delete, "-m", "addrtype", "--dst-type", "LOCAL", "-j", c.Name)
260+
c.Output(Delete, "-m", "addrtype", "--dst-type", "LOCAL", "!", "--dst", "127.0.0.0/8", "-j", c.Name)
261+
c.Output(Delete, "-m", "addrtype", "--dst-type", "LOCAL", "-j", c.Name) // Created in versions <= 0.1.6
260262

261263
c.Prerouting(Delete)
262264
c.Output(Delete)

iptables/iptables_test.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,11 @@ func TestPrerouting(t *testing.T) {
131131
t.Fatal(err)
132132
}
133133

134-
rule := []string{
135-
"-j", natChain.Name}
136-
137-
rule = append(rule, args...)
138-
139-
if !Exists(natChain.Table, "PREROUTING", rule...) {
134+
if !Exists(natChain.Table, "PREROUTING", args...) {
140135
t.Fatalf("rule does not exist")
141136
}
142137

143-
delRule := append([]string{"-D", "PREROUTING", "-t", string(Nat)}, rule...)
138+
delRule := append([]string{"-D", "PREROUTING", "-t", string(Nat)}, args...)
144139
if _, err = Raw(delRule...); err != nil {
145140
t.Fatal(err)
146141
}
@@ -156,17 +151,12 @@ func TestOutput(t *testing.T) {
156151
t.Fatal(err)
157152
}
158153

159-
rule := []string{
160-
"-j", natChain.Name}
161-
162-
rule = append(rule, args...)
163-
164-
if !Exists(natChain.Table, "OUTPUT", rule...) {
154+
if !Exists(natChain.Table, "OUTPUT", args...) {
165155
t.Fatalf("rule does not exist")
166156
}
167157

168158
delRule := append([]string{"-D", "OUTPUT", "-t",
169-
string(natChain.Table)}, rule...)
159+
string(natChain.Table)}, args...)
170160
if _, err = Raw(delRule...); err != nil {
171161
t.Fatal(err)
172162
}

0 commit comments

Comments
 (0)