return from further processign networkpolicy if base rule failed to gets created

This commit is contained in:
Murali Reddy committed 2019-05-06 16:27:38 +05:30
1 parent 4a1fbc7c38
commit ea2084d871
1 file changed
+28 -10
+28 -10
View File
@@ -1,10 +1,9 @@
package npc package npc
import ( import (
"fmt"
"sync" "sync"
goiptables "github.com/coreos/go-iptables/iptables"
"github.com/pkg/errors" "github.com/pkg/errors"
coreapi "k8s.io/api/core/v1" coreapi "k8s.io/api/core/v1"
extnapi "k8s.io/api/extensions/v1beta1" extnapi "k8s.io/api/extensions/v1beta1"
@@ -39,7 +38,7 @@ type controller struct {
nss map[string]*ns // ns name -> ns struct nss map[string]*ns // ns name -> ns struct
nsSelectors *selectorSet // selector string -> nsSelector nsSelectors *selectorSet // selector string -> nsSelector
defaultEgressDrop bool defaultEgressDrop bool // flag to track if base iptable rule to drop egress traffic is added or not
} }
func New(nodeName string, ipt iptables.Interface, ips ipset.Interface) NetworkPolicyController { func New(nodeName string, ipt iptables.Interface, ips ipset.Interface) NetworkPolicyController {
@@ -126,15 +125,17 @@ func (npc *controller) AddNetworkPolicy(obj interface{}) error {
// lazily add default rule to drop egress traffic only when network policies are applied // lazily add default rule to drop egress traffic only when network policies are applied
if !npc.defaultEgressDrop { if !npc.defaultEgressDrop {
npc.defaultEgressDrop = true egressNetworkPolicy, err := isEgressNetworkPolicy(obj)
ipt, err := goiptables.New()
if err != nil { if err != nil {
common.Log.Fatalf("Failed to create iptables handler %v", err.Error()) return err
} }
if err := ipt.Append(TableFilter, EgressChain, if egressNetworkPolicy {
"-m", "mark", "!", "--mark", EgressMark, "-j", "DROP"); err != nil { npc.defaultEgressDrop = true
common.Log.Errorf("Failed to add iptable rule to drop egress traffic from the pods by default due to %s", err.Error()) if err := npc.ipt.Append(TableFilter, EgressChain,
npc.defaultEgressDrop = false "-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())
}
} }
} }
@@ -218,3 +219,20 @@ func nsName(obj interface{}) (string, error) {
return "", errInvalidNetworkPolicyObjType return "", errInvalidNetworkPolicyObjType
} }
func isEgressNetworkPolicy(obj interface{}) (bool, error) {
if policy, ok := obj.(*networkingv1.NetworkPolicy); ok {
if len(policy.Spec.PolicyTypes) > 0 {
for _, policyType := range policy.Spec.PolicyTypes {
if policyType == networkingv1.PolicyTypeEgress {
return true, nil
}
}
}
if policy.Spec.Egress != nil {
return true, nil
}
return false, nil
}
return false, errInvalidNetworkPolicyObjType
}