...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package recover
17
18 import (
19 "context"
20
21 "github.com/pkg/errors"
22
23 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
24 ctrlclient "github.com/chaos-mesh/chaos-mesh/pkg/ctrl/client"
25 "github.com/chaos-mesh/chaos-mesh/pkg/ctrl/server/model"
26 )
27
28 func SelectPods(ctx context.Context, client *ctrlclient.CtrlClient, selector v1alpha1.PodSelectorSpec) ([]*PartialPod, error) {
29 selectInput := model.PodSelectorInput{
30 Pods: map[string]interface{}{},
31 NodeSelectors: map[string]interface{}{},
32 Nodes: selector.Nodes,
33 PodPhaseSelectors: selector.PodPhaseSelectors,
34 Namespaces: selector.Namespaces,
35 FieldSelectors: map[string]interface{}{},
36 LabelSelectors: map[string]interface{}{},
37 AnnotationSelectors: map[string]interface{}{},
38 }
39
40 for k, v := range selector.Pods {
41 selectInput.Pods[k] = v
42 }
43
44 for k, v := range selector.NodeSelectors {
45 selectInput.NodeSelectors[k] = v
46 }
47
48 for k, v := range selector.FieldSelectors {
49 selectInput.FieldSelectors[k] = v
50 }
51
52 for k, v := range selector.LabelSelectors {
53 selectInput.LabelSelectors[k] = v
54 }
55
56 for k, v := range selector.AnnotationSelectors {
57 selectInput.AnnotationSelectors[k] = v
58 }
59
60 podsQuery := new(struct {
61 Pods []*PartialPod `graphql:"pods(selector: $selector)"`
62 })
63
64 err := client.QueryClient.Query(ctx, podsQuery, map[string]interface{}{"selector": selectInput})
65 if err != nil {
66 return nil, errors.Wrapf(err, "select pods with selector: %+v", selector)
67 }
68
69 return podsQuery.Pods, nil
70 }
71