Skip to content

Commit 4507240

Browse files
authored
Merge pull request #1102 from mashhurs/legacy-template-with-es-v8-plus-bugfix
Resolve ILM settings based loaded template or/and `template_api`
2 parents 397921d + 0dd560a commit 4507240

File tree

4 files changed

+102
-24
lines changed

4 files changed

+102
-24
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 11.13.1
2+
- Avoid crash by ensuring ILM settings are injected in the correct location depending on the default (or custom) template format, template_api setting and ES version [#1102](https://github.com/logstash-plugins/logstash-output-elasticsearch/pull/1102)
3+
14
## 11.13.0
25
- add technology preview support for allowing events to individually encode a default pipeline with `[@metadata][target_ingest_pipeline]` (as part of a technology preview, this feature may change without notice) [#1113](https://github.com/logstash-plugins/logstash-output-elasticsearch/pull/1113)
36

lib/logstash/outputs/elasticsearch/template_manager.rb

+26-3
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,38 @@ def self.add_ilm_settings_to_template(plugin, template)
4646
# definition - remove any existing definition of 'template'
4747
template.delete('template') if template.include?('template') if plugin.maximum_seen_major_version < 8
4848
template['index_patterns'] = "#{plugin.ilm_rollover_alias}-*"
49-
settings = template_settings(plugin, template)
49+
settings = resolve_template_settings(plugin, template)
5050
if settings && (settings['index.lifecycle.name'] || settings['index.lifecycle.rollover_alias'])
5151
plugin.logger.info("Overwriting index lifecycle name and rollover alias as ILM is enabled")
5252
end
5353
settings.update({ 'index.lifecycle.name' => plugin.ilm_policy, 'index.lifecycle.rollover_alias' => plugin.ilm_rollover_alias})
5454
end
5555

56-
def self.template_settings(plugin, template)
57-
plugin.maximum_seen_major_version < 8 ? template['settings']: template['template']['settings']
56+
def self.resolve_template_settings(plugin, template)
57+
if template.key?('template')
58+
plugin.logger.trace("Resolving ILM template settings: under 'template' key", :template => template, :template_api => plugin.template_api, :es_version => plugin.maximum_seen_major_version)
59+
composable_index_template_settings(template)
60+
elsif template.key?('settings')
61+
plugin.logger.trace("Resolving ILM template settings: under 'settings' key", :template => template, :template_api => plugin.template_api, :es_version => plugin.maximum_seen_major_version)
62+
legacy_index_template_settings(template)
63+
else
64+
template_endpoint = template_endpoint(plugin)
65+
plugin.logger.trace("Resolving ILM template settings: template doesn't have 'settings' or 'template' fields, falling back to auto detection", :template => template, :template_api => plugin.template_api, :es_version => plugin.maximum_seen_major_version, :template_endpoint => template_endpoint)
66+
template_endpoint == INDEX_TEMPLATE_ENDPOINT ?
67+
composable_index_template_settings(template) :
68+
legacy_index_template_settings(template)
69+
end
70+
end
71+
72+
# Sets ['settings'] field to be compatible with _template API structure
73+
def self.legacy_index_template_settings(template)
74+
template['settings'] ||= {}
75+
end
76+
77+
# Sets the ['template']['settings'] fields if not exist to be compatible with _index_template API structure
78+
def self.composable_index_template_settings(template)
79+
template['template'] ||= {}
80+
template['template']['settings'] ||= {}
5881
end
5982

6083
# Template name - if template_name set, use it

logstash-output-elasticsearch.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'logstash-output-elasticsearch'
3-
s.version = '11.13.0'
3+
s.version = '11.13.1'
44
s.licenses = ['apache-2.0']
55
s.summary = "Stores logs in Elasticsearch"
66
s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"

spec/unit/outputs/elasticsearch/template_manager_spec.rb

+72-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require "logstash/devutils/rspec/spec_helper"
1+
require_relative "../../../../spec/spec_helper"
22
require "logstash/outputs/elasticsearch/template_manager"
33

44
describe LogStash::Outputs::ElasticSearch::TemplateManager do
@@ -33,33 +33,85 @@
3333
end
3434
end
3535

36-
describe "index template with ilm settings" do
36+
context "index template with ilm settings" do
3737
let(:plugin_settings) { {"manage_template" => true, "template_overwrite" => true} }
3838
let(:plugin) { LogStash::Outputs::ElasticSearch.new(plugin_settings) }
3939

40-
describe "in version 8+" do
41-
let(:file_path) { described_class.default_template_path(8) }
42-
let(:template) { described_class.read_template_file(file_path)}
40+
describe "with custom template" do
4341

44-
it "should update settings" do
45-
expect(plugin).to receive(:maximum_seen_major_version).at_least(:once).and_return(8)
46-
described_class.add_ilm_settings_to_template(plugin, template)
47-
expect(template['template']['settings']['index.lifecycle.name']).not_to eq(nil)
48-
expect(template['template']['settings']['index.lifecycle.rollover_alias']).not_to eq(nil)
49-
expect(template.include?('settings')).to be_falsey
42+
describe "in version 8+" do
43+
let(:file_path) { described_class.default_template_path(8) }
44+
let(:template) { described_class.read_template_file(file_path)}
45+
46+
it "should update settings" do
47+
expect(plugin).to receive(:maximum_seen_major_version).at_least(:once).and_return(8)
48+
described_class.add_ilm_settings_to_template(plugin, template)
49+
expect(template['template']['settings']['index.lifecycle.name']).not_to eq(nil)
50+
expect(template['template']['settings']['index.lifecycle.rollover_alias']).not_to eq(nil)
51+
expect(template.include?('settings')).to be_falsey
52+
end
53+
end
54+
55+
describe "in version < 8" do
56+
let(:file_path) { described_class.default_template_path(7) }
57+
let(:template) { described_class.read_template_file(file_path)}
58+
59+
it "should update settings" do
60+
expect(plugin).to receive(:maximum_seen_major_version).at_least(:once).and_return(7)
61+
described_class.add_ilm_settings_to_template(plugin, template)
62+
expect(template['settings']['index.lifecycle.name']).not_to eq(nil)
63+
expect(template['settings']['index.lifecycle.rollover_alias']).not_to eq(nil)
64+
expect(template.include?('template')).to be_falsey
65+
end
5066
end
5167
end
5268

53-
describe "in version < 8" do
54-
let(:file_path) { described_class.default_template_path(7) }
55-
let(:template) { described_class.read_template_file(file_path)}
69+
context "resolve template setting" do
70+
let(:plugin_settings) { super().merge({"template_api" => template_api}) }
71+
72+
describe "with composable template API" do
73+
let(:template_api) { "composable" }
5674

57-
it "should update settings" do
58-
expect(plugin).to receive(:maximum_seen_major_version).at_least(:once).and_return(7)
59-
described_class.add_ilm_settings_to_template(plugin, template)
60-
expect(template['settings']['index.lifecycle.name']).not_to eq(nil)
61-
expect(template['settings']['index.lifecycle.rollover_alias']).not_to eq(nil)
62-
expect(template.include?('template')).to be_falsey
75+
it 'resolves composable index template API compatible setting' do
76+
expect(plugin).to receive(:maximum_seen_major_version).at_least(:once).and_return(8) # required to log
77+
template = {}
78+
described_class.resolve_template_settings(plugin, template)
79+
expect(template["template"]["settings"]).not_to eq(nil)
80+
end
81+
end
82+
83+
describe "with legacy template API" do
84+
let(:template_api) { "legacy" }
85+
86+
it 'resolves legacy index template API compatible setting' do
87+
expect(plugin).to receive(:maximum_seen_major_version).at_least(:once).and_return(7) # required to log
88+
template = {}
89+
described_class.resolve_template_settings(plugin, template)
90+
expect(template["settings"]).not_to eq(nil)
91+
end
92+
end
93+
94+
describe "with `template_api => 'auto'`" do
95+
let(:template_api) { "auto" }
96+
97+
describe "with ES < 8 versions" do
98+
99+
it 'resolves legacy index template API compatible setting' do
100+
expect(plugin).to receive(:maximum_seen_major_version).at_least(:once).and_return(7)
101+
template = {}
102+
described_class.resolve_template_settings(plugin, template)
103+
expect(template["settings"]).not_to eq(nil)
104+
end
105+
end
106+
107+
describe "with ES >= 8 versions" do
108+
it 'resolves composable index template API compatible setting' do
109+
expect(plugin).to receive(:maximum_seen_major_version).at_least(:once).and_return(8)
110+
template = {}
111+
described_class.resolve_template_settings(plugin, template)
112+
expect(template["template"]["settings"]).not_to eq(nil)
113+
end
114+
end
63115
end
64116
end
65117
end

0 commit comments

Comments
 (0)