From c33cd2657c579d4bff8e3b48da97b7926a04b5f3 Mon Sep 17 00:00:00 2001 From: Mike Bryant Date: Tue, 11 Jul 2017 17:43:24 +0100 Subject: [PATCH] Refactor iptables usage in weave-npc to make it testable --- npc/controller.go | 6 +++--- npc/iptables/iptables.go | 7 +++++++ npc/namespace.go | 6 +++--- npc/rule.go | 6 +++--- 4 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 npc/iptables/iptables.go diff --git a/npc/controller.go b/npc/controller.go index c2a10510..a0ad6c78 100644 --- a/npc/controller.go +++ b/npc/controller.go @@ -3,13 +3,13 @@ package npc import ( "sync" - "github.com/coreos/go-iptables/iptables" "github.com/pkg/errors" coreapi "k8s.io/client-go/pkg/api/v1" extnapi "k8s.io/client-go/pkg/apis/extensions/v1beta1" "github.com/weaveworks/weave/common" "github.com/weaveworks/weave/npc/ipset" + "github.com/weaveworks/weave/npc/iptables" ) type NetworkPolicyController interface { @@ -31,14 +31,14 @@ type controller struct { nodeName string // my node name - ipt *iptables.IPTables + ipt iptables.Interface ips ipset.Interface nss map[string]*ns // ns name -> ns struct nsSelectors *selectorSet // selector string -> nsSelector } -func New(nodeName string, ipt *iptables.IPTables, ips ipset.Interface) NetworkPolicyController { +func New(nodeName string, ipt iptables.Interface, ips ipset.Interface) NetworkPolicyController { c := &controller{ nodeName: nodeName, ipt: ipt, diff --git a/npc/iptables/iptables.go b/npc/iptables/iptables.go new file mode 100644 index 00000000..771a5f6c --- /dev/null +++ b/npc/iptables/iptables.go @@ -0,0 +1,7 @@ +package iptables + +type Interface interface { + Append(table, chain string, rulespec ...string) error + Delete(table, chain string, rulespec ...string) error + Insert(table, chain string, pos int, rulespec ...string) error +} diff --git a/npc/namespace.go b/npc/namespace.go index e340edc9..3749ed36 100644 --- a/npc/namespace.go +++ b/npc/namespace.go @@ -3,7 +3,6 @@ package npc import ( "encoding/json" - "github.com/coreos/go-iptables/iptables" "k8s.io/client-go/pkg/api/unversioned" coreapi "k8s.io/client-go/pkg/api/v1" extnapi "k8s.io/client-go/pkg/apis/extensions/v1beta1" @@ -12,10 +11,11 @@ import ( "github.com/weaveworks/weave/common" "github.com/weaveworks/weave/npc/ipset" + "github.com/weaveworks/weave/npc/iptables" ) type ns struct { - ipt *iptables.IPTables // interface to iptables + ipt iptables.Interface // interface to iptables ips ipset.Interface // interface to ipset name string // k8s Namespace name @@ -32,7 +32,7 @@ type ns struct { rules *ruleSet } -func newNS(name, nodeName string, ipt *iptables.IPTables, ips ipset.Interface, nsSelectors *selectorSet) (*ns, error) { +func newNS(name, nodeName string, ipt iptables.Interface, ips ipset.Interface, nsSelectors *selectorSet) (*ns, error) { allPods, err := newSelectorSpec(&unversioned.LabelSelector{}, name, ipset.HashIP) if err != nil { return nil, err diff --git a/npc/rule.go b/npc/rule.go index 709e74d3..07854424 100644 --- a/npc/rule.go +++ b/npc/rule.go @@ -3,10 +3,10 @@ package npc import ( "strings" - "github.com/coreos/go-iptables/iptables" "k8s.io/client-go/pkg/types" "github.com/weaveworks/weave/common" + "github.com/weaveworks/weave/npc/iptables" ) type ruleSpec struct { @@ -35,11 +35,11 @@ func newRuleSpec(proto *string, srcHost *selectorSpec, dstHost *selectorSpec, ds } type ruleSet struct { - ipt *iptables.IPTables + ipt iptables.Interface users map[string]map[types.UID]struct{} } -func newRuleSet(ipt *iptables.IPTables) *ruleSet { +func newRuleSet(ipt iptables.Interface) *ruleSet { return &ruleSet{ipt, make(map[string]map[types.UID]struct{})} }