Skip to content

Commit

Permalink
change apipa network gw address from .1 to .2 (#2933)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ZetaoZhuang authored Oct 22, 2024
1 parent 2085f66 commit 7d6ce69
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
20 changes: 18 additions & 2 deletions cns/hnsclient/hnsclient_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/pkg/errors"
)

// TODO redesign hnsclient on windows
const (
// Name of the external hns network
ExtHnsNetworkName = "ext"
Expand Down Expand Up @@ -53,6 +54,9 @@ const (
// Name of the loopback adapter needed to create Host NC apipa network
hostNCLoopbackAdapterName = "LoopbackAdapterHostNCConnectivity"

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

Expand Down Expand Up @@ -301,7 +305,7 @@ func createHostNCApipaNetwork(
if interfaceExists, _ := networkcontainers.InterfaceExists(hostNCLoopbackAdapterName); !interfaceExists {
ipconfig := cns.IPConfiguration{
IPSubnet: cns.IPSubnet{
IPAddress: localIPConfiguration.GatewayIPAddress,
IPAddress: hnsLoopbackAdapterIPAddress,
PrefixLength: localIPConfiguration.IPSubnet.PrefixLength,
},
GatewayIPAddress: localIPConfiguration.GatewayIPAddress,
Expand Down Expand Up @@ -510,7 +514,7 @@ func configureHostNCApipaEndpoint(
endpointPolicies, err := configureAclSettingHostNCApipaEndpoint(
protocolList,
networkContainerApipaIP,
hostApipaIP,
hnsLoopbackAdapterIPAddress,
allowNCToHostCommunication,
allowHostToNCCommunication,
ncPolicies)
Expand Down Expand Up @@ -573,6 +577,7 @@ func CreateHostNCApipaEndpoint(
return endpoint.Id, nil
}

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

// updateGwForLocalIPConfiguration applies change on gw IP address for apipa NW and endpoint.
// 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.
// This func is to overwrite the address to 169.254.128.2 when the gateway address is 169.254.128.1
func updateGwForLocalIPConfiguration(localIPConfiguration *cns.IPConfiguration) {
// When gw address is 169.254.128.1, should use .2 instead. If gw address is not .1, that mean this value is
// configured from dnc, we should keep it
if localIPConfiguration.GatewayIPAddress == "169.254.128.1" {
localIPConfiguration.GatewayIPAddress = defaultHnsGwIPAddress
}
}

func getHostNCApipaEndpointName(
networkContainerID string) string {
return hostNCApipaEndpointNamePrefix + "-" + networkContainerID
Expand Down
35 changes: 35 additions & 0 deletions cns/hnsclient/hnsclient_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package hnsclient

import (
"testing"

"github.com/Azure/azure-container-networking/cns"
"github.com/stretchr/testify/assert"
)

func TestAdhocAdjustIPConfig(t *testing.T) {
tests := []struct {
name string
ipConfig cns.IPConfiguration
expected cns.IPConfiguration
}{
{
name: "expect no change when gw address is not 169.254.128.1",
ipConfig: cns.IPConfiguration{GatewayIPAddress: "169.254.128.3"},
expected: cns.IPConfiguration{GatewayIPAddress: "169.254.128.3"},
},
{
name: "expect default gw address is set when gw address is 169.254.128.1",
ipConfig: cns.IPConfiguration{GatewayIPAddress: "169.254.128.1"},
expected: cns.IPConfiguration{GatewayIPAddress: "169.254.128.2"},
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
updateGwForLocalIPConfiguration(&tt.ipConfig)
assert.Equal(t, tt.expected.GatewayIPAddress, tt.ipConfig.GatewayIPAddress)
})
}
}

0 comments on commit 7d6ce69

Please sign in to comment.