/
podList.go
64 lines (53 loc) · 1.53 KB
/
podList.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package cluster
import (
"context"
"fmt"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
type PodList struct {
podClient corev1.PodInterface
listOptions metav1.ListOptions
}
func (pl *PodList) Len(ctx context.Context, expected int) error {
list, err := pl.podClient.List(ctx, pl.listOptions)
if err != nil {
return fmt.Errorf("could not list pods for listOptions %s: %w", pl.listOptions.String(), err)
}
itemsLen := len(list.Items)
if itemsLen != expected {
return fmt.Errorf("did not find expected number of pods: expected: %d; actual: %d", expected, itemsLen)
}
return nil
}
// Raw queries the kubernetes API and returns the pod list as plain kubernetes API objects.
func (pl *PodList) Raw(ctx context.Context) (*v1.PodList, error) {
return pl.podClient.List(ctx, pl.listOptions)
}
type PodListSelector struct {
podClient corev1.PodInterface
listOptions metav1.ListOptions
}
func (pls *PodListSelector) ByLabels(labels string) *PodListSelector {
ps := &PodListSelector{
podClient: pls.podClient,
listOptions: pls.listOptions,
}
ps.listOptions.LabelSelector = labels
return ps
}
func (pls *PodListSelector) ByFieldSelector(fieldSelector string) *PodListSelector {
ps := &PodListSelector{
podClient: pls.podClient,
listOptions: pls.listOptions,
}
ps.listOptions.FieldSelector = fieldSelector
return ps
}
func (pls *PodListSelector) List() *PodList {
return &PodList{
podClient: pls.podClient,
listOptions: pls.listOptions,
}
}