npc: Rename policy{Ingress,Egress} to policyType{Ingress,Egress}
This commit is contained in:
1 parent
dee24aec65
commit
49224ec9f7
6 files changed
+27
-27
No files matched your search
+15
-15
@@ -24,7 +24,7 @@ func (ns *ns) analysePolicyLegacy(policy *extnapi.NetworkPolicy) (
|
|||||||
podSelectors = make(map[string]*selectorSpec)
|
podSelectors = make(map[string]*selectorSpec)
|
||||||
rules = make(map[string]*ruleSpec)
|
rules = make(map[string]*ruleSpec)
|
||||||
|
|
||||||
policyTypes := []policyType{ingressPolicy}
|
policyTypes := []policyType{policyTypeIngress}
|
||||||
targetSelector, err := newSelectorSpec(&policy.Spec.PodSelector, policyTypes, ns.name, ipset.HashIP)
|
targetSelector, err := newSelectorSpec(&policy.Spec.PodSelector, policyTypes, ns.name, ipset.HashIP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
@@ -46,13 +46,13 @@ func (ns *ns) analysePolicyLegacy(policy *extnapi.NetworkPolicy) (
|
|||||||
// From is not provided, this rule matches all sources (traffic not restricted by source).
|
// From is not provided, this rule matches all sources (traffic not restricted by source).
|
||||||
if ingressRule.Ports == nil {
|
if ingressRule.Ports == nil {
|
||||||
// Ports is not provided, this rule matches all ports (traffic not restricted by port).
|
// Ports is not provided, this rule matches all ports (traffic not restricted by port).
|
||||||
rule := newRuleSpec(ingressPolicy, nil, nil, targetSelector, nil)
|
rule := newRuleSpec(policyTypeIngress, nil, nil, targetSelector, nil)
|
||||||
rules[rule.key] = rule
|
rules[rule.key] = rule
|
||||||
} else {
|
} else {
|
||||||
// Ports is present and contains at least one item, then this rule allows traffic
|
// Ports is present and contains at least one item, then this rule allows traffic
|
||||||
// only if the traffic matches at least one port in the ports list.
|
// only if the traffic matches at least one port in the ports list.
|
||||||
withNormalisedProtoAndPortLegacy(ingressRule.Ports, func(proto, port string) {
|
withNormalisedProtoAndPortLegacy(ingressRule.Ports, func(proto, port string) {
|
||||||
rule := newRuleSpec(ingressPolicy, &proto, nil, targetSelector, &port)
|
rule := newRuleSpec(policyTypeIngress, &proto, nil, targetSelector, &port)
|
||||||
rules[rule.key] = rule
|
rules[rule.key] = rule
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -78,13 +78,13 @@ func (ns *ns) analysePolicyLegacy(policy *extnapi.NetworkPolicy) (
|
|||||||
|
|
||||||
if ingressRule.Ports == nil {
|
if ingressRule.Ports == nil {
|
||||||
// Ports is not provided, this rule matches all ports (traffic not restricted by port).
|
// Ports is not provided, this rule matches all ports (traffic not restricted by port).
|
||||||
rule := newRuleSpec(ingressPolicy, nil, srcSelector, targetSelector, nil)
|
rule := newRuleSpec(policyTypeIngress, nil, srcSelector, targetSelector, nil)
|
||||||
rules[rule.key] = rule
|
rules[rule.key] = rule
|
||||||
} else {
|
} else {
|
||||||
// Ports is present and contains at least one item, then this rule allows traffic
|
// Ports is present and contains at least one item, then this rule allows traffic
|
||||||
// only if the traffic matches at least one port in the ports list.
|
// only if the traffic matches at least one port in the ports list.
|
||||||
withNormalisedProtoAndPortLegacy(ingressRule.Ports, func(proto, port string) {
|
withNormalisedProtoAndPortLegacy(ingressRule.Ports, func(proto, port string) {
|
||||||
rule := newRuleSpec(ingressPolicy, &proto, srcSelector, targetSelector, &port)
|
rule := newRuleSpec(policyTypeIngress, &proto, srcSelector, targetSelector, &port)
|
||||||
rules[rule.key] = rule
|
rules[rule.key] = rule
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -109,10 +109,10 @@ func (ns *ns) analysePolicy(policy *networkingv1.NetworkPolicy) (
|
|||||||
|
|
||||||
for _, pt := range policy.Spec.PolicyTypes {
|
for _, pt := range policy.Spec.PolicyTypes {
|
||||||
if pt == networkingv1.PolicyTypeIngress {
|
if pt == networkingv1.PolicyTypeIngress {
|
||||||
policyTypes = append(policyTypes, ingressPolicy)
|
policyTypes = append(policyTypes, policyTypeIngress)
|
||||||
}
|
}
|
||||||
if pt == networkingv1.PolicyTypeEgress {
|
if pt == networkingv1.PolicyTypeEgress {
|
||||||
policyTypes = append(policyTypes, egressPolicy)
|
policyTypes = append(policyTypes, policyTypeEgress)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If empty, matches all pods in a namespace
|
// If empty, matches all pods in a namespace
|
||||||
@@ -137,11 +137,11 @@ func (ns *ns) analysePolicy(policy *networkingv1.NetworkPolicy) (
|
|||||||
|
|
||||||
if allSources {
|
if allSources {
|
||||||
if allPorts {
|
if allPorts {
|
||||||
rule := newRuleSpec(ingressPolicy, nil, nil, targetSelector, nil)
|
rule := newRuleSpec(policyTypeIngress, nil, nil, targetSelector, nil)
|
||||||
rules[rule.key] = rule
|
rules[rule.key] = rule
|
||||||
} else {
|
} else {
|
||||||
withNormalisedProtoAndPort(ingressRule.Ports, func(proto, port string) {
|
withNormalisedProtoAndPort(ingressRule.Ports, func(proto, port string) {
|
||||||
rule := newRuleSpec(ingressPolicy, &proto, nil, targetSelector, &port)
|
rule := newRuleSpec(policyTypeIngress, &proto, nil, targetSelector, &port)
|
||||||
rules[rule.key] = rule
|
rules[rule.key] = rule
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -167,11 +167,11 @@ func (ns *ns) analysePolicy(policy *networkingv1.NetworkPolicy) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
if allPorts {
|
if allPorts {
|
||||||
rule := newRuleSpec(ingressPolicy, nil, srcSelector, targetSelector, nil)
|
rule := newRuleSpec(policyTypeIngress, nil, srcSelector, targetSelector, nil)
|
||||||
rules[rule.key] = rule
|
rules[rule.key] = rule
|
||||||
} else {
|
} else {
|
||||||
withNormalisedProtoAndPort(ingressRule.Ports, func(proto, port string) {
|
withNormalisedProtoAndPort(ingressRule.Ports, func(proto, port string) {
|
||||||
rule := newRuleSpec(ingressPolicy, &proto, srcSelector, targetSelector, &port)
|
rule := newRuleSpec(policyTypeIngress, &proto, srcSelector, targetSelector, &port)
|
||||||
rules[rule.key] = rule
|
rules[rule.key] = rule
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -190,11 +190,11 @@ func (ns *ns) analysePolicy(policy *networkingv1.NetworkPolicy) (
|
|||||||
|
|
||||||
if allDestinations {
|
if allDestinations {
|
||||||
if allPorts {
|
if allPorts {
|
||||||
rule := newRuleSpec(egressPolicy, nil, targetSelector, nil, nil)
|
rule := newRuleSpec(policyTypeEgress, nil, targetSelector, nil, nil)
|
||||||
rules[rule.key] = rule
|
rules[rule.key] = rule
|
||||||
} else {
|
} else {
|
||||||
withNormalisedProtoAndPort(egressRule.Ports, func(proto, port string) {
|
withNormalisedProtoAndPort(egressRule.Ports, func(proto, port string) {
|
||||||
rule := newRuleSpec(egressPolicy, &proto, targetSelector, nil, &port)
|
rule := newRuleSpec(policyTypeEgress, &proto, targetSelector, nil, &port)
|
||||||
rules[rule.key] = rule
|
rules[rule.key] = rule
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -227,11 +227,11 @@ func (ns *ns) analysePolicy(policy *networkingv1.NetworkPolicy) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
if allPorts {
|
if allPorts {
|
||||||
rule := newRuleSpec(egressPolicy, nil, targetSelector, dstRuleHost, nil)
|
rule := newRuleSpec(policyTypeEgress, nil, targetSelector, dstRuleHost, nil)
|
||||||
rules[rule.key] = rule
|
rules[rule.key] = rule
|
||||||
} else {
|
} else {
|
||||||
withNormalisedProtoAndPort(egressRule.Ports, func(proto, port string) {
|
withNormalisedProtoAndPort(egressRule.Ports, func(proto, port string) {
|
||||||
rule := newRuleSpec(egressPolicy, &proto, targetSelector, dstRuleHost, &port)
|
rule := newRuleSpec(policyTypeEgress, &proto, targetSelector, dstRuleHost, &port)
|
||||||
rules[rule.key] = rule
|
rules[rule.key] = rule
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -288,10 +288,10 @@ func (ns *ns) updatePod(oldObj, newObj *coreapi.Pod) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !ns.legacy {
|
if !ns.legacy {
|
||||||
if err := ns.addOrRemoveToDefaultAllowIPSet(ps, oldObj, newObj, oldMatch, newMatch, ingressPolicy); err != nil {
|
if err := ns.addOrRemoveToDefaultAllowIPSet(ps, oldObj, newObj, oldMatch, newMatch, policyTypeIngress); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := ns.addOrRemoveToDefaultAllowIPSet(ps, oldObj, newObj, oldMatch, newMatch, egressPolicy); err != nil {
|
if err := ns.addOrRemoveToDefaultAllowIPSet(ps, oldObj, newObj, oldMatch, newMatch, policyTypeEgress); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -645,7 +645,7 @@ func podComment(pod *coreapi.Pod) string {
|
|||||||
|
|
||||||
func (ns *ns) defaultAllowIPSetName(pt policyType) ipset.Name {
|
func (ns *ns) defaultAllowIPSetName(pt policyType) ipset.Name {
|
||||||
ipset := ns.ingressDefaultAllowIPSet
|
ipset := ns.ingressDefaultAllowIPSet
|
||||||
if pt == egressPolicy {
|
if pt == policyTypeEgress {
|
||||||
ipset = ns.egressDefaultAllowIPSet
|
ipset = ns.egressDefaultAllowIPSet
|
||||||
}
|
}
|
||||||
return ipset
|
return ipset
|
||||||
|
|||||||
+3
-3
@@ -55,21 +55,21 @@ func newRuleSpec(policyType policyType, proto *string, srcHost, dstHost ruleHost
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (spec *ruleSpec) iptChain() string {
|
func (spec *ruleSpec) iptChain() string {
|
||||||
if spec.policyType == egressPolicy {
|
if spec.policyType == policyTypeEgress {
|
||||||
return EgressCustomChain
|
return EgressCustomChain
|
||||||
}
|
}
|
||||||
return IngressChain
|
return IngressChain
|
||||||
}
|
}
|
||||||
|
|
||||||
func (spec *ruleSpec) iptRuleSpecs() [][]string {
|
func (spec *ruleSpec) iptRuleSpecs() [][]string {
|
||||||
if spec.policyType == ingressPolicy {
|
if spec.policyType == policyTypeIngress {
|
||||||
rule := make([]string, len(spec.args))
|
rule := make([]string, len(spec.args))
|
||||||
copy(rule, spec.args)
|
copy(rule, spec.args)
|
||||||
rule = append(rule, "-j", "ACCEPT")
|
rule = append(rule, "-j", "ACCEPT")
|
||||||
return [][]string{rule}
|
return [][]string{rule}
|
||||||
}
|
}
|
||||||
|
|
||||||
// egressPolicy
|
// policyTypeEgress
|
||||||
ruleMark := make([]string, len(spec.args))
|
ruleMark := make([]string, len(spec.args))
|
||||||
copy(ruleMark, spec.args)
|
copy(ruleMark, spec.args)
|
||||||
ruleMark = append(ruleMark, "-j", EgressMarkChain)
|
ruleMark = append(ruleMark, "-j", EgressMarkChain)
|
||||||
|
|||||||
+1
-1
@@ -9,7 +9,7 @@ func TestRegressionHandleNil3095(t *testing.T) {
|
|||||||
// Test for handling nil values
|
// Test for handling nil values
|
||||||
// https://github.com/weaveworks/weave/issues/3095
|
// https://github.com/weaveworks/weave/issues/3095
|
||||||
|
|
||||||
rs := newRuleSpec(ingressPolicy, nil, nil, nil, nil)
|
rs := newRuleSpec(policyTypeIngress, nil, nil, nil, nil)
|
||||||
|
|
||||||
require.NotEqual(t, rs.key, "")
|
require.NotEqual(t, rs.key, "")
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -114,10 +114,10 @@ func (ss *selectorSet) addToMatching(user types.UID, labelMap map[string]string,
|
|||||||
foundEgress := false
|
foundEgress := false
|
||||||
for _, s := range ss.entries {
|
for _, s := range ss.entries {
|
||||||
if s.matches(labelMap) {
|
if s.matches(labelMap) {
|
||||||
if ss.targetSelectorExist(s, ingressPolicy) {
|
if ss.targetSelectorExist(s, policyTypeIngress) {
|
||||||
foundIngress = true
|
foundIngress = true
|
||||||
}
|
}
|
||||||
if ss.targetSelectorExist(s, egressPolicy) {
|
if ss.targetSelectorExist(s, policyTypeEgress) {
|
||||||
foundEgress = true
|
foundEgress = true
|
||||||
}
|
}
|
||||||
if err := s.addEntry(user, entry, comment); err != nil {
|
if err := s.addEntry(user, entry, comment); err != nil {
|
||||||
|
|||||||
+3
-3
@@ -3,12 +3,12 @@ package npc
|
|||||||
type policyType byte
|
type policyType byte
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ingressPolicy policyType = iota
|
policyTypeIngress policyType = iota
|
||||||
egressPolicy
|
policyTypeEgress
|
||||||
)
|
)
|
||||||
|
|
||||||
func policyTypeStr(policyType policyType) string {
|
func policyTypeStr(policyType policyType) string {
|
||||||
if policyType == egressPolicy {
|
if policyType == policyTypeEgress {
|
||||||
return "egress"
|
return "egress"
|
||||||
}
|
}
|
||||||
return "ingress"
|
return "ingress"
|
||||||
|
|||||||
Reference in new issue
Block a user