diff --git a/cmd/es-rollover/app/init/action.go b/cmd/es-rollover/app/init/action.go index 63ae5214849..4651aba3ece 100644 --- a/cmd/es-rollover/app/init/action.go +++ b/cmd/es-rollover/app/init/action.go @@ -10,8 +10,6 @@ import ( "net/http" "strings" - "github.com/elastic/go-elasticsearch/v8/typedapi/types" - "github.com/jaegertracing/jaeger/cmd/es-rollover/app" "github.com/jaegertracing/jaeger/pkg/es" "github.com/jaegertracing/jaeger/pkg/es/client" @@ -58,10 +56,7 @@ func (c Action) Do() error { return err } if !policyExist { - err := createDefaultILMPolicy(c.ILMClient, c.Config.ILMPolicyName) - if err != nil { - return fmt.Errorf("ILM policy %s Create/Update failed", c.Config.ILMPolicyName) - } + return fmt.Errorf("ILM policy %s doesn't exist in Elasticsearch. Please create it and re-run init", c.Config.ILMPolicyName) } } rolloverIndices := app.RolloverIndices(c.Config.Archive, c.Config.SkipDependencies, c.Config.AdaptiveSampling, c.Config.Config.IndexPrefix) @@ -73,64 +68,6 @@ func (c Action) Do() error { return nil } -const esILMPolicy = ` -{ - "policy": { - "phases": { - "hot": { - "min_age": "9999ms", - "actions": { - "rollover": { - "max_age": "1m" - }, - "set_priority": { - "priority": 9999 - } - } - }, - "delete": { - "min_age": "9999m", - "actions": { - "delete": {} - } - } - } - } -} -` - -func createDefaultILMPolicy(c client.IndexManagementLifecycleAPI, policy string) error { - ilmPolicy := types.NewIlmPolicy() - err := json.Unmarshal([]byte(esILMPolicy), ilmPolicy) - if err != nil { - return fmt.Errorf("error unmarshalling ILM policy: %w", err) - } - err = c.CreateOrUpdate(policy, client.Policy{ILMPolicy: *ilmPolicy}) - if err != nil { - var esErr client.ResponseError - if errors.As(err, &esErr) { - if esErr.StatusCode != http.StatusBadRequest || esErr.Body == nil { - return esErr.Err - } - // check for the reason of the error - jsonError := map[string]any{} - err := json.Unmarshal(esErr.Body, &jsonError) - if err != nil { - // return unmarshal error - return err - } - errorMap := jsonError["error"].(map[string]any) - // check for reason, ignore already exist error - if strings.Contains(errorMap["type"].(string), "resource_already_exists_exception") { - return nil - } - } - // Return any other error unrelated to the response - return err - } - return nil -} - func createIndexIfNotExist(c client.IndexAPI, index string) error { err := c.CreateIndex(index) if err != nil { diff --git a/cmd/es-rollover/app/init/action_test.go b/cmd/es-rollover/app/init/action_test.go index b7d55a94bc6..0f87f579ea2 100644 --- a/cmd/es-rollover/app/init/action_test.go +++ b/cmd/es-rollover/app/init/action_test.go @@ -110,19 +110,12 @@ func TestRolloverAction(t *testing.T) { }, }, { - name: "create ILM policy", - setupCallExpectations: func(indexClient *mocks.IndexAPI, clusterClient *mocks.ClusterAPI, ilmClient *mocks.IndexManagementLifecycleAPI) { + name: "ilm doesnt exist", + setupCallExpectations: func(_ *mocks.IndexAPI, clusterClient *mocks.ClusterAPI, ilmClient *mocks.IndexManagementLifecycleAPI) { clusterClient.On("Version").Return(uint(7), nil) ilmClient.On("Exists", "myilmpolicy").Return(false, nil) - indexClient.On("CreateTemplate", mock.Anything, "jaeger-span").Return(nil) - indexClient.On("CreateIndex", "jaeger-span-archive-000001").Return(nil) - indexClient.On("GetJaegerIndices", "").Return([]client.Index{}, nil) - indexClient.On("CreateAlias", []client.Alias{ - {Index: "jaeger-span-archive-000001", Name: "jaeger-span-archive-read", IsWriteIndex: false}, - {Index: "jaeger-span-archive-000001", Name: "jaeger-span-archive-write", IsWriteIndex: true}, - }).Return(nil) - ilmClient.On("CreateOrUpdate", "myilmpolicy", mock.Anything).Return(nil) }, + expectedErr: errors.New("ILM policy myilmpolicy doesn't exist in Elasticsearch. Please create it and re-run init"), config: Config{ Config: app.Config{ Archive: true, diff --git a/plugin/storage/integration/elasticsearch_test.go b/plugin/storage/integration/elasticsearch_test.go index 3eee8e8379d..3bab9cc4180 100644 --- a/plugin/storage/integration/elasticsearch_test.go +++ b/plugin/storage/integration/elasticsearch_test.go @@ -44,7 +44,6 @@ const ( dependenciesTemplateName = "jaeger-dependencies" primaryNamespace = "es" archiveNamespace = "es-archive" - ilmPolicy = "jaeger-ilm-policy" ) type ESStorageIntegration struct { @@ -245,33 +244,3 @@ func (s *ESStorageIntegration) cleanESIndexTemplates(t *testing.T, prefix string } return nil } - -func TestElasticsearchStorage_ILMPolicy(t *testing.T) { - SkipUnlessEnv(t, "elasticsearch", "opensearch") - t.Cleanup(func() { - testutils.VerifyGoLeaksOnceForES(t) - }) - c := getESHttpClient(t) - require.NoError(t, healthCheck(c)) - s := &ESStorageIntegration{} - s.initializeES(t, c, true) - esVersion, err := s.getVersion() - require.NoError(t, err) - if esVersion == 8 { - request := s.v8Client.ILM.GetLifecycle.WithPolicy(ilmPolicy) - ilmPolicyExistsResponse, err := s.v8Client.ILM.GetLifecycle(request) - require.NoError(t, err) - assert.Equal(t, 200, ilmPolicyExistsResponse.StatusCode) - } - s.cleanEsILMPolicy(t) -} - -func (s *ESStorageIntegration) cleanEsILMPolicy(t *testing.T) error { - version, err := s.getVersion() - require.NoError(t, err) - if version == 8 { - _, err := s.v8Client.ILM.RemovePolicy(ilmPolicy) - require.NoError(t, err) - } - return nil -}