|
| 1 | +package datapath |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/Azure/azure-container-networking/test/internal/k8sutils" |
| 9 | + "github.com/pkg/errors" |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | + apiv1 "k8s.io/api/core/v1" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + "k8s.io/client-go/kubernetes" |
| 14 | + restclient "k8s.io/client-go/rest" |
| 15 | +) |
| 16 | + |
| 17 | +func podTest(ctx context.Context, clientset *kubernetes.Clientset, srcPod *apiv1.Pod, cmd []string, rc *restclient.Config, passFunc func(string) error) error { |
| 18 | + logrus.Infof("podTest() - %v %v", srcPod.Name, cmd) |
| 19 | + output, err := k8sutils.ExecCmdOnPod(ctx, clientset, srcPod.Namespace, srcPod.Name, cmd, rc) |
| 20 | + if err != nil { |
| 21 | + return errors.Wrapf(err, "failed to execute command on pod: %v", srcPod.Name) |
| 22 | + } |
| 23 | + return passFunc(string(output)) |
| 24 | +} |
| 25 | + |
| 26 | +func WindowsPodToPodPingTestSameNode(ctx context.Context, clientset *kubernetes.Clientset, nodeName, podNamespace, labelSelector string, rc *restclient.Config) error { |
| 27 | + logrus.Infof("Get Pods for Node: %s", nodeName) |
| 28 | + pods, err := k8sutils.GetPodsByNode(ctx, clientset, podNamespace, labelSelector, nodeName) |
| 29 | + if err != nil { |
| 30 | + logrus.Error(err) |
| 31 | + return errors.Wrap(err, "k8s api call") |
| 32 | + } |
| 33 | + if len(pods.Items) <= 1 { |
| 34 | + return errors.New("Less than 2 pods on node") |
| 35 | + } |
| 36 | + |
| 37 | + // Get first pod on this node |
| 38 | + firstPod, err := clientset.CoreV1().Pods(podNamespace).Get(ctx, pods.Items[0].Name, metav1.GetOptions{}) |
| 39 | + if err != nil { |
| 40 | + return errors.Wrap(err, fmt.Sprintf("Getting pod %s failed with %v", firstPod.Name, err)) |
| 41 | + } |
| 42 | + logrus.Infof("First pod: %v %v", firstPod.Name, firstPod.Status.PodIP) |
| 43 | + |
| 44 | + // Get the second pod on this node |
| 45 | + secondPod, err := clientset.CoreV1().Pods(podNamespace).Get(ctx, pods.Items[1].Name, metav1.GetOptions{}) |
| 46 | + if err != nil { |
| 47 | + return errors.Wrap(err, fmt.Sprintf("Getting pod %s failed with %v", secondPod.Name, err)) |
| 48 | + } |
| 49 | + logrus.Infof("Second pod: %v %v", secondPod.Name, secondPod.Status.PodIP) |
| 50 | + |
| 51 | + // Ping the second pod from the first pod |
| 52 | + return podTest(ctx, clientset, firstPod, []string{"ping", secondPod.Status.PodIP}, rc, pingPassedWindows) |
| 53 | +} |
| 54 | + |
| 55 | +func WindowsPodToPodPingTestDiffNode(ctx context.Context, clientset *kubernetes.Clientset, nodeName1, nodeName2, podNamespace, labelSelector string, rc *restclient.Config) error { |
| 56 | + logrus.Infof("Get Pods for Node 1: %s", nodeName1) |
| 57 | + // Node 1 |
| 58 | + pods, err := k8sutils.GetPodsByNode(ctx, clientset, podNamespace, labelSelector, nodeName1) |
| 59 | + if err != nil { |
| 60 | + logrus.Error(err) |
| 61 | + return errors.Wrap(err, "k8s api call") |
| 62 | + } |
| 63 | + firstPod, err := clientset.CoreV1().Pods(podNamespace).Get(ctx, pods.Items[0].Name, metav1.GetOptions{}) |
| 64 | + if err != nil { |
| 65 | + return errors.Wrap(err, fmt.Sprintf("Getting pod %s failed with %v", firstPod.Name, err)) |
| 66 | + } |
| 67 | + logrus.Infof("First pod: %v %v", firstPod.Name, firstPod.Status.PodIP) |
| 68 | + |
| 69 | + logrus.Infof("Get Pods for Node 2: %s", nodeName2) |
| 70 | + // Node 2 |
| 71 | + pods, err = k8sutils.GetPodsByNode(ctx, clientset, podNamespace, labelSelector, nodeName2) |
| 72 | + if err != nil { |
| 73 | + logrus.Error(err) |
| 74 | + return errors.Wrap(err, "k8s api call") |
| 75 | + } |
| 76 | + secondPod, err := clientset.CoreV1().Pods(podNamespace).Get(ctx, pods.Items[0].Name, metav1.GetOptions{}) |
| 77 | + if err != nil { |
| 78 | + return errors.Wrap(err, fmt.Sprintf("Getting pod %s failed with %v", secondPod.Name, err)) |
| 79 | + } |
| 80 | + logrus.Infof("Second pod: %v %v", secondPod.Name, secondPod.Status.PodIP) |
| 81 | + |
| 82 | + // Ping the second pod from the first pod located on different nodes |
| 83 | + return podTest(ctx, clientset, firstPod, []string{"ping", secondPod.Status.PodIP}, rc, pingPassedWindows) |
| 84 | +} |
| 85 | + |
| 86 | +func WindowsPodToNode(ctx context.Context, clientset *kubernetes.Clientset, nodeName, nodeIP, podNamespace, labelSelector string, rc *restclient.Config) error { |
| 87 | + logrus.Infof("Get Pods by Node: %s %s", nodeName, nodeIP) |
| 88 | + pods, err := k8sutils.GetPodsByNode(ctx, clientset, podNamespace, labelSelector, nodeName) |
| 89 | + if err != nil { |
| 90 | + logrus.Error(err) |
| 91 | + return errors.Wrap(err, "k8s api call") |
| 92 | + } |
| 93 | + if len(pods.Items) <= 1 { |
| 94 | + return errors.New("Less than 2 pods on node") |
| 95 | + } |
| 96 | + // Get first pod on this node |
| 97 | + firstPod, err := clientset.CoreV1().Pods(podNamespace).Get(ctx, pods.Items[0].Name, metav1.GetOptions{}) |
| 98 | + if err != nil { |
| 99 | + return errors.Wrap(err, fmt.Sprintf("Getting pod %s failed with %v", firstPod.Name, err)) |
| 100 | + } |
| 101 | + logrus.Infof("First pod: %v", firstPod.Name) |
| 102 | + |
| 103 | + // Get the second pod on this node |
| 104 | + secondPod, err := clientset.CoreV1().Pods(podNamespace).Get(ctx, pods.Items[1].Name, metav1.GetOptions{}) |
| 105 | + if err != nil { |
| 106 | + return errors.Wrap(err, fmt.Sprintf("Getting pod %s failed with %v", secondPod.Name, err)) |
| 107 | + } |
| 108 | + logrus.Infof("Second pod: %v", secondPod.Name) |
| 109 | + |
| 110 | + // Ping from pod to node |
| 111 | + resultOne := podTest(ctx, clientset, firstPod, []string{"ping", nodeIP}, rc, pingPassedWindows) |
| 112 | + resultTwo := podTest(ctx, clientset, secondPod, []string{"ping", nodeIP}, rc, pingPassedWindows) |
| 113 | + |
| 114 | + if resultOne != nil { |
| 115 | + return resultOne |
| 116 | + } |
| 117 | + |
| 118 | + if resultTwo != nil { |
| 119 | + return resultTwo |
| 120 | + } |
| 121 | + |
| 122 | + return nil |
| 123 | +} |
| 124 | + |
| 125 | +func WindowsPodToInternet(ctx context.Context, clientset *kubernetes.Clientset, nodeName, podNamespace, labelSelector string, rc *restclient.Config) error { |
| 126 | + logrus.Infof("Get Pods by Node: %s", nodeName) |
| 127 | + pods, err := k8sutils.GetPodsByNode(ctx, clientset, podNamespace, labelSelector, nodeName) |
| 128 | + if err != nil { |
| 129 | + logrus.Error(err) |
| 130 | + return errors.Wrap(err, "k8s api call") |
| 131 | + } |
| 132 | + if len(pods.Items) <= 1 { |
| 133 | + return errors.New("Less than 2 pods on node") |
| 134 | + } |
| 135 | + |
| 136 | + // Get first pod on this node |
| 137 | + firstPod, err := clientset.CoreV1().Pods(podNamespace).Get(ctx, pods.Items[0].Name, metav1.GetOptions{}) |
| 138 | + if err != nil { |
| 139 | + return errors.Wrap(err, fmt.Sprintf("Getting pod %s failed with %v", firstPod.Name, err)) |
| 140 | + } |
| 141 | + logrus.Infof("First pod: %v", firstPod.Name) |
| 142 | + |
| 143 | + // Get the second pod on this node |
| 144 | + secondPod, err := clientset.CoreV1().Pods(podNamespace).Get(ctx, pods.Items[1].Name, metav1.GetOptions{}) |
| 145 | + if err != nil { |
| 146 | + return errors.Wrap(err, fmt.Sprintf("Getting pod %s failed with %v", secondPod.Name, err)) |
| 147 | + } |
| 148 | + logrus.Infof("Second pod: %v", secondPod.Name) |
| 149 | + |
| 150 | + resultOne := podTest(ctx, clientset, firstPod, []string{"powershell", "Invoke-WebRequest", "www.bing.com", "-UseBasicParsing"}, rc, webRequestPassedWindows) |
| 151 | + resultTwo := podTest(ctx, clientset, secondPod, []string{"powershell", "Invoke-WebRequest", "www.bing.com", "-UseBasicParsing"}, rc, webRequestPassedWindows) |
| 152 | + |
| 153 | + if resultOne != nil { |
| 154 | + return resultOne |
| 155 | + } |
| 156 | + |
| 157 | + if resultTwo != nil { |
| 158 | + return resultTwo |
| 159 | + } |
| 160 | + |
| 161 | + return nil |
| 162 | +} |
| 163 | + |
| 164 | +func webRequestPassedWindows(output string) error { |
| 165 | + const searchString = "200 OK" |
| 166 | + if strings.Contains(output, searchString) { |
| 167 | + return nil |
| 168 | + } |
| 169 | + return errors.Wrapf(errors.New("Output did not contain \"200 OK\""), "output was: %s", output) |
| 170 | +} |
| 171 | + |
| 172 | +func pingPassedWindows(output string) error { |
| 173 | + const searchString = "0% loss" |
| 174 | + if strings.Contains(output, searchString) { |
| 175 | + return nil |
| 176 | + } |
| 177 | + return errors.Wrapf(errors.New("Ping did not contain\"0% loss\""), "output was: %s", output) |
| 178 | +} |
0 commit comments