Skip to content

change apipa network gw address from .1 to .2 #2933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Oct 23, 2024
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)
})
}
}
Loading