Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

lazily add iptables rule to drop egress traffic from the pods only if network policies are applied #3639

Merged
merged 2 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions npc/controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package npc

import (
"fmt"
"sync"

"github.com/pkg/errors"
Expand Down Expand Up @@ -35,8 +36,9 @@ type controller struct {
ipt iptables.Interface
ips ipset.Interface

nss map[string]*ns // ns name -> ns struct
nsSelectors *selectorSet // selector string -> nsSelector
nss map[string]*ns // ns name -> ns struct
nsSelectors *selectorSet // selector string -> nsSelector
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 {
Expand Down Expand Up @@ -121,11 +123,26 @@ func (npc *controller) AddNetworkPolicy(obj interface{}) error {
npc.Lock()
defer npc.Unlock()

// lazily add default rule to drop egress traffic only when network policies are applied
if !npc.defaultEgressDrop {
egressNetworkPolicy, err := isEgressNetworkPolicy(obj)
if err != nil {
return err
}
if egressNetworkPolicy {
npc.defaultEgressDrop = true
if err := npc.ipt.Append(TableFilter, 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())
}
}
}

nsName, err := nsName(obj)
if err != nil {
return err
}

common.Log.Infof("EVENT AddNetworkPolicy %s", js(obj))
return npc.withNS(nsName, func(ns *ns) error {
return errors.Wrap(ns.addNetworkPolicy(obj), "add network policy")
Expand Down Expand Up @@ -202,3 +219,20 @@ func nsName(obj interface{}) (string, error) {

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
}
1 change: 0 additions & 1 deletion prog/weave-npc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ func createBaseRules(ipt *iptables.IPTables, ips ipset.Interface) error {
{"-m", "state", "--state", "NEW", "-j", string(npc.EgressDefaultChain)},
{"-m", "state", "--state", "NEW", "-m", "mark", "!", "--mark", npc.EgressMark, "-j", string(npc.EgressCustomChain)},
{"-m", "state", "--state", "NEW", "-m", "mark", "!", "--mark", npc.EgressMark, "-j", "NFLOG", "--nflog-group", "86"},
{"-m", "mark", "!", "--mark", npc.EgressMark, "-j", "DROP"},
}...)
if err := net.AddChainWithRules(ipt, npc.TableFilter, npc.EgressChain, ruleSpecs); err != nil {
return err
Expand Down