-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathselect_workloads.go
More file actions
133 lines (116 loc) · 3.41 KB
/
select_workloads.go
File metadata and controls
133 lines (116 loc) · 3.41 KB
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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
)
func selectWorkloads(scanner *bufio.Scanner) ([]Workload, error) {
fmt.Println("Input the workload id which should be selected, example: 0,1,2,3; or a range, example 1-9")
fmt.Print("Input: ")
if !scanner.Scan() {
return nil, fmt.Errorf("input nothing")
}
input := scanner.Text()
var workloadsIDs []string
if strings.Contains(input, "-") {
ids := strings.Split(input, "-")
startIDStr, endIDStr := ids[0], ids[1]
startID, err := strconv.Atoi(startIDStr)
if err != nil {
return nil, err
}
endID, err := strconv.Atoi(endIDStr)
if err != nil {
return nil, err
}
for i := startID; i <= endID; i++ {
workloadsIDs = append(workloadsIDs, strconv.Itoa(i))
}
} else {
workloadsIDs = strings.Split(input, ",")
}
selectedWorkloads := make([]Workload, len(workloadsIDs))
for i, id := range workloadsIDs {
idInt, err := strconv.Atoi(id)
if err != nil {
return nil, fmt.Errorf("error converting workload id '%s' to int", id)
}
if idInt >= len(workloads) {
return nil, fmt.Errorf("wrong workload id '%s'", id)
}
selectedWorkloads[i] = workloads[idInt]
}
printSelectedWorkloadsTable(selectedWorkloads, "")
fmt.Print("Press 'Enter' to confirm the workloads, or input others to skip: ")
if !scanner.Scan() {
return nil, fmt.Errorf("input nothing")
}
confirmInput := scanner.Text()
if confirmInput != "" {
fmt.Println("You have skipped the workloads.")
return nil, fmt.Errorf("confirm failed")
}
return selectedWorkloads, nil
}
func printSelectedWorkloadsTable(selectedWorkloads []Workload, namespace string) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.SetStyle(table.StyleLight)
armSupported := CheckAllWorkloadsArm(selectedWorkloads)
t.AppendHeader(table.Row{"ID", "Namespace", "Kind", "Name", "Replicas", "Available", "Ready",
"MigratePatched", "ARMSupported", "ARMPatched", "Priority"})
for id, w := range selectedWorkloads {
if namespace == "" || namespace == w.Namespace {
t.AppendRow(table.Row{
id,
w.Namespace,
w.Kind,
w.Name,
w.Replicas,
w.Available,
func() interface{} {
if w.Ready {
return text.Colors{text.FgGreen}.Sprint("True")
}
return text.Colors{text.FgRed}.Sprint("False")
}(),
func() interface{} {
if w.MigratePatched {
return text.Colors{text.FgGreen}.Sprint("True")
}
return text.Colors{text.FgRed}.Sprint("False")
}(),
func() interface{} {
if armSupported[id].Err != nil {
fmt.Printf("Failed to check arm support for workload %s %s/%s: %v",
w.Kind, w.Namespace, w.Name, armSupported[id].Err)
return text.Colors{text.FgRed}.Sprint("Unknown")
}
if armSupported[id].Supported {
return text.Colors{text.FgGreen}.Sprint("True")
}
return text.Colors{text.FgRed}.Sprint("False")
}(),
func() interface{} {
if w.ARMPatched {
return text.Colors{text.FgGreen}.Sprint("True")
}
return text.Colors{text.FgRed}.Sprint("False")
}(),
w.Priority,
})
}
}
t.SetColumnConfigs([]table.ColumnConfig{
{Number: 1, AutoMerge: true, Colors: text.Colors{text.FgCyan}},
{Number: 2, AutoMerge: true, Colors: text.Colors{text.FgCyan}},
{Number: 3, AutoMerge: true, Colors: text.Colors{text.FgCyan}},
})
t.Style().Options.SeparateRows = true
t.Render()
return
}