GetPodList returns a PodList matching the namespace and label selector
(client coreclient.PodsGetter, namespace string, selector string, timeout time.Duration, sortBy func([]*corev1.Pod) sort.Interface)
| 38 | |
| 39 | // GetPodList returns a PodList matching the namespace and label selector |
| 40 | func GetPodList(client coreclient.PodsGetter, namespace string, selector string, timeout time.Duration, sortBy func([]*corev1.Pod) sort.Interface) (*corev1.PodList, error) { |
| 41 | options := metav1.ListOptions{LabelSelector: selector} |
| 42 | |
| 43 | podList, err := client.Pods(namespace).List(context.TODO(), options) |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | |
| 48 | pods := []*corev1.Pod{} |
| 49 | for i := range podList.Items { |
| 50 | pod := podList.Items[i] |
| 51 | pods = append(pods, &pod) |
| 52 | } |
| 53 | if len(pods) > 0 { |
| 54 | sort.Sort(sortBy(pods)) |
| 55 | for i, pod := range pods { |
| 56 | podList.Items[i] = *pod |
| 57 | } |
| 58 | return podList, nil |
| 59 | } |
| 60 | |
| 61 | // Watch until we observe a pod |
| 62 | options.ResourceVersion = podList.ResourceVersion |
| 63 | w, err := client.Pods(namespace).Watch(context.TODO(), options) |
| 64 | if err != nil { |
| 65 | return nil, err |
| 66 | } |
| 67 | defer w.Stop() |
| 68 | |
| 69 | condition := func(event watch.Event) (bool, error) { |
| 70 | return event.Type == watch.Added || event.Type == watch.Modified, nil |
| 71 | } |
| 72 | |
| 73 | ctx, cancel := watchtools.ContextWithOptionalTimeout(context.Background(), timeout) |
| 74 | defer cancel() |
| 75 | event, err := watchtools.UntilWithoutRetry(ctx, w, condition) |
| 76 | if err != nil { |
| 77 | return nil, err |
| 78 | } |
| 79 | |
| 80 | po, ok := event.Object.(*corev1.Pod) |
| 81 | if !ok { |
| 82 | return nil, fmt.Errorf("%#v is not a pod event", event) |
| 83 | } else { |
| 84 | podList.Items = append(podList.Items, *po) |
| 85 | } |
| 86 | return podList, nil |
| 87 | } |
| 88 | |
| 89 | // GetFirstPod returns a pod matching the namespace and label selector |
| 90 | // and the number of all pods that match the label selector. |
searching dependent graphs…