Move name constants to separate 'chains' package, to reduce dependencies
We don't want everyone that uses the `net` package to get a transitive dependency on the Kubernetes APIs required by `npc`.
This commit is contained in:
1 parent
9928ddb401
commit
a3c76e141b
8 files changed
+51
-43
No files matched your search
+1
-10
@@ -2,16 +2,7 @@ package npc
|
||||
|
||||
const (
|
||||
TableFilter = "filter"
|
||||
|
||||
MainChain = "WEAVE-NPC"
|
||||
DefaultChain = "WEAVE-NPC-DEFAULT"
|
||||
IngressChain = "WEAVE-NPC-INGRESS"
|
||||
|
||||
EgressChain = "WEAVE-NPC-EGRESS"
|
||||
EgressDefaultChain = "WEAVE-NPC-EGRESS-DEFAULT"
|
||||
EgressCustomChain = "WEAVE-NPC-EGRESS-CUSTOM"
|
||||
EgressMarkChain = "WEAVE-NPC-EGRESS-ACCEPT"
|
||||
EgressMark = "0x40000/0x40000"
|
||||
EgressMark = "0x40000/0x40000"
|
||||
|
||||
IpsetNamePrefix = "weave-"
|
||||
|
||||
|
||||
+2
-1
@@ -12,6 +12,7 @@ import (
|
||||
"k8s.io/client-go/kubernetes"
|
||||
|
||||
"github.com/weaveworks/weave/common"
|
||||
"github.com/weaveworks/weave/common/chains"
|
||||
"github.com/weaveworks/weave/net/ipset"
|
||||
"github.com/weaveworks/weave/npc/iptables"
|
||||
)
|
||||
@@ -157,7 +158,7 @@ func (npc *controller) AddNetworkPolicy(obj interface{}) error {
|
||||
}
|
||||
if egressNetworkPolicy {
|
||||
npc.defaultEgressDrop = true
|
||||
if err := npc.ipt.Append(TableFilter, EgressChain,
|
||||
if err := npc.ipt.Append(TableFilter, chains.EgressChain,
|
||||
"-m", "mark", "!", "--mark", EgressMark, "-j", "DROP"); err != nil {
|
||||
npc.defaultEgressDrop = false
|
||||
return fmt.Errorf("Failed to add iptable rule to drop egress traffic from the pods by default due to %s", err.Error())
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/weaveworks/weave/common/chains"
|
||||
"github.com/weaveworks/weave/net/ipset"
|
||||
coreapi "k8s.io/api/core/v1"
|
||||
networkingv1 "k8s.io/api/networking/v1"
|
||||
@@ -582,8 +583,8 @@ func TestEgressPolicyWithIPBlock(t *testing.T) {
|
||||
require.True(t, m.entriesExist(exceptIPSetName, "192.168.48.2/32"))
|
||||
|
||||
// Each egress rule is represented as two iptables rules (-J MARK and -J RETURN).
|
||||
require.Equal(t, 2, len(ipt.rules[EgressCustomChain]))
|
||||
for rule := range ipt.rules[EgressCustomChain] {
|
||||
require.Equal(t, 2, len(ipt.rules[chains.EgressCustomChain]))
|
||||
for rule := range ipt.rules[chains.EgressCustomChain] {
|
||||
require.Contains(t, rule, "-d 192.168.48.0/24 -m set ! --match-set "+exceptIPSetName+" dst")
|
||||
}
|
||||
|
||||
@@ -690,8 +691,8 @@ func TestIngressPolicyWithIPBlockAndPortSpecified(t *testing.T) {
|
||||
require.Equal(t, 1, len(m.sets[runBarIPSetName].subSets))
|
||||
require.True(t, m.entriesExist(runBarIPSetName, barPodIP))
|
||||
|
||||
require.Equal(t, 1, len(ipt.rules[IngressChain]))
|
||||
for rule := range ipt.rules[IngressChain] {
|
||||
require.Equal(t, 1, len(ipt.rules[chains.IngressChain]))
|
||||
for rule := range ipt.rules[chains.IngressChain] {
|
||||
require.Contains(t, rule, "-s 192.168.48.4/32 -m set --match-set "+runBarIPSetName+" dst --dport 80")
|
||||
}
|
||||
}
|
||||
+4
-3
@@ -12,6 +12,7 @@ import (
|
||||
"k8s.io/apimachinery/pkg/util/uuid"
|
||||
|
||||
"github.com/weaveworks/weave/common"
|
||||
"github.com/weaveworks/weave/common/chains"
|
||||
"github.com/weaveworks/weave/net/ipset"
|
||||
"github.com/weaveworks/weave/npc/iptables"
|
||||
)
|
||||
@@ -455,12 +456,12 @@ func (ns *ns) updateDefaultAllowIPSetEntry(oldObj, newObj *coreapi.Pod, ipsetNam
|
||||
|
||||
func bypassRules(namespace string, ingress, egress ipset.Name) map[string][][]string {
|
||||
return map[string][][]string{
|
||||
DefaultChain: {
|
||||
chains.DefaultChain: {
|
||||
{"-m", "set", "--match-set", string(ingress), "dst", "-j", "ACCEPT",
|
||||
"-m", "comment", "--comment", "DefaultAllow ingress isolation for namespace: " + namespace},
|
||||
},
|
||||
EgressDefaultChain: {
|
||||
{"-m", "set", "--match-set", string(egress), "src", "-j", EgressMarkChain,
|
||||
chains.EgressDefaultChain: {
|
||||
{"-m", "set", "--match-set", string(egress), "src", "-j", chains.EgressMarkChain,
|
||||
"-m", "comment", "--comment", "DefaultAllow egress isolation for namespace: " + namespace},
|
||||
{"-m", "set", "--match-set", string(egress), "src", "-j", "RETURN",
|
||||
"-m", "comment", "--comment", "DefaultAllow egress isolation for namespace: " + namespace},
|
||||
|
||||
+4
-3
@@ -8,6 +8,7 @@ import (
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
|
||||
"github.com/weaveworks/weave/common"
|
||||
"github.com/weaveworks/weave/common/chains"
|
||||
"github.com/weaveworks/weave/npc/iptables"
|
||||
)
|
||||
|
||||
@@ -55,9 +56,9 @@ func newRuleSpec(policyType policyType, proto *string, srcHost, dstHost ruleHost
|
||||
|
||||
func (spec *ruleSpec) iptChain() string {
|
||||
if spec.policyType == policyTypeEgress {
|
||||
return EgressCustomChain
|
||||
return chains.EgressCustomChain
|
||||
}
|
||||
return IngressChain
|
||||
return chains.IngressChain
|
||||
}
|
||||
|
||||
func (spec *ruleSpec) iptRuleSpecs() [][]string {
|
||||
@@ -71,7 +72,7 @@ func (spec *ruleSpec) iptRuleSpecs() [][]string {
|
||||
// policyTypeEgress
|
||||
ruleMark := make([]string, len(spec.args))
|
||||
copy(ruleMark, spec.args)
|
||||
ruleMark = append(ruleMark, "-j", EgressMarkChain)
|
||||
ruleMark = append(ruleMark, "-j", chains.EgressMarkChain)
|
||||
ruleReturn := make([]string, len(spec.args))
|
||||
copy(ruleReturn, spec.args)
|
||||
ruleReturn = append(ruleReturn, "-j", "RETURN")
|
||||
|
||||
Reference in new issue
Block a user