enhance ilm show, add ilm update, rename modify to update everywhere (#58)

This commit is contained in:
T. von Dein
2026-06-29 11:48:29 +02:00
parent b7a87051c0
commit f9eded5f92
4 changed files with 135 additions and 19 deletions

View File

@@ -36,7 +36,8 @@ func Ilm(conf *cfg.Config) *cli.Command {
IlmStatus(conf), IlmStatus(conf),
IlmList(conf), IlmList(conf),
IlmShow(conf), IlmShow(conf),
IlmCreate(conf), IlmCreate(conf, false),
IlmCreate(conf, true),
IlmForecast(conf), IlmForecast(conf),
IlmExplain(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{ return &cli.Command{
Name: "create", Name: name,
Aliases: []string{"+"}, Aliases: []string{alias},
Usage: "create a index lifecycle policy", Usage: usage,
UsageText: "create [options] <policy>", UsageText: name + " [options] <policy>",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{

View File

@@ -154,8 +154,8 @@ func IndexCreate(conf *cfg.Config, modify bool) *cli.Command {
usage := "create a new index" usage := "create a new index"
if modify { if modify {
name = "modify" name = "update"
usage = "modify anindex" usage = "update an index"
} }
return &cli.Command{ return &cli.Command{

View File

@@ -83,8 +83,8 @@ func IndexTemplateCreate(conf *cfg.Config, modify bool) *cli.Command {
required := true required := true
if modify { if modify {
name = "modify" name = "update"
alias = "mod" alias = "upd"
required = false required = false
} }

View File

@@ -149,12 +149,21 @@ func IlmShowTree(conf *cfg.Config, ilm types.IlmPolicy) error {
for _, phase := range IlmPhaseOrder { for _, phase := range IlmPhaseOrder {
if phase == "hot" { 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, phase,
indent, formatDuration(parseDuration(ilm.Phases.Hot.Actions.Rollover.MaxAge.(string))), 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 += " " indent += " "
continue continue
} }
@@ -175,10 +184,33 @@ func IlmShowTree(conf *cfg.Config, ilm types.IlmPolicy) error {
) )
if current.Actions.SearchableSnapshot != nil { if current.Actions.SearchableSnapshot != nil {
fmt.Printf("%s snapshot repo: %s\n", fmt.Printf("%s roll to snapshot repo: %s\n",
indent, current.Actions.SearchableSnapshot.SnapshotRepository, 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 += " " indent += " "
@@ -287,6 +319,24 @@ func IlmExplain(conf *cfg.Config, index string) error {
} }
func IlmCreate(conf *cfg.Config, policyname 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) ilm := conf.DefaultCluster.ES().Ilm.PutLifecycle(policyname)
cfg := conf.Ilm cfg := conf.Ilm
@@ -298,6 +348,16 @@ func IlmCreate(conf *cfg.Config, policyname string) error {
rollover := &types.RolloverAction{} rollover := &types.RolloverAction{}
haveroll := false 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 != "" { if cfg.HotMinAge != "" {
hot.MinAge = cfg.HotMinAge hot.MinAge = cfg.HotMinAge
} }
@@ -322,12 +382,22 @@ func IlmCreate(conf *cfg.Config, policyname string) error {
hot.Actions = actions.IlmActionsCaster() hot.Actions = actions.IlmActionsCaster()
phases.PhasesCaster().Hot = &hot phases.PhasesCaster().Hot = &hot
} else {
if policy != nil {
// update
phases.PhasesCaster().Hot = policy.Phases.Hot
}
} }
if cfg.HaveWarm() { if cfg.HaveWarm() {
warm := types.Phase{} warm := types.Phase{}
var actions types.IlmActionsVariant = esdsl.NewIlmActions() var actions types.IlmActionsVariant = esdsl.NewIlmActions()
if policy != nil {
// update
actions = policy.Phases.Warm.Actions
}
if cfg.WarmForceMerge != 0 { if cfg.WarmForceMerge != 0 {
actions.IlmActionsCaster().Forcemerge = &types.ForceMergeAction{MaxNumSegments: cfg.WarmForceMerge} actions.IlmActionsCaster().Forcemerge = &types.ForceMergeAction{MaxNumSegments: cfg.WarmForceMerge}
} }
@@ -346,12 +416,22 @@ func IlmCreate(conf *cfg.Config, policyname string) error {
warm.Actions = actions.IlmActionsCaster() warm.Actions = actions.IlmActionsCaster()
phases.PhasesCaster().Warm = &warm phases.PhasesCaster().Warm = &warm
} else {
if policy != nil && policy.Phases.Warm != nil {
// update
phases.PhasesCaster().Warm = policy.Phases.Warm
}
} }
if cfg.HaveCold() { if cfg.HaveCold() {
cold := types.Phase{} cold := types.Phase{}
var actions types.IlmActionsVariant = esdsl.NewIlmActions() var actions types.IlmActionsVariant = esdsl.NewIlmActions()
if policy != nil {
// update
actions = policy.Phases.Cold.Actions
}
if cfg.ColdForceMerge != 0 { if cfg.ColdForceMerge != 0 {
actions.IlmActionsCaster().Forcemerge = &types.ForceMergeAction{MaxNumSegments: cfg.ColdForceMerge} actions.IlmActionsCaster().Forcemerge = &types.ForceMergeAction{MaxNumSegments: cfg.ColdForceMerge}
} }
@@ -371,12 +451,22 @@ func IlmCreate(conf *cfg.Config, policyname string) error {
cold.Actions = actions.IlmActionsCaster() cold.Actions = actions.IlmActionsCaster()
phases.PhasesCaster().Cold = &cold phases.PhasesCaster().Cold = &cold
} else {
if policy != nil && policy.Phases.Cold != nil {
// update
phases.PhasesCaster().Cold = policy.Phases.Cold
}
} }
if cfg.HaveFrozen() { if cfg.HaveFrozen() {
froze := types.Phase{} froze := types.Phase{}
var actions types.IlmActionsVariant = esdsl.NewIlmActions() var actions types.IlmActionsVariant = esdsl.NewIlmActions()
if policy != nil {
// update
actions = policy.Phases.Frozen.Actions
}
if cfg.FrozenMinAge != "" { if cfg.FrozenMinAge != "" {
froze.MinAge = cfg.FrozenMinAge froze.MinAge = cfg.FrozenMinAge
} }
@@ -388,6 +478,11 @@ func IlmCreate(conf *cfg.Config, policyname string) error {
froze.Actions = actions.IlmActionsCaster() froze.Actions = actions.IlmActionsCaster()
phases.PhasesCaster().Frozen = &froze phases.PhasesCaster().Frozen = &froze
} else {
if policy != nil && policy.Phases.Frozen != nil {
// update
phases.PhasesCaster().Frozen = policy.Phases.Frozen
}
} }
if cfg.HaveDelete() { if cfg.HaveDelete() {
@@ -395,6 +490,11 @@ func IlmCreate(conf *cfg.Config, policyname string) error {
delete := types.DeleteAction{} delete := types.DeleteAction{}
var actions types.IlmActionsVariant = esdsl.NewIlmActions() var actions types.IlmActionsVariant = esdsl.NewIlmActions()
if policy != nil {
// update
actions = policy.Phases.Delete.Actions
}
if cfg.DeleteMinAge != "" { if cfg.DeleteMinAge != "" {
del.MinAge = cfg.DeleteMinAge del.MinAge = cfg.DeleteMinAge
} }
@@ -406,16 +506,21 @@ func IlmCreate(conf *cfg.Config, policyname string) error {
actions.IlmActionsCaster().Delete = &delete actions.IlmActionsCaster().Delete = &delete
del.Actions = actions.IlmActionsCaster() del.Actions = actions.IlmActionsCaster()
phases.PhasesCaster().Delete = &del phases.PhasesCaster().Delete = &del
} else {
if policy != nil && policy.Phases.Delete != nil {
// update
phases.PhasesCaster().Delete = policy.Phases.Delete
}
} }
put := &putlifecycle.Request{} put := &putlifecycle.Request{}
policy := &types.IlmPolicy{} newpolicy := &types.IlmPolicy{}
policy.IlmPolicyCaster().Phases = *phases.PhasesCaster() newpolicy.IlmPolicyCaster().Phases = *phases.PhasesCaster()
put.Policy = policy put.Policy = newpolicy
ilm.Request(put) ilm.Request(put)
_, err := ilm.Do(context.Background()) _, err = ilm.Do(context.Background())
if err != nil { if err != nil {
return fmt.Errorf("failed create ilm policy: %s", esErrorString(err)) return fmt.Errorf("failed create ilm policy: %s", esErrorString(err))
} }