Skip to content

Commit f532d5c

Browse files
authored
Merge pull request kubernetes#78612 from ksubrmnn/retry
Kube-Proxy wait when HNS network not found
2 parents 4b4420f + 39aa6ab commit f532d5c

File tree

3 files changed

+83
-26
lines changed

3 files changed

+83
-26
lines changed

pkg/proxy/winkernel/hnsV1.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (hns hnsV1) getEndpointByIpAddress(ip string, networkName string) (*endpoin
9696
func (hns hnsV1) createEndpoint(ep *endpointsInfo, networkName string) (*endpointsInfo, error) {
9797
hnsNetwork, err := hcsshim.GetHNSNetworkByName(networkName)
9898
if err != nil {
99-
return nil, fmt.Errorf("Could not find network %s: %v", networkName, err)
99+
return nil, err
100100
}
101101
hnsEndpoint := &hcsshim.HNSEndpoint{
102102
MacAddress: ep.macAddress,
@@ -112,13 +112,13 @@ func (hns hnsV1) createEndpoint(ep *endpointsInfo, networkName string) (*endpoin
112112
}
113113
paPolicyJson, err := json.Marshal(paPolicy)
114114
if err != nil {
115-
return nil, fmt.Errorf("PA Policy creation failed: %v", err)
115+
return nil, err
116116
}
117117
hnsEndpoint.Policies = append(hnsEndpoint.Policies, paPolicyJson)
118118
}
119119
createdEndpoint, err = hnsNetwork.CreateRemoteEndpoint(hnsEndpoint)
120120
if err != nil {
121-
return nil, fmt.Errorf("Remote endpoint creation failed: %v", err)
121+
return nil, err
122122
}
123123

124124
} else {

pkg/proxy/winkernel/hnsV2.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (hns hnsV2) getEndpointByIpAddress(ip string, networkName string) (*endpoin
104104
func (hns hnsV2) createEndpoint(ep *endpointsInfo, networkName string) (*endpointsInfo, error) {
105105
hnsNetwork, err := hcn.GetNetworkByName(networkName)
106106
if err != nil {
107-
return nil, fmt.Errorf("Could not find network %s: %v", networkName, err)
107+
return nil, err
108108
}
109109
var flags hcn.EndpointFlags
110110
if !ep.isLocal {
@@ -141,12 +141,12 @@ func (hns hnsV2) createEndpoint(ep *endpointsInfo, networkName string) (*endpoin
141141
}
142142
createdEndpoint, err = hnsNetwork.CreateRemoteEndpoint(hnsEndpoint)
143143
if err != nil {
144-
return nil, fmt.Errorf("Remote endpoint creation failed: %v", err)
144+
return nil, err
145145
}
146146
} else {
147147
createdEndpoint, err = hnsNetwork.CreateEndpoint(hnsEndpoint)
148148
if err != nil {
149-
return nil, fmt.Errorf("Local endpoint creation failed: %v", err)
149+
return nil, err
150150
}
151151
}
152152
return &endpointsInfo{

pkg/proxy/winkernel/proxier.go

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,17 @@ func newEndpointInfo(ip string, port uint16, isLocal bool, hns HostNetworkServic
184184
return info
185185
}
186186

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+
187198
func (ep *endpointsInfo) Cleanup() {
188199
Log(ep, "Endpoint Cleanup", 3)
189200
ep.refCount--
@@ -544,11 +555,27 @@ func NewProxier(
544555
}
545556
}
546557

558+
klog.V(3).Infof("Cleaning up old HNS policy lists")
559+
deleteAllHnsLoadBalancerPolicy()
560+
561+
// Get HNS network information
547562
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+
}
551577
}
578+
552579
klog.V(1).Infof("Hns Network loaded with info = %v", hnsNetworkInfo)
553580
isDSR := config.EnableDSR
554581
if isDSR && !utilfeature.DefaultFeatureGate.Enabled(genericfeatures.WinDSR) {
@@ -588,20 +615,6 @@ func NewProxier(
588615
if len(hostMac) == 0 {
589616
return nil, fmt.Errorf("Could not find host mac address for %s", nodeIP)
590617
}
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-
}
605618
}
606619

607620
proxier := &Proxier{
@@ -838,6 +851,25 @@ func (proxier *Proxier) OnEndpointsSynced() {
838851
proxier.syncProxyRules()
839852
}
840853

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+
841873
// <endpointsMap> is updated by this function (based on the given changes).
842874
// <changes> map is cleared after applying them.
843875
func (proxier *Proxier) updateEndpointsMap() (result updateEndpointMapResult) {
@@ -968,6 +1000,20 @@ func (proxier *Proxier) syncProxyRules() {
9681000
return
9691001
}
9701002

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+
9711017
// We assume that if this was called, we really want to sync them,
9721018
// even if nothing changed in the meantime. In other words, callers are
9731019
// responsible for detecting no-op changes and not calling this function.
@@ -983,6 +1029,17 @@ func (proxier *Proxier) syncProxyRules() {
9831029
}
9841030
}
9851031

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+
9861043
klog.V(3).Infof("Syncing Policies")
9871044

9881045
// Program HNS by adding corresponding policies for each service.
@@ -992,8 +1049,6 @@ func (proxier *Proxier) syncProxyRules() {
9921049
continue
9931050
}
9941051

995-
hnsNetworkName := proxier.network.name
996-
hns := proxier.hns
9971052
if proxier.network.networkType == "Overlay" {
9981053
serviceVipEndpoint, _ := hns.getEndpointByIpAddress(svcInfo.clusterIP.String(), hnsNetworkName)
9991054
if serviceVipEndpoint == nil {
@@ -1055,7 +1110,9 @@ func (proxier *Proxier) syncProxyRules() {
10551110
networkName := proxier.network.name
10561111
updatedNetwork, err := hns.getNetworkByName(networkName)
10571112
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
10591116
}
10601117
proxier.network = *updatedNetwork
10611118
var providerAddress string

0 commit comments

Comments
 (0)