-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.go
182 lines (169 loc) · 4.63 KB
/
results.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package k8s
import "fmt"
// Results is the base construction for a search result
type Results struct {
Kind string
XData []XD
}
// GetContainers returns a Containers Result by converting a Pod Result (WiP*)
func (r *Results) GetContainers() (Results, error) {
var results Results
if r.Kind != "Pod" {
return results, fmt.Errorf("cannot convert Results of kind %v into a Containers", r.Kind)
}
return results, nil
}
// GetMany returns Results from the given kind and a list of names contained in an string array
// kind options are "pods", "nodes", "replicasets", "deployments", "services", "ingresses"
func (rc *RawClient) GetMany(kind string, names []string) (Results, error) {
res, err := rc.findManyResults(kind, names)
if err != nil {
return res, err
}
return res, nil
}
// GetPods return pod based Results based on the search strings
func (rc *RawClient) GetPods(search ...string) (Results, error) {
//res, err := rc.findResults("pods", search)
res, err := rc.findManyResults("pods", search)
if err != nil {
return res, err
}
return res, nil
}
// GetNodes returns node based Results based on the search string
func (rc *RawClient) GetNodes(search ...string) (Results, error) {
//res, err := rc.findResults("nodes", search)
res, err := rc.findManyResults("nodes", search)
if err != nil {
return res, err
}
return res, nil
}
// GetRS returns replicaset based Results based on the search string
func (rc *RawClient) GetRS(search ...string) (Results, error) {
//res, err := rc.findResults("replicasets", search)
res, err := rc.findManyResults("replicasets", search)
if err != nil {
return res, err
}
return res, nil
}
// GetSvc returns node service Results based on the search string
func (rc *RawClient) GetSvc(search ...string) (Results, error) {
//res, err := rc.findResults("services", search)
res, err := rc.findManyResults("services", search)
if err != nil {
return res, err
}
return res, nil
}
// GetDeployments returns deployment based Results based on the search string
func (rc *RawClient) GetDeployments(search ...string) (Results, error) {
//res, err := rc.findResults("deployments", search)
res, err := rc.findManyResults("deployments", search)
if err != nil {
return res, err
}
return res, nil
}
// GetIngress returns ingress based Results based on the search string
func (rc *RawClient) GetIngress(search ...string) (Results, error) {
//res, err := rc.findResults("ingresses", search)
res, err := rc.findManyResults("ingresses", search)
if err != nil {
return res, err
}
return res, nil
}
// findResults returns Results based on the kind and target name search string
func (rc *RawClient) findResults(kind, search string) (Results, error) {
var res Results
switch kind {
case "pods":
res.Kind = "Pod"
case "nodes":
res.Kind = "Node"
case "replicasets":
res.Kind = "ReplicaSet"
case "deployments":
res.Kind = "Deployment"
case "services":
res.Kind = "Service"
case "ingresses":
res.Kind = "Ingress"
default:
return res, fmt.Errorf("No resources found for the given request")
}
found, err := rc.SearchFor(kind, search)
if err != nil {
return res, err
}
data, err := createXD(found)
if err != nil {
return res, err
}
res.XData = data
return res, nil
}
// findManyResults returns Results based on the kind and target names contained in an array []string
func (rc *RawClient) findManyResults(kind string, names []string) (Results, error) {
var res Results
var errd error
switch kind {
case "pods":
res.Kind = "Pod"
case "nodes":
res.Kind = "Node"
case "replicasets":
res.Kind = "ReplicaSet"
case "deployments":
res.Kind = "Deployment"
case "services":
res.Kind = "Service"
case "ingresses":
res.Kind = "Ingress"
default:
return res, fmt.Errorf("No resources found for the given request")
}
//var data []XD
data := make([]XD, 0, len(names))
xdChan := make(chan []XD, 100)
errChan := make(chan error, 10)
all, err := rc.RawAll(kind)
if err != nil {
return res, err
}
for _, n := range names {
//go rc.getXD(n, &all, xdChan, errChan)
go rc.getXD(n, all, xdChan, errChan)
}
for i := 0; i < len(names); i++ {
select {
case err := <-errChan:
errd = err
case xd := <-xdChan:
for _, x := range xd {
data = append(data, x)
}
}
}
res.XData = data
return res, errd
}
//func (rc *RawClient) getXD(name string, all *[]byte, xdChan chan []XD, errChan chan error) {
func (rc *RawClient) getXD(name string, all []byte, xdChan chan []XD, errChan chan error) {
var found []string
if rc.exactMatches {
found = parseExact(all, name)
} else {
found = parseFor(all, name)
}
data, err := createXD(found)
if err != nil {
errChan <- err
return
}
xdChan <- data
return
}