Skip to content
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

Fix template/policy is always overwritten #11671

Merged
merged 3 commits into from
Apr 7, 2019
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
2 changes: 2 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Management configuration backup file will now have a timestamps in their name. {pull}11034[11034]
- Relax validation of the X-Pack license UID value. {issue}11640[11640]
- Fix a parsing error with the X-Pack license check on 32-bit system. {issue}11650[11650]
- Fix ILM policy always being overwritten. {pull}11671[11671]
- Fix template always being overwritten. {pull}11671[11671]

*Auditbeat*

Expand Down
2 changes: 1 addition & 1 deletion libbeat/cmd/instance/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ func (b *Beat) registerESIndexManagement() error {
func (b *Beat) indexSetupCallback() elasticsearch.ConnectCallback {
return func(esClient *elasticsearch.Client) error {
m := b.index.Manager(esClient, idxmgmt.BeatsAssets(b.Fields))
return m.Setup(true, true)
return m.Setup(false, false)
}
}

Expand Down
2 changes: 1 addition & 1 deletion libbeat/idxmgmt/idxmgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type ESClient interface {
// Manager is used to initialize indices, ILM policies, and aliases within the
// Elastic Stack.
type Manager interface {
Setup(template, policy bool) error
Setup(forceTemplate, forcePolicy bool) error
}

// DefaultSupport initializes the default index management support used by most Beats.
Expand Down
8 changes: 7 additions & 1 deletion libbeat/idxmgmt/ilm/ilm.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,14 @@ type Supporter interface {
// Manager uses an APIHandler to install a policy.
type Manager interface {
Enabled() (bool, error)

EnsureAlias() error
EnsurePolicy(overwrite bool) error

// EnsurePolicy installs a policy if it does not exist. The policy is always
// written if overwrite is set.
// The created flag is set to true only if a new policy is created. `created`
// is false if an existing policy gets overwritten.
EnsurePolicy(overwrite bool) (created bool, err error)
}

// APIHandler defines the interface between a remote service and the Manager.
Expand Down
6 changes: 5 additions & 1 deletion libbeat/idxmgmt/ilm/ilm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,11 @@ func TestDefaultSupport_Manager_EnsurePolicy(t *testing.T) {
calls []onCall
overwrite bool
cfg map[string]interface{}
create bool
fail error
}{
"create new policy": {
create: true,
odacremolbap marked this conversation as resolved.
Show resolved Hide resolved
calls: []onCall{
onHasILMPolicy(testPolicy.Name).Return(false, nil),
onCreateILMPolicy(testPolicy).Return(nil),
Expand Down Expand Up @@ -258,14 +260,16 @@ func TestDefaultSupport_Manager_EnsurePolicy(t *testing.T) {

h := newMockHandler(test.calls...)
m := createManager(t, h, test.cfg)
err := m.EnsurePolicy(test.overwrite)
created, err := m.EnsurePolicy(test.overwrite)

if test.fail == nil {
assert.Equal(t, test.create, created)
require.NoError(t, err)
} else {
require.Error(t, err)
assert.Equal(t, test.fail, ErrReason(err))
}

h.AssertExpectations(t)
})
}
Expand Down
6 changes: 3 additions & 3 deletions libbeat/idxmgmt/ilm/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ func (*noopSupport) Alias() Alias { return Alias{} }
func (*noopSupport) Policy() Policy { return Policy{} }
func (*noopSupport) Manager(_ APIHandler) Manager { return (*noopManager)(nil) }

func (*noopManager) Enabled() (bool, error) { return false, nil }
func (*noopManager) EnsureAlias() error { return errOf(ErrOpNotAvailable) }
func (*noopManager) EnsurePolicy(_ bool) error { return errOf(ErrOpNotAvailable) }
func (*noopManager) Enabled() (bool, error) { return false, nil }
func (*noopManager) EnsureAlias() error { return errOf(ErrOpNotAvailable) }
func (*noopManager) EnsurePolicy(_ bool) (bool, error) { return false, errOf(ErrOpNotAvailable) }
8 changes: 4 additions & 4 deletions libbeat/idxmgmt/ilm/std.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,26 +114,26 @@ func (m *singlePolicyManager) EnsureAlias() error {
return m.client.CreateAlias(m.alias)
}

func (m *singlePolicyManager) EnsurePolicy(overwrite bool) error {
func (m *singlePolicyManager) EnsurePolicy(overwrite bool) (bool, error) {
log := m.log
overwrite = overwrite || m.overwrite

exists := true
if m.checkExists && !overwrite {
b, err := m.client.HasILMPolicy(m.policy.Name)
odacremolbap marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
return false, err
}
exists = b
}

if !exists || overwrite {
return m.client.CreateILMPolicy(m.policy)
return !exists, m.client.CreateILMPolicy(m.policy)
odacremolbap marked this conversation as resolved.
Show resolved Hide resolved
}

log.Infof("do not generate ilm policy: exists=%v, overwrite=%v",
exists, overwrite)
return nil
return false, nil
}

func (c *infoCache) Valid() bool {
Expand Down
4 changes: 2 additions & 2 deletions libbeat/idxmgmt/mockilm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ func (m *mockILMSupport) EnsureAlias() error {
}

func onEnsurePolicy() onCall { return makeOnCall("EnsurePolicy") }
func (m *mockILMSupport) EnsurePolicy(overwrite bool) error {
func (m *mockILMSupport) EnsurePolicy(overwrite bool) (bool, error) {
args := m.Called()
return args.Error(0)
return args.Bool(0), args.Error(1)
}

func makeOnCall(name string, args ...interface{}) onCall {
Expand Down
12 changes: 9 additions & 3 deletions libbeat/idxmgmt/std.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ func (s *indexSupport) BuildSelector(cfg *common.Config) (outputs.IndexSelector,
}, nil
}

func (m *indexManager) Setup(template, policy bool) error {
return m.load(template, policy)
func (m *indexManager) Setup(forceTemplate, forcePolicy bool) error {
return m.load(forceTemplate, forcePolicy)
}

func (m *indexManager) Load() error {
Expand Down Expand Up @@ -231,10 +231,16 @@ func (m *indexManager) load(forceTemplate, forcePolicy bool) error {

// install ilm policy
if withILM {
if err := m.ilm.EnsurePolicy(forcePolicy); err != nil {
policyCreated, err := m.ilm.EnsurePolicy(forcePolicy)
odacremolbap marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
log.Info("ILM policy successfully loaded.")

// The template should be updated if a new policy is created.
if policyCreated {
forceTemplate = true
}
}

// create and install template
Expand Down