From 6069453685cb7beaac8a5293261e8b5a1e1924c0 Mon Sep 17 00:00:00 2001 From: Oliver Trosien Date: Tue, 14 May 2019 11:54:32 +0200 Subject: [PATCH] #51 Stale value in 'current-scaling-operation' causing ES Operator to fail Signed-off-by: Oliver Trosien --- operator/es_client.go | 5 +++++ operator/es_client_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/operator/es_client.go b/operator/es_client.go index 36f4d69..ef83bf5 100644 --- a/operator/es_client.go +++ b/operator/es_client.go @@ -436,6 +436,11 @@ func (c *ESClient) UpdateIndexSettings(indices []ESIndex) error { } if resp.StatusCode() != http.StatusOK { + // if the index doesn't exist ES would return a 404 + if resp.StatusCode() == http.StatusNotFound { + log.Warnf("Index '%s' not found, assuming it has been deleted.", index.Index) + return nil + } return fmt.Errorf("code status %d - %s", resp.StatusCode(), resp.Body()) } } diff --git a/operator/es_client_test.go b/operator/es_client_test.go index 0bc7e7a..615a8ca 100644 --- a/operator/es_client_test.go +++ b/operator/es_client_test.go @@ -159,7 +159,33 @@ func TestUpdateIndexSettings(t *testing.T) { err := systemUnderTest.UpdateIndexSettings(indices) assert.NoError(t, err) +} + +func TestUpdateIndexSettingsIgnoresUnknownIndex(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + httpmock.RegisterResponder("GET", "http://elasticsearch:9200/_cluster/health", + httpmock.NewStringResponder(200, `{"status":"green"}`)) + httpmock.RegisterResponder("PUT", "http://elasticsearch:9200/myindex/_settings", + httpmock.NewStringResponder(404, `{}`)) + url, _ := url.Parse("http://elasticsearch:9200") + systemUnderTest := &ESClient{ + Endpoint: url, + } + + indices := make([]ESIndex, 0, 1) + indices = append(indices, ESIndex{ + Primaries: 1, + Replicas: 1, + Index: "myindex", + }) + err := systemUnderTest.UpdateIndexSettings(indices) + info := httpmock.GetCallCountInfo() + + assert.NoError(t, err) + require.EqualValues(t, 1, info["PUT http://elasticsearch:9200/myindex/_settings"]) } func TestCreateIndex(t *testing.T) {