Skip to content

Commit 96dd204

Browse files
authored
Merge branch 'master' into filter-by-ilm-policy
2 parents b2ead63 + 6a9f540 commit 96dd204

File tree

2 files changed

+105
-28
lines changed

2 files changed

+105
-28
lines changed

cmd_ilm_list.go

Lines changed: 88 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/elastic/go-elasticsearch/v8/typedapi/types"
1717
"github.com/elastic/go-elasticsearch/v8/typedapi/types/enums/bytes"
1818
"github.com/olekukonko/tablewriter"
19+
"github.com/olekukonko/tablewriter/renderer"
20+
"github.com/olekukonko/tablewriter/tw"
1921
"github.com/urfave/cli/v3"
2022
)
2123

@@ -25,11 +27,14 @@ func ilmList(ctx context.Context, cmd *cli.Command) error {
2527
username := cmd.String("username")
2628
password := cmd.String("password")
2729

30+
action := cmd.String("action")
2831
phase := cmd.String("phase")
2932
ilmPolicy := cmd.String("ilm-policy")
3033
sortColumns := cmd.StringSlice("sort")
31-
minSizeStr := cmd.String("min-size")
34+
minPriSizeStr := cmd.String("min-pri-size")
35+
minTotalSizeStr := cmd.String("min-total-size")
3236
minAge := time.Duration(cmd.Int("min-age-days")) * 24 * time.Hour
37+
format := cmd.String("format")
3338

3439
if !isRegionValid(region) {
3540
return fmt.Errorf("region %q is not a known Elastic Cloud region", region)
@@ -40,17 +45,25 @@ func ilmList(ctx context.Context, cmd *cli.Command) error {
4045
return fmt.Errorf(`invalid region, expected format "<provider>-<region>", e.g. "azure-westeurope"`)
4146
}
4247

43-
allowedSortColumns := []string{"age", "size"}
48+
allowedSortColumns := []string{"age", "pri-size", "total-size"}
4449
for _, sortColumn := range sortColumns {
4550
if !slices.Contains(allowedSortColumns, sortColumn) {
4651
return fmt.Errorf("column %q is not allowed for sorting, use one of %v", sortColumn, allowedSortColumns)
4752
}
4853
}
4954

50-
var minSize int64
5155
var err error
52-
if minSizeStr != "" {
53-
minSize, err = units.FromHumanSize(minSizeStr)
56+
var minPriSize int64
57+
if minPriSizeStr != "" {
58+
minPriSize, err = units.FromHumanSize(minPriSizeStr)
59+
if err != nil {
60+
return fmt.Errorf("failed to parse minimum size: %w", err)
61+
}
62+
}
63+
64+
var minTotalSize int64
65+
if minTotalSizeStr != "" {
66+
minTotalSize, err = units.FromHumanSize(minTotalSizeStr)
5467
if err != nil {
5568
return fmt.Errorf("failed to parse minimum size: %w", err)
5669
}
@@ -72,20 +85,28 @@ func ilmList(ctx context.Context, cmd *cli.Command) error {
7285
return err
7386
}
7487

75-
indices, err := client.Cat.Indices().H("index", "store.size").Bytes(bytes.B).Do(ctx)
88+
indices, err := client.Cat.Indices().H("index", "store.size", "pri.store.size").Bytes(bytes.B).Do(ctx)
7689
if err != nil {
7790
return err
7891
}
7992

80-
sizes := make(map[string]int64, len(indices))
93+
totalSizes := make(map[string]int64, len(indices))
94+
priSizes := make(map[string]int64, len(indices))
8195

8296
for _, index := range indices {
83-
size, err := strconv.ParseInt(*index.StoreSize, 10, 64)
97+
totalSize, err := strconv.ParseInt(*index.StoreSize, 10, 64)
98+
if err != nil {
99+
return err
100+
}
101+
102+
totalSizes[*index.Index] = totalSize
103+
104+
priSize, err := strconv.ParseInt(*index.PriStoreSize, 10, 64)
84105
if err != nil {
85106
return err
86107
}
87108

88-
sizes[*index.Index] = size
109+
priSizes[*index.Index] = priSize
89110
}
90111

91112
ilms, err := client.Ilm.ExplainLifecycle("_all").OnlyManaged(true).Do(ctx)
@@ -94,13 +115,14 @@ func ilmList(ctx context.Context, cmd *cli.Command) error {
94115
}
95116

96117
type indexDetails struct {
97-
name string
98-
phase string
99-
action string
100-
step string
101-
policy string
102-
age time.Duration
103-
size int64
118+
name string
119+
phase string
120+
action string
121+
step string
122+
policy string
123+
age time.Duration
124+
priSize int64
125+
totalSize int64
104126
}
105127

106128
indexILM := make([]indexDetails, 0, len(ilms.Indices))
@@ -111,6 +133,10 @@ func ilmList(ctx context.Context, cmd *cli.Command) error {
111133
continue
112134
}
113135

136+
if action != "" && action != *managed.Action {
137+
continue
138+
}
139+
114140
if phase != "" && phase != *managed.Phase {
115141
continue
116142
}
@@ -119,9 +145,15 @@ func ilmList(ctx context.Context, cmd *cli.Command) error {
119145
continue
120146
}
121147

122-
size := sizes[index]
148+
priSize := priSizes[index]
123149

124-
if size < minSize {
150+
if priSize < minPriSize {
151+
continue
152+
}
153+
154+
totalSize := totalSizes[index]
155+
156+
if totalSize < minTotalSize {
125157
continue
126158
}
127159

@@ -134,7 +166,7 @@ func ilmList(ctx context.Context, cmd *cli.Command) error {
134166
continue
135167
}
136168

137-
indexILM = append(indexILM, indexDetails{name: index, phase: *managed.Phase, action: *managed.Action, step: *managed.Step, policy: *managed.Policy, age: age, size: size})
169+
indexILM = append(indexILM, indexDetails{name: index, phase: *managed.Phase, action: *managed.Action, step: *managed.Step, policy: *managed.Policy, age: age, priSize: priSize, totalSize: totalSize})
138170
}
139171

140172
// Apply the sort criteria as less functions controlled by:
@@ -162,10 +194,19 @@ func ilmList(ctx context.Context, cmd *cli.Command) error {
162194
return false, false
163195
})
164196

165-
case "size":
197+
case "pri-size":
166198
lessFuncs = append(lessFuncs, func(i, j int) (final bool, less bool) {
167-
if indexILM[i].size != indexILM[j].size {
168-
return true, indexILM[i].size > indexILM[j].size
199+
if indexILM[i].priSize != indexILM[j].priSize {
200+
return true, indexILM[i].priSize > indexILM[j].priSize
201+
}
202+
203+
return false, false
204+
})
205+
206+
case "total-size":
207+
lessFuncs = append(lessFuncs, func(i, j int) (final bool, less bool) {
208+
if indexILM[i].totalSize != indexILM[j].totalSize {
209+
return true, indexILM[i].totalSize > indexILM[j].totalSize
169210
}
170211

171212
return false, false
@@ -187,12 +228,34 @@ func ilmList(ctx context.Context, cmd *cli.Command) error {
187228

188229
data := make([][]string, 0, len(indexILM))
189230
for _, item := range indexILM {
190-
data = append(data, []string{item.name, item.phase, item.action, item.step, item.policy, formatDuration(item.age), units.BytesSize(float64(item.size))})
231+
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))})
232+
}
233+
234+
opts := []tablewriter.Option{
235+
tablewriter.WithRenderer(
236+
renderer.NewBlueprint(),
237+
),
238+
}
239+
switch format {
240+
case "compact":
241+
opts = []tablewriter.Option{
242+
tablewriter.WithRenderer(
243+
renderer.NewBlueprint(
244+
tw.Rendition{
245+
Borders: tw.BorderNone,
246+
Settings: tw.Settings{
247+
Lines: tw.LinesNone,
248+
Separators: tw.SeparatorsNone,
249+
},
250+
},
251+
),
252+
),
253+
}
191254
}
192255

193-
table := tablewriter.NewWriter(os.Stdout)
256+
table := tablewriter.NewTable(os.Stdout, opts...)
194257
table.Header([]string{
195-
"Index", "Phase", "Action", "Step", "Policy", "Age", "Size",
258+
"Index", "Phase", "Action", "Step", "Policy", "Age", "Pri Size", "Total Size",
196259
})
197260
err = table.Bulk(data)
198261
if err != nil {

main.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ func run(ctx context.Context, args []string) error {
6363
Name: "list",
6464
Usage: "list ilm managed indices filtered by phase",
6565
Flags: []cli.Flag{
66+
&cli.StringFlag{
67+
Name: "action",
68+
Aliases: []string{"a"},
69+
Usage: "Filter to only include indices in the given action",
70+
},
6671
&cli.StringFlag{
6772
Name: "phase",
6873
Aliases: []string{"p"},
@@ -76,16 +81,25 @@ func run(ctx context.Context, args []string) error {
7681
&cli.StringSliceFlag{
7782
Name: "sort",
7883
Aliases: []string{"s"},
79-
Usage: "Sort indices by the given columns, allowed columns are: age, size",
84+
Usage: "Sort indices by the given columns, allowed columns are: age, pri-size, total-size",
85+
},
86+
&cli.StringFlag{
87+
Name: "min-total-size",
88+
Usage: "Minimum total size (primary shard + replicas) of index in order to be contained in the result, supported units: k, m, g, t, p",
8089
},
8190
&cli.StringFlag{
82-
Name: "min-size",
83-
Usage: "Minimum size of index in order to be contained in the result, supported units: k, m, g, t, p",
91+
Name: "min-pri-size",
92+
Usage: "Minimum primary shard size of index in order to be contained in the result, supported units: k, m, g, t, p",
8493
},
8594
&cli.IntFlag{
8695
Name: "min-age-days",
8796
Usage: "Minimum age of index in days in order to be contained in the result",
8897
},
98+
&cli.StringFlag{
99+
Name: "format",
100+
Aliases: []string{"f"},
101+
Usage: "Format for the result: table, compact (default: table)",
102+
},
89103
},
90104
Action: ilmList,
91105
},

0 commit comments

Comments
 (0)