diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index c757ba838..00083d534 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -14,6 +14,9 @@ const ( ElasticsearchDefaultImage = "quay.io/openshift/origin-logging-elasticsearch6" ProxyDefaultImage = "quay.io/openshift/origin-elasticsearch-proxy:latest" TheoreticalShardMaxSizeInMB = 40960 + + // OcpTemplatePrefix is the prefix all operator generated templates + OcpTemplatePrefix = "ocp-gen" ) var ( diff --git a/pkg/elasticsearch/templates.go b/pkg/elasticsearch/templates.go index 45c45fe8d..197159abc 100644 --- a/pkg/elasticsearch/templates.go +++ b/pkg/elasticsearch/templates.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" + "github.com/openshift/elasticsearch-operator/pkg/constants" "github.com/openshift/elasticsearch-operator/pkg/log" estypes "github.com/openshift/elasticsearch-operator/pkg/types/elasticsearch" "github.com/openshift/elasticsearch-operator/pkg/utils" @@ -72,7 +73,7 @@ func (ec *esClient) ListTemplates() (sets.String, error) { func (ec *esClient) GetIndexTemplates() (map[string]estypes.GetIndexTemplate, error) { payload := &EsRequest{ Method: http.MethodGet, - URI: "_template/common.*", + URI: fmt.Sprintf("_template/common.*,%s-*", constants.OcpTemplatePrefix), } ec.fnSendEsRequest(ec.cluster, ec.namespace, payload, ec.k8sClient) diff --git a/pkg/k8shandler/index_management.go b/pkg/k8shandler/index_management.go index 2888255f4..804f5e509 100644 --- a/pkg/k8shandler/index_management.go +++ b/pkg/k8shandler/index_management.go @@ -7,16 +7,12 @@ import ( "k8s.io/apimachinery/pkg/util/sets" logging "github.com/openshift/elasticsearch-operator/pkg/apis/logging/v1" + "github.com/openshift/elasticsearch-operator/pkg/constants" "github.com/openshift/elasticsearch-operator/pkg/indexmanagement" "github.com/openshift/elasticsearch-operator/pkg/log" esapi "github.com/openshift/elasticsearch-operator/pkg/types/elasticsearch" ) -const ( - //ocpTemplatePrefix is the prefix all operator generated templates - ocpTemplatePrefix = "ocp-gen" -) - func (er *ElasticsearchRequest) CreateOrUpdateIndexManagement() error { cluster := er.cluster @@ -80,7 +76,7 @@ func (er *ElasticsearchRequest) cullIndexManagement(mappings []logging.IndexMana difference := existing.Difference(mappingNames) for _, template := range difference.List() { - if strings.HasPrefix(template, ocpTemplatePrefix) { + if strings.HasPrefix(template, constants.OcpTemplatePrefix) { if err := esClient.DeleteIndexTemplate(template); err != nil { log.Error(err, "Unable to delete stale template in order to reconcile", "template", template) } @@ -112,7 +108,7 @@ func (er *ElasticsearchRequest) initializeIndexIfNeeded(mapping logging.IndexMan } func formatTemplateName(name string) string { - return fmt.Sprintf("%s-%s", ocpTemplatePrefix, name) + return fmt.Sprintf("%s-%s", constants.OcpTemplatePrefix, name) } func formatWriteAlias(mapping logging.IndexManagementPolicyMappingSpec) string { @@ -130,5 +126,17 @@ func (er *ElasticsearchRequest) createOrUpdateIndexTemplate(mapping logging.Inde aliases := append(mapping.Aliases, mapping.Name) template := esapi.NewIndexTemplate(pattern, aliases, primaryShards, replicas) + // check to compare the current index templates vs what we just generated + templates, err := esClient.GetIndexTemplates() + if err != nil { + return err + } + + for templateName := range templates { + if templateName == name { + return nil + } + } + return esClient.CreateIndexTemplate(name, template) } diff --git a/pkg/k8shandler/index_management_test.go b/pkg/k8shandler/index_management_test.go index 70bb89df1..2ac72a642 100644 --- a/pkg/k8shandler/index_management_test.go +++ b/pkg/k8shandler/index_management_test.go @@ -7,6 +7,7 @@ import ( . "github.com/onsi/gomega" elasticsearch "github.com/openshift/elasticsearch-operator/pkg/apis/logging/v1" + "github.com/openshift/elasticsearch-operator/pkg/constants" "github.com/openshift/elasticsearch-operator/test/helpers" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client/fake" @@ -97,8 +98,18 @@ var _ = Describe("Index Management", func() { }) Describe("#createOrUpdateIndexTemplate", func() { BeforeEach(func() { + + templateURI := fmt.Sprintf("_template/common.*,%s-*", constants.OcpTemplatePrefix) + chatter = helpers.NewFakeElasticsearchChatter( map[string]helpers.FakeElasticsearchResponses{ + templateURI: { + { + Error: nil, + StatusCode: 200, + Body: `{}`, + }, + }, "_template/ocp-gen-node.infra": { { Error: nil, @@ -108,7 +119,7 @@ var _ = Describe("Index Management", func() { }, }, ) - request.esClient = helpers.NewFakeElasticsearchClient("elastichsearch", "openshift-logging", request.client, chatter) + request.esClient = helpers.NewFakeElasticsearchClient("elasticsearch", "openshift-logging", request.client, chatter) }) It("should create an elasticsearch index template to support the index", func() { Expect(request.createOrUpdateIndexTemplate(mapping)).To(BeNil()) diff --git a/pkg/k8shandler/migrations/pvc_naming.go b/pkg/k8shandler/migrations/pvc_naming.go new file mode 100644 index 000000000..561d510a2 --- /dev/null +++ b/pkg/k8shandler/migrations/pvc_naming.go @@ -0,0 +1,9 @@ +package migrations + +func (mr *migrationRequest) createRenamedPVC() error { + return nil +} + +func (mr *migrationRequest) rebindPV() error { + return nil +}