-
Notifications
You must be signed in to change notification settings - Fork 6
/
listpods.go
104 lines (91 loc) · 2.63 KB
/
listpods.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package client
import (
"context"
"errors"
"fmt"
"github.com/wish/ctl/pkg/client/filter"
"github.com/wish/ctl/pkg/client/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os"
"strings"
"sync"
)
// ListPods returns a list of all pod that match the query
func (c *Client) ListPods(contextStr string, namespace string, options ListOptions) ([]types.PodDiscovery, error) {
cs, err := c.getContextInterface(contextStr)
if err != nil {
return nil, err
}
pods, err := cs.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
var items []types.PodDiscovery
for _, pod := range pods.Items {
p := types.PodDiscovery{contextStr, pod}
c.Transform(&p)
if filter.MatchLabel(p, options.LabelMatch) && (options.Search == nil || options.Search.MatchString(p.Name)) && (options.StatusMatch.State == "" || p.Status.Phase == options.StatusMatch.State) {
items = append(items, p)
}
}
return items, nil
}
// ListPodsOverContexts is like ListPods but operates over multiple clusters
func (c *Client) ListPodsOverContexts(contexts []string, namespace string, options ListOptions) ([]types.PodDiscovery, error) {
if len(contexts) == 0 {
contexts = c.GetFilteredContexts(options.LabelMatch)
} else {
contexts = c.FilterContexts(contexts, options.LabelMatch)
}
var wait sync.WaitGroup
wait.Add(len(contexts))
var mutex sync.Mutex
var ret []types.PodDiscovery
var failed []string
for _, ctx := range contexts {
go func(ctx string) {
defer wait.Done()
pods, err := c.ListPods(ctx, namespace, options)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not connect to cluster \"%s\": %v\n", ctx, err)
mutex.Lock()
failed = append(failed, ctx)
mutex.Unlock()
return
}
mutex.Lock()
for _, pod := range pods {
ret = append(ret, pod)
}
mutex.Unlock()
}(ctx)
}
wait.Wait()
if failed != nil {
return ret, errors.New("failed connecting to the following contexts: " + strings.Join(failed, ", "))
}
sortObjs(ret)
return ret, nil
}
// ListPodsOfJob returns a list of all pods belonging to a job
func (c *Client) ListPodsOfJob(contexts []string, namespace, jobName string, options ListOptions) ([]types.PodDiscovery, error) {
pods, err := c.ListPodsOverContexts(contexts, namespace, options)
if err != nil {
return nil, err
}
job, err := c.findJob(contexts, namespace, jobName, options)
if err != nil {
return nil, err
}
var ret []types.PodDiscovery
for _, p := range pods {
// Check if has owner reference
for _, o := range p.OwnerReferences {
if o.UID == job.UID { // matches
ret = append(ret, p)
break
}
}
}
return ret, nil
}