@@ -387,6 +387,19 @@ func (nsc *NetworkServicesController) sync() error {
387387 return nil
388388}
389389
390+ // Lookup service ip, protocol, port by given fwmark value (reverse of generateFwmark)
391+ func (nsc * NetworkServicesController ) lookupServiceByFWMark (FWMark uint32 ) (string , string , int ) {
392+ for _ , svc := range nsc .serviceMap {
393+ for _ , externalIP := range svc .externalIPs {
394+ gfwmark := generateFwmark (externalIP , svc .protocol , fmt .Sprint (svc .port ))
395+ if FWMark == gfwmark {
396+ return externalIP , svc .protocol , svc .port
397+ }
398+ }
399+ }
400+ return "" , "" , 0
401+ }
402+
390403func getIpvsFirewallInputChainRule () []string {
391404 // The iptables rule for use in {setup,cleanup}IpvsFirewall.
392405 return []string {
@@ -579,15 +592,27 @@ func (nsc *NetworkServicesController) syncIpvsFirewall() error {
579592 ipvsServicesSets := make ([]string , 0 , len (ipvsServices ))
580593
581594 for _ , ipvsService := range ipvsServices {
582- protocol := "udp"
583- if ipvsService .Protocol == syscall .IPPROTO_TCP {
584- protocol = "tcp"
595+ var address , protocol string
596+ var port int
597+ if ipvsService .Address != nil {
598+ address = ipvsService .Address .String ()
599+ if ipvsService .Protocol == syscall .IPPROTO_TCP {
600+ protocol = "tcp"
601+ } else {
602+ protocol = "udp"
603+ }
604+ port = int (ipvsService .Port )
605+ } else if ipvsService .FWMark != 0 {
606+ address , protocol , port = nsc .lookupServiceByFWMark (ipvsService .FWMark )
607+ if address == "" {
608+ continue
609+ }
585610 }
586611
587- serviceIPsSet := ipvsService . Address . String ()
612+ serviceIPsSet := address
588613 serviceIPsSets = append (serviceIPsSets , serviceIPsSet )
589614
590- ipvsServicesSet := fmt .Sprintf ("%s,%s:%d" , ipvsService . Address . String () , protocol , ipvsService . Port )
615+ ipvsServicesSet := fmt .Sprintf ("%s,%s:%d" , address , protocol , port )
591616 ipvsServicesSets = append (ipvsServicesSets , ipvsServicesSet )
592617
593618 }
0 commit comments