Skip to content

Add max_primary_shard_docs condition to ILM rollover #845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## [Unreleased]

- Add `max_primary_shard_docs` condition to ILM rollover ([#845](https://github.com/elastic/terraform-provider-elasticstack/pull/845))

## [0.11.13] - 2025-01-09

- Support 8.15.5 in acc tests ([#963](https://github.com/elastic/terraform-provider-elasticstack/pull/963)).
Expand All @@ -11,7 +13,7 @@

### Breaking changes

- Support multiple group by fields in SLOs ([#870](https://github.com/elastic/terraform-provider-elasticstack/pull/878)). This changes to type of the `group_by` attribute of the `elasticstack_kibana_slo` resource from a String to a list of Strings. Any existing SLO defintions will need to update `group_by = "field"` to `group_by = ["field"]`.
- Support multiple group by fields in SLOs ([#870](https://github.com/elastic/terraform-provider-elasticstack/pull/878)). This changes to type of the `group_by` attribute of the `elasticstack_kibana_slo` resource from a String to a list of Strings. Any existing SLO defintions will need to update `group_by = "field"` to `group_by = ["field"]`.

### Changes
- Handle NPE in integration policy secrets ([#946](https://github.com/elastic/terraform-provider-elasticstack/pull/946))
Expand Down
1 change: 1 addition & 0 deletions docs/resources/elasticsearch_index_lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ Optional:

- `max_age` (String) Triggers rollover after the maximum elapsed time from index creation is reached.
- `max_docs` (Number) Triggers rollover after the specified maximum number of documents is reached.
- `max_primary_shard_docs` (Number) Triggers rollover when the largest primary shard in the index reaches a certain number of documents. Supported from Elasticsearch version **8.2**
- `max_primary_shard_size` (String) Triggers rollover when the largest primary shard in the index reaches a certain size.
- `max_size` (String) Triggers rollover when the index reaches a certain size.
- `min_age` (String) Prevents rollover until after the minimum elapsed time from index creation is reached. Supported from Elasticsearch version **8.4**
Expand Down
2 changes: 1 addition & 1 deletion internal/elasticsearch/index/data_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestAccResourceDataStream(t *testing.T) {
// generate renadom name
// generate random name
dsName := sdkacctest.RandStringFromCharSet(22, sdkacctest.CharSetAlpha)

resource.Test(t, resource.TestCase{
Expand Down
30 changes: 20 additions & 10 deletions internal/elasticsearch/index/ilm.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ var supportedActions = map[string]*schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"max_primary_shard_docs": {
Description: "Triggers rollover when the largest primary shard in the index reaches a certain number of documents. Supported from Elasticsearch version **8.2**",
Type: schema.TypeInt,
Optional: true,
},
"max_primary_shard_size": {
Description: "Triggers rollover when the largest primary shard in the index reaches a certain size.",
Type: schema.TypeString,
Expand All @@ -288,16 +293,16 @@ var supportedActions = map[string]*schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"min_primary_shard_size": {
Description: "Prevents rollover until the largest primary shard in the index reaches a certain size. Supported from Elasticsearch version **8.4**",
Type: schema.TypeString,
Optional: true,
},
"min_primary_shard_docs": {
Description: "Prevents rollover until the largest primary shard in the index reaches a certain number of documents. Supported from Elasticsearch version **8.4**",
Type: schema.TypeInt,
Optional: true,
},
"min_primary_shard_size": {
Description: "Prevents rollover until the largest primary shard in the index reaches a certain size. Supported from Elasticsearch version **8.4**",
Type: schema.TypeString,
Optional: true,
},
},
},
},
Expand Down Expand Up @@ -529,7 +534,7 @@ func expandPhase(p map[string]interface{}, serverVersion *version.Version) (*mod
}
}
case "rollover":
actions[actionName], diags = expandAction(a, serverVersion, "max_age", "max_docs", "max_size", "max_primary_shard_size", "min_age", "min_docs", "min_size", "min_primary_shard_size", "min_primary_shard_docs")
actions[actionName], diags = expandAction(a, serverVersion, "max_age", "max_docs", "max_size", "max_primary_shard_docs", "max_primary_shard_size", "min_age", "min_docs", "min_size", "min_primary_shard_docs", "min_primary_shard_size")
case "searchable_snapshot":
actions[actionName], diags = expandAction(a, serverVersion, "snapshot_repository", "force_merge_index")
case "set_priority":
Expand Down Expand Up @@ -562,21 +567,26 @@ func expandPhase(p map[string]interface{}, serverVersion *version.Version) (*mod
return &phase, diags
}

var RolloverMinConditionsMinSupportedVersion = version.Must(version.NewVersion("8.4.0"))
var (
RolloverMinConditionsMinSupportedVersion = version.Must(version.NewVersion("8.4.0"))
MaxPrimaryShardDocsMinSupportedVersion = version.Must(version.NewVersion("8.2.0"))
)

var ilmActionSettingOptions = map[string]struct {
skipEmptyCheck bool
def interface{}
minVersion *version.Version
}{
"allow_write_after_shrink": {def: false, minVersion: version.Must(version.NewVersion("8.14.0"))},
"number_of_replicas": {skipEmptyCheck: true},
"total_shards_per_node": {skipEmptyCheck: true, def: -1, minVersion: version.Must(version.NewVersion("7.16.0"))},
"priority": {skipEmptyCheck: true},
"max_primary_shard_docs": {def: 0, minVersion: MaxPrimaryShardDocsMinSupportedVersion},
"min_age": {def: "", minVersion: RolloverMinConditionsMinSupportedVersion},
"min_docs": {def: 0, minVersion: RolloverMinConditionsMinSupportedVersion},
"min_size": {def: "", minVersion: RolloverMinConditionsMinSupportedVersion},
"min_primary_shard_size": {def: "", minVersion: RolloverMinConditionsMinSupportedVersion},
"min_primary_shard_docs": {def: 0, minVersion: RolloverMinConditionsMinSupportedVersion},
"allow_write_after_shrink": {def: false, minVersion: version.Must(version.NewVersion("8.14.0"))},
"min_primary_shard_size": {def: "", minVersion: RolloverMinConditionsMinSupportedVersion},
"total_shards_per_node": {skipEmptyCheck: true, def: -1, minVersion: version.Must(version.NewVersion("7.16.0"))},
}

func expandAction(a []interface{}, serverVersion *version.Version, settings ...string) (map[string]interface{}, diag.Diagnostics) {
Expand Down
38 changes: 36 additions & 2 deletions internal/elasticsearch/index/ilm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ func TestAccResourceILMRolloverConditions(t *testing.T) {
CheckDestroy: checkResourceILMDestroy,
ProtoV6ProviderFactories: acctest.Providers,
Steps: []resource.TestStep{
{
SkipFunc: versionutils.CheckIfVersionIsUnsupported(index.MaxPrimaryShardDocsMinSupportedVersion),
Config: testAccResourceILMCreateWithMaxPrimaryShardDocs(policyName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test_rollover", "name", policyName),
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test_rollover", "hot.0.rollover.0.max_primary_shard_docs", "5000"),
),
},
{
SkipFunc: versionutils.CheckIfVersionIsUnsupported(index.RolloverMinConditionsMinSupportedVersion),
Config: testAccResourceILMCreateWithRolloverConditions(policyName),
Expand All @@ -135,12 +143,13 @@ func TestAccResourceILMRolloverConditions(t *testing.T) {
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test_rollover", "hot.0.rollover.0.max_age", "7d"),
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test_rollover", "hot.0.rollover.0.max_docs", "10000"),
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test_rollover", "hot.0.rollover.0.max_size", "100gb"),
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test_rollover", "hot.0.rollover.0.max_primary_shard_docs", "5000"),
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test_rollover", "hot.0.rollover.0.max_primary_shard_size", "50gb"),
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test_rollover", "hot.0.rollover.0.min_age", "3d"),
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test_rollover", "hot.0.rollover.0.min_docs", "1000"),
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test_rollover", "hot.0.rollover.0.min_size", "50gb"),
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test_rollover", "hot.0.rollover.0.min_primary_shard_size", "25gb"),
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test_rollover", "hot.0.rollover.0.min_primary_shard_docs", "500"),
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test_rollover", "hot.0.rollover.0.min_primary_shard_size", "25gb"),
),
},
},
Expand Down Expand Up @@ -273,12 +282,37 @@ resource "elasticstack_elasticsearch_index_lifecycle" "test_rollover" {
max_age = "7d"
max_docs = 10000
max_size = "100gb"
max_primary_shard_docs = 5000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll want to update the tests to only specify this when it's supported by the backing cluster. That means adding a case covering this attribute which is skipped when it's not supported (example)

max_primary_shard_size = "50gb"
min_age = "3d"
min_docs = 1000
min_size = "50gb"
min_primary_shard_size = "25gb"
min_primary_shard_docs = 500
min_primary_shard_size = "25gb"
}

readonly {}
}

delete {
delete {}
}
}
`, name)
}

func testAccResourceILMCreateWithMaxPrimaryShardDocs(name string) string {
return fmt.Sprintf(`
provider "elasticstack" {
elasticsearch {}
}

resource "elasticstack_elasticsearch_index_lifecycle" "test_rollover" {
name = "%s"

hot {
rollover {
max_primary_shard_docs = 5000
}

readonly {}
Expand Down
Loading