@@ -231,8 +231,20 @@ func (c *ServicesController) addServiceFunc(obj interface{}) {
231231 if err == nil && ep != nil && hasValidEndpointIP (ep ) && hasValidServiceIP (svc ) {
232232 se .Endpoint = ep
233233 c .Services .Set (svc .Namespace , svc .Name , se )
234+ svcIP := svc .Status .LoadBalancer .Ingress [0 ].IP
235+ podIP := ep .Subsets [0 ].Addresses [0 ].IP
234236 // Ensure NAT mapping rules are set.
235- c .Proxy .EnsureRules (svc .Status .LoadBalancer .Ingress [0 ].IP , ep .Subsets [0 ].Addresses [0 ].IP )
237+ c .Proxy .EnsureRules (svcIP , podIP )
238+ // Apply (or remove) port filtering depending on annotation value.
239+ if wholeIPPassthrough (svc ) {
240+ if err := c .Proxy .DeletePortFilter (svcIP , podIP ); err != nil {
241+ log .Error (err , "failed to delete port filter" , "svcIP" , svcIP , "podIP" , podIP )
242+ }
243+ } else {
244+ if err := c .Proxy .EnsurePortFilter (svcIP , podIP , svc .Spec .Ports ); err != nil {
245+ log .Error (err , "failed to ensure port filter" , "svcIP" , svcIP , "podIP" , podIP )
246+ }
247+ }
236248 }
237249}
238250
@@ -253,7 +265,12 @@ func (c *ServicesController) deleteServiceFunc(obj interface{}) {
253265 return
254266 }
255267
256- c .Proxy .DeleteRules (se .Service .Status .LoadBalancer .Ingress [0 ].IP , se .Endpoint .Subsets [0 ].Addresses [0 ].IP )
268+ svcIP := se .Service .Status .LoadBalancer .Ingress [0 ].IP
269+ podIP := se .Endpoint .Subsets [0 ].Addresses [0 ].IP
270+ if err := c .Proxy .DeletePortFilter (svcIP , podIP ); err != nil {
271+ log .Error (err , "failed to delete port filter on svc deletion" , "svcIP" , svcIP , "podIP" , podIP )
272+ }
273+ c .Proxy .DeleteRules (svcIP , podIP )
257274 c .Services .Delete (svc .Namespace , svc .Name )
258275}
259276
@@ -270,10 +287,12 @@ func (c *ServicesController) updateServiceFunc(oldObj, newObj interface{}) {
270287 if ! hasWholeIPAnnotation (svc ) {
271288 if se , exists := c .Services .Get (svc .Namespace , svc .Name ); exists {
272289 if hasValidServiceIP (se .Service ) && hasValidEndpointIP (se .Endpoint ) {
273- c .Proxy .DeleteRules (
274- se .Service .Status .LoadBalancer .Ingress [0 ].IP ,
275- se .Endpoint .Subsets [0 ].Addresses [0 ].IP ,
276- )
290+ svcIP := se .Service .Status .LoadBalancer .Ingress [0 ].IP
291+ podIP := se .Endpoint .Subsets [0 ].Addresses [0 ].IP
292+ if err := c .Proxy .DeletePortFilter (svcIP , podIP ); err != nil {
293+ log .Error (err , "failed to delete port filter" , "svcIP" , svcIP , "podIP" , podIP )
294+ }
295+ c .Proxy .DeleteRules (svcIP , podIP )
277296 }
278297 c .Services .Delete (svc .Namespace , svc .Name )
279298 }
@@ -284,10 +303,12 @@ func (c *ServicesController) updateServiceFunc(oldObj, newObj interface{}) {
284303 if ! hasValidServiceIP (svc ) {
285304 if se , exists := c .Services .Get (svc .Namespace , svc .Name ); exists {
286305 if hasValidServiceIP (se .Service ) && hasValidEndpointIP (se .Endpoint ) {
287- c .Proxy .DeleteRules (
288- se .Service .Status .LoadBalancer .Ingress [0 ].IP ,
289- se .Endpoint .Subsets [0 ].Addresses [0 ].IP ,
290- )
306+ svcIP := se .Service .Status .LoadBalancer .Ingress [0 ].IP
307+ podIP := se .Endpoint .Subsets [0 ].Addresses [0 ].IP
308+ if err := c .Proxy .DeletePortFilter (svcIP , podIP ); err != nil {
309+ log .Error (err , "failed to delete port filter" , "svcIP" , svcIP , "podIP" , podIP )
310+ }
311+ c .Proxy .DeleteRules (svcIP , podIP )
291312 }
292313 c .Services .Delete (svc .Namespace , svc .Name )
293314 }
@@ -324,10 +345,18 @@ func (c *ServicesController) updateServiceFunc(oldObj, newObj interface{}) {
324345
325346 // At this point, both the Service and Endpoint have valid IPs.
326347 // Ensure NAT mapping is up-to-date.
327- c .Proxy .EnsureRules (
328- svc .Status .LoadBalancer .Ingress [0 ].IP ,
329- ep .Subsets [0 ].Addresses [0 ].IP ,
330- )
348+ svcIP := svc .Status .LoadBalancer .Ingress [0 ].IP
349+ podIP := ep .Subsets [0 ].Addresses [0 ].IP
350+ c .Proxy .EnsureRules (svcIP , podIP )
351+ if wholeIPPassthrough (svc ) {
352+ if err := c .Proxy .DeletePortFilter (svcIP , podIP ); err != nil {
353+ log .Error (err , "failed to delete port filter" , "svcIP" , svcIP , "podIP" , podIP )
354+ }
355+ } else {
356+ if err := c .Proxy .EnsurePortFilter (svcIP , podIP , svc .Spec .Ports ); err != nil {
357+ log .Error (err , "failed to ensure port filter" , "svcIP" , svcIP , "podIP" , podIP )
358+ }
359+ }
331360
332361 // Update or add the service mapping with the new endpoint.
333362 c .Services .Set (svc .Namespace , svc .Name , & ServiceEndpoints {Service : svc , Endpoint : ep })
@@ -354,10 +383,19 @@ func (c *ServicesController) addEndpointFunc(obj interface{}) {
354383
355384 // If both the Service and the Endpoint have valid IPs, ensure NAT mapping rules.
356385 if hasValidServiceIP (se .Service ) && hasValidEndpointIP (ep ) {
357- c .Proxy .EnsureRules (
358- se .Service .Status .LoadBalancer .Ingress [0 ].IP ,
359- ep .Subsets [0 ].Addresses [0 ].IP ,
360- )
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+ }
361399 }
362400}
363401
@@ -377,7 +415,12 @@ func (c *ServicesController) deleteEndpointFunc(obj interface{}) {
377415 if ! hasValidServiceIP (se .Service ) || ! hasValidEndpointIP (se .Endpoint ) {
378416 return
379417 }
380- 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 )
381424 // Set the endpoint to nil.
382425 c .Services .SetEndpoint (ep .Namespace , ep .Name , nil )
383426}
@@ -397,7 +440,12 @@ func (c *ServicesController) updateEndpointFunc(oldObj, newObj interface{}) {
397440 }
398441 if ! hasValidEndpointIP (ep ) {
399442 if hasValidServiceIP (se .Service ) && hasValidEndpointIP (se .Endpoint ) {
400- 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 )
401449 }
402450 c .Services .SetEndpoint (ep .Namespace , ep .Name , ep )
403451 return
@@ -408,7 +456,18 @@ func (c *ServicesController) updateEndpointFunc(oldObj, newObj interface{}) {
408456 if ! hasValidEndpointIP (ep ) {
409457 return
410458 }
411- 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+ }
412471 c .Services .SetEndpoint (ep .Namespace , ep .Name , ep )
413472}
414473
@@ -447,10 +506,28 @@ func hasValidEndpointIP(ep *v1.Endpoints) bool {
447506 return ep .Subsets [0 ].Addresses [0 ].IP != ""
448507}
449508
450- // hasWholeIPAnnotation checks if the service has the wholeIP annotation set to true.
509+ const wholeIPAnnotation = "networking.cozystack.io/wholeIP"
510+
511+ // hasWholeIPAnnotation reports whether the service is opted into cozy-proxy
512+ // management via the wholeIP annotation. Any value triggers management — the
513+ // value's meaning (passthrough vs port-filter) is determined by wholeIPPassthrough.
451514func hasWholeIPAnnotation (svc * v1.Service ) bool {
452- val , ok := svc .Annotations ["networking.cozystack.io/wholeIP" ]
453- return ok && val == "true"
515+ if svc == nil {
516+ return false
517+ }
518+ _ , ok := svc .Annotations [wholeIPAnnotation ]
519+ return ok
520+ }
521+
522+ // wholeIPPassthrough reports whether ingress traffic should bypass port
523+ // filtering. Defaults to true (current behavior) for any value other than
524+ // the explicit string "false", preserving backward compatibility for services
525+ // rendered by older charts (annotation: "true") and services with no value.
526+ func wholeIPPassthrough (svc * v1.Service ) bool {
527+ if svc == nil || svc .Annotations == nil {
528+ return true
529+ }
530+ return svc .Annotations [wholeIPAnnotation ] != "false"
454531}
455532
456533// cleanupRemovedServices performs an initial cleanup for removed services.
@@ -478,5 +555,27 @@ func (c *ServicesController) cleanupRemovedServices() error {
478555 if err := c .Proxy .CleanupRules (keepMap ); err != nil {
479556 return fmt .Errorf ("failed to perform initial cleanup: %w" , err )
480557 }
558+ // Build per-svc port filter snapshot for services in non-passthrough mode.
559+ // Keyed by svcIP (caller convenience); the entry carries podIP and ports
560+ // because the actual nft set keys are pod IPs (post-DNAT match).
561+ keepFilters := make (map [string ]nat.PortFilterEntry )
562+ for _ , se := range allServices {
563+ if se .Service == nil || se .Endpoint == nil {
564+ continue
565+ }
566+ if ! hasValidServiceIP (se .Service ) || ! hasValidEndpointIP (se .Endpoint ) {
567+ continue
568+ }
569+ if wholeIPPassthrough (se .Service ) {
570+ continue
571+ }
572+ keepFilters [se .Service .Status .LoadBalancer .Ingress [0 ].IP ] = nat.PortFilterEntry {
573+ PodIP : se .Endpoint .Subsets [0 ].Addresses [0 ].IP ,
574+ Ports : se .Service .Spec .Ports ,
575+ }
576+ }
577+ if err := c .Proxy .CleanupPortFilters (keepFilters ); err != nil {
578+ return fmt .Errorf ("failed to perform port-filter cleanup: %w" , err )
579+ }
481580 return nil
482581}
0 commit comments