Skip to content

Commit

Permalink
Improve GetSriovNicIPs error message
Browse files Browse the repository at this point in the history
In case of error, it is useful to know the reason why it
was not possible to get the IP address of SR-IOV interface.

Add description to each returned error and return an error
in case the requested interface is not found.

Add "github.com/stretchr/testify/assert" dependency adn
unit tests for GetSriovNicIPs

Signed-off-by: Andrea Panattoni <[email protected]>
  • Loading branch information
zeeke committed May 5, 2022
1 parent 5cbf1af commit e6926a0
Show file tree
Hide file tree
Showing 17 changed files with 5,046 additions and 3 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/openshift/machine-config-operator v0.0.1-0.20201023110058-6c8bd9b2915c
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.1.1
github.com/stretchr/testify v1.6.1
github.com/vishvananda/netlink v1.1.0
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
Expand Down Expand Up @@ -103,6 +104,7 @@ require (
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/nxadm/tail v1.4.4 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.7.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.10.0 // indirect
Expand Down
13 changes: 10 additions & 3 deletions test/util/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,25 @@ func GetNicsByPrefix(pod *k8sv1.Pod, ifcPrefix string) ([]string, error) {
// GetSriovNicIPs returns the list of ip addresses related to the given
// interface name for the given pod.
func GetSriovNicIPs(pod *k8sv1.Pod, ifcName string) ([]string, error) {

networksStatus, ok := pod.ObjectMeta.Annotations["k8s.v1.cni.cncf.io/networks-status"]
if !ok {
return nil, fmt.Errorf("pod [%s] has no annotation `k8s.v1.cni.cncf.io/networks-status`", pod.Name)
}

var nets []Network
err := json.Unmarshal([]byte(pod.ObjectMeta.Annotations["k8s.v1.cni.cncf.io/networks-status"]), &nets)
err := json.Unmarshal([]byte(networksStatus), &nets)
if err != nil {
return nil, err
return nil, fmt.Errorf("can't unmarshal annotation `k8s.v1.cni.cncf.io/networks-status`: %w", err)
}
for _, net := range nets {
if net.Interface != ifcName {
continue
}
return net.Ips, nil
}
return nil, nil

return nil, fmt.Errorf("interface [%s] not found in pod annotation", ifcName)
}

// Return a definition of a macvlan NetworkAttachmentDefinition
Expand Down
76 changes: 76 additions & 0 deletions test/util/network/network_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package network

import (
"testing"

"github.com/stretchr/testify/assert"

k8sv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestGetSriovNicIPs(t *testing.T) {
networksStatus := `[{
"name": "network1",
"interface": "eth0",
"ips": [
"10.132.2.200"
],
"mac": "0a:58:0a:84:02:c8",
"default": true,
"dns": {}
},{
"name": "sriov-conformance-testing/test-multi-networkpolicy-sriov-network",
"interface": "net1",
"ips": [
"2.2.2.49"
],
"mac": "96:a2:09:fb:4d:c3",
"dns": {},
"device-info": {
"type": "pci",
"version": "1.0.0",
"pci": {
"pci-address": "0000:19:00.4"
}
}
}]`

p := &k8sv1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"k8s.v1.cni.cncf.io/networks-status": networksStatus,
},
},
}

ips, err := GetSriovNicIPs(p, "eth0")
assert.NoError(t, err)
assert.Contains(t, ips, "10.132.2.200")

ips, err = GetSriovNicIPs(p, "net1")
assert.NoError(t, err)
assert.Contains(t, ips, "2.2.2.49")

_, err = GetSriovNicIPs(p, "eth999")
assert.Error(t, err)
assert.Contains(t, err.Error(), "interface [eth999] not found")
}

func TestGetSriovNicIPsErrors(t *testing.T) {
p := &k8sv1.Pod{}
_, err := GetSriovNicIPs(p, "eth0")
assert.Error(t, err)
assert.Contains(t, err.Error(), "has no annotation `k8s.v1.cni.cncf.io/networks-status`")

p = &k8sv1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"k8s.v1.cni.cncf.io/networks-status": "xxx",
},
},
}
_, err = GetSriovNicIPs(p, "eth0")
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid character")
}
27 changes: 27 additions & 0 deletions vendor/github.com/pmezard/go-difflib/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e6926a0

Please sign in to comment.