|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +package efa |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + _ "embed" |
| 8 | + "fmt" |
| 9 | + "log" |
| 10 | + |
| 11 | + "github.com/aws/aws-k8s-tester/internal/e2e" |
| 12 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 13 | + corev1 "k8s.io/api/core/v1" |
| 14 | + v1 "k8s.io/api/core/v1" |
| 15 | + "k8s.io/client-go/kubernetes" |
| 16 | + "sigs.k8s.io/e2e-framework/pkg/env" |
| 17 | + "sigs.k8s.io/e2e-framework/pkg/envconf" |
| 18 | + |
| 19 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + testenv env.Environment |
| 24 | + ec2Client e2e.EC2Client |
| 25 | + |
| 26 | + testImage *string |
| 27 | + |
| 28 | + pingPongSize *string |
| 29 | + pingPongIters *int |
| 30 | + pingPongDeadlineSeconds *int |
| 31 | + |
| 32 | + nodeType *string |
| 33 | + expectedEFADeviceCount *int |
| 34 | + |
| 35 | + verbose *bool |
| 36 | +) |
| 37 | + |
| 38 | +const ( |
| 39 | + EFA_RESOURCE_NAME = "vpc.amazonaws.com/efa" |
| 40 | + TEST_NAMESPACE_NAME = "efa-tests" |
| 41 | +) |
| 42 | + |
| 43 | +func getEfaCapacity(node corev1.Node) int { |
| 44 | + capacity, ok := node.Status.Capacity[v1.ResourceName(EFA_RESOURCE_NAME)] |
| 45 | + if !ok { |
| 46 | + return 0 |
| 47 | + } |
| 48 | + return int(capacity.Value()) |
| 49 | +} |
| 50 | + |
| 51 | +func getEfaNodes(ctx context.Context, config *envconf.Config) ([]corev1.Node, error) { |
| 52 | + var efaNodes []corev1.Node |
| 53 | + clientset, err := kubernetes.NewForConfig(config.Client().RESTConfig()) |
| 54 | + if err != nil { |
| 55 | + return []corev1.Node{}, fmt.Errorf("failed to create Kubernetes client: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + nodes, err := clientset.CoreV1().Nodes().List(ctx, metav1.ListOptions{}) |
| 59 | + if err != nil { |
| 60 | + return []corev1.Node{}, fmt.Errorf("failed to list nodes: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + if len(nodes.Items) == 0 { |
| 64 | + return []corev1.Node{}, fmt.Errorf("no nodes found in the cluster") |
| 65 | + } |
| 66 | + |
| 67 | + for _, node := range nodes.Items { |
| 68 | + instanceType := node.Labels["node.kubernetes.io/instance-type"] |
| 69 | + |
| 70 | + if aws.ToString(nodeType) != "" && instanceType != aws.ToString(nodeType) { |
| 71 | + log.Printf("[INFO] Skipping node %s (type: %s), node is not of target type %s", node.Name, instanceType, aws.ToString(nodeType)) |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + numEfaDevices, err := e2e.GetNonZeroResourceCapacity(&node, EFA_RESOURCE_NAME) |
| 76 | + if err != nil { |
| 77 | + log.Printf("[INFO] Skipping node %s (type: %s): %v", node.Name, instanceType, err) |
| 78 | + continue |
| 79 | + } |
| 80 | + |
| 81 | + allocatable, ok := node.Status.Allocatable[v1.ResourceName(EFA_RESOURCE_NAME)] |
| 82 | + if !ok { |
| 83 | + log.Printf("[INFO] Skipping node %s (type: %s), node does not have EFA allocatable", node.Name, instanceType) |
| 84 | + continue |
| 85 | + } else if int(allocatable.Value()) != numEfaDevices { |
| 86 | + log.Printf("[INFO] Skipping node %s (type: %s), node is busy (%d of %d EFA device(s) allocatable)", node.Name, instanceType, allocatable.Value(), numEfaDevices) |
| 87 | + continue |
| 88 | + } |
| 89 | + |
| 90 | + expectedDeviceCount := aws.ToInt(expectedEFADeviceCount) |
| 91 | + if expectedDeviceCount == 0 { |
| 92 | + instanceInfo, err := ec2Client.DescribeInstanceType(instanceType) |
| 93 | + if err != nil { |
| 94 | + return []corev1.Node{}, err |
| 95 | + } |
| 96 | + expectedDeviceCount = int(aws.ToInt32(instanceInfo.NetworkInfo.EfaInfo.MaximumEfaInterfaces)) |
| 97 | + } |
| 98 | + |
| 99 | + if expectedDeviceCount != numEfaDevices { |
| 100 | + return []corev1.Node{}, fmt.Errorf("unexpected EFA device capacity on node %s: expected %d, got %d", node.Name, expectedDeviceCount, numEfaDevices) |
| 101 | + } |
| 102 | + |
| 103 | + efaNodes = append(efaNodes, node) |
| 104 | + } |
| 105 | + |
| 106 | + if len(efaNodes) == 0 { |
| 107 | + return []corev1.Node{}, fmt.Errorf("no nodes with EFA capacity found in the cluster") |
| 108 | + } |
| 109 | + |
| 110 | + return efaNodes, nil |
| 111 | +} |
0 commit comments