-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up stale IP addresses on Antrea host gateway
When a Node leaves a K8s cluster and joins it again later, it is likely that it will receive a different Pod CIDR. If we simply add the new gateway IP (first IP in the CIDR) to the host gateway interface without removing the previous one, we have observed that it can lead to connectivity issues. This commit ensures that all stale IPs are removed when configuring the gateway interface on an Agent restart. Link-local addresses are left untouched. An integration test is added for both Linux & Windows. Fixes #1685
- Loading branch information
1 parent
0b3304c
commit a41787f
Showing
6 changed files
with
375 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2021 Antrea Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package agent | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"net" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/vmware-tanzu/antrea/pkg/agent/util" | ||
) | ||
|
||
func init() { | ||
rand.Seed(time.Now().UTC().UnixNano()) | ||
} | ||
|
||
func addrEqual(addr1, addr2 *net.IPNet) bool { | ||
size1, _ := addr1.Mask.Size() | ||
size2, _ := addr2.Mask.Size() | ||
return addr1.IP.Equal(addr2.IP) && size1 == size2 | ||
} | ||
|
||
func TestConfigureLinkAddresses(t *testing.T) { | ||
// #nosec G404: random number generator not used for security purposes | ||
suffix := rand.Uint32() | ||
ifaceName := fmt.Sprintf("test%x", suffix) | ||
createTestInterface(t, ifaceName) | ||
defer deleteTestInterface(t, ifaceName) | ||
ifaceIdx := setTestInterfaceUp(t, ifaceName) | ||
|
||
addrs := getTestInterfaceAddresses(t, ifaceName) | ||
t.Logf("Found the following initial addresses: %v", addrs) | ||
nAddrs := len(addrs) | ||
// there can be up to one IPv6 link-local address and one IPv4 | ||
// link-local address (on Windows) | ||
assert.LessOrEqual(t, nAddrs, 2) | ||
|
||
_, dummyAddr, _ := net.ParseCIDR("192.0.2.0/24") | ||
|
||
addTestInterfaceAddress(t, ifaceName, dummyAddr) | ||
addrs2 := getTestInterfaceAddresses(t, ifaceName) | ||
nAddrs2 := len(addrs2) | ||
assert.Equal(t, nAddrs+1, nAddrs2) | ||
|
||
_, ipAddr, _ := net.ParseCIDR("192.0.3.0/24") | ||
err := util.ConfigureLinkAddresses(ifaceIdx, []*net.IPNet{ipAddr}) | ||
require.NoError(t, err) | ||
|
||
addrs3 := getTestInterfaceAddresses(t, ifaceName) | ||
nAddrs3 := len(addrs3) | ||
assert.Equal(t, nAddrs+1, nAddrs3) | ||
|
||
foundDummy := false | ||
foundActual := false | ||
for _, addr := range addrs3 { | ||
switch { | ||
case addrEqual(addr, dummyAddr): | ||
foundDummy = true | ||
case addrEqual(addr, ipAddr): | ||
foundActual = true | ||
} | ||
} | ||
assert.True(t, foundActual, "IP address was not assigned to test interface") | ||
assert.False(t, foundDummy, "Dummy IP address should have been removed from test interface") | ||
} |
Oops, something went wrong.