Merge remote-tracking branch 'gobomb/fix-nil-ns-pointer'
Needed a few modifications to resolve conflicts due to changed datatype of UID.
This commit is contained in:
commit
f9e27f76d6
3 files changed
+35
-24
No files matched your search
+5
-5
@@ -61,9 +61,9 @@ func New(nodeName string, ipt iptables.Interface, ips ipset.Interface, clientset
|
|||||||
|
|
||||||
func (npc *controller) onNewNsSelector(selector *selector) error {
|
func (npc *controller) onNewNsSelector(selector *selector) error {
|
||||||
for _, ns := range npc.nss {
|
for _, ns := range npc.nss {
|
||||||
if ns.namespace != nil {
|
if ns.namespaceUID != "" {
|
||||||
if selector.matchesNamespaceSelector(ns.namespace.ObjectMeta.Labels) {
|
if selector.matchesNamespaceSelector(ns.namespaceLabels) {
|
||||||
if err := selector.addEntry(nsuid(ns.namespace), string(ns.allPods.ipsetName), namespaceComment(ns)); err != nil {
|
if err := selector.addEntry(ns.namespaceUID, string(ns.allPods.ipsetName), namespaceComment(ns)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -74,10 +74,10 @@ func (npc *controller) onNewNsSelector(selector *selector) error {
|
|||||||
|
|
||||||
func (npc *controller) onNewNamespacePodsSelector(selector *selector) error {
|
func (npc *controller) onNewNamespacePodsSelector(selector *selector) error {
|
||||||
for _, ns := range npc.nss {
|
for _, ns := range npc.nss {
|
||||||
if ns.namespace != nil && len(ns.pods) > 0 {
|
if ns.namespaceUID != "" && len(ns.pods) > 0 {
|
||||||
for _, pod := range ns.pods {
|
for _, pod := range ns.pods {
|
||||||
if hasIP(pod) {
|
if hasIP(pod) {
|
||||||
if selector.matchesNamespacedPodSelector(pod.ObjectMeta.Labels, ns.namespace.ObjectMeta.Labels) {
|
if selector.matchesNamespacedPodSelector(pod.ObjectMeta.Labels, ns.namespaceLabels) {
|
||||||
if err := selector.addEntry(uid(pod), pod.Status.PodIP, podComment(pod)); err != nil {
|
if err := selector.addEntry(uid(pod), pod.Status.PodIP, podComment(pod)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -160,6 +160,7 @@ func TestRegressionPolicyNamespaceOrdering3059(t *testing.T) {
|
|||||||
sourceNamespace := &coreapi.Namespace{
|
sourceNamespace := &coreapi.Namespace{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "source",
|
Name: "source",
|
||||||
|
UID: "source",
|
||||||
Labels: map[string]string{
|
Labels: map[string]string{
|
||||||
"app": "source",
|
"app": "source",
|
||||||
},
|
},
|
||||||
@@ -169,6 +170,7 @@ func TestRegressionPolicyNamespaceOrdering3059(t *testing.T) {
|
|||||||
destinationNamespace := &coreapi.Namespace{
|
destinationNamespace := &coreapi.Namespace{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "destination",
|
Name: "destination",
|
||||||
|
UID: "destination",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -249,6 +251,7 @@ func TestDefaultAllow(t *testing.T) {
|
|||||||
defaultNamespace := &coreapi.Namespace{
|
defaultNamespace := &coreapi.Namespace{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "default",
|
Name: "default",
|
||||||
|
UID: "default",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
controller.AddNamespace(defaultNamespace)
|
controller.AddNamespace(defaultNamespace)
|
||||||
@@ -378,6 +381,7 @@ func TestOutOfOrderPodEvents(t *testing.T) {
|
|||||||
defaultNamespace := &coreapi.Namespace{
|
defaultNamespace := &coreapi.Namespace{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "default",
|
Name: "default",
|
||||||
|
UID: "default",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
client.CoreV1().Namespaces().Create(defaultNamespace)
|
client.CoreV1().Namespaces().Create(defaultNamespace)
|
||||||
@@ -462,6 +466,7 @@ func TestNewTargetSelector(t *testing.T) {
|
|||||||
defaultNamespace := &coreapi.Namespace{
|
defaultNamespace := &coreapi.Namespace{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "default",
|
Name: "default",
|
||||||
|
UID: "default",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
client.CoreV1().Namespaces().Create(defaultNamespace)
|
client.CoreV1().Namespaces().Create(defaultNamespace)
|
||||||
@@ -540,11 +545,13 @@ func TestEgressPolicyWithIPBlock(t *testing.T) {
|
|||||||
defaultNamespace := &coreapi.Namespace{
|
defaultNamespace := &coreapi.Namespace{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "default",
|
Name: "default",
|
||||||
|
UID: "default",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
nonDefaultNamespace := &coreapi.Namespace{
|
nonDefaultNamespace := &coreapi.Namespace{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "non-default",
|
Name: "non-default",
|
||||||
|
UID: "non-default",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
client.CoreV1().Namespaces().Create(defaultNamespace)
|
client.CoreV1().Namespaces().Create(defaultNamespace)
|
||||||
@@ -635,6 +642,7 @@ func TestIngressPolicyWithIPBlockAndPortSpecified(t *testing.T) {
|
|||||||
defaultNamespace := &coreapi.Namespace{
|
defaultNamespace := &coreapi.Namespace{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "default",
|
Name: "default",
|
||||||
|
UID: "default",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+22
-19
@@ -22,11 +22,12 @@ type ns struct {
|
|||||||
ipt iptables.Interface // interface to iptables
|
ipt iptables.Interface // interface to iptables
|
||||||
ips ipset.Interface // interface to ipset
|
ips ipset.Interface // interface to ipset
|
||||||
|
|
||||||
name string // k8s Namespace name
|
name string // k8s Namespace name
|
||||||
nodeName string // my node name
|
nodeName string // my node name
|
||||||
namespace *coreapi.Namespace // k8s Namespace object
|
namespaceUID ipset.UID // k8s Namespace UID
|
||||||
pods map[ipset.UID]*coreapi.Pod // k8s Pod objects by UID
|
namespaceLabels map[string]string // k8s Namespace labels
|
||||||
policies map[ipset.UID]interface{} // k8s NetworkPolicy objects by UID
|
pods map[ipset.UID]*coreapi.Pod // k8s Pod objects by UID
|
||||||
|
policies map[ipset.UID]interface{} // k8s NetworkPolicy objects by UID
|
||||||
|
|
||||||
uid ipset.UID // surrogate UID to own allPods selector
|
uid ipset.UID // surrogate UID to own allPods selector
|
||||||
allPods *selectorSpec // hash:ip ipset of all pod IPs in this namespace
|
allPods *selectorSpec // hash:ip ipset of all pod IPs in this namespace
|
||||||
@@ -54,7 +55,8 @@ func newNS(name, nodeName string, ipt iptables.Interface, ips ipset.Interface, n
|
|||||||
ipt: ipt,
|
ipt: ipt,
|
||||||
ips: ips,
|
ips: ips,
|
||||||
name: name,
|
name: name,
|
||||||
namespace: namespaceObj,
|
namespaceUID: nsuid(namespaceObj),
|
||||||
|
namespaceLabels: namespaceObj.ObjectMeta.Labels,
|
||||||
nodeName: nodeName,
|
nodeName: nodeName,
|
||||||
pods: make(map[ipset.UID]*coreapi.Pod),
|
pods: make(map[ipset.UID]*coreapi.Pod),
|
||||||
policies: make(map[ipset.UID]interface{}),
|
policies: make(map[ipset.UID]interface{}),
|
||||||
@@ -88,7 +90,7 @@ func newNS(name, nodeName string, ipt iptables.Interface, ips ipset.Interface, n
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ns *ns) empty() bool {
|
func (ns *ns) empty() bool {
|
||||||
return len(ns.pods) == 0 && len(ns.policies) == 0 && ns.namespace == nil
|
return len(ns.pods) == 0 && len(ns.policies) == 0 && ns.namespaceUID == ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ns *ns) destroy() error {
|
func (ns *ns) destroy() error {
|
||||||
@@ -181,7 +183,7 @@ func (ns *ns) addPod(obj *coreapi.Pod) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ns.namespacedPodsSelectors.addToMatchingNamespacedPodSelector(uid(obj), obj.ObjectMeta.Labels, ns.namespace.ObjectMeta.Labels, obj.Status.PodIP, podComment(obj))
|
err = ns.namespacedPodsSelectors.addToMatchingNamespacedPodSelector(uid(obj), obj.ObjectMeta.Labels, ns.namespaceLabels, obj.Status.PodIP, podComment(obj))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -204,7 +206,7 @@ func (ns *ns) updatePod(oldObj, newObj *coreapi.Pod) error {
|
|||||||
if err := ns.ips.DelEntry(uid(oldObj), ns.egressDefaultAllowIPSet, oldObj.Status.PodIP); err != nil {
|
if err := ns.ips.DelEntry(uid(oldObj), ns.egressDefaultAllowIPSet, oldObj.Status.PodIP); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := ns.namespacedPodsSelectors.delFromMatchingNamespacedPodSelector(uid(oldObj), oldObj.ObjectMeta.Labels, ns.namespace.ObjectMeta.Labels, oldObj.Status.PodIP); err != nil {
|
if err := ns.namespacedPodsSelectors.delFromMatchingNamespacedPodSelector(uid(oldObj), oldObj.ObjectMeta.Labels, ns.namespaceLabels, oldObj.Status.PodIP); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,7 +229,7 @@ func (ns *ns) updatePod(oldObj, newObj *coreapi.Pod) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = ns.namespacedPodsSelectors.addToMatchingNamespacedPodSelector(uid(newObj), newObj.ObjectMeta.Labels, ns.namespace.ObjectMeta.Labels, newObj.Status.PodIP, podComment(newObj))
|
err = ns.namespacedPodsSelectors.addToMatchingNamespacedPodSelector(uid(newObj), newObj.ObjectMeta.Labels, ns.namespaceLabels, newObj.Status.PodIP, podComment(newObj))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -272,8 +274,8 @@ func (ns *ns) updatePod(oldObj, newObj *coreapi.Pod) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, ps := range ns.namespacedPodsSelectors.entries {
|
for _, ps := range ns.namespacedPodsSelectors.entries {
|
||||||
oldMatch := ps.matchesNamespacedPodSelector(oldObj.ObjectMeta.Labels, ns.namespace.ObjectMeta.Labels)
|
oldMatch := ps.matchesNamespacedPodSelector(oldObj.ObjectMeta.Labels, ns.namespaceLabels)
|
||||||
newMatch := ps.matchesNamespacedPodSelector(newObj.ObjectMeta.Labels, ns.namespace.ObjectMeta.Labels)
|
newMatch := ps.matchesNamespacedPodSelector(newObj.ObjectMeta.Labels, ns.namespaceLabels)
|
||||||
if oldMatch == newMatch && oldObj.Status.PodIP == newObj.Status.PodIP {
|
if oldMatch == newMatch && oldObj.Status.PodIP == newObj.Status.PodIP {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -326,7 +328,7 @@ func (ns *ns) deletePod(obj *coreapi.Pod) error {
|
|||||||
if err := ns.podSelectors.delFromMatchingPodSelector(uid(obj), obj.ObjectMeta.Labels, obj.Status.PodIP); err != nil {
|
if err := ns.podSelectors.delFromMatchingPodSelector(uid(obj), obj.ObjectMeta.Labels, obj.Status.PodIP); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := ns.namespacedPodsSelectors.delFromMatchingNamespacedPodSelector(uid(obj), obj.ObjectMeta.Labels, ns.namespace.ObjectMeta.Labels, obj.Status.PodIP); err != nil {
|
if err := ns.namespacedPodsSelectors.delFromMatchingNamespacedPodSelector(uid(obj), obj.ObjectMeta.Labels, ns.namespaceLabels, obj.Status.PodIP); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -488,7 +490,8 @@ func (ns *ns) deleteBypassRules() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ns *ns) addNamespace(obj *coreapi.Namespace) error {
|
func (ns *ns) addNamespace(obj *coreapi.Namespace) error {
|
||||||
ns.namespace = obj
|
ns.namespaceUID = ipset.UID(obj.ObjectMeta.UID)
|
||||||
|
ns.namespaceLabels = obj.ObjectMeta.Labels
|
||||||
|
|
||||||
// Insert a rule to bypass policies
|
// Insert a rule to bypass policies
|
||||||
if err := ns.ensureBypassRules(); err != nil {
|
if err := ns.ensureBypassRules(); err != nil {
|
||||||
@@ -501,10 +504,10 @@ func (ns *ns) addNamespace(obj *coreapi.Namespace) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ns *ns) updateNamespace(oldObj, newObj *coreapi.Namespace) error {
|
func (ns *ns) updateNamespace(oldObj, newObj *coreapi.Namespace) error {
|
||||||
ns.namespace = newObj
|
|
||||||
|
|
||||||
// Re-evaluate namespace selector membership if labels have changed
|
// Re-evaluate namespace selector membership if labels have changed
|
||||||
if !equals(oldObj.ObjectMeta.Labels, newObj.ObjectMeta.Labels) {
|
if !equals(oldObj.ObjectMeta.Labels, newObj.ObjectMeta.Labels) {
|
||||||
|
ns.namespaceLabels = newObj.ObjectMeta.Labels
|
||||||
|
|
||||||
for _, selector := range ns.nsSelectors.entries {
|
for _, selector := range ns.nsSelectors.entries {
|
||||||
oldMatch := selector.matchesNamespaceSelector(oldObj.ObjectMeta.Labels)
|
oldMatch := selector.matchesNamespaceSelector(oldObj.ObjectMeta.Labels)
|
||||||
newMatch := selector.matchesNamespaceSelector(newObj.ObjectMeta.Labels)
|
newMatch := selector.matchesNamespaceSelector(newObj.ObjectMeta.Labels)
|
||||||
@@ -512,12 +515,12 @@ func (ns *ns) updateNamespace(oldObj, newObj *coreapi.Namespace) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if oldMatch {
|
if oldMatch {
|
||||||
if err := selector.delEntry(nsuid(ns.namespace), string(ns.allPods.ipsetName)); err != nil {
|
if err := selector.delEntry(ns.namespaceUID, string(ns.allPods.ipsetName)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if newMatch {
|
if newMatch {
|
||||||
if err := selector.addEntry(nsuid(ns.namespace), string(ns.allPods.ipsetName), namespaceComment(ns)); err != nil {
|
if err := selector.addEntry(ns.namespaceUID, string(ns.allPods.ipsetName), namespaceComment(ns)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -528,7 +531,7 @@ func (ns *ns) updateNamespace(oldObj, newObj *coreapi.Namespace) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ns *ns) deleteNamespace(obj *coreapi.Namespace) error {
|
func (ns *ns) deleteNamespace(obj *coreapi.Namespace) error {
|
||||||
ns.namespace = nil
|
ns.namespaceUID = ""
|
||||||
|
|
||||||
// Remove bypass rule
|
// Remove bypass rule
|
||||||
if err := ns.deleteBypassRules(); err != nil {
|
if err := ns.deleteBypassRules(); err != nil {
|
||||||
|
|||||||
Reference in new issue
Block a user