Skip to content
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

Specify network interface for ICMP tests #1853

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions cnf-certification-test/networking/icmp/icmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ func BuildNetTestContext(pods []*provider.Pod, aIPVersion netcommons.IPVersion,
logger.Info("Skipping pod %q because it is excluded from %q connectivity tests only", put.Name, aType)
continue
}
for netKey, multusIPAddress := range put.MultusIPs {
for netKey, multusNetworkInterface := range put.MultusNetworkInterfaces {
// The first container is used to get the network namespace
processContainerIpsPerNet(put.Containers[0], netKey, multusIPAddress, netsUnderTest, aIPVersion, logger)
processContainerIpsPerNet(put.Containers[0], netKey, multusNetworkInterface.IPs, multusNetworkInterface.Interface, netsUnderTest, aIPVersion, logger)
}
continue
}

const defaultNetKey = "default"
defaultIPAddress := put.Status.PodIPs
// The first container is used to get the network namespace
processContainerIpsPerNet(put.Containers[0], defaultNetKey, netcommons.PodIPsToStringList(defaultIPAddress), netsUnderTest, aIPVersion, logger)
processContainerIpsPerNet(put.Containers[0], defaultNetKey, netcommons.PodIPsToStringList(defaultIPAddress), "", netsUnderTest, aIPVersion, logger)
}
return netsUnderTest
}
Expand All @@ -81,6 +81,7 @@ func BuildNetTestContext(pods []*provider.Pod, aIPVersion netcommons.IPVersion,
func processContainerIpsPerNet(containerID *provider.Container,
netKey string,
ipAddresses []string,
ifName string,
netsUnderTest map[string]netcommons.NetTestContext,
aIPVersion netcommons.IPVersion,
logger *log.Logger) {
Expand All @@ -103,6 +104,9 @@ func processContainerIpsPerNet(containerID *provider.Container,
entry.TesterSource.ContainerIdentifier = containerID
// if multiple interfaces are present for this network on this container/pod, pick the first one as the tester source ip
entry.TesterSource.IP = ipAddressesFiltered[firstIPIndex]
if ifName != "" {
entry.TesterSource.InterfaceName = ifName
}
// do no include tester's IP in the list of destination IPs to ping
firstIPIndex++
}
Expand All @@ -111,6 +115,10 @@ func processContainerIpsPerNet(containerID *provider.Container,
ipDestEntry := netcommons.ContainerIP{}
ipDestEntry.ContainerIdentifier = containerID
ipDestEntry.IP = aIP
// if the interface name is not empty, then add it to the destination entry
if ifName != "" {
ipDestEntry.InterfaceName = ifName
}
entry.DestTargets = append(entry.DestTargets, ipDestEntry)
}

Expand Down Expand Up @@ -220,7 +228,12 @@ func RunNetworkingTests( //nolint:funlen

// TestPing Initiates a ping test between a source container and network (1 ip) and a destination container and network (1 ip)
var TestPing = func(sourceContainerID *provider.Container, targetContainerIP netcommons.ContainerIP, count int) (results PingResults, err error) {
command := fmt.Sprintf("ping -c %d %s", count, targetContainerIP.IP)
// Specify the interface to use for the ping test (if any)
interfaceFlag := fmt.Sprintf("-I %s", targetContainerIP.InterfaceName)
if targetContainerIP.InterfaceName == "" {
interfaceFlag = ""
}
command := fmt.Sprintf("ping %s -c %d %s", interfaceFlag, count, targetContainerIP.IP)
stdout, stderr, err := crclient.ExecCommandContainerNSEnter(command, sourceContainerID)
if err != nil || stderr != "" {
results.outcome = testhelper.ERROR
Expand Down
55 changes: 39 additions & 16 deletions cnf-certification-test/networking/icmp/icmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ func TestProcessContainerIpsPerNet(t *testing.T) {
tt.args.containerID,
tt.args.netKey,
tt.args.ipAddresses,
"",
tt.args.netsUnderTest,
tt.args.aIPVersion,
log.GetLogger(),
Expand Down Expand Up @@ -407,7 +408,8 @@ func TestBuildNetTestContext(t *testing.T) {
wantNetsUnderTest: map[string]netcommons.NetTestContext{
"tnf/mynet-ipv4-0": {
TesterSource: netcommons.ContainerIP{
IP: "192.168.0.3",
IP: "192.168.0.3",
InterfaceName: "net1",
ContainerIdentifier: &provider.Container{
Container: &corev1.Container{
Name: "test1",
Expand All @@ -421,7 +423,8 @@ func TestBuildNetTestContext(t *testing.T) {
},
DestTargets: []netcommons.ContainerIP{
{
IP: "192.168.0.4",
IP: "192.168.0.4",
InterfaceName: "net1",
ContainerIdentifier: &provider.Container{
Container: &corev1.Container{
Name: "test2",
Expand All @@ -438,7 +441,8 @@ func TestBuildNetTestContext(t *testing.T) {
"tnf/mynet-ipv4-1": {
TesterContainerNodeName: "",
TesterSource: netcommons.ContainerIP{
IP: "192.168.1.3",
IP: "192.168.1.3",
InterfaceName: "net2",
ContainerIdentifier: &provider.Container{
Container: &corev1.Container{
Name: "test1",
Expand All @@ -452,7 +456,8 @@ func TestBuildNetTestContext(t *testing.T) {
},
DestTargets: []netcommons.ContainerIP{
{
IP: "192.168.1.4",
IP: "192.168.1.4",
InterfaceName: "net2",
ContainerIdentifier: &provider.Container{
Container: &corev1.Container{
Name: "test2",
Expand All @@ -477,7 +482,8 @@ func TestBuildNetTestContext(t *testing.T) {
wantNetsUnderTest: map[string]netcommons.NetTestContext{
"tnf/mynet-ipv4-0": {
TesterSource: netcommons.ContainerIP{
IP: "192.168.0.3",
IP: "192.168.0.3",
InterfaceName: "net1",
ContainerIdentifier: &provider.Container{
Container: &corev1.Container{
Name: "test1",
Expand All @@ -494,7 +500,8 @@ func TestBuildNetTestContext(t *testing.T) {
"tnf/mynet-ipv4-1": {
TesterContainerNodeName: "",
TesterSource: netcommons.ContainerIP{
IP: "192.168.1.3",
IP: "192.168.1.3",
InterfaceName: "net2",
ContainerIdentifier: &provider.Container{
Container: &corev1.Container{
Name: "test1",
Expand All @@ -516,11 +523,9 @@ func TestBuildNetTestContext(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for idx := range tt.args.pods {
tt.args.pods[idx].MultusIPs = make(map[string][]string)
tt.args.pods[idx].MultusNetworkInterfaces = make(map[string]provider.CniNetworkInterface)
var err error
tt.args.pods[idx].MultusIPs, err = provider.GetPodIPsPerNet(
tt.args.pods[idx].GetAnnotations()[provider.CniNetworksStatusKey],
)
tt.args.pods[idx].MultusNetworkInterfaces, err = provider.GetPodIPsPerNet(tt.args.pods[idx].GetAnnotations()[provider.CniNetworksStatusKey])
if err != nil {
fmt.Printf("Could not decode networks-status annotation")
}
Expand Down Expand Up @@ -567,8 +572,17 @@ var (
},
},
},
MultusIPs: map[string][]string{
"": {},
MultusNetworkInterfaces: map[string]provider.CniNetworkInterface{
"tnf/mynet-ipv4-0": {
Interface: "net1",
Name: "mynet-ipv4-0",
IPs: []string{"192.168.0.3"},
},
"tnf/mynet-ipv4-1": {
Interface: "net2",
Name: "mynet-ipv4-1",
IPs: []string{"192.168.1.3"},
},
},
SkipNetTests: false,
SkipMultusNetTests: false,
Expand Down Expand Up @@ -605,8 +619,17 @@ var (
},
},
},
MultusIPs: map[string][]string{
"": {},
MultusNetworkInterfaces: map[string]provider.CniNetworkInterface{
"tnf/mynet-ipv4-0": {
Interface: "net1",
Name: "mynet-ipv4-0",
IPs: []string{"192.168.0.4"},
},
"tnf/mynet-ipv4-1": {
Interface: "net2",
Name: "mynet-ipv4-1",
IPs: []string{"192.168.1.4"},
},
},
SkipNetTests: false,
SkipMultusNetTests: false,
Expand Down Expand Up @@ -643,7 +666,7 @@ var (
},
},
},
MultusIPs: map[string][]string{
MultusNetworkInterfaces: map[string]provider.CniNetworkInterface{
"": {},
},
SkipNetTests: true,
Expand Down Expand Up @@ -681,7 +704,7 @@ var (
},
},
},
MultusIPs: map[string][]string{
MultusNetworkInterfaces: map[string]provider.CniNetworkInterface{
"": {},
},
SkipNetTests: false,
Expand Down
4 changes: 3 additions & 1 deletion cnf-certification-test/networking/netcommons/netcommons.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ type ContainerIP struct {
IP string
// targetContainerIdentifier container identifier including namespace, pod name, container name, node name, and container UID
ContainerIdentifier *provider.Container
// interfaceName is the interface we want to target for the ping test
InterfaceName string
}

// String displays the NetTestContext data structure
func (testContext NetTestContext) String() string {
func (testContext *NetTestContext) String() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("From initiating container: %s\n", testContext.TesterSource.String()))
if len(testContext.DestTargets) == 0 {
Expand Down
14 changes: 7 additions & 7 deletions pkg/provider/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ const (

type Pod struct {
*corev1.Pod
Containers []*Container
MultusIPs map[string][]string
MultusPCIs []string
SkipNetTests bool
SkipMultusNetTests bool
Containers []*Container
MultusNetworkInterfaces map[string]CniNetworkInterface
MultusPCIs []string
SkipNetTests bool
SkipMultusNetTests bool
}

func NewPod(aPod *corev1.Pod) (out Pod) {
var err error
out.Pod = aPod
out.MultusIPs = make(map[string][]string)
out.MultusIPs, err = GetPodIPsPerNet(aPod.GetAnnotations()[CniNetworksStatusKey])
out.MultusNetworkInterfaces = make(map[string]CniNetworkInterface)
out.MultusNetworkInterfaces, err = GetPodIPsPerNet(aPod.GetAnnotations()[CniNetworksStatusKey])
if err != nil {
log.Error("Could not get IPs for Pod %q (namespace %q), err: %v", aPod.Name, aPod.Namespace, err)
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ type MachineConfig struct {
} `json:"config"`
}

type cniNetworkInterface struct {
type CniNetworkInterface struct {
Name string `json:"name"`
Interface string `json:"interface"`
IPs []string `json:"ips"`
Expand Down Expand Up @@ -447,13 +447,13 @@ func GetRuntimeUID(cs *corev1.ContainerStatus) (runtime, uid string) {
// GetPodIPsPerNet gets the IPs of a pod.
// CNI annotation "k8s.v1.cni.cncf.io/networks-status".
// Returns (ips, error).
func GetPodIPsPerNet(annotation string) (ips map[string][]string, err error) {
func GetPodIPsPerNet(annotation string) (ips map[string]CniNetworkInterface, err error) {
// This is a map indexed with the network name (network attachment) and
// listing all the IPs created in this subnet and belonging to the pod namespace
// The list of ips pr net is parsed from the content of the "k8s.v1.cni.cncf.io/networks-status" annotation.
ips = make(map[string][]string)
ips = make(map[string]CniNetworkInterface)

var cniInfo []cniNetworkInterface
var cniInfo []CniNetworkInterface
err = json.Unmarshal([]byte(annotation), &cniInfo)
if err != nil {
return nil, fmt.Errorf("could not unmarshal network-status annotation, err: %v", err)
Expand All @@ -462,14 +462,14 @@ func GetPodIPsPerNet(annotation string) (ips map[string][]string, err error) {
// Otherwise add all non default interfaces
for _, cniInterface := range cniInfo {
if !cniInterface.Default {
ips[cniInterface.Name] = cniInterface.IPs
ips[cniInterface.Name] = cniInterface
}
}
return ips, nil
}

func GetPciPerPod(annotation string) (pciAddr []string, err error) {
var cniInfo []cniNetworkInterface
var cniInfo []CniNetworkInterface
err = json.Unmarshal([]byte(annotation), &cniInfo)
if err != nil {
return nil, fmt.Errorf("could not unmarshal network-status annotation, err: %v", err)
Expand Down
Loading