@@ -184,6 +184,17 @@ func newEndpointInfo(ip string, port uint16, isLocal bool, hns HostNetworkServic
184
184
return info
185
185
}
186
186
187
+ func newSourceVIP (hns HostNetworkService , network string , ip string , mac string , providerAddress string ) (* endpointsInfo , error ) {
188
+ hnsEndpoint := & endpointsInfo {
189
+ ip : ip ,
190
+ isLocal : true ,
191
+ macAddress : mac ,
192
+ providerAddress : providerAddress ,
193
+ }
194
+ ep , err := hns .createEndpoint (hnsEndpoint , network )
195
+ return ep , err
196
+ }
197
+
187
198
func (ep * endpointsInfo ) Cleanup () {
188
199
Log (ep , "Endpoint Cleanup" , 3 )
189
200
ep .refCount --
@@ -544,11 +555,27 @@ func NewProxier(
544
555
}
545
556
}
546
557
558
+ klog .V (3 ).Infof ("Cleaning up old HNS policy lists" )
559
+ deleteAllHnsLoadBalancerPolicy ()
560
+
561
+ // Get HNS network information
547
562
hnsNetworkInfo , err := hns .getNetworkByName (hnsNetworkName )
548
- if err != nil {
549
- klog .Errorf ("Unable to find Hns Network specified by %s. Please check environment variable KUBE_NETWORK or network-name flag" , hnsNetworkName )
550
- return nil , err
563
+ for err != nil {
564
+ klog .Errorf ("Unable to find HNS Network specified by %s. Please check network name and CNI deployment" , hnsNetworkName )
565
+ time .Sleep (1 * time .Second )
566
+ hnsNetworkInfo , err = hns .getNetworkByName (hnsNetworkName )
567
+ }
568
+
569
+ // Network could have been detected before Remote Subnet Routes are applied or ManagementIP is updated
570
+ // Sleep and update the network to include new information
571
+ if hnsNetworkInfo .networkType == "Overlay" {
572
+ time .Sleep (10 * time .Second )
573
+ hnsNetworkInfo , err = hns .getNetworkByName (hnsNetworkName )
574
+ if err != nil {
575
+ return nil , fmt .Errorf ("Could not find HNS network %s" , hnsNetworkName )
576
+ }
551
577
}
578
+
552
579
klog .V (1 ).Infof ("Hns Network loaded with info = %v" , hnsNetworkInfo )
553
580
isDSR := config .EnableDSR
554
581
if isDSR && ! utilfeature .DefaultFeatureGate .Enabled (genericfeatures .WinDSR ) {
@@ -588,20 +615,6 @@ func NewProxier(
588
615
if len (hostMac ) == 0 {
589
616
return nil , fmt .Errorf ("Could not find host mac address for %s" , nodeIP )
590
617
}
591
-
592
- existingSourceVip , _ := hns .getEndpointByIpAddress (sourceVip , hnsNetworkName )
593
- if existingSourceVip == nil {
594
- hnsEndpoint := & endpointsInfo {
595
- ip : sourceVip ,
596
- isLocal : true ,
597
- macAddress : hostMac ,
598
- providerAddress : nodeIP .String (),
599
- }
600
- _ , err = hns .createEndpoint (hnsEndpoint , hnsNetworkName )
601
- if err != nil {
602
- return nil , fmt .Errorf ("Source Vip endpoint creation failed: %v" , err )
603
- }
604
- }
605
618
}
606
619
607
620
proxier := & Proxier {
@@ -838,6 +851,25 @@ func (proxier *Proxier) OnEndpointsSynced() {
838
851
proxier .syncProxyRules ()
839
852
}
840
853
854
+ func (proxier * Proxier ) cleanupAllPolicies () {
855
+ for svcName , svcInfo := range proxier .serviceMap {
856
+ svcInfo .cleanupAllPolicies (proxier .endpointsMap [svcName ])
857
+ }
858
+ }
859
+
860
+ func isNetworkNotFoundError (err error ) bool {
861
+ if err == nil {
862
+ return false
863
+ }
864
+ if _ , ok := err .(hcn.NetworkNotFoundError ); ok {
865
+ return true
866
+ }
867
+ if _ , ok := err .(hcsshim.NetworkNotFoundError ); ok {
868
+ return true
869
+ }
870
+ return false
871
+ }
872
+
841
873
// <endpointsMap> is updated by this function (based on the given changes).
842
874
// <changes> map is cleared after applying them.
843
875
func (proxier * Proxier ) updateEndpointsMap () (result updateEndpointMapResult ) {
@@ -968,6 +1000,20 @@ func (proxier *Proxier) syncProxyRules() {
968
1000
return
969
1001
}
970
1002
1003
+ hnsNetworkName := proxier .network .name
1004
+ hns := proxier .hns
1005
+
1006
+ prevNetworkID := proxier .network .id
1007
+ updatedNetwork , err := hns .getNetworkByName (hnsNetworkName )
1008
+ if updatedNetwork == nil || updatedNetwork .id != prevNetworkID || isNetworkNotFoundError (err ) {
1009
+ klog .Infof ("The HNS network %s is not present or has changed since the last sync. Please check the CNI deployment" , hnsNetworkName )
1010
+ proxier .cleanupAllPolicies ()
1011
+ if updatedNetwork != nil {
1012
+ proxier .network = * updatedNetwork
1013
+ }
1014
+ return
1015
+ }
1016
+
971
1017
// We assume that if this was called, we really want to sync them,
972
1018
// even if nothing changed in the meantime. In other words, callers are
973
1019
// responsible for detecting no-op changes and not calling this function.
@@ -983,6 +1029,17 @@ func (proxier *Proxier) syncProxyRules() {
983
1029
}
984
1030
}
985
1031
1032
+ if proxier .network .networkType == "Overlay" {
1033
+ existingSourceVip , err := hns .getEndpointByIpAddress (proxier .sourceVip , hnsNetworkName )
1034
+ if existingSourceVip == nil {
1035
+ _ , err = newSourceVIP (hns , hnsNetworkName , proxier .sourceVip , proxier .hostMac , proxier .nodeIP .String ())
1036
+ }
1037
+ if err != nil {
1038
+ klog .Errorf ("Source Vip endpoint creation failed: %v" , err )
1039
+ return
1040
+ }
1041
+ }
1042
+
986
1043
klog .V (3 ).Infof ("Syncing Policies" )
987
1044
988
1045
// Program HNS by adding corresponding policies for each service.
@@ -992,8 +1049,6 @@ func (proxier *Proxier) syncProxyRules() {
992
1049
continue
993
1050
}
994
1051
995
- hnsNetworkName := proxier .network .name
996
- hns := proxier .hns
997
1052
if proxier .network .networkType == "Overlay" {
998
1053
serviceVipEndpoint , _ := hns .getEndpointByIpAddress (svcInfo .clusterIP .String (), hnsNetworkName )
999
1054
if serviceVipEndpoint == nil {
@@ -1055,7 +1110,9 @@ func (proxier *Proxier) syncProxyRules() {
1055
1110
networkName := proxier .network .name
1056
1111
updatedNetwork , err := hns .getNetworkByName (networkName )
1057
1112
if err != nil {
1058
- klog .Fatalf ("Failed to get network %v: %v" , networkName , err )
1113
+ klog .Errorf ("Unable to find HNS Network specified by %s. Please check network name and CNI deployment" , hnsNetworkName )
1114
+ proxier .cleanupAllPolicies ()
1115
+ return
1059
1116
}
1060
1117
proxier .network = * updatedNetwork
1061
1118
var providerAddress string
0 commit comments