Skip to content

Commit

Permalink
Merge pull request #413 from zeeke/annotation-network-status
Browse files Browse the repository at this point in the history
Fix networks-status annotation in e2e tests
  • Loading branch information
adrianchiris authored Mar 12, 2023
2 parents eb3d22a + a89b836 commit 8c3bf58
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
8 changes: 4 additions & 4 deletions test/util/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func CreateSriovPolicy(clientSet *testclient.ClientSet, generatedName string, op
func GetNicsByPrefix(pod *k8sv1.Pod, ifcPrefix string) ([]string, error) {
var nets []Network
nics := []string{}
err := json.Unmarshal([]byte(pod.ObjectMeta.Annotations["k8s.v1.cni.cncf.io/networks-status"]), &nets)
err := json.Unmarshal([]byte(pod.ObjectMeta.Annotations[netattdefv1.NetworkStatusAnnot]), &nets)
if err != nil {
return nil, err
}
Expand All @@ -102,15 +102,15 @@ 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"]
networksStatus, ok := pod.ObjectMeta.Annotations[netattdefv1.NetworkStatusAnnot]
if !ok {
return nil, fmt.Errorf("pod [%s] has no annotation `k8s.v1.cni.cncf.io/networks-status`", pod.Name)
return nil, fmt.Errorf("pod [%s] has no annotation `%s`", netattdefv1.NetworkStatusAnnot, pod.Name)
}

var nets []Network
err := json.Unmarshal([]byte(networksStatus), &nets)
if err != nil {
return nil, fmt.Errorf("can't unmarshal annotation `k8s.v1.cni.cncf.io/networks-status`: %w", err)
return nil, fmt.Errorf("can't unmarshal annotation `%s`: %w", netattdefv1.NetworkStatusAnnot, err)
}
for _, net := range nets {
if net.Interface != ifcName {
Expand Down
9 changes: 5 additions & 4 deletions test/util/network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package network
import (
"testing"

netattdefv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
"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 := `[{
networkStatus := `[{
"name": "network1",
"interface": "eth0",
"ips": [
Expand Down Expand Up @@ -39,7 +40,7 @@ func TestGetSriovNicIPs(t *testing.T) {
p := &k8sv1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"k8s.v1.cni.cncf.io/networks-status": networksStatus,
netattdefv1.NetworkStatusAnnot: networkStatus,
},
},
}
Expand All @@ -61,12 +62,12 @@ 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`")
assert.Contains(t, err.Error(), "has no annotation `k8s.v1.cni.cncf.io/network-status`")

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

0 comments on commit 8c3bf58

Please sign in to comment.