Skip to content

Commit

Permalink
Add ARP/NDP responders to external IP assigner (#3318)
Browse files Browse the repository at this point in the history
Apart from assigning IPs to the dummy interface, this commit will also create
raw sockets and listen for ARP requests packets (IPv4) and Neighbor Solicitation
packets (IPv6). This fixes the issue that Egress cannot work in IPv6 mode
as the system would not reply to Neighbor Advertisement from external
interfaces if the IP is assigned to the dummy interface. The IP assigner
will skip managing the dummy device if dummyDeviceName is empty. This avoids
the kernel creating a local route, which has a higher priority than the routes
installed by antrea-proxy in proxyAll mode.

Signed-off-by: Tianyi Huang <hty690@126.com>
  • Loading branch information
hty690 committed Feb 23, 2022
1 parent 01002ed commit a87ef55
Show file tree
Hide file tree
Showing 18 changed files with 1,540 additions and 244 deletions.
7 changes: 1 addition & 6 deletions cmd/antrea-agent/agent.go
Expand Up @@ -41,7 +41,6 @@ import (
"antrea.io/antrea/pkg/agent/flowexporter"
"antrea.io/antrea/pkg/agent/flowexporter/exporter"
"antrea.io/antrea/pkg/agent/interfacestore"
"antrea.io/antrea/pkg/agent/ipassigner"
"antrea.io/antrea/pkg/agent/memberlist"
"antrea.io/antrea/pkg/agent/metrics"
"antrea.io/antrea/pkg/agent/multicast"
Expand Down Expand Up @@ -307,13 +306,11 @@ func run(o *Options) error {
var externalIPPoolController *externalippool.ExternalIPPoolController
var externalIPController *serviceexternalip.ServiceExternalIPController
var memberlistCluster *memberlist.Cluster
var localIPDetector ipassigner.LocalIPDetector

if features.DefaultFeatureGate.Enabled(features.Egress) || features.DefaultFeatureGate.Enabled(features.ServiceExternalIP) {
externalIPPoolController = externalippool.NewExternalIPPoolController(
crdClient, externalIPPoolInformer,
)
localIPDetector = ipassigner.NewLocalIPDetector()
var nodeTransportIP net.IP
if nodeConfig.NodeTransportIPv4Addr != nil {
nodeTransportIP = nodeConfig.NodeTransportIPv4Addr.IP
Expand All @@ -332,7 +329,7 @@ func run(o *Options) error {
if features.DefaultFeatureGate.Enabled(features.Egress) {
egressController, err = egress.NewEgressController(
ofClient, antreaClientProvider, crdClient, ifaceStore, routeClient, nodeConfig.Name, nodeConfig.NodeTransportInterfaceName,
memberlistCluster, egressInformer, nodeInformer, localIPDetector,
memberlistCluster, egressInformer, nodeInformer,
)
if err != nil {
return fmt.Errorf("error creating new Egress controller: %v", err)
Expand All @@ -346,7 +343,6 @@ func run(o *Options) error {
memberlistCluster,
serviceInformer,
endpointsInformer,
localIPDetector,
)
if err != nil {
return fmt.Errorf("error creating new ServiceExternalIP controller: %v", err)
Expand Down Expand Up @@ -531,7 +527,6 @@ func run(o *Options) error {

if features.DefaultFeatureGate.Enabled(features.Egress) || features.DefaultFeatureGate.Enabled(features.ServiceExternalIP) {
go externalIPPoolController.Run(stopCh)
go localIPDetector.Run(stopCh)
go memberlistCluster.Run(stopCh)
}

Expand Down
5 changes: 5 additions & 0 deletions go.mod
Expand Up @@ -27,6 +27,10 @@ require (
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.1.0
github.com/k8snetworkplumbingwg/sriov-cni v2.1.0+incompatible
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd
github.com/mdlayher/arp v0.0.0-20191213142603-f72070a231fc
github.com/mdlayher/ethernet v0.0.0-20190606142754-0394541c37b7
github.com/mdlayher/ndp v0.0.0-20210831201139-f982b8766fb5
github.com/mdlayher/raw v0.0.0-20211126142749-4eae47f3d54b
github.com/miekg/dns v1.1.43
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822
github.com/onsi/ginkgo v1.16.4
Expand Down Expand Up @@ -146,6 +150,7 @@ require (
github.com/streamrail/concurrent-map v0.0.0-20160823150647-8bf1e9bacbf6 // indirect
github.com/ti-mo/netfilter v0.3.1 // indirect
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae // indirect
gitlab.com/golang-commonmark/puny v0.0.0-20191124015043-9f83538fa04f // indirect
go.etcd.io/etcd v0.5.0-alpha.5.0.20200910180754-dd1b699fc489 // indirect
go.opencensus.io v0.22.3 // indirect
go.uber.org/atomic v1.7.0 // indirect
Expand Down
21 changes: 21 additions & 0 deletions go.sum
Expand Up @@ -356,6 +356,7 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand Down Expand Up @@ -518,10 +519,17 @@ github.com/mattn/go-shellwords v1.0.3/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vq
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 h1:I0XW9+e1XWDxdcEniV4rQAIOPUGDq67JSCiRCgGCZLI=
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/mdlayher/arp v0.0.0-20191213142603-f72070a231fc h1:m7rJJJeXrYCFpsxXYapkDW53wJCDmf9bsIXUg0HoeQY=
github.com/mdlayher/arp v0.0.0-20191213142603-f72070a231fc/go.mod h1:eOj1DDj3NAZ6yv+WafaKzY37MFZ58TdfIhQ+8nQbiis=
github.com/mdlayher/ethernet v0.0.0-20190313224307-5b5fc417d966/go.mod h1:5s5p/sMJ6sNsFl6uCh85lkFGV8kLuIYJCRJLavVJwvg=
github.com/mdlayher/ethernet v0.0.0-20190606142754-0394541c37b7 h1:lez6TS6aAau+8wXUP3G9I3TGlmPFEq2CTxBaRqY6AGE=
github.com/mdlayher/ethernet v0.0.0-20190606142754-0394541c37b7/go.mod h1:U6ZQobyTjI/tJyq2HG+i/dfSoFUt8/aZCM+GKtmFk/Y=
github.com/mdlayher/ethtool v0.0.0-20210210192532-2b88debcdd43 h1:WgyLFv10Ov49JAQI/ZLUkCZ7VJS3r74hwFIGXJsgZlY=
github.com/mdlayher/ethtool v0.0.0-20210210192532-2b88debcdd43/go.mod h1:+t7E0lkKfbBsebllff1xdTmyJt8lH37niI6kwFk9OTo=
github.com/mdlayher/genetlink v1.0.0 h1:OoHN1OdyEIkScEmRgxLEe2M9U8ClMytqA5niynLtfj0=
github.com/mdlayher/genetlink v1.0.0/go.mod h1:0rJ0h4itni50A86M2kHcgS85ttZazNt7a8H2a2cw0Gc=
github.com/mdlayher/ndp v0.0.0-20210831201139-f982b8766fb5 h1:w1Lne6x7QKrrkAviNvSd0M3NYblM1n76Pv2FsGkNzzI=
github.com/mdlayher/ndp v0.0.0-20210831201139-f982b8766fb5/go.mod h1:32w/5dDZWVSEOxyniAgKK4d7dHTuO6TCxWmUznQe3f8=
github.com/mdlayher/netlink v0.0.0-20190409211403-11939a169225/go.mod h1:eQB3mZE4aiYnlUsyGGCOpPETfdQq4Jhsgf1fk3cwQaA=
github.com/mdlayher/netlink v1.0.0/go.mod h1:KxeJAFOFLG6AjpyDkQ/iIhxygIUKD+vcwqcnu43w/+M=
github.com/mdlayher/netlink v1.1.0/go.mod h1:H4WCitaheIsdF9yOYu8CFmCgQthAPIWZmcKp9uZHgmY=
Expand All @@ -533,6 +541,10 @@ github.com/mdlayher/netlink v1.2.2-0.20210123213345-5cc92139ae3e/go.mod h1:bacnN
github.com/mdlayher/netlink v1.3.0/go.mod h1:xK/BssKuwcRXHrtN04UBkwQ6dY9VviGGuriDdoPSWys=
github.com/mdlayher/netlink v1.4.0 h1:n3ARR+Fm0dDv37dj5wSWZXDKcy+U0zwcXS3zKMnSiT0=
github.com/mdlayher/netlink v1.4.0/go.mod h1:dRJi5IABcZpBD2A3D0Mv/AiX8I9uDEu5oGkAVrekmf8=
github.com/mdlayher/raw v0.0.0-20190313224157-43dbcdd7739d/go.mod h1:r1fbeITl2xL/zLbVnNHFyOzQJTgr/3fpf1lJX/cjzR8=
github.com/mdlayher/raw v0.0.0-20190606142536-fef19f00fc18/go.mod h1:7EpbotpCmVZcu+KCX4g9WaRNuu11uyhiW7+Le1dKawg=
github.com/mdlayher/raw v0.0.0-20211126142749-4eae47f3d54b h1:MHcTarUMC4sFA7eiyR8IEJ6j2PgmgXR+B9X2IIMjh7A=
github.com/mdlayher/raw v0.0.0-20211126142749-4eae47f3d54b/go.mod h1:7EpbotpCmVZcu+KCX4g9WaRNuu11uyhiW7+Le1dKawg=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
github.com/miekg/dns v1.1.43 h1:JKfpVSCB84vrAmHzyrsxB5NAr5kLoMXZArPSw7Qlgyg=
Expand Down Expand Up @@ -746,6 +758,8 @@ github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
gitlab.com/golang-commonmark/puny v0.0.0-20191124015043-9f83538fa04f h1:Wku8eEdeJqIOFHtrfkYUByc4bCaTeA6fL0UJgfEiFMI=
gitlab.com/golang-commonmark/puny v0.0.0-20191124015043-9f83538fa04f/go.mod h1:Tiuhl+njh/JIg0uS/sOJVYi0x2HEa5rc1OAaVsb5tAs=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.5 h1:XAzx9gjCb0Rxj7EoqcClPD1d5ZBxZJk0jbuoPHenBt0=
Expand Down Expand Up @@ -843,8 +857,10 @@ golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190313220215-9f648a60d977/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190419010253-1f3472d942ba/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
Expand All @@ -865,6 +881,7 @@ golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200904194848-62affa334b73/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
Expand Down Expand Up @@ -893,6 +910,7 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
Expand All @@ -913,10 +931,12 @@ golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190321052220-f7bb7a8bee54/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190411185658-b44545bcd369/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190418153312-f0ce4c0180be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190514135907-3a4b5fb9f71f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190606122018-79a91cf218c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -949,6 +969,7 @@ golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200331124033-c3d80250170d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200602100848-8d3cce7afc34/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
4 changes: 2 additions & 2 deletions pkg/agent/controller/egress/egress_controller.go
Expand Up @@ -155,7 +155,6 @@ func NewEgressController(
cluster *memberlist.Cluster,
egressInformer crdinformers.EgressInformer,
nodeInformer coreinformers.NodeInformer,
localIPDetector ipassigner.LocalIPDetector,
) (*EgressController, error) {
c := &EgressController{
ofClient: ofClient,
Expand All @@ -172,7 +171,7 @@ func NewEgressController(
egressStates: map[string]*egressState{},
egressIPStates: map[string]*egressIPState{},
egressBindings: map[string]*egressBinding{},
localIPDetector: localIPDetector,
localIPDetector: ipassigner.NewLocalIPDetector(),
idAllocator: newIDAllocator(minEgressMark, maxEgressMark),
cluster: cluster,
}
Expand Down Expand Up @@ -292,6 +291,7 @@ func (c *EgressController) Run(stopCh <-chan struct{}) {

go c.localIPDetector.Run(stopCh)

go c.ipAssigner.Run(stopCh)
if !cache.WaitForNamedCacheSync(controllerName, stopCh, c.egressListerSynced, c.localIPDetector.HasSynced) {
return
}
Expand Down
95 changes: 43 additions & 52 deletions pkg/agent/controller/serviceexternalip/controller.go
Expand Up @@ -23,7 +23,6 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
apimachinerytypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -51,9 +50,6 @@ const (

externalIPIndex = "externalIP"
externalIPPoolIndex = "externalIPPool"

// ingressDummyDevice is the dummy device that holds the Service external IPs configured to the system by antrea-agent.
ingressDummyDevice = "antrea-ingress0"
)

type externalIPState struct {
Expand All @@ -78,9 +74,11 @@ type ServiceExternalIPController struct {
externalIPStates map[apimachinerytypes.NamespacedName]externalIPState
externalIPStatesMutex sync.RWMutex

cluster memberlist.Interface
ipAssigner ipassigner.IPAssigner
localIPDetector ipassigner.LocalIPDetector
cluster memberlist.Interface
ipAssigner ipassigner.IPAssigner

assignedIPs map[string]sets.String
assignedIPsMutex sync.Mutex
}

func NewServiceExternalIPController(
Expand All @@ -90,7 +88,6 @@ func NewServiceExternalIPController(
cluster memberlist.Interface,
serviceInformer coreinformers.ServiceInformer,
endpointsInformer coreinformers.EndpointsInformer,
localIPDetector ipassigner.LocalIPDetector,
) (*ServiceExternalIPController, error) {
c := &ServiceExternalIPController{
nodeName: nodeName,
Expand All @@ -104,9 +101,9 @@ func NewServiceExternalIPController(
endpointsLister: endpointsInformer.Lister(),
endpointsListerSynced: endpointsInformer.Informer().HasSynced,
externalIPStates: make(map[apimachinerytypes.NamespacedName]externalIPState),
localIPDetector: localIPDetector,
assignedIPs: make(map[string]sets.String),
}
ipAssigner, err := ipassigner.NewIPAssigner(nodeTransportInterface, ingressDummyDevice)
ipAssigner, err := ipassigner.NewIPAssigner(nodeTransportInterface, "")
if err != nil {
return nil, fmt.Errorf("initializing service external IP assigner failed: %v", err)
}
Expand Down Expand Up @@ -158,7 +155,6 @@ func NewServiceExternalIPController(
resyncPeriod,
)

c.localIPDetector.AddEventHandler(c.onLocalIPUpdate)
c.cluster.AddClusterEventHandler(c.enqueueServicesByExternalIPPool)
return c, nil
}
Expand Down Expand Up @@ -236,6 +232,7 @@ func (c *ServiceExternalIPController) enqueueServicesByExternalIPPool(eipName st
// workqueue.
func (c *ServiceExternalIPController) Run(stopCh <-chan struct{}) {
defer c.queue.ShutDown()
go c.ipAssigner.Run(stopCh)

klog.Infof("Starting %s", controllerName)
defer klog.Infof("Shutting down %s", controllerName)
Expand All @@ -244,50 +241,12 @@ func (c *ServiceExternalIPController) Run(stopCh <-chan struct{}) {
return
}

c.removeStaleExternalIPs()

for i := 0; i < defaultWorkers; i++ {
go wait.Until(c.worker, time.Second, stopCh)
}
<-stopCh
}

// removeStaleExternalIPs unassigns stale external IPs that shouldn't be present on this Node.
// This function will only delete IPs which are caused by Service changes when the agent on this Node was
// not running. Those IPs should be deleted caused by migration will be deleted by processNextWorkItem.
func (c *ServiceExternalIPController) removeStaleExternalIPs() {
desiredExternalIPs := sets.NewString()
services, _ := c.serviceLister.List(labels.Everything())
for _, service := range services {
if service.Spec.Type == corev1.ServiceTypeLoadBalancer &&
service.ObjectMeta.Annotations[types.ServiceExternalIPPoolAnnotationKey] != "" &&
len(service.Status.LoadBalancer.Ingress) != 0 {
desiredExternalIPs.Insert(service.Status.LoadBalancer.Ingress[0].IP)
}
}
actualExternalIPs := c.ipAssigner.AssignedIPs()
for ip := range actualExternalIPs.Difference(desiredExternalIPs) {
if err := c.ipAssigner.UnassignIP(ip); err != nil {
klog.ErrorS(err, "Failed to clean up stale service external IP", "ip", ip)
}
}
}

func (c *ServiceExternalIPController) onLocalIPUpdate(ip string, added bool) {
services, _ := c.serviceInformer.GetIndexer().ByIndex(externalIPIndex, ip)
if len(services) == 0 {
return
}
if added {
klog.Infof("Detected service external IP address %s added to this Node", ip)
} else {
klog.Infof("Detected service external IP address %s deleted from this Node", ip)
}
for _, s := range services {
c.enqueueService(s)
}
}

// worker is a long-running function that will continually call the processNextWorkItem function in
// order to read and process a message on the workqueue.
func (c *ServiceExternalIPController) worker() {
Expand Down Expand Up @@ -325,7 +284,7 @@ func (c *ServiceExternalIPController) deleteService(service apimachinerytypes.Na
if state, exist = c.externalIPStates[service]; !exist {
return nil
}
if err := c.ipAssigner.UnassignIP(state.ip); err != nil {
if err := c.unassignIP(state.ip, service); err != nil {
return err
}
delete(c.externalIPStates, service)
Expand Down Expand Up @@ -440,9 +399,41 @@ func (c *ServiceExternalIPController) syncService(key apimachinerytypes.Namespac
return err
}
}
return c.ipAssigner.AssignIP(currentExternalIP)
return c.assignIP(currentExternalIP, key)
}
return c.ipAssigner.UnassignIP(currentExternalIP)
return c.unassignIP(currentExternalIP, key)
}

func (c *ServiceExternalIPController) assignIP(ip string, service apimachinerytypes.NamespacedName) error {
c.assignedIPsMutex.Lock()
defer c.assignedIPsMutex.Unlock()
if _, ok := c.assignedIPs[ip]; !ok {
if err := c.ipAssigner.AssignIP(ip); err != nil {
return err
}
c.assignedIPs[ip] = sets.NewString(service.String())
} else {
c.assignedIPs[ip].Insert(service.String())
}
return nil
}

func (c *ServiceExternalIPController) unassignIP(ip string, service apimachinerytypes.NamespacedName) error {
c.assignedIPsMutex.Lock()
defer c.assignedIPsMutex.Unlock()
assigned, ok := c.assignedIPs[ip]
if !ok {
return nil
}
if assigned.Len() == 1 && assigned.Has(service.String()) {
if err := c.ipAssigner.UnassignIP(ip); err != nil {
return err
}
delete(c.assignedIPs, ip)
return nil
}
assigned.Delete(service.String())
return nil
}

// nodesHasHealthyServiceEndpoint returns the set of Nodes which has at least one healthy endpoint.
Expand Down

0 comments on commit a87ef55

Please sign in to comment.