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

     1  package deprovisioning
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/kyma-project/kyma-environment-broker/internal/process"
     8  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
     9  
    10  	reconcilerApi "github.com/kyma-incubator/reconciler/pkg/keb"
    11  	"github.com/kyma-project/kyma-environment-broker/internal"
    12  	kebError "github.com/kyma-project/kyma-environment-broker/internal/error"
    13  	"github.com/kyma-project/kyma-environment-broker/internal/reconciler"
    14  	"github.com/sirupsen/logrus"
    15  )
    16  
    17  type CheckClusterDeregistrationStep struct {
    18  	reconcilerClient reconciler.Client
    19  	timeout          time.Duration
    20  	operationManager *process.OperationManager
    21  }
    22  
    23  func NewCheckClusterDeregistrationStep(os storage.Operations, cli reconciler.Client, timeout time.Duration) *CheckClusterDeregistrationStep {
    24  	return &CheckClusterDeregistrationStep{
    25  		reconcilerClient: cli,
    26  		timeout:          timeout,
    27  		operationManager: process.NewOperationManager(os),
    28  	}
    29  }
    30  
    31  func (s *CheckClusterDeregistrationStep) Name() string {
    32  	return "Check_Cluster_Deregistration"
    33  }
    34  
    35  func (s *CheckClusterDeregistrationStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) {
    36  	if !operation.ClusterConfigurationDeleted {
    37  		log.Infof("Cluster deregistration has not be executed, skipping")
    38  		return operation, 0, nil
    39  	}
    40  	if operation.ClusterConfigurationVersion == 0 {
    41  		log.Info("ClusterConfigurationVersion is zero, skipping")
    42  		return operation, 0, nil
    43  	}
    44  	timeSinceTriggered := operation.TimeSinceReconcilerDeregistrationTriggered()
    45  	if timeSinceTriggered > s.timeout {
    46  		log.Errorf("Cluster deregistration has reached the time limit: %s", s.timeout)
    47  		modifiedOp, d, _ := s.operationManager.UpdateOperation(operation, func(op *internal.Operation) {
    48  			op.ClusterConfigurationVersion = 0
    49  			op.ExcutedButNotCompleted = append(operation.ExcutedButNotCompleted, s.Name())
    50  		}, log)
    51  		return modifiedOp, d, nil
    52  	}
    53  
    54  	state, err := s.reconcilerClient.GetCluster(operation.RuntimeID, operation.ClusterConfigurationVersion)
    55  	if kebError.IsNotFoundError(err) {
    56  		log.Info("cluster already deleted")
    57  		modifiedOp, d, _ := s.operationManager.UpdateOperation(operation, func(op *internal.Operation) {
    58  			op.ClusterConfigurationVersion = 0
    59  		}, log)
    60  		return modifiedOp, d, nil
    61  	}
    62  	if kebError.IsTemporaryError(err) {
    63  		log.Errorf("Reconciler GetCluster method failed (temporary error, retrying): %s", err.Error())
    64  		return operation, 1 * time.Minute, nil
    65  	}
    66  	if err != nil {
    67  		log.Errorf("Reconciler GetCluster method failed: %s", err.Error())
    68  		modifiedOp, d, _ := s.operationManager.UpdateOperation(operation, func(op *internal.Operation) {
    69  			op.ClusterConfigurationVersion = 0
    70  			op.ExcutedButNotCompleted = append(operation.ExcutedButNotCompleted, s.Name())
    71  		}, log)
    72  		return modifiedOp, d, nil
    73  	}
    74  	log.Debugf("Cluster configuration status %s", state.Status)
    75  
    76  	switch state.Status {
    77  	case reconcilerApi.StatusDeletePending, reconcilerApi.StatusDeleting, reconcilerApi.StatusDeleteErrorRetryable:
    78  		return operation, 30 * time.Second, nil
    79  	case reconcilerApi.StatusDeleted:
    80  		msg := fmt.Sprintf("Reconciler succeeded in %s.", timeSinceTriggered)
    81  		log.Info(msg)
    82  		operation.EventInfof(msg)
    83  		modifiedOp, d, _ := s.operationManager.UpdateOperation(operation, func(op *internal.Operation) {
    84  			op.ClusterConfigurationVersion = 0
    85  		}, log)
    86  		return modifiedOp, d, nil
    87  	case reconcilerApi.StatusDeleteError, reconcilerApi.StatusError:
    88  		errMsg := fmt.Sprintf("Reconciler deletion failed. %v", reconciler.PrettyFailures(state))
    89  		log.Warnf(errMsg)
    90  		modifiedOp, d, _ := s.operationManager.UpdateOperation(operation, func(op *internal.Operation) {
    91  			op.ClusterConfigurationVersion = 0
    92  			op.ExcutedButNotCompleted = append(operation.ExcutedButNotCompleted, s.Name())
    93  		}, log)
    94  		return modifiedOp, d, nil
    95  	default:
    96  		log.Warnf("Unexpected state: %s", state.Status)
    97  		return operation, time.Minute, nil
    98  	}
    99  }