From f9eded5f926f5d9125515dc3d031746af5068a5b Mon Sep 17 00:00:00 2001 From: "T. von Dein" Date: Mon, 29 Jun 2026 11:48:29 +0200 Subject: [PATCH] enhance ilm show, add ilm update, rename modify to update everywhere (#58) --- cmd/ilm.go | 23 +++++--- cmd/index.go | 4 +- cmd/index_template.go | 4 +- pkg/es/ilm.go | 123 ++++++++++++++++++++++++++++++++++++++---- 4 files changed, 135 insertions(+), 19 deletions(-) diff --git a/cmd/ilm.go b/cmd/ilm.go index ca72b56..e999de5 100644 --- a/cmd/ilm.go +++ b/cmd/ilm.go @@ -36,7 +36,8 @@ func Ilm(conf *cfg.Config) *cli.Command { IlmStatus(conf), IlmList(conf), IlmShow(conf), - IlmCreate(conf), + IlmCreate(conf, false), + IlmCreate(conf, true), IlmForecast(conf), IlmExplain(conf), }, @@ -127,12 +128,22 @@ func IlmShow(conf *cfg.Config) *cli.Command { } } -func IlmCreate(conf *cfg.Config) *cli.Command { +func IlmCreate(conf *cfg.Config, modify bool) *cli.Command { + name := "create" + alias := "+" + usage := "create a new lifecycle policy" + + if modify { + name = "update" + usage = "update an lifecycle policy" + alias = "upd" + } + return &cli.Command{ - Name: "create", - Aliases: []string{"+"}, - Usage: "create a index lifecycle policy", - UsageText: "create [options] ", + Name: name, + Aliases: []string{alias}, + Usage: usage, + UsageText: name + " [options] ", Flags: []cli.Flag{ &cli.StringFlag{ diff --git a/cmd/index.go b/cmd/index.go index 2488bf4..20be4d9 100644 --- a/cmd/index.go +++ b/cmd/index.go @@ -154,8 +154,8 @@ func IndexCreate(conf *cfg.Config, modify bool) *cli.Command { usage := "create a new index" if modify { - name = "modify" - usage = "modify anindex" + name = "update" + usage = "update an index" } return &cli.Command{ diff --git a/cmd/index_template.go b/cmd/index_template.go index 180901e..b00bd72 100644 --- a/cmd/index_template.go +++ b/cmd/index_template.go @@ -83,8 +83,8 @@ func IndexTemplateCreate(conf *cfg.Config, modify bool) *cli.Command { required := true if modify { - name = "modify" - alias = "mod" + name = "update" + alias = "upd" required = false } diff --git a/pkg/es/ilm.go b/pkg/es/ilm.go index 6514f80..804192e 100644 --- a/pkg/es/ilm.go +++ b/pkg/es/ilm.go @@ -149,12 +149,21 @@ func IlmShowTree(conf *cfg.Config, ilm types.IlmPolicy) error { 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", + fmt.Printf("%s%s phase:\n%s rollover after %s\n", indent, phase, indent, formatDuration(parseDuration(ilm.Phases.Hot.Actions.Rollover.MaxAge.(string))), - indent, ilm.Phases.Hot.Actions.Rollover.MaxPrimaryShardSize, ) + if ilm.Phases.Hot.Actions.Rollover.MaxPrimaryShardSize != "" { + fmt.Printf("%s rollover when storage > %s\n", + indent, ilm.Phases.Hot.Actions.Rollover.MaxPrimaryShardSize) + } + + if ilm.Phases.Hot.Actions.Rollover.MaxDocs != nil { + fmt.Printf("%s rollover docs > %d\n", + indent, *ilm.Phases.Hot.Actions.Rollover.MaxDocs) + } + indent += " " continue } @@ -175,10 +184,33 @@ func IlmShowTree(conf *cfg.Config, ilm types.IlmPolicy) error { ) if current.Actions.SearchableSnapshot != nil { - fmt.Printf("%s snapshot repo: %s\n", - indent, current.Actions.SearchableSnapshot.SnapshotRepository, - ) + fmt.Printf("%s roll to snapshot repo: %s\n", + indent, current.Actions.SearchableSnapshot.SnapshotRepository) } + + if current.Actions.Forcemerge != nil { + fmt.Printf("%s force merge segments: %d\n", + indent, current.Actions.Forcemerge.MaxNumSegments) + } + + if current.Actions.Shrink != nil { + fmt.Printf("%s shrink shards: %d\n", + indent, *current.Actions.Shrink.NumberOfShards) + } + + if current.Actions.SetPriority != nil { + fmt.Printf("%s priority: %d\n", + indent, *current.Actions.SetPriority.Priority) + } + + if current.Actions.Delete != nil && current.Actions.Delete.DeleteSearchableSnapshot != nil { + fmt.Printf("%s delete searchable snapshots: %t\n", + indent, *current.Actions.Delete.DeleteSearchableSnapshot) + } + } + + if phase == "delete" { + fmt.Printf("%s delete immediately\n", indent) } indent += " " @@ -287,6 +319,24 @@ func IlmExplain(conf *cfg.Config, index string) error { } func IlmCreate(conf *cfg.Config, policyname string) error { + var policy *types.IlmPolicy = nil + + res, err := conf.DefaultCluster.ES().Ilm.GetLifecycle(). + Policy(policyname). + Do(context.Background()) + if err == nil { + if conf.Debug { + repr.Println(res) + } + + ilm, exists := res[policyname] + if !exists { + return errors.New("no ilm policy retrieved") + } + + policy = &ilm.Policy + } + ilm := conf.DefaultCluster.ES().Ilm.PutLifecycle(policyname) cfg := conf.Ilm @@ -298,6 +348,16 @@ func IlmCreate(conf *cfg.Config, policyname string) error { rollover := &types.RolloverAction{} haveroll := false + if policy != nil { + // update + actions = policy.Phases.Hot.Actions + + if policy.Phases.Hot.Actions.Rollover != nil { + rollover = policy.Phases.Hot.Actions.Rollover + haveroll = true + } + } + if cfg.HotMinAge != "" { hot.MinAge = cfg.HotMinAge } @@ -322,12 +382,22 @@ func IlmCreate(conf *cfg.Config, policyname string) error { hot.Actions = actions.IlmActionsCaster() phases.PhasesCaster().Hot = &hot + } else { + if policy != nil { + // update + phases.PhasesCaster().Hot = policy.Phases.Hot + } } if cfg.HaveWarm() { warm := types.Phase{} var actions types.IlmActionsVariant = esdsl.NewIlmActions() + if policy != nil { + // update + actions = policy.Phases.Warm.Actions + } + if cfg.WarmForceMerge != 0 { actions.IlmActionsCaster().Forcemerge = &types.ForceMergeAction{MaxNumSegments: cfg.WarmForceMerge} } @@ -346,12 +416,22 @@ func IlmCreate(conf *cfg.Config, policyname string) error { warm.Actions = actions.IlmActionsCaster() phases.PhasesCaster().Warm = &warm + } else { + if policy != nil && policy.Phases.Warm != nil { + // update + phases.PhasesCaster().Warm = policy.Phases.Warm + } } if cfg.HaveCold() { cold := types.Phase{} var actions types.IlmActionsVariant = esdsl.NewIlmActions() + if policy != nil { + // update + actions = policy.Phases.Cold.Actions + } + if cfg.ColdForceMerge != 0 { actions.IlmActionsCaster().Forcemerge = &types.ForceMergeAction{MaxNumSegments: cfg.ColdForceMerge} } @@ -371,12 +451,22 @@ func IlmCreate(conf *cfg.Config, policyname string) error { cold.Actions = actions.IlmActionsCaster() phases.PhasesCaster().Cold = &cold + } else { + if policy != nil && policy.Phases.Cold != nil { + // update + phases.PhasesCaster().Cold = policy.Phases.Cold + } } if cfg.HaveFrozen() { froze := types.Phase{} var actions types.IlmActionsVariant = esdsl.NewIlmActions() + if policy != nil { + // update + actions = policy.Phases.Frozen.Actions + } + if cfg.FrozenMinAge != "" { froze.MinAge = cfg.FrozenMinAge } @@ -388,6 +478,11 @@ func IlmCreate(conf *cfg.Config, policyname string) error { froze.Actions = actions.IlmActionsCaster() phases.PhasesCaster().Frozen = &froze + } else { + if policy != nil && policy.Phases.Frozen != nil { + // update + phases.PhasesCaster().Frozen = policy.Phases.Frozen + } } if cfg.HaveDelete() { @@ -395,6 +490,11 @@ func IlmCreate(conf *cfg.Config, policyname string) error { delete := types.DeleteAction{} var actions types.IlmActionsVariant = esdsl.NewIlmActions() + if policy != nil { + // update + actions = policy.Phases.Delete.Actions + } + if cfg.DeleteMinAge != "" { del.MinAge = cfg.DeleteMinAge } @@ -406,16 +506,21 @@ func IlmCreate(conf *cfg.Config, policyname string) error { actions.IlmActionsCaster().Delete = &delete del.Actions = actions.IlmActionsCaster() phases.PhasesCaster().Delete = &del + } else { + if policy != nil && policy.Phases.Delete != nil { + // update + phases.PhasesCaster().Delete = policy.Phases.Delete + } } put := &putlifecycle.Request{} - policy := &types.IlmPolicy{} - policy.IlmPolicyCaster().Phases = *phases.PhasesCaster() - put.Policy = policy + newpolicy := &types.IlmPolicy{} + newpolicy.IlmPolicyCaster().Phases = *phases.PhasesCaster() + put.Policy = newpolicy ilm.Request(put) - _, err := ilm.Do(context.Background()) + _, err = ilm.Do(context.Background()) if err != nil { return fmt.Errorf("failed create ilm policy: %s", esErrorString(err)) }