npc: Add ruleHost interface

The idea for the interface is taken from the kitt1987/ipblock work.

The interface is going to be implemented by selectorSpec and
ipBlockSpec.
This commit is contained in:
Martynas Pumputis authored and Martynas Pumputis committed 2018-07-24 10:06:23 +02:00
1 parent affdc6f5af
commit 2e85018c26
2 files changed
+34 -9

No files matched your search

+15 -9
View File
@@ -10,30 +10,36 @@ import (
"github.com/weaveworks/weave/npc/iptables" "github.com/weaveworks/weave/npc/iptables"
) )
type ruleHost interface {
// getRuleSpec returns source or destination specification and comment which
// are used in an iptables rule.
//
// If src=true then the rulespec is for source, otherwise - for destination.
getRuleSpec(src bool) ([]string, string)
}
type ruleSpec struct { type ruleSpec struct {
key string key string
args []string args []string
policyType policyType policyType policyType
} }
func newRuleSpec(policyType policyType, proto *string, srcHost *selectorSpec, dstHost *selectorSpec, dstPort *string) *ruleSpec { func newRuleSpec(policyType policyType, proto *string, srcHost, dstHost ruleHost, dstPort *string) *ruleSpec {
args := []string{} args := []string{}
if proto != nil { if proto != nil {
args = append(args, "-p", *proto) args = append(args, "-p", *proto)
} }
srcComment := "anywhere" srcComment := "anywhere"
if srcHost != nil { if srcHost != nil {
args = append(args, "-m", "set", "--match-set", string(srcHost.ipsetName), "src") rule, comment := srcHost.getRuleSpec(true)
if srcHost.nsName != "" { args = append(args, rule...)
srcComment = fmt.Sprintf("pods: namespace: %s, selector: %s", srcHost.nsName, srcHost.key) srcComment = comment
} else {
srcComment = fmt.Sprintf("namespaces: selector: %s", srcHost.key)
}
} }
dstComment := "anywhere" dstComment := "anywhere"
if dstHost != nil { if dstHost != nil {
args = append(args, "-m", "set", "--match-set", string(dstHost.ipsetName), "dst") rule, comment := dstHost.getRuleSpec(false)
dstComment = fmt.Sprintf("pods: namespace: %s, selector: %s", dstHost.nsName, dstHost.key) args = append(args, rule...)
dstComment = comment
} }
if dstPort != nil { if dstPort != nil {
args = append(args, "--dport", *dstPort) args = append(args, "--dport", *dstPort)
+19
View File
@@ -1,6 +1,8 @@
package npc package npc
import ( import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
@@ -37,6 +39,23 @@ func newSelectorSpec(json *metav1.LabelSelector, policyType []policyType, nsName
nsName: nsName}, nil nsName: nsName}, nil
} }
func (spec *selectorSpec) getRuleSpec(src bool) ([]string, string) {
dir := "dst"
if src {
dir = "src"
}
rule := []string{"-m", "set", "--match-set", string(spec.ipsetName), dir}
comment := "anywhere"
if spec.nsName != "" {
comment = fmt.Sprintf("pods: namespace: %s, selector: %s", spec.nsName, spec.key)
} else {
comment = fmt.Sprintf("namespaces: selector: %s", spec.key)
}
return rule, comment
}
type selector struct { type selector struct {
ips ipset.Interface ips ipset.Interface
spec *selectorSpec spec *selectorSpec