-
Notifications
You must be signed in to change notification settings - Fork 179
/
list.go
251 lines (240 loc) · 6.94 KB
/
list.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package serving
import (
"encoding/json"
"fmt"
"os"
"strings"
"sync"
"text/tabwriter"
"github.com/kubeflow/arena/pkg/apis/types"
"github.com/kubeflow/arena/pkg/apis/utils"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
var (
errUnknownServingJobType = fmt.Errorf("unknown serving types,only support: %v", utils.GetServingJobTypes())
)
func ListServingJobs(namespace string, allNamespace bool, servingType types.ServingJobType) ([]ServingJob, error) {
if servingType == types.UnknownServingJob {
return nil, fmt.Errorf("Unknown serving job type,arena only supports: [%s]", utils.GetSupportServingJobTypesInfo())
}
processers := GetAllProcesser()
if servingType != types.AllServingJob {
processer, ok := processers[servingType]
if !ok {
return nil, fmt.Errorf("unknown processer %v,please define it", servingType)
}
return processer.ListServingJobs(namespace, allNamespace)
}
servingJobs := []ServingJob{}
var wg sync.WaitGroup
locker := new(sync.RWMutex)
noPrivileges := false
for _, pr := range processers {
wg.Add(1)
p := pr
go func() {
defer wg.Done()
jobs, err := p.ListServingJobs(namespace, allNamespace)
if err != nil {
if strings.Contains(err.Error(), "forbidden: User") {
log.Debugf("the user has no privileges to get the serving job %v,reason: %v", p.Type(), err)
noPrivileges = true
return
}
log.Debugf("failed to get serving jobs whose type are %v", p.Type())
return
}
locker.Lock()
servingJobs = append(servingJobs, jobs...)
locker.Unlock()
}()
}
wg.Wait()
if noPrivileges {
item := fmt.Sprintf("namespace %v", namespace)
if allNamespace {
item = fmt.Sprintf("all namespaces")
}
return nil, fmt.Errorf("the user has no privileges to list the serving jobs in %v", item)
}
return servingJobs, nil
}
func DisplayAllServingJobs(jobs []ServingJob, allNamespace bool, format types.FormatStyle) {
jobInfos := []types.ServingJobInfo{}
for _, job := range jobs {
jobInfos = append(jobInfos, job.Convert2JobInfo())
}
switch format {
case types.JsonFormat:
data, _ := json.MarshalIndent(jobInfos, "", " ")
fmt.Printf("%v", string(data))
return
case types.YamlFormat:
data, _ := yaml.Marshal(jobInfos)
fmt.Printf("%v", string(data))
return
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
header := []string{}
if allNamespace {
header = append(header, "NAMESPACE")
}
gpus := float64(0)
for _, jobinfo := range jobInfos {
for _, instance := range jobinfo.Instances {
gpus += instance.RequestGPUs
}
}
fields := []string{"NAME", "TYPE", "VERSION", "DESIRED", "AVAILABLE", "ADDRESS", "PORTS"}
header = append(header, fields...)
if gpus != float64(0) {
header = append(header, "GPU")
}
PrintLine(w, header...)
jobInfosMap := addTrafficWeight(jobInfos)
for _, jobInfo := range jobInfos {
jobInfo, ok := jobInfosMap[genServingJobKey(jobInfo)]
if !ok {
log.Debugf("not found job in job map,skip to print it")
continue
}
line := []string{}
if allNamespace {
line = append(line, jobInfo.Namespace)
}
endpointAddress := jobInfo.IPAddress
ports := []string{}
for _, e := range jobInfo.Endpoints {
port := ""
if e.NodePort != 0 {
port = fmt.Sprintf("%v:%v->%v", strings.ToUpper(e.Name), e.NodePort, e.Port)
} else {
port = fmt.Sprintf("%v:%v", strings.ToUpper(e.Name), e.Port)
}
ports = append(ports, port)
}
if len(ports) == 0 {
ports = append(ports, "N/A")
}
items := []string{
jobInfo.Name,
fmt.Sprintf("%v", jobInfo.Type),
jobInfo.Version,
fmt.Sprintf("%v", jobInfo.Desired),
fmt.Sprintf("%v", jobInfo.Available),
endpointAddress,
strings.Join(ports, ","),
}
if gpus != float64(0) {
jobGPUs := float64(0)
for _, instance := range jobInfo.Instances {
jobGPUs += instance.RequestGPUs
}
items = append(items, fmt.Sprintf("%v", jobGPUs))
}
line = append(line, items...)
PrintLine(w, line...)
}
_ = w.Flush()
}
type ServingJobGroup struct {
Id string
Namespace string
JobType types.ServingJobType
JobName string
}
func addTrafficWeight(allJobInfos []types.ServingJobInfo) map[string]types.ServingJobInfo {
type ServingJobGroup struct {
id string
namespace string
jobType types.ServingJobType
jobName string
ip string
endpoints []types.Endpoint
items []types.ServingJobInfo
}
servingJobsGroup := map[string]*ServingJobGroup{}
servingJobMap := map[string]types.ServingJobInfo{}
for _, jobInfo := range allJobInfos {
key := genServingJobGroupKey(jobInfo)
if servingJobsGroup[key] == nil {
servingJobsGroup[key] = &ServingJobGroup{
id: key,
namespace: jobInfo.Namespace,
jobName: jobInfo.Name,
jobType: types.ServingJobType(jobInfo.Type),
items: []types.ServingJobInfo{},
}
}
servingJobMap[genServingJobKey(jobInfo)] = jobInfo
if jobInfo.IPAddress != "" && jobInfo.IPAddress != "N/A" {
servingJobsGroup[key].ip = jobInfo.IPAddress
}
if jobInfo.Endpoints != nil && len(jobInfo.Endpoints) != 0 {
servingJobsGroup[key].endpoints = jobInfo.Endpoints
}
servingJobsGroup[key].items = append(servingJobsGroup[key].items, jobInfo)
}
if len(servingJobsGroup) == len(allJobInfos) {
return servingJobMap
}
istioClient, err := initIstioClient()
if err != nil {
log.Debugf("failed to get istio client when querying traffic weight,reason: %v", err)
return servingJobMap
}
for key, group := range servingJobsGroup {
if len(group.items) == 1 {
continue
}
weights, err := getVirtualServiceWeight(istioClient, group.namespace, group.jobName)
if err != nil {
log.Debugf("failed to get virtual service weight,reason: %v", err)
continue
}
// if the weight is 0,fix it with 100
if len(weights) == 1 {
for version, weight := range weights {
if weight == int32(0) {
weights[version] = 100
}
}
}
jobHasBeenChanged := map[string]bool{}
for version, weight := range weights {
completeName := fmt.Sprintf("%v/%v", key, version)
target := servingJobMap[completeName]
switch weight {
case int32(0):
target.IPAddress = "N/A"
target.Endpoints = []types.Endpoint{}
case int32(100):
target.IPAddress = fmt.Sprintf("%v", target.IPAddress)
target.Endpoints = group.endpoints
default:
target.IPAddress = fmt.Sprintf("%v(%v%%)", target.IPAddress, weight)
target.Endpoints = group.endpoints
}
jobHasBeenChanged[completeName] = true
servingJobMap[completeName] = target
}
for _, item := range group.items {
jobKey := genServingJobKey(item)
if jobHasBeenChanged[jobKey] {
continue
}
t := servingJobMap[jobKey]
t.IPAddress = "N/A"
t.Endpoints = []types.Endpoint{}
servingJobMap[jobKey] = t
}
}
return servingJobMap
}
func genServingJobKey(jobInfo types.ServingJobInfo) string {
return fmt.Sprintf("%v/%v", genServingJobGroupKey(jobInfo), jobInfo.Version)
}
func genServingJobGroupKey(jobInfo types.ServingJobInfo) string {
return fmt.Sprintf("%v/%v/%v", jobInfo.Namespace, jobInfo.Type, jobInfo.Name)
}