Skip to content

Commit d66ef65

Browse files
authored
add data_stream_template_do_not_use_wildcard in elasticsearch_data_st… (#1)
add data_stream_template_use_index_patterns_wildcard config param in elasticsearch_data_stream
1 parent aac238f commit d66ef65

File tree

3 files changed

+134
-3
lines changed

3 files changed

+134
-3
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,6 +1561,50 @@ Default value is `false`.
15611561

15621562
**NOTE:** This parameter requests to install elasticsearch-xpack gem.
15631563

1564+
### data_stream_template_use_index_patterns_wildcard
1565+
1566+
Specify whether index patterns should include a wildcard (*) when creating an index template. This is particularly useful to prevent errors in scenarios where index templates are generated automatically, and multiple services with distinct suffixes are in use.
1567+
1568+
Default value is `true`.
1569+
1570+
Consider the following JSON error response when index patterns clash due to wildcard usage:
1571+
```json
1572+
{
1573+
"error": {
1574+
"root_cause": [
1575+
{
1576+
"type": "illegal_argument_exception",
1577+
"reason": "index template [eks-kube-apiserver] has index patterns [eks-kube-apiserver*] matching patterns from existing templates [eks-kube-apiserver-audit] with patterns (eks-kube-apiserver-audit => [eks-kube-apiserver-audit*]) that have the same priority [0], multiple index templates may not match during index creation, please use a different priority"
1578+
}
1579+
],
1580+
"type": "illegal_argument_exception",
1581+
"reason": "index template [eks-kube-apiserver] has index patterns [eks-kube-apiserver*] matching patterns from existing templates [eks-kube-apiserver-audit] with patterns (eks-kube-apiserver-audit => [eks-kube-apiserver-audit*]) that have the same priority [0], multiple index templates may not match during index creation, please use a different priority"
1582+
},
1583+
"status": 400
1584+
}
1585+
```
1586+
1587+
#### Usage Examples
1588+
1589+
When `data_stream_template_use_index_patterns_wildcard` is set to `true` (default):
1590+
1591+
```
1592+
data_stream_name: foo
1593+
data_stream_template_use_index_patterns_wildcard: true
1594+
```
1595+
1596+
In this case, the resulting index patterns will be: `["foo*"]`
1597+
1598+
When `data_stream_template_use_index_patterns_wildcard` is set to `false`:
1599+
1600+
```
1601+
data_stream_name: foo
1602+
data_stream_template_use_index_patterns_wildcard: false
1603+
```
1604+
1605+
The resulting index patterns will be: `["foo"]`
1606+
1607+
15641608
## Troubleshooting
15651609

15661610
See [Troubleshooting document](README.Troubleshooting.md)

lib/fluent/plugin/out_elasticsearch_data_stream.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class ElasticsearchOutputDataStream < ElasticsearchOutput
1313
config_param :data_stream_template_name, :string, :default => nil
1414
config_param :data_stream_ilm_policy, :string, :default => nil
1515
config_param :data_stream_ilm_policy_overwrite, :bool, :default => false
16+
config_param :data_stream_template_use_index_patterns_wildcard, :bool, :default => true
1617

1718
# Elasticsearch 7.9 or later always support new style of index template.
1819
config_set_default :use_legacy_template, false
@@ -112,8 +113,9 @@ def create_ilm_policy(datastream_name, template_name, ilm_name, host = nil)
112113

113114
def create_index_template(datastream_name, template_name, ilm_name, host = nil)
114115
return if data_stream_exist?(datastream_name, host) or template_exists?(template_name, host)
116+
wildcard = @data_stream_template_use_index_patterns_wildcard ? '*' : ''
115117
body = {
116-
"index_patterns" => ["#{datastream_name}*"],
118+
"index_patterns" => ["#{datastream_name}#{wildcard}"],
117119
"data_stream" => {},
118120
"template" => {
119121
"settings" => {

test/plugin/test_out_elasticsearch_data_stream.rb

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ def ilm_endpoint
3434
'_ilm'.freeze
3535
end
3636

37+
def index_template_endpoint
38+
'_index_template'.freeze
39+
end
40+
3741
def driver(conf='', es_version=elasticsearch_version.to_i, client_version=elasticsearch_version)
3842
# For request stub to detect compatibility.
3943
@es_version ||= es_version
@@ -124,7 +128,6 @@ def stub_nonexistent_template?(name="foo_tpl", url="http://localhost:9200")
124128
stub_request(:get, "#{url}/_index_template/#{name}").to_return(:status => [404, TRANSPORT_CLASS::Transport::Errors::NotFound], :headers => {'x-elastic-product' => 'Elasticsearch'})
125129
end
126130

127-
128131
def push_bulk_request(req_body)
129132
# bulk data must be pair of OP and records
130133
# {"create": {}}\nhttp://localhost:9200/_ilm/policy/foo_ilm_bar
@@ -486,7 +489,8 @@ def test_datastream_configure
486489
'@type' => ELASTIC_DATA_STREAM_TYPE,
487490
'data_stream_name' => 'foo',
488491
'data_stream_ilm_name' => "foo_ilm_policy",
489-
'data_stream_template_name' => "foo_tpl"
492+
'data_stream_template_name' => "foo_tpl",
493+
'data_stream_template_use_index_patterns_wildcard' => false
490494
})
491495
assert_equal "foo", driver(conf).instance.data_stream_name
492496
end
@@ -556,6 +560,21 @@ def test_configure_compression
556560
assert_equal true, driver(config).instance.compression
557561
end
558562

563+
def test_configure_wildcard_usage
564+
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
565+
566+
config = %{
567+
data_stream_name foo
568+
data_stream_template_name foo_tpl
569+
data_stream_ilm_name foo_ilm_policy
570+
data_stream_template_use_index_patterns_wildcard false
571+
}
572+
573+
stub_default
574+
575+
assert_equal false, driver(config).instance.data_stream_template_use_index_patterns_wildcard
576+
end
577+
559578
def test_check_compression_strategy
560579
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
561580

@@ -722,6 +741,72 @@ def test_template_and_ilm_unset
722741
assert_equal "foo_policy", driver(conf).instance.data_stream_ilm_name
723742
end
724743

744+
def test_template_index_patterns_with_wildcard
745+
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
746+
747+
stub_existent_ilm?
748+
749+
stub_nonexistent_data_stream?
750+
stub_data_stream
751+
752+
stub_nonexistent_template?("foo_template")
753+
stub_request(:put, "http://localhost:9200/#{index_template_endpoint}/foo_template").with do |req|
754+
# bulk data must be pair of OP and records
755+
# {"create": {}}\nhttp://localhost:9200/_index_template//foo_template
756+
# {"@timestamp": ...}
757+
push_bulk_request(req.body)
758+
end.to_return({:status => 200, :body => "{}", :headers => { 'Content-Type' => 'json', 'x-elastic-product' => 'Elasticsearch' } })
759+
760+
conf = config_element(
761+
'ROOT', '', {
762+
'@type' => ELASTIC_DATA_STREAM_TYPE,
763+
'data_stream_name' => 'foo',
764+
'data_stream_ilm_name' => 'foo_ilm_policy',
765+
'data_stream_template_use_index_patterns_wildcard' => false
766+
})
767+
768+
assert_nothing_raised {
769+
driver(conf)
770+
}
771+
772+
assert_requested(:put, "http://localhost:9200/#{index_template_endpoint}/foo_template",
773+
body: '{"index_patterns":["foo"],"data_stream":{},"template":{"settings":{"index.lifecycle.name":"foo_ilm_policy"}}}',
774+
times: 1)
775+
end
776+
777+
def test_template_index_patterns_without_wildcard
778+
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
779+
780+
stub_existent_ilm?
781+
782+
stub_nonexistent_data_stream?
783+
stub_data_stream
784+
785+
stub_nonexistent_template?("foo_template")
786+
stub_request(:put, "http://localhost:9200/#{index_template_endpoint}/foo_template").with do |req|
787+
# bulk data must be pair of OP and records
788+
# {"create": {}}\nhttp://localhost:9200/_index_template//foo_template
789+
# {"@timestamp": ...}
790+
push_bulk_request(req.body)
791+
end.to_return({:status => 200, :body => "{}", :headers => { 'Content-Type' => 'json', 'x-elastic-product' => 'Elasticsearch' } })
792+
793+
conf = config_element(
794+
'ROOT', '', {
795+
'@type' => ELASTIC_DATA_STREAM_TYPE,
796+
'data_stream_name' => 'foo',
797+
'data_stream_ilm_name' => 'foo_ilm_policy',
798+
'data_stream_template_use_index_patterns_wildcard' => true
799+
})
800+
801+
assert_nothing_raised {
802+
driver(conf)
803+
}
804+
805+
assert_requested(:put, "http://localhost:9200/#{index_template_endpoint}/foo_template",
806+
body: '{"index_patterns":["foo*"],"data_stream":{},"template":{"settings":{"index.lifecycle.name":"foo_ilm_policy"}}}',
807+
times: 1)
808+
end
809+
725810
def test_placeholder
726811
omit REQUIRED_ELASTIC_MESSAGE unless data_stream_supported?
727812

0 commit comments

Comments
 (0)