Skip to content

Commit 0b1a052

Browse files
controllers: wire port-filter calls into endpoint event handlers
Port-filter state was previously only updated on Service events. When an Endpoint changed (pod added / removed / IP changed) without a concurrent Service event, the firewall could remain stale until the next Service update. Mirror the EnsureRules / DeleteRules invocations in addEndpointFunc, updateEndpointFunc, and deleteEndpointFunc so that the port-filter set tracks endpoint changes in real time. Addresses gemini-code-assist review feedback on PR #11. Signed-off-by: mattia-eleuteri <[email protected]>
1 parent 7242c97 commit 0b1a052

1 file changed

Lines changed: 37 additions & 7 deletions

File tree

‎pkg/controllers/services_controller.go‎

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,19 @@ func (c *ServicesController) addEndpointFunc(obj interface{}) {
383383

384384
// If both the Service and the Endpoint have valid IPs, ensure NAT mapping rules.
385385
if hasValidServiceIP(se.Service) && hasValidEndpointIP(ep) {
386-
c.Proxy.EnsureRules(
387-
se.Service.Status.LoadBalancer.Ingress[0].IP,
388-
ep.Subsets[0].Addresses[0].IP,
389-
)
386+
svcIP := se.Service.Status.LoadBalancer.Ingress[0].IP
387+
podIP := ep.Subsets[0].Addresses[0].IP
388+
c.Proxy.EnsureRules(svcIP, podIP)
389+
// Mirror the port-filter state to the new endpoint.
390+
if wholeIPPassthrough(se.Service) {
391+
if err := c.Proxy.DeletePortFilter(svcIP, podIP); err != nil {
392+
log.Error(err, "failed to delete port filter on endpoint add", "svcIP", svcIP, "podIP", podIP)
393+
}
394+
} else {
395+
if err := c.Proxy.EnsurePortFilter(svcIP, podIP, se.Service.Spec.Ports); err != nil {
396+
log.Error(err, "failed to ensure port filter on endpoint add", "svcIP", svcIP, "podIP", podIP)
397+
}
398+
}
390399
}
391400
}
392401

@@ -406,7 +415,12 @@ func (c *ServicesController) deleteEndpointFunc(obj interface{}) {
406415
if !hasValidServiceIP(se.Service) || !hasValidEndpointIP(se.Endpoint) {
407416
return
408417
}
409-
c.Proxy.DeleteRules(se.Service.Status.LoadBalancer.Ingress[0].IP, se.Endpoint.Subsets[0].Addresses[0].IP)
418+
svcIP := se.Service.Status.LoadBalancer.Ingress[0].IP
419+
podIP := se.Endpoint.Subsets[0].Addresses[0].IP
420+
if err := c.Proxy.DeletePortFilter(svcIP, podIP); err != nil {
421+
log.Error(err, "failed to delete port filter on endpoint delete", "svcIP", svcIP, "podIP", podIP)
422+
}
423+
c.Proxy.DeleteRules(svcIP, podIP)
410424
// Set the endpoint to nil.
411425
c.Services.SetEndpoint(ep.Namespace, ep.Name, nil)
412426
}
@@ -426,7 +440,12 @@ func (c *ServicesController) updateEndpointFunc(oldObj, newObj interface{}) {
426440
}
427441
if !hasValidEndpointIP(ep) {
428442
if hasValidServiceIP(se.Service) && hasValidEndpointIP(se.Endpoint) {
429-
c.Proxy.DeleteRules(se.Service.Status.LoadBalancer.Ingress[0].IP, se.Endpoint.Subsets[0].Addresses[0].IP)
443+
svcIP := se.Service.Status.LoadBalancer.Ingress[0].IP
444+
oldPodIP := se.Endpoint.Subsets[0].Addresses[0].IP
445+
if err := c.Proxy.DeletePortFilter(svcIP, oldPodIP); err != nil {
446+
log.Error(err, "failed to delete port filter on endpoint invalidation", "svcIP", svcIP, "podIP", oldPodIP)
447+
}
448+
c.Proxy.DeleteRules(svcIP, oldPodIP)
430449
}
431450
c.Services.SetEndpoint(ep.Namespace, ep.Name, ep)
432451
return
@@ -437,7 +456,18 @@ func (c *ServicesController) updateEndpointFunc(oldObj, newObj interface{}) {
437456
if !hasValidEndpointIP(ep) {
438457
return
439458
}
440-
c.Proxy.EnsureRules(se.Service.Status.LoadBalancer.Ingress[0].IP, ep.Subsets[0].Addresses[0].IP)
459+
svcIP := se.Service.Status.LoadBalancer.Ingress[0].IP
460+
podIP := ep.Subsets[0].Addresses[0].IP
461+
c.Proxy.EnsureRules(svcIP, podIP)
462+
if wholeIPPassthrough(se.Service) {
463+
if err := c.Proxy.DeletePortFilter(svcIP, podIP); err != nil {
464+
log.Error(err, "failed to delete port filter on endpoint update", "svcIP", svcIP, "podIP", podIP)
465+
}
466+
} else {
467+
if err := c.Proxy.EnsurePortFilter(svcIP, podIP, se.Service.Spec.Ports); err != nil {
468+
log.Error(err, "failed to ensure port filter on endpoint update", "svcIP", svcIP, "podIP", podIP)
469+
}
470+
}
441471
c.Services.SetEndpoint(ep.Namespace, ep.Name, ep)
442472
}
443473

0 commit comments

Comments
 (0)