Skip to content

Commit 342ea5a

Browse files
authored
Prevent masquerading pod -> NodeIP traffic (#174)
* Move getNodeIP logic to utils package Remove redundant ipset lookups utils.NewIPSet() does this for us. * Don't masquerade pod -> nodeAddrsIPSet traffic Previously with Pod egress enabled, this would get masqueraded. This change also adds cleanup for said ipset. * Enhanced cleanup of Pod egress, overlay networking - Delete old/bad pod egress iptables rule(s) from old versions - When pod egress or overlay are disabled, cleanup as needed * Update IPSet.Sets to map type * ipset enhancements - Avoid providing method that would delete all ipset sets on a system - New method DestroyAllWithin() destroys sets tracked by an IPSet - Create() now handles cases where Sets/System state are not in sync - Refresh() now handles leftover -temp set gracefully - Swap() now uses ipset swap - Delete() improved sync of Sets and system state - Get() now validates if map element exists before trying - etc * Update routes controller to reflect ipset changes
1 parent 3debbfa commit 342ea5a

File tree

5 files changed

+384
-196
lines changed

5 files changed

+384
-196
lines changed

app/controllers/network_policy_controller.go

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"errors"
88
"fmt"
99
"net"
10-
"os/exec"
1110
"strconv"
1211
"strings"
1312
"sync"
@@ -20,7 +19,6 @@ import (
2019
"github.com/golang/glog"
2120
"k8s.io/client-go/kubernetes"
2221
api "k8s.io/client-go/pkg/api/v1"
23-
apiv1 "k8s.io/client-go/pkg/api/v1"
2422
apiextensions "k8s.io/client-go/pkg/apis/extensions/v1beta1"
2523
networking "k8s.io/client-go/pkg/apis/networking/v1"
2624
)
@@ -45,7 +43,7 @@ type NetworkPolicyController struct {
4543

4644
// list of all active network policies expressed as networkPolicyInfo
4745
networkPoliciesInfo *[]networkPolicyInfo
48-
ipset *utils.IPSet
46+
ipSetHandler *utils.IPSet
4947
}
5048

5149
// internal structure to represent a network policy
@@ -168,11 +166,6 @@ func (npc *NetworkPolicyController) Sync() error {
168166
npc.mu.Lock()
169167
defer npc.mu.Unlock()
170168

171-
_, err = exec.LookPath("ipset")
172-
if err != nil {
173-
return errors.New("Ensure ipset package is installed: " + err.Error())
174-
}
175-
176169
start := time.Now()
177170
defer func() {
178171
glog.Infof("sync iptables took %v", time.Since(start))
@@ -239,7 +232,7 @@ func (npc *NetworkPolicyController) syncNetworkPolicyChains() (map[string]bool,
239232

240233
// create a ipset for all destination pod ip's matched by the policy spec PodSelector
241234
destPodIpSetName := policyDestinationPodIpSetName(policy.namespace, policy.name)
242-
destPodIpSet, err := npc.ipset.Create(destPodIpSetName, utils.TypeHashIP, utils.OptionTimeout, "0")
235+
destPodIpSet, err := npc.ipSetHandler.Create(destPodIpSetName, utils.TypeHashIP, utils.OptionTimeout, "0")
243236
if err != nil {
244237
return nil, nil, fmt.Errorf("failed to create ipset: %s", err.Error())
245238
}
@@ -274,7 +267,7 @@ func (npc *NetworkPolicyController) syncNetworkPolicyChains() (map[string]bool,
274267

275268
if len(ingressRule.srcPods) != 0 {
276269
srcPodIpSetName := policySourcePodIpSetName(policy.namespace, policy.name, i)
277-
srcPodIpSet, err := npc.ipset.Create(srcPodIpSetName, utils.TypeHashIP, utils.OptionTimeout, "0")
270+
srcPodIpSet, err := npc.ipSetHandler.Create(srcPodIpSetName, utils.TypeHashIP, utils.OptionTimeout, "0")
278271
if err != nil {
279272
return nil, nil, fmt.Errorf("failed to create ipset: %s", err.Error())
280273
}
@@ -869,21 +862,6 @@ func policySourcePodIpSetName(namespace, policyName string, ingressRuleNo int) s
869862
return "KUBE-SRC-" + encoded[:16]
870863
}
871864

872-
func getNodeIP(node *apiv1.Node) (net.IP, error) {
873-
addresses := node.Status.Addresses
874-
addressMap := make(map[apiv1.NodeAddressType][]apiv1.NodeAddress)
875-
for i := range addresses {
876-
addressMap[addresses[i].Type] = append(addressMap[addresses[i].Type], addresses[i])
877-
}
878-
if addresses, ok := addressMap[apiv1.NodeInternalIP]; ok {
879-
return net.ParseIP(addresses[0].Address), nil
880-
}
881-
if addresses, ok := addressMap[apiv1.NodeExternalIP]; ok {
882-
return net.ParseIP(addresses[0].Address), nil
883-
}
884-
return nil, errors.New("host IP unknown")
885-
}
886-
887865
// Cleanup cleanup configurations done
888866
func (npc *NetworkPolicyController) Cleanup() {
889867

@@ -961,7 +939,7 @@ func (npc *NetworkPolicyController) Cleanup() {
961939
}
962940

963941
// delete all ipsets
964-
err = npc.ipset.Destroy()
942+
err = npc.ipSetHandler.DestroyAllWithin()
965943
if err != nil {
966944
glog.Errorf("Failed to clean up ipsets: " + err.Error())
967945
}
@@ -989,7 +967,7 @@ func NewNetworkPolicyController(clientset *kubernetes.Clientset, config *options
989967

990968
npc.nodeHostName = node.Name
991969

992-
nodeIP, err := getNodeIP(node)
970+
nodeIP, err := utils.GetNodeIP(node)
993971
if err != nil {
994972
return nil, err
995973
}
@@ -1003,7 +981,7 @@ func NewNetworkPolicyController(clientset *kubernetes.Clientset, config *options
1003981
if err != nil {
1004982
return nil, err
1005983
}
1006-
npc.ipset = ipset
984+
npc.ipSetHandler = ipset
1007985

1008986
watchers.PodWatcher.RegisterHandler(&npc)
1009987
watchers.NetworkPolicyWatcher.RegisterHandler(&npc)

0 commit comments

Comments
 (0)