Skip to content

Commit

Permalink
Specify network interface for ICMP tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sebrandon1 committed Feb 1, 2024
1 parent a318cec commit b278fdb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 24 deletions.
18 changes: 14 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[0].IPs, multusNetworkInterface[0].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 Down Expand Up @@ -111,6 +112,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 +225,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
13 changes: 7 additions & 6 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 @@ -516,9 +517,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].MultusNetworkInterfaces, err = provider.GetPodIPsPerNet(
tt.args.pods[idx].GetAnnotations()[provider.CniNetworksStatusKey],
)
if err != nil {
Expand Down Expand Up @@ -567,7 +568,7 @@ var (
},
},
},
MultusIPs: map[string][]string{
MultusNetworkInterfaces: map[string][]provider.CniNetworkInterface{
"": {},
},
SkipNetTests: false,
Expand Down Expand Up @@ -605,7 +606,7 @@ var (
},
},
},
MultusIPs: map[string][]string{
MultusNetworkInterfaces: map[string][]provider.CniNetworkInterface{
"": {},
},
SkipNetTests: false,
Expand Down Expand Up @@ -643,7 +644,7 @@ var (
},
},
},
MultusIPs: map[string][]string{
MultusNetworkInterfaces: map[string][]provider.CniNetworkInterface{
"": {},
},
SkipNetTests: true,
Expand Down Expand Up @@ -681,7 +682,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] = append(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

0 comments on commit b278fdb

Please sign in to comment.