Skip to content

Commit 42826b8

Browse files
committed
change apipa network gw address from .1 to .2 (#2933)
* change gw address to .2 * refactor and add UTs * fix lint * fix loopback adapter default gw address * remote address should always be .1 * address comment * adjust comment * ddress comment * fix func comment * fix loopback adapter gw
1 parent b29bdac commit 42826b8

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

cns/hnsclient/hnsclient_windows.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/Microsoft/hcsshim/hcn"
1717
)
1818

19+
// TODO redesign hnsclient on windows
1920
const (
2021
// Name of the external hns network
2122
ExtHnsNetworkName = "ext"
@@ -52,6 +53,9 @@ const (
5253
// Name of the loopback adapter needed to create Host NC apipa network
5354
hostNCLoopbackAdapterName = "LoopbackAdapterHostNCConnectivity"
5455

56+
// HNS rehydration issue requires this GW to be different than the loopback adapter ip, so we set it to .2
57+
defaultHnsGwIPAddress = "169.254.128.2"
58+
hnsLoopbackAdapterIPAddress = "169.254.128.1"
5559
// protocolTCP indicates the TCP protocol identifier in HCN
5660
protocolTCP = "6"
5761

@@ -297,7 +301,7 @@ func createHostNCApipaNetwork(
297301
if interfaceExists, _ := networkcontainers.InterfaceExists(hostNCLoopbackAdapterName); !interfaceExists {
298302
ipconfig := cns.IPConfiguration{
299303
IPSubnet: cns.IPSubnet{
300-
IPAddress: localIPConfiguration.GatewayIPAddress,
304+
IPAddress: hnsLoopbackAdapterIPAddress,
301305
PrefixLength: localIPConfiguration.IPSubnet.PrefixLength,
302306
},
303307
GatewayIPAddress: localIPConfiguration.GatewayIPAddress,
@@ -506,7 +510,7 @@ func configureHostNCApipaEndpoint(
506510
endpointPolicies, err := configureAclSettingHostNCApipaEndpoint(
507511
protocolList,
508512
networkContainerApipaIP,
509-
hostApipaIP,
513+
hnsLoopbackAdapterIPAddress,
510514
allowNCToHostCommunication,
511515
allowHostToNCCommunication,
512516
ncPolicies)
@@ -569,6 +573,7 @@ func CreateHostNCApipaEndpoint(
569573
return endpoint.Id, nil
570574
}
571575

576+
updateGwForLocalIPConfiguration(&localIPConfiguration)
572577
if network, err = createHostNCApipaNetwork(localIPConfiguration); err != nil {
573578
logger.Errorf("[Azure CNS] Failed to create HostNCApipaNetwork. Error: %v", err)
574579
return "", err
@@ -600,6 +605,17 @@ func CreateHostNCApipaEndpoint(
600605
return endpoint.Id, nil
601606
}
602607

608+
// updateGwForLocalIPConfiguration applies change on gw IP address for apipa NW and endpoint.
609+
// Currently, cns using the same ip address "169.254.128.1" for both apipa gw and loopback adapter. This cause conflict issue when hns get restarted and not able to rehydrate the apipa endpoints.
610+
// This func is to overwrite the address to 169.254.128.2 when the gateway address is 169.254.128.1
611+
func updateGwForLocalIPConfiguration(localIPConfiguration *cns.IPConfiguration) {
612+
// When gw address is 169.254.128.1, should use .2 instead. If gw address is not .1, that mean this value is
613+
// configured from dnc, we should keep it
614+
if localIPConfiguration.GatewayIPAddress == "169.254.128.1" {
615+
localIPConfiguration.GatewayIPAddress = defaultHnsGwIPAddress
616+
}
617+
}
618+
603619
func getHostNCApipaEndpointName(
604620
networkContainerID string) string {
605621
return hostNCApipaEndpointNamePrefix + "-" + networkContainerID
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package hnsclient
2+
3+
import (
4+
"testing"
5+
6+
"github.com/Azure/azure-container-networking/cns"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestAdhocAdjustIPConfig(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
ipConfig cns.IPConfiguration
14+
expected cns.IPConfiguration
15+
}{
16+
{
17+
name: "expect no change when gw address is not 169.254.128.1",
18+
ipConfig: cns.IPConfiguration{GatewayIPAddress: "169.254.128.3"},
19+
expected: cns.IPConfiguration{GatewayIPAddress: "169.254.128.3"},
20+
},
21+
{
22+
name: "expect default gw address is set when gw address is 169.254.128.1",
23+
ipConfig: cns.IPConfiguration{GatewayIPAddress: "169.254.128.1"},
24+
expected: cns.IPConfiguration{GatewayIPAddress: "169.254.128.2"},
25+
},
26+
}
27+
28+
for _, tt := range tests {
29+
tt := tt
30+
t.Run(tt.name, func(t *testing.T) {
31+
updateGwForLocalIPConfiguration(&tt.ipConfig)
32+
assert.Equal(t, tt.expected.GatewayIPAddress, tt.ipConfig.GatewayIPAddress)
33+
})
34+
}
35+
}

0 commit comments

Comments
 (0)