-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_ilm_list.go
More file actions
357 lines (296 loc) · 8.7 KB
/
cmd_ilm_list.go
File metadata and controls
357 lines (296 loc) · 8.7 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package main
import (
"context"
"fmt"
"hash/fnv"
"os"
"slices"
"sort"
"strconv"
"strings"
"time"
"github.com/docker/go-units"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/typedapi/types"
"github.com/elastic/go-elasticsearch/v8/typedapi/types/enums/bytes"
"github.com/olekukonko/tablewriter"
"github.com/olekukonko/tablewriter/renderer"
"github.com/olekukonko/tablewriter/tw"
"github.com/urfave/cli/v3"
)
func ilmList(ctx context.Context, cmd *cli.Command) error {
deployment := cmd.String("deployment")
region := cmd.String("region")
username := cmd.String("username")
password := cmd.String("password")
action := cmd.String("action")
phase := cmd.String("phase")
ilmPolicy := cmd.String("ilm-policy")
sortColumns := cmd.StringSlice("sort")
minPriSizeStr := cmd.String("min-pri-size")
minTotalSizeStr := cmd.String("min-total-size")
minAge := time.Duration(cmd.Int("min-age-days")) * 24 * time.Hour
format := cmd.String("format")
if !isRegionValid(region) {
return fmt.Errorf("region %q is not a known Elastic Cloud region", region)
}
regionParts := strings.Split(region, "-")
if len(regionParts) != 2 {
return fmt.Errorf(`invalid region, expected format "<provider>-<region>", e.g. "azure-westeurope"`)
}
allowedSortColumns := []string{"age", "pri-size", "total-size"}
for _, sortColumn := range sortColumns {
if !slices.Contains(allowedSortColumns, sortColumn) {
return fmt.Errorf("column %q is not allowed for sorting, use one of %v", sortColumn, allowedSortColumns)
}
}
var err error
var minPriSize int64
if minPriSizeStr != "" {
minPriSize, err = units.FromHumanSize(minPriSizeStr)
if err != nil {
return fmt.Errorf("failed to parse minimum size: %w", err)
}
}
var minTotalSize int64
if minTotalSizeStr != "" {
minTotalSize, err = units.FromHumanSize(minTotalSizeStr)
if err != nil {
return fmt.Errorf("failed to parse minimum size: %w", err)
}
}
provider := regionParts[0]
providerRegion := regionParts[1]
deploymentURL := fmt.Sprintf("https://%s.es.%s.%s.elastic-cloud.com", deployment, providerRegion, provider)
client, err := elasticsearch.NewTypedClient(elasticsearch.Config{
Addresses: []string{
deploymentURL,
},
Username: username,
Password: password,
})
if err != nil {
return err
}
indices, err := client.Cat.Indices().H("index", "store.size", "pri.store.size").Bytes(bytes.B).Do(ctx)
if err != nil {
return err
}
totalSizes := make(map[string]int64, len(indices))
priSizes := make(map[string]int64, len(indices))
for _, index := range indices {
totalSize, err := strconv.ParseInt(*index.StoreSize, 10, 64)
if err != nil {
return err
}
totalSizes[*index.Index] = totalSize
priSize, err := strconv.ParseInt(*index.PriStoreSize, 10, 64)
if err != nil {
return err
}
priSizes[*index.Index] = priSize
}
ilms, err := client.Ilm.ExplainLifecycle("_all").OnlyManaged(true).Do(ctx)
if err != nil {
return err
}
type indexDetails struct {
name string
phase string
action string
step string
policy string
age time.Duration
priSize int64
totalSize int64
}
indexILM := make([]indexDetails, 0, len(ilms.Indices))
for index, ilm := range ilms.Indices {
managed, ok := ilm.(*types.LifecycleExplainManaged)
if !ok {
continue
}
if action != "" && action != *managed.Action {
continue
}
if phase != "" && phase != *managed.Phase {
continue
}
if ilmPolicy != "" && ilmPolicy != *managed.Policy {
continue
}
priSize := priSizes[index]
if priSize < minPriSize {
continue
}
totalSize := totalSizes[index]
if totalSize < minTotalSize {
continue
}
age, err := parseESDuration(managed.Age)
if err != nil {
return err
}
if age < minAge {
continue
}
indexILM = append(indexILM, indexDetails{name: index, phase: *managed.Phase, action: *managed.Action, step: *managed.Step, policy: *managed.Policy, age: age, priSize: priSize, totalSize: totalSize})
}
// Apply the sort criteria as less functions controlled by:
// - might the result contain multiple phases
// - provided sort columns, applied in order
lessFuncs := []func(i, j int) (final, less bool){}
if phase == "" {
lessFuncs = append(lessFuncs, func(i, j int) (final bool, less bool) {
if indexILM[i].phase != indexILM[j].phase {
return true, phaseLess(indexILM[i].phase, indexILM[j].phase)
}
return false, false
})
}
for _, sortColumn := range sortColumns {
switch sortColumn {
case "age":
lessFuncs = append(lessFuncs, func(i, j int) (final bool, less bool) {
if indexILM[i].age != indexILM[j].age {
return true, indexILM[i].age > indexILM[j].age
}
return false, false
})
case "pri-size":
lessFuncs = append(lessFuncs, func(i, j int) (final bool, less bool) {
if indexILM[i].priSize != indexILM[j].priSize {
return true, indexILM[i].priSize > indexILM[j].priSize
}
return false, false
})
case "total-size":
lessFuncs = append(lessFuncs, func(i, j int) (final bool, less bool) {
if indexILM[i].totalSize != indexILM[j].totalSize {
return true, indexILM[i].totalSize > indexILM[j].totalSize
}
return false, false
})
}
}
// Sort indices by the lessFuncs.
sort.Slice(indexILM, func(i, j int) bool {
for _, lessFunc := range lessFuncs {
final, less := lessFunc(i, j)
if final {
return less
}
}
return indexILM[i].name < indexILM[j].name
})
data := make([][]string, 0, len(indexILM))
for _, item := range indexILM {
data = append(data, []string{item.name, item.phase, item.action, item.step, item.policy, formatDuration(item.age), units.BytesSize(float64(item.priSize)), units.BytesSize(float64(item.totalSize))})
}
opts := []tablewriter.Option{
tablewriter.WithRenderer(
renderer.NewBlueprint(),
),
}
switch format {
case "compact":
opts = []tablewriter.Option{
tablewriter.WithRenderer(
renderer.NewBlueprint(
tw.Rendition{
Borders: tw.BorderNone,
Settings: tw.Settings{
Lines: tw.LinesNone,
Separators: tw.SeparatorsNone,
},
},
),
),
}
}
table := tablewriter.NewTable(os.Stdout, opts...)
table.Header([]string{
"Index", "Phase", "Action", "Step", "Policy", "Age", "Pri Size", "Total Size",
})
err = table.Bulk(data)
if err != nil {
return err
}
err = table.Render()
if err != nil {
return err
}
return nil
}
// parseESDuration parses the duration format of Elasticsearch to a Go duration.
// ES duration does support days, which are not supported by Go durations. For
// simplicity reasons, days are just converted to 24 hours. This is not the correct
// thing to do in all cases (e.g. daylight saving), but these execeptions are
// accepted in this case, since it is not expected, that this difference will
// matter for the use case at hand.
func parseESDuration(esDuration types.Duration) (time.Duration, error) {
durationStr, ok := esDuration.(string)
if !ok {
return 0, fmt.Errorf("unexpected type for types.Duration, got %T, want: string", esDuration)
}
if strings.HasSuffix(durationStr, "d") {
// Days are not supported by go durations, handle it manually
durationStr = strings.TrimSuffix(durationStr, "d")
days, err := strconv.ParseFloat(durationStr, 64)
if err != nil {
return 0, err
}
return time.Duration(days) * 24 * time.Hour, nil
}
return time.ParseDuration(durationStr)
}
const (
day = time.Minute * 60 * 24
year = 365 * day
)
// formatDuration returns a given formatDuration rounded as formatted string
// with a focus on days and years.
// The rounding has the following logic:
// - If the formatDuration is less than a day, use the standard formatting from Go (hours, minutes and seconds, leading units, which are 0 are omitted).
// - If the formatDuration more than a year, prepend the number of years. A year is always considered to be 365 days, leap years are ignored.
// - Append the remainder of days, omit any more fine grained units.
func formatDuration(d time.Duration) string {
if d < day {
return d.String()
}
var b strings.Builder
if d >= year {
years := d / year
fmt.Fprintf(&b, "%dy", years)
d -= years * year
}
days := d / day
fmt.Fprintf(&b, "%dd", days)
return b.String()
}
var phaseOrder = map[string]int64{
"hot": 0,
"warm": 1,
"cold": 2,
"frozen": 3,
}
// phaseLess returns if phase a has the lower order than phase b.
// Unknown phases are sorted at the end, same phases properly grouped together.
func phaseLess(a, b string) bool {
var aa int64
var bb int64
var ok bool
aa, ok = phaseOrder[a]
if !ok {
h := fnv.New32a()
h.Write([]byte(a))
aa = 1<<32 + int64(h.Sum32())
}
bb, ok = phaseOrder[b]
if !ok {
h := fnv.New32a()
h.Write([]byte(b))
bb = 1<<32 + int64(h.Sum32())
}
return aa < bb
}