Skip to content

Commit

Permalink
network: move ipmasq management into platform-specific files
Browse files Browse the repository at this point in the history
First part of changes to get flannel building/running on Windows
  • Loading branch information
rakelkar authored and jroggeman committed Oct 25, 2017
1 parent 6d0945f commit 2c96da7
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 22 deletions.
23 changes: 1 addition & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"strings"
"syscall"

"github.com/coreos/go-iptables/iptables"
"github.com/coreos/pkg/flagutil"
log "github.com/golang/glog"
"golang.org/x/net/context"
Expand Down Expand Up @@ -285,7 +284,7 @@ func main() {

// Set up ipMasq if needed
if opts.ipMasq {
go setupIPMasq(config, bn)
go network.SetupAndEnsureIPMasq(config.Network, bn.Lease())
}

if err := WriteSubnetFile(opts.subnetFile, config.Network, opts.ipMasq, bn); err != nil {
Expand Down Expand Up @@ -553,26 +552,6 @@ func mustRunHealthz() {
}
}

func setupIPMasq(config *subnet.Config, bn backend.Network) {
ipt, err := iptables.New()
if err != nil {
// if we can't find iptables, give up and return
log.Errorf("Failed to set up IP Masquerade. iptables was not found: %v", err)
return
}
defer func() {
network.TeardownIPMasq(ipt, config.Network, bn.Lease())
}()
for {
// Ensure that all the rules exist every 5 seconds
if err := network.EnsureIPMasq(ipt, config.Network, bn.Lease()); err != nil {
log.Errorf("Failed to ensure IP Masquerade: %v", err)
}
time.Sleep(5 * time.Second)
}

}

func ReadSubnetFromSubnetFile(path string) ip.IP4Net {
var prevSubnet ip.IP4Net
if _, err := os.Stat(path); !os.IsNotExist(err) {
Expand Down
26 changes: 26 additions & 0 deletions network/ipmasq.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build linux

// Copyright 2015 flannel authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -22,6 +24,8 @@ import (

"github.com/coreos/flannel/pkg/ip"
"github.com/coreos/flannel/subnet"
"github.com/coreos/go-iptables/iptables"
"time"
)

type IPTablesRules interface {
Expand Down Expand Up @@ -61,6 +65,28 @@ func ipMasqRulesExist(ipt IPTablesRules, ipn ip.IP4Net, lease *subnet.Lease) (bo
return true, nil
}

func SetupAndEnsureIPMasq(network ip.IP4Net, lease *subnet.Lease) {
ipt, err := iptables.New()
if err != nil {
// if we can't find iptables, give up and return
log.Errorf("Failed to setup IP Masquerade. IPTables was not found: %v", err)
return
}

defer func() {
TeardownIPMasq(ipt, network, lease)
}()

for {
// Ensure that all the rules exist every 5 seconds
if err := EnsureIPMasq(ipt, network, lease); err != nil {
log.Errorf("Failed to ensure IP Masquerade: %v", err)
}

time.Sleep(5 * time.Second)
}
}

func EnsureIPMasq(ipt IPTablesRules, ipn ip.IP4Net, lease *subnet.Lease) error {
exists, err := ipMasqRulesExist(ipt, ipn, lease)
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions network/ipmasq_unspecified.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// +build !linux,!windows

// Copyright 2015 flannel authors
//
// 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 network

import (
"errors"

"github.com/coreos/flannel/pkg/ip"
"github.com/coreos/flannel/subnet"
)

func SetupIPMasq(ipt IPTablesRules, ipn ip.IP4Net, lease *subnet.Lease) error {
return errors.New("SetupIPMasq not implemented for this platform")
}

func TeardownIPMasq(ipt IPTablesRules, ipn ip.IP4Net, lease *subnet.Lease) error {
return errors.New("TeardownIPMasq not implemented for this platform")
}
33 changes: 33 additions & 0 deletions network/ipmasq_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// +build windows

// Copyright 2015 flannel authors
//
// 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 network

import (
"github.com/coreos/flannel/pkg/ip"
"github.com/coreos/flannel/subnet"
)

func SetupIPMasq(ipt IPTablesRules, ipn ip.IP4Net, lease *subnet.Lease) error {
return nil
}

func TeardownIPMasq(ipt IPTablesRules, ipn ip.IP4Net, lease *subnet.Lease) error {
return nil
}

func SetupAndEnsureIPMasq(network ip.IP4Net, lease *subnet.Lease) {
}

0 comments on commit 2c96da7

Please sign in to comment.