github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/process/upgrade_kyma/check_cluster_configuration.go (about)

     1  package upgrade_kyma
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	reconcilerApi "github.com/kyma-incubator/reconciler/pkg/keb"
     8  	"github.com/kyma-project/kyma-environment-broker/internal"
     9  	"github.com/kyma-project/kyma-environment-broker/internal/avs"
    10  	kebError "github.com/kyma-project/kyma-environment-broker/internal/error"
    11  	"github.com/kyma-project/kyma-environment-broker/internal/process"
    12  	"github.com/kyma-project/kyma-environment-broker/internal/reconciler"
    13  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    14  	"github.com/sirupsen/logrus"
    15  )
    16  
    17  // CheckClusterConfigurationStep checks if the SKR configuration is applied (by reconciler)
    18  type CheckClusterConfigurationStep struct {
    19  	reconcilerClient      reconciler.Client
    20  	operationManager      *process.UpgradeKymaOperationManager
    21  	evaluationManager     *avs.EvaluationManager
    22  	reconciliationTimeout time.Duration
    23  }
    24  
    25  func NewCheckClusterConfigurationStep(os storage.Operations,
    26  	reconcilerClient reconciler.Client,
    27  	evaluationManager *avs.EvaluationManager,
    28  	provisioningTimeout time.Duration) *CheckClusterConfigurationStep {
    29  	return &CheckClusterConfigurationStep{
    30  		reconcilerClient:      reconcilerClient,
    31  		operationManager:      process.NewUpgradeKymaOperationManager(os),
    32  		evaluationManager:     evaluationManager,
    33  		reconciliationTimeout: provisioningTimeout,
    34  	}
    35  }
    36  
    37  var _ Step = (*CheckClusterConfigurationStep)(nil)
    38  
    39  func (s *CheckClusterConfigurationStep) Name() string {
    40  	return "Check_Cluster_Configuration"
    41  }
    42  
    43  func (s *CheckClusterConfigurationStep) Run(operation internal.UpgradeKymaOperation, log logrus.FieldLogger) (internal.UpgradeKymaOperation, time.Duration, error) {
    44  	if time.Since(operation.UpdatedAt) > s.reconciliationTimeout {
    45  		log.Infof("operation has reached the time limit: updated operation time: %s", operation.UpdatedAt)
    46  		return s.restoreAvsFailOperation(operation, fmt.Sprintf("operation has reached the time limit: %s", s.reconciliationTimeout), log)
    47  	}
    48  
    49  	if operation.ClusterConfigurationVersion == 0 || !operation.ClusterConfigurationApplied {
    50  		// upgrade was trigerred in reconciler, no need to call provisioner and create UpgradeRuntimeInput
    51  		// TODO: deal with skipping steps in case of calling reconciler for Kyma 2.0 upgrade - introduce stages
    52  		log.Infof("Cluster configuration not yet created, skipping")
    53  		return operation, 0, nil
    54  	}
    55  
    56  	state, err := s.reconcilerClient.GetCluster(operation.InstanceDetails.RuntimeID, operation.ClusterConfigurationVersion)
    57  	if kebError.IsTemporaryError(err) {
    58  		log.Errorf("Reconciler GetCluster method failed (temporary error, retrying): %s", err.Error())
    59  		return operation, 1 * time.Minute, nil
    60  	}
    61  	if err != nil {
    62  		log.Errorf("Reconciler GetCluster method failed: %s", err.Error())
    63  		return s.restoreAvsFailOperation(operation, fmt.Sprintf("unable to get cluster state: %s", err.Error()), log)
    64  	}
    65  	log.Debugf("Cluster configuration status %s", state.Status)
    66  
    67  	// Ensure AVS evaluations status:
    68  	//  - set to maintenance in reconcile_pending, reconciling, error_retryable
    69  	//  - restore when reconciler status is ready or unknown/failure (operation terminal)
    70  	switch state.Status {
    71  	case reconcilerApi.StatusReconciling, reconcilerApi.StatusReconcilePending, reconcilerApi.StatusReconcileErrorRetryable:
    72  		if !s.evaluationManager.IsMaintenanceModeDisabled() {
    73  			operation, err = SetAvsStatusMaintenance(s.evaluationManager, s.operationManager, operation, log)
    74  		}
    75  	default:
    76  		operation, err = RestoreAvsStatus(s.evaluationManager, s.operationManager, operation, log)
    77  	}
    78  	if err != nil {
    79  		if kebError.IsTemporaryError(err) {
    80  			return operation, 30 * time.Second, nil
    81  		}
    82  		return s.operationManager.OperationFailed(operation, err.Error(), nil, log)
    83  	}
    84  
    85  	switch state.Status {
    86  	case reconcilerApi.StatusReconciling, reconcilerApi.StatusReconcilePending:
    87  		return operation, 30 * time.Second, nil
    88  	case reconcilerApi.StatusReconcileErrorRetryable:
    89  		log.Infof("Reconciler failed with retryable, rechecking in 10 minutes.")
    90  		return operation, 10 * time.Minute, nil
    91  	case reconcilerApi.StatusReady:
    92  		return s.operationManager.OperationSucceeded(operation, "Cluster configuration ready", log)
    93  	case reconcilerApi.StatusError:
    94  		errMsg := fmt.Sprintf("Reconciler failed. %v", reconciler.PrettyFailures(state))
    95  		log.Warnf(errMsg)
    96  		return s.operationManager.OperationFailed(operation, errMsg, nil, log)
    97  	default:
    98  		return s.operationManager.OperationFailed(operation, fmt.Sprintf("unknown cluster status: %s", state.Status), nil, log)
    99  	}
   100  }
   101  
   102  func (s *CheckClusterConfigurationStep) restoreAvsFailOperation(operation internal.UpgradeKymaOperation, description string, log logrus.FieldLogger) (internal.UpgradeKymaOperation, time.Duration, error) {
   103  	operation, err := RestoreAvsStatus(s.evaluationManager, s.operationManager, operation, log)
   104  	if kebError.IsTemporaryError(err) {
   105  		return operation, 30 * time.Second, nil
   106  	}
   107  	return s.operationManager.OperationFailed(operation, description, err, log)
   108  }