Skip to content

Commit a624d76

Browse files
authored
Merge pull request kubernetes#131428 from princepereira/automated-cherry-pick-of-#131138-upstream-release-1.32
Automated cherry pick of kubernetes#131138: Fix for HNS local endpoint was being deleted instead of the remote endpoint.
2 parents d5df907 + b23e637 commit a624d76

File tree

4 files changed

+130
-34
lines changed

4 files changed

+130
-34
lines changed

pkg/proxy/winkernel/hns.go

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -133,40 +133,43 @@ func (hns hns) getAllEndpointsByNetwork(networkName string) (map[string]*(endpoi
133133
continue
134134
}
135135

136-
// Add to map with key endpoint ID or IP address
137-
// Storing this is expensive in terms of memory, however there is a bug in Windows Server 2019 that can cause two endpoints to be created with the same IP address.
138-
// TODO: Store by IP only and remove any lookups by endpoint ID.
139-
endpointInfos[ep.Id] = &endpointInfo{
140-
ip: ep.IpConfigurations[0].IpAddress,
141-
isLocal: uint32(ep.Flags&hcn.EndpointFlagsRemoteEndpoint) == 0,
142-
macAddress: ep.MacAddress,
143-
hnsID: ep.Id,
144-
hns: hns,
145-
// only ready and not terminating endpoints were added to HNS
146-
ready: true,
147-
serving: true,
148-
terminating: false,
149-
}
150-
endpointInfos[ep.IpConfigurations[0].IpAddress] = endpointInfos[ep.Id]
136+
for index, ipConfig := range ep.IpConfigurations {
137+
138+
if index > 1 {
139+
// Expecting only ipv4 and ipv6 ipaddresses
140+
// This is highly unlikely to happen, but if it does, we should log a warning
141+
// and break out of the loop
142+
klog.Warning("Endpoint ipconfiguration holds more than 2 IP addresses.", "hnsID", ep.Id, "IP", ipConfig.IpAddress, "ipConfigCount", len(ep.IpConfigurations))
143+
break
144+
}
151145

152-
if len(ep.IpConfigurations) == 1 {
153-
continue
154-
}
146+
isLocal := uint32(ep.Flags&hcn.EndpointFlagsRemoteEndpoint) == 0
155147

156-
// If ipFamilyPolicy is RequireDualStack or PreferDualStack, then there will be 2 IPS (iPV4 and IPV6)
157-
// in the endpoint list
158-
endpointDualstack := &endpointInfo{
159-
ip: ep.IpConfigurations[1].IpAddress,
160-
isLocal: uint32(ep.Flags&hcn.EndpointFlagsRemoteEndpoint) == 0,
161-
macAddress: ep.MacAddress,
162-
hnsID: ep.Id,
163-
hns: hns,
164-
// only ready and not terminating endpoints were added to HNS
165-
ready: true,
166-
serving: true,
167-
terminating: false,
148+
if existingEp, ok := endpointInfos[ipConfig.IpAddress]; ok && isLocal {
149+
// If the endpoint is already part of the queried endpoints map and is local,
150+
// then we should not add it again to the map
151+
// This is to avoid overwriting the remote endpoint info with a local endpoint.
152+
klog.V(3).InfoS("Endpoint already exists in queried endpoints map; skipping.", "newLocalEndpoint", ep, "ipConfig", ipConfig, "existingEndpoint", existingEp)
153+
continue
154+
}
155+
156+
// Add to map with key endpoint ID or IP address
157+
// Storing this is expensive in terms of memory, however there is a bug in Windows Server 2019 and 2022 that can cause two endpoints (local and remote) to be created with the same IP address.
158+
// TODO: Store by IP only and remove any lookups by endpoint ID.
159+
epInfo := &endpointInfo{
160+
ip: ipConfig.IpAddress,
161+
isLocal: isLocal,
162+
macAddress: ep.MacAddress,
163+
hnsID: ep.Id,
164+
hns: hns,
165+
// only ready and not terminating endpoints were added to HNS
166+
ready: true,
167+
serving: true,
168+
terminating: false,
169+
}
170+
endpointInfos[ep.Id] = epInfo
171+
endpointInfos[ipConfig.IpAddress] = epInfo
168172
}
169-
endpointInfos[ep.IpConfigurations[1].IpAddress] = endpointDualstack
170173
}
171174
klog.V(3).InfoS("Queried endpoints from network", "network", networkName)
172175
klog.V(5).InfoS("Queried endpoints details", "network", networkName, "endpointInfos", endpointInfos)

pkg/proxy/winkernel/hns_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,70 @@ func TestGetAllEndpointsByNetwork(t *testing.T) {
114114
}
115115
}
116116

