github.com/verrazzano/verrazzano-monitoring-operator@v0.0.30/pkg/upgrade/upgrade.go (about)

     1  // Copyright (C) 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package upgrade
     5  
     6  import (
     7  	"fmt"
     8  
     9  	vmcontrollerv1 "github.com/verrazzano/verrazzano-monitoring-operator/pkg/apis/vmcontroller/v1"
    10  	"github.com/verrazzano/verrazzano-monitoring-operator/pkg/config"
    11  	"github.com/verrazzano/verrazzano-monitoring-operator/pkg/opensearch"
    12  	dashboards "github.com/verrazzano/verrazzano-monitoring-operator/pkg/opensearch_dashboards"
    13  	"github.com/verrazzano/verrazzano-monitoring-operator/pkg/resources"
    14  	"github.com/verrazzano/verrazzano-monitoring-operator/pkg/util/logs/vzlog"
    15  )
    16  
    17  type Monitor struct {
    18  	running bool
    19  	ch      chan error
    20  }
    21  
    22  func (m *Monitor) MigrateOldIndices(log vzlog.VerrazzanoLogger, vmi *vmcontrollerv1.VerrazzanoMonitoringInstance,
    23  	o *opensearch.OSClient, od *dashboards.OSDashboardsClient) error {
    24  	if !o.IsOpenSearchReady(vmi) {
    25  		return nil
    26  	}
    27  
    28  	// if not already migrating, start migrating indices
    29  	if !m.running {
    30  		m.run(log, vmi, o, od)
    31  		return nil
    32  	}
    33  
    34  	complete, err := m.isUpgradeComplete()
    35  	// an error occurred during reindex
    36  	if err != nil {
    37  		// reset the monitor so we can retry the upgrade
    38  		m.reset()
    39  		return err
    40  	}
    41  	// reindex is still in progress
    42  	if !complete {
    43  		log.Info("Data stream reindex is in progress.")
    44  		return nil
    45  	}
    46  	// reindex was successful
    47  	m.reset()
    48  	return nil
    49  }
    50  
    51  func (m *Monitor) isUpgradeComplete() (bool, error) {
    52  	if m.ch == nil {
    53  		return false, nil
    54  	}
    55  
    56  	select {
    57  	case e := <-m.ch:
    58  		return true, e
    59  	default:
    60  		return false, nil
    61  	}
    62  }
    63  
    64  func (m *Monitor) reset() {
    65  	m.running = false
    66  	close(m.ch)
    67  }
    68  
    69  func (m *Monitor) run(log vzlog.VerrazzanoLogger, vmi *vmcontrollerv1.VerrazzanoMonitoringInstance,
    70  	o *opensearch.OSClient, od *dashboards.OSDashboardsClient) {
    71  	ch := make(chan error)
    72  	m.running = true
    73  	m.ch = ch
    74  	// configuration is done asynchronously, as this does not need to be blocking
    75  	go func() {
    76  		if !vmi.Spec.Elasticsearch.Enabled {
    77  			ch <- nil
    78  			return
    79  		}
    80  
    81  		openSearchEndpoint := resources.GetOpenSearchHTTPEndpoint(vmi)
    82  		// Make sure that the data stream template is created before re-indexing
    83  		exists, err := o.DataStreamExists(openSearchEndpoint, config.DataStreamName())
    84  		if err != nil {
    85  			ch <- fmt.Errorf("failed to verify existence of data stream: %v", err)
    86  			return
    87  		}
    88  
    89  		// If the migration data stream exists, the old backing indices must be reindexed
    90  		if exists {
    91  			// During upgrade, reindex and delete old indices
    92  			if err := o.MigrateIndicesToDataStreams(log, vmi, openSearchEndpoint); err != nil {
    93  				ch <- err
    94  				return
    95  			}
    96  
    97  			// Update if any index patterns configured for old indices in OpenSearch Dashboards
    98  			err = od.UpdatePatterns(log, vmi)
    99  			if err != nil {
   100  				ch <- fmt.Errorf("error in updating index patterns"+
   101  					" in OpenSearch Dashboards: %v", err)
   102  				return
   103  			}
   104  		}
   105  		ch <- nil
   106  	}()
   107  }