Skip to content

Commit

Permalink
fix: labels might be nil
Browse files Browse the repository at this point in the history
(cherry picked from commit ab834b5)
  • Loading branch information
oilbeater committed Apr 27, 2020
1 parent eb9c9fd commit 95d479f
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/controller/network_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,9 @@ func (c *Controller) podMatchNetworkPolicies(pod *corev1.Pod) []string {

func isPodMatchNetworkPolicy(pod *corev1.Pod, podNs *corev1.Namespace, policy *netv1.NetworkPolicy, policyNs string) bool {
sel, _ := metav1.LabelSelectorAsSelector(&policy.Spec.PodSelector)
if pod.Labels == nil {
pod.Labels = map[string]string{}
}
if podNs.Name == policyNs && sel.Matches(labels.Set(pod.Labels)) {
return true
}
Expand Down Expand Up @@ -510,6 +513,9 @@ func isPodMatchPolicyPeer(pod *corev1.Pod, podNs *corev1.Namespace, policyPeer *

} else {
nsSel, _ := metav1.LabelSelectorAsSelector(policyPeer.NamespaceSelector)
if podNs.Labels == nil {
podNs.Labels = map[string]string{}
}
if !nsSel.Matches(labels.Set(podNs.Labels)) {
return false
}
Expand All @@ -520,6 +526,9 @@ func isPodMatchPolicyPeer(pod *corev1.Pod, podNs *corev1.Namespace, policyPeer *
}

sel, _ := metav1.LabelSelectorAsSelector(policyPeer.PodSelector)
if pod.Labels == nil {
pod.Labels = map[string]string{}
}
return sel.Matches(labels.Set(pod.Labels))
}

Expand All @@ -539,6 +548,9 @@ func isNamespaceMatchNetworkPolicy(ns *corev1.Namespace, policy *netv1.NetworkPo
for _, npp := range npr.From {
if npp.NamespaceSelector != nil {
nsSel, _ := metav1.LabelSelectorAsSelector(npp.NamespaceSelector)
if ns.Labels == nil {
ns.Labels = map[string]string{}
}
if nsSel.Matches(labels.Set(ns.Labels)) {
return true
}
Expand All @@ -550,6 +562,9 @@ func isNamespaceMatchNetworkPolicy(ns *corev1.Namespace, policy *netv1.NetworkPo
for _, npp := range npr.To {
if npp.NamespaceSelector != nil {
nsSel, _ := metav1.LabelSelectorAsSelector(npp.NamespaceSelector)
if ns.Labels == nil {
ns.Labels = map[string]string{}
}
if nsSel.Matches(labels.Set(ns.Labels)) {
return true
}
Expand Down

0 comments on commit 95d479f

Please sign in to comment.