-
Notifications
You must be signed in to change notification settings - Fork 346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pod filtering for Services based on Namespaces in NodePortLocal #1927
Pod filtering for Services based on Namespaces in NodePortLocal #1927
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1927 +/- ##
==========================================
- Coverage 41.56% 41.17% -0.39%
==========================================
Files 201 116 -85
Lines 17374 14693 -2681
==========================================
- Hits 7221 6050 -1171
+ Misses 9123 8124 -999
+ Partials 1030 519 -511
Flags with carried forward coverage won't be shown. Click here to find out more.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please link this PR to the appropriate issue by adding Fixes #1926
to the commit message and PR description. You can also remove (#1926)
from the commit title. The idea is that the issue should be closed automatically when the PR is merged, not just to reference the issue in the commit message.
@@ -255,7 +255,9 @@ func (c *NPLController) getPodsFromService(svc *corev1.Service) []string { | |||
return pods | |||
} | |||
for _, pod := range podList { | |||
pods = append(pods, podKeyFunc(pod)) | |||
if pod.GetNamespace() == svc.GetNamespace() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of c.podLister.List(labels.SelectorFromSet(labels.Set(svc.Spec.Selector)))
above, you should get a PodNamespaceLister
object first and list only Pods from that Namespace. The following should work and will be more efficient if many Pods across different Namespaces share the same labels:
podList, err := c.podLister.Pods(svc.Namespace).List(labels.SelectorFromSet(labels.Set(svc.Spec.Selector)))
@@ -272,7 +274,8 @@ func (c *NPLController) isNPLEnabledForServiceOfPod(obj interface{}) bool { | |||
svc, isSvc := service.(*corev1.Service) | |||
// Selecting Services NOT of type NodePort, with Service selector matching Pod labels. | |||
if isSvc && svc.Spec.Type != corev1.ServiceTypeNodePort { | |||
if matchSvcSelectorPodLabels(svc.Spec.Selector, pod.GetLabels()) { | |||
if matchSvcSelectorPodLabels(svc.Spec.Selector, pod.GetLabels()) && | |||
pod.GetNamespace() == svc.GetNamespace() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not really specific to this PR, but is there a difference between using the getters (e.g. pod.GetNamespace()
) or accessing the fields directly (e.g. pod.Namespace
)? @tnqn do you know?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think no real difference, but I woud just use pod.Namespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, it would be good to make it uniform and stop using the getters in the NPL controller. We don't use them anywhere else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The GetXXX methods are normally used when a function needs to be generic to handle different kinds of objects. The object is then converted to or passed in as metav1.Object
…n NodePortLocal Also includes UT to check for namespace based pod filtering via Services.
e449cb1
to
dcbdd5f
Compare
@@ -272,7 +274,8 @@ func (c *NPLController) isNPLEnabledForServiceOfPod(obj interface{}) bool { | |||
svc, isSvc := service.(*corev1.Service) | |||
// Selecting Services NOT of type NodePort, with Service selector matching Pod labels. | |||
if isSvc && svc.Spec.Type != corev1.ServiceTypeNodePort { | |||
if matchSvcSelectorPodLabels(svc.Spec.Selector, pod.GetLabels()) { | |||
if matchSvcSelectorPodLabels(svc.Spec.Selector, pod.GetLabels()) && | |||
pod.GetNamespace() == svc.GetNamespace() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think no real difference, but I woud just use pod.Namespace.
@@ -272,7 +272,8 @@ func (c *NPLController) isNPLEnabledForServiceOfPod(obj interface{}) bool { | |||
svc, isSvc := service.(*corev1.Service) | |||
// Selecting Services NOT of type NodePort, with Service selector matching Pod labels. | |||
if isSvc && svc.Spec.Type != corev1.ServiceTypeNodePort { | |||
if matchSvcSelectorPodLabels(svc.Spec.Selector, pod.GetLabels()) { | |||
if matchSvcSelectorPodLabels(svc.Spec.Selector, pod.GetLabels()) && | |||
pod.GetNamespace() == svc.GetNamespace() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not compare Namespace first before labels, which can drop many Pods faster?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
/test-e2e |
/test-networkpolicy |
/test-conformance |
Also includes UT to check for namespace based pod filtering via Services.
Fixes #1926