Skip to content
Closed
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
38 changes: 30 additions & 8 deletions pkg/network/node/networkpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ type networkPolicyPlugin struct {

warnedPolicies map[ktypes.UID]string
skippedPolicies map[ktypes.UID]string

// ips includes the ips of pods that have been created and are affected by an
// existing network policy
ips []string
}

// npNamespace tracks NetworkPolicy-related data for a Namespace
Expand Down Expand Up @@ -109,6 +113,8 @@ func NewNetworkPolicyPlugin() osdnPolicy {

warnedPolicies: make(map[ktypes.UID]string),
skippedPolicies: make(map[ktypes.UID]string),

ips: []string{},
}
}

Expand Down Expand Up @@ -330,6 +336,16 @@ func (np *networkPolicyPlugin) syncFlows() {
npns.mustSync = false
}
}

for _, ip := range np.ips {
// remove network isolation from pods that have been added and
// caused a flow recalculation
klog.Errorf("KEYWORD: why is maybe this invalid (%s)", ip)
otx.DeleteFlows("table=27, cookie=1/-1, ip, nw_src=%s", ip)
otx.DeleteFlows("table=80, cookie=1/-1, ip, nw_src=%s", ip)
}
// reset the list of pods started since the last sync
np.ips = []string{}
if err := otx.Commit(); err != nil {
klog.Errorf("Error syncing OVS flows: %v", err)
}
Expand Down Expand Up @@ -932,14 +948,7 @@ func (np *networkPolicyPlugin) handleAddOrUpdatePod(obj, old interface{}, eventT
}

np.lock.Lock()
defer func() {
otx := np.node.oc.NewTransaction()
otx.DeleteFlows("table=27, cookie=1/-1, ip, nw_src=%s", pod.Status.PodIP)
otx.DeleteFlows("table=80, cookie=1/-1, ip, nw_src=%s", pod.Status.PodIP)
otx.Commit()

np.lock.Unlock()
}()
defer np.lock.Unlock()

np.refreshPodNetworkPolicies(pod)
}
Expand Down Expand Up @@ -1019,17 +1028,30 @@ func (np *networkPolicyPlugin) refreshNamespaceNetworkPolicies() {
}

func (np *networkPolicyPlugin) refreshPodNetworkPolicies(pod *corev1.Pod) {
podNeedsSync := false
podNs := np.namespacesByName[pod.Namespace]
for _, npns := range np.namespaces {
for _, npp := range npns.policies {
if (npp.watchesOwnPods && npns == podNs) || npp.watchesAllPods {
if !podNeedsSync && len(pod.Status.PodIP) > 0 {
np.ips = append(np.ips, pod.Status.PodIP)
podNeedsSync = true
}
npns.mustRecalculate = true
}
}
if npns.mustRecalculate && npns.inUse {
np.syncNamespace(npns)
}
}
if !podNeedsSync && len(pod.Status.PodIP) > 0 {
otx := np.node.oc.NewTransaction()
otx.DeleteFlows("table=27, cookie=1/-1, ip, nw_src=%s", pod.Status.PodIP)
otx.DeleteFlows("table=80, cookie=1/-1, ip, nw_src=%s", pod.Status.PodIP)
if err := otx.Commit(); err != nil {
klog.Errorf("Error syncing OVS flows to remove isolation from pod %s with ip %s (%v)", pod.Name, pod.Status.PodIP, err)
}
}
}

func getPodFullName(pod *corev1.Pod) string {
Expand Down