Skip to content

Commit 7d6ce69

Browse files
authored
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 2085f66 commit 7d6ce69

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
@@ -17,6 +17,7 @@ import (
1717
"github.com/pkg/errors"
1818
)
1919

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

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

@@ -301,7 +305,7 @@ func createHostNCApipaNetwork(
301305
if interfaceExists, _ := networkcontainers.InterfaceExists(hostNCLoopbackAdapterName); !interfaceExists {
302306
ipconfig := cns.IPConfiguration{
303307
IPSubnet: cns.IPSubnet{
304-
IPAddress: localIPConfiguration.GatewayIPAddress,
308+
IPAddress: hnsLoopbackAdapterIPAddress,
305309
PrefixLength: localIPConfiguration.IPSubnet.PrefixLength,
306310
},
307311
GatewayIPAddress: localIPConfiguration.GatewayIPAddress,
@@ -510,7 +514,7 @@ func configureHostNCApipaEndpoint(
510514
endpointPolicies, err := configureAclSettingHostNCApipaEndpoint(
511515
protocolList,
512516
networkContainerApipaIP,
513-
hostApipaIP,
517+
hnsLoopbackAdapterIPAddress,
514518
allowNCToHostCommunication,
515519
allowHostToNCCommunication,
516520
ncPolicies)
@@ -573,6 +577,7 @@ func CreateHostNCApipaEndpoint(
573577
return endpoint.Id, nil
574578
}
575579

580+
updateGwForLocalIPConfiguration(&localIPConfiguration)
576581
if network, err = createHostNCApipaNetwork(localIPConfiguration); err != nil {
577582
logger.Errorf("[Azure CNS] Failed to create HostNCApipaNetwork. Error: %v", err)
578583
return "", err
@@ -604,6 +609,17 @@ func CreateHostNCApipaEndpoint(
604609
return endpoint.Id, nil
605610
}
606611

612+
// updateGwForLocalIPConfiguration applies change on gw IP address for apipa NW and endpoint.
613+
// 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.
614+
// This func is to overwrite the address to 169.254.128.2 when the gateway address is 169.254.128.1
615+
func updateGwForLocalIPConfiguration(localIPConfiguration *cns.IPConfiguration) {
616+
// When gw address is 169.254.128.1, should use .2 instead. If gw address is not .1, that mean this value is
617+
// configured from dnc, we should keep it
618+
if localIPConfiguration.GatewayIPAddress == "169.254.128.1" {
619+
localIPConfiguration.GatewayIPAddress = defaultHnsGwIPAddress
620+
}
621+
}
622+
607623
func getHostNCApipaEndpointName(
608624
networkContainerID string) string {
609625
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)