117+
func TestGetAllEndpointsByNetworkWithDupEP(t *testing.T) {
118+
hcnMock := getHcnMock("L2Bridge")
119+
hns := hns{hcn: hcnMock}
120+
121+
ipv4Config := &hcn.IpConfig{
122+
IpAddress: epIpAddress,
123+
}
124+
ipv6Config := &hcn.IpConfig{
125+
IpAddress: epIpv6Address,
126+
}
127+
remoteEndpoint := &hcn.HostComputeEndpoint{
128+
IpConfigurations: []hcn.IpConfig{*ipv4Config, *ipv6Config},
129+
MacAddress: epMacAddress,
130+
SchemaVersion: hcn.SchemaVersion{
131+
Major: 2,
132+
Minor: 0,
133+
},
134+
Flags: hcn.EndpointFlagsRemoteEndpoint,
135+
}
136+
Network, _ := hcnMock.GetNetworkByName(testNetwork)
137+
remoteEndpoint, err := hns.hcn.CreateEndpoint(Network, remoteEndpoint)
138+
if err != nil {
139+
t.Error(err)
140+
}
141+
142+
// Create a duplicate local endpoint with the same IP address
143+
dupLocalEndpoint := &hcn.HostComputeEndpoint{
144+
IpConfigurations: []hcn.IpConfig{*ipv4Config, *ipv6Config},
145+
MacAddress: epMacAddress,
146+
SchemaVersion: hcn.SchemaVersion{
147+
Major: 2,
148+
Minor: 0,
149+
},
150+
}
151+
152+
dupLocalEndpoint, err = hns.hcn.CreateEndpoint(Network, dupLocalEndpoint)
153+
if err != nil {
154+
t.Error(err)
155+
}
156+
157+
mapEndpointsInfo, err := hns.getAllEndpointsByNetwork(Network.Name)
158+
if err != nil {
159+
t.Error(err)
160+
}
161+
endpointIpv4, ipv4EpPresent := mapEndpointsInfo[ipv4Config.IpAddress]
162+
assert.True(t, ipv4EpPresent, "IPV4 endpoint is missing in Dualstack mode")
163+
assert.Equal(t, endpointIpv4.ip, epIpAddress, "IPV4 IP is missing in Dualstack mode")
164+
assert.Equal(t, endpointIpv4.hnsID, remoteEndpoint.Id, "HNS ID is not matching with remote endpoint")
165+
166+
endpointIpv6, ipv6EpPresent := mapEndpointsInfo[ipv6Config.IpAddress]
167+
assert.True(t, ipv6EpPresent, "IPV6 endpoint is missing in Dualstack mode")
168+
assert.Equal(t, endpointIpv6.ip, epIpv6Address, "IPV6 IP is missing in Dualstack mode")
169+
assert.Equal(t, endpointIpv6.hnsID, remoteEndpoint.Id, "HNS ID is not matching with remote endpoint")
170+
171+
err = hns.hcn.DeleteEndpoint(remoteEndpoint)
172+
if err != nil {
173+
t.Error(err)
174+
}
175+
err = hns.hcn.DeleteEndpoint(dupLocalEndpoint)
176+
if err != nil {
177+
t.Error(err)
178+
}
179+
}
180+
117181
func TestGetEndpointByID(t *testing.T) {
118182
// TODO: remove skip once the test has been fixed.
119183
t.Skip("Skipping failing test on Windows.")

pkg/proxy/winkernel/proxier.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,11 @@ func (ep *endpointInfo) DecrementRefCount() {
492492
if !ep.IsLocal() && ep.refCount != nil && *ep.refCount > 0 {
493493
*ep.refCount--
494494
}
495+
refCount := 0
496+
if ep.refCount != nil {
497+
refCount = int(*ep.refCount)
498+
}
499+
klog.V(5).InfoS("Endpoint RefCount after decrement.", "endpointInfo", ep, "refCount", refCount)
495500
}
496501

497502
func (ep *endpointInfo) Cleanup() {
@@ -1709,10 +1714,14 @@ func (proxier *Proxier) syncProxyRules() {
17091714

17101715
// remove stale endpoint refcount entries
17111716
for epIP := range proxier.terminatedEndpoints {
1712-
if epToDelete := queriedEndpoints[epIP]; epToDelete != nil && epToDelete.hnsID != "" {
1717+
klog.V(5).InfoS("Terminated endpoints ready for deletion", "epIP", epIP)
1718+
if epToDelete := queriedEndpoints[epIP]; epToDelete != nil && epToDelete.hnsID != "" && !epToDelete.IsLocal() {
17131719
if refCount := proxier.endPointsRefCount.getRefCount(epToDelete.hnsID); refCount == nil || *refCount == 0 {
1714-
klog.V(3).InfoS("Deleting unreferenced remote endpoint", "hnsID", epToDelete.hnsID)
1715-
proxier.hns.deleteEndpoint(epToDelete.hnsID)
1720+
klog.V(3).InfoS("Deleting unreferenced remote endpoint", "hnsID", epToDelete.hnsID, "IP", epToDelete.ip)
1721+
err := proxier.hns.deleteEndpoint(epToDelete.hnsID)
1722+
if err != nil {
1723+
klog.ErrorS(err, "Deleting unreferenced remote endpoint failed", "hnsID", epToDelete.hnsID)
1724+
}
17161725
}
17171726
}
17181727
}

pkg/proxy/winkernel/proxier_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,26 @@ func NewFakeProxier(syncPeriod time.Duration, minSyncPeriod time.Duration, hostn
129129
return proxier
130130
}
131131

132+
func getHcnMock(networkType string) *fakehcn.HcnMock {
133+
var remoteSubnets []*remoteSubnetInfo
134+
rs := &remoteSubnetInfo{
135+
destinationPrefix: destinationPrefix,
136+
isolationID: 4096,
137+
providerAddress: providerAddress,
138+
drMacAddress: macAddress,
139+
}
140+
remoteSubnets = append(remoteSubnets, rs)
141+
hnsNetworkInfo := &hnsNetworkInfo{
142+
id: strings.ToUpper(guid),
143+
name: testNetwork,
144+
networkType: networkType,
145+
remoteSubnets: remoteSubnets,
146+
}
147+
hnsNetwork := newHnsNetwork(hnsNetworkInfo)
148+
hcnMock := fakehcn.NewHcnMock(hnsNetwork)
149+
return hcnMock
150+
}
151+
132152
func TestCreateServiceVip(t *testing.T) {
133153
syncPeriod := 30 * time.Second
134154
proxier := NewFakeProxier(syncPeriod, syncPeriod, "testhost", netutils.ParseIPSloppy("10.0.0.1"), NETWORK_TYPE_OVERLAY)

0 commit comments

Comments
 (0)