add 'ilm show -t' to show a better visualization of a ilm policy (#57)

This commit is contained in:
T. von Dein
2026-06-29 10:11:50 +02:00
parent 81987b75f2
commit b7a87051c0
3 changed files with 77 additions and 0 deletions

View File

@@ -103,6 +103,15 @@ func IlmShow(conf *cfg.Config) *cli.Command {
Usage: "show details about an index lifecycle policy", Usage: "show details about an index lifecycle policy",
UsageText: "show <policy>", UsageText: "show <policy>",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "tree",
Usage: "display policy as a tree",
Destination: &conf.Ilm.Tree,
Aliases: []string{"t"},
},
},
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
policy := cmd.Args().Get(0) policy := cmd.Args().Get(0)
if policy == "" { if policy == "" {

View File

@@ -42,6 +42,7 @@ type Ilm struct {
DeleteSearchableSnapshots bool DeleteSearchableSnapshots bool
MinAge, FromPhase, Within string // forecast MinAge, FromPhase, Within string // forecast
Tree bool
} }
func (cfg *Ilm) HaveHot() bool { func (cfg *Ilm) HaveHot() bool {

View File

@@ -121,6 +121,10 @@ func IlmShow(conf *cfg.Config, policy string) error {
return errors.New("no ilm policy retrieved") return errors.New("no ilm policy retrieved")
} }
if conf.Ilm.Tree {
return IlmShowTree(conf, ilm.Policy)
}
table := printer.NewTable(conf, 2, 5) table := printer.NewTable(conf, 2, 5)
table.Addheaders("ilm policy setting", "value") table.Addheaders("ilm policy setting", "value")
@@ -135,6 +139,69 @@ func IlmShow(conf *cfg.Config, policy string) error {
return table.Print() return table.Print()
} }
// Visualize phases or a ILM policy, taking into account that the
// minAge for the current phases is set in the next phase
func IlmShowTree(conf *cfg.Config, ilm types.IlmPolicy) error {
indent := ""
table := printer.NewTable(conf, 4, 0)
table.Addheaders("phase", "min age", "min size", "snapshot repo")
for _, phase := range IlmPhaseOrder {
if phase == "hot" {
fmt.Printf("%s%s phase:\n%s rollover after %s\n%s or rollover when storage > %s\n",
indent, phase,
indent, formatDuration(parseDuration(ilm.Phases.Hot.Actions.Rollover.MaxAge.(string))),
indent, ilm.Phases.Hot.Actions.Rollover.MaxPrimaryShardSize,
)
indent += " "
continue
}
current := getIlmCurrentPhase(ilm, phase)
if current == nil {
continue
}
next := findNextPhase(ilm, phase)
fmt.Printf("%s%s phase:\n", indent, phase)
if next != nil {
fmt.Printf("%s rollover after %s\n",
indent, formatDuration(next.minage),
)
if current.Actions.SearchableSnapshot != nil {
fmt.Printf("%s snapshot repo: %s\n",
indent, current.Actions.SearchableSnapshot.SnapshotRepository,
)
}
}
indent += " "
}
return nil
}
func getIlmCurrentPhase(policy types.IlmPolicy, currentPhase string) *types.Phase {
switch currentPhase {
case "hot":
return policy.Phases.Hot
case "warm":
return policy.Phases.Warm
case "cold":
return policy.Phases.Cold
case "frozen":
return policy.Phases.Frozen
default:
return policy.Phases.Delete
}
}
func ilmPhaseString(phase *types.Phase, short bool) string { func ilmPhaseString(phase *types.Phase, short bool) string {
if phase == nil { if phase == nil {
return "" return ""