|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/* |
| 18 | +This soak tests places a specified number of pods on each node and then |
| 19 | +repeatedly sends queries to a service running on these pods via |
| 20 | +a serivce |
| 21 | +*/ |
| 22 | + |
| 23 | +package endpoints |
| 24 | + |
| 25 | +import ( |
| 26 | + "fmt" |
| 27 | + "sort" |
| 28 | + "time" |
| 29 | + |
| 30 | + "github.com/onsi/ginkgo" |
| 31 | + v1 "k8s.io/api/core/v1" |
| 32 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 33 | + "k8s.io/apimachinery/pkg/types" |
| 34 | + clientset "k8s.io/client-go/kubernetes" |
| 35 | + e2elog "k8s.io/kubernetes/test/e2e/framework/log" |
| 36 | +) |
| 37 | + |
| 38 | +// ServiceStartTimeout is how long to wait for a service endpoint to be resolvable. |
| 39 | +const ServiceStartTimeout = 3 * time.Minute |
| 40 | + |
| 41 | +// PortsByPodName is a map that maps pod name to container ports. |
| 42 | +type PortsByPodName map[string][]int |
| 43 | + |
| 44 | +// PortsByPodUID is a map that maps pod UID to container ports. |
| 45 | +type PortsByPodUID map[types.UID][]int |
| 46 | + |
| 47 | +// GetContainerPortsByPodUID returns a PortsByPodUID map on the given endpoints. |
| 48 | +func GetContainerPortsByPodUID(ep *v1.Endpoints) PortsByPodUID { |
| 49 | + m := PortsByPodUID{} |
| 50 | + for _, ss := range ep.Subsets { |
| 51 | + for _, port := range ss.Ports { |
| 52 | + for _, addr := range ss.Addresses { |
| 53 | + containerPort := port.Port |
| 54 | + if _, ok := m[addr.TargetRef.UID]; !ok { |
| 55 | + m[addr.TargetRef.UID] = make([]int, 0) |
| 56 | + } |
| 57 | + m[addr.TargetRef.UID] = append(m[addr.TargetRef.UID], int(containerPort)) |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + return m |
| 62 | +} |
| 63 | + |
| 64 | +func translatePodNameToUID(c clientset.Interface, ns string, expectedEndpoints PortsByPodName) (PortsByPodUID, error) { |
| 65 | + portsByUID := make(PortsByPodUID) |
| 66 | + for name, portList := range expectedEndpoints { |
| 67 | + pod, err := c.CoreV1().Pods(ns).Get(name, metav1.GetOptions{}) |
| 68 | + if err != nil { |
| 69 | + return nil, fmt.Errorf("failed to get pod %s, that's pretty weird. validation failed: %s", name, err) |
| 70 | + } |
| 71 | + portsByUID[pod.ObjectMeta.UID] = portList |
| 72 | + } |
| 73 | + return portsByUID, nil |
| 74 | +} |
| 75 | + |
| 76 | +func validatePorts(ep PortsByPodUID, expectedEndpoints PortsByPodUID) error { |
| 77 | + if len(ep) != len(expectedEndpoints) { |
| 78 | + // should not happen because we check this condition before |
| 79 | + return fmt.Errorf("invalid number of endpoints got %v, expected %v", ep, expectedEndpoints) |
| 80 | + } |
| 81 | + for podUID := range expectedEndpoints { |
| 82 | + if _, ok := ep[podUID]; !ok { |
| 83 | + return fmt.Errorf("endpoint %v not found", podUID) |
| 84 | + } |
| 85 | + if len(ep[podUID]) != len(expectedEndpoints[podUID]) { |
| 86 | + return fmt.Errorf("invalid list of ports for uid %v. Got %v, expected %v", podUID, ep[podUID], expectedEndpoints[podUID]) |
| 87 | + } |
| 88 | + sort.Ints(ep[podUID]) |
| 89 | + sort.Ints(expectedEndpoints[podUID]) |
| 90 | + for index := range ep[podUID] { |
| 91 | + if ep[podUID][index] != expectedEndpoints[podUID][index] { |
| 92 | + return fmt.Errorf("invalid list of ports for uid %v. Got %v, expected %v", podUID, ep[podUID], expectedEndpoints[podUID]) |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +// ValidateEndpointsPorts validates that the given service exists and is served by the given expectedEndpoints. |
| 100 | +func ValidateEndpointsPorts(c clientset.Interface, namespace, serviceName string, expectedEndpoints PortsByPodName) error { |
| 101 | + ginkgo.By(fmt.Sprintf("waiting up to %v for service %s in namespace %s to expose endpoints %v", ServiceStartTimeout, serviceName, namespace, expectedEndpoints)) |
| 102 | + i := 1 |
| 103 | + for start := time.Now(); time.Since(start) < ServiceStartTimeout; time.Sleep(1 * time.Second) { |
| 104 | + ep, err := c.CoreV1().Endpoints(namespace).Get(serviceName, metav1.GetOptions{}) |
| 105 | + if err != nil { |
| 106 | + e2elog.Logf("Get endpoints failed (%v elapsed, ignoring for 5s): %v", time.Since(start), err) |
| 107 | + continue |
| 108 | + } |
| 109 | + portsByPodUID := GetContainerPortsByPodUID(ep) |
| 110 | + expectedPortsByPodUID, err := translatePodNameToUID(c, namespace, expectedEndpoints) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + if len(portsByPodUID) == len(expectedEndpoints) { |
| 115 | + err := validatePorts(portsByPodUID, expectedPortsByPodUID) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + e2elog.Logf("successfully validated that service %s in namespace %s exposes endpoints %v (%v elapsed)", |
| 120 | + serviceName, namespace, expectedEndpoints, time.Since(start)) |
| 121 | + return nil |
| 122 | + } |
| 123 | + if i%5 == 0 { |
| 124 | + e2elog.Logf("Unexpected endpoints: found %v, expected %v (%v elapsed, will retry)", portsByPodUID, expectedEndpoints, time.Since(start)) |
| 125 | + } |
| 126 | + i++ |
| 127 | + } |
| 128 | + if pods, err := c.CoreV1().Pods(metav1.NamespaceAll).List(metav1.ListOptions{}); err == nil { |
| 129 | + for _, pod := range pods.Items { |
| 130 | + e2elog.Logf("Pod %s\t%s\t%s\t%s", pod.Namespace, pod.Name, pod.Spec.NodeName, pod.DeletionTimestamp) |
| 131 | + } |
| 132 | + } else { |
| 133 | + e2elog.Logf("Can't list pod debug info: %v", err) |
| 134 | + } |
| 135 | + return fmt.Errorf("Timed out waiting for service %s in namespace %s to expose endpoints %v (%v elapsed)", serviceName, namespace, expectedEndpoints, ServiceStartTimeout) |
| 136 | +} |
0 commit comments