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

     1  package deprovisioning
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/kyma-project/kyma-environment-broker/internal"
     8  	kebErrors "github.com/kyma-project/kyma-environment-broker/internal/error"
     9  	"github.com/kyma-project/kyma-environment-broker/internal/process"
    10  	"github.com/kyma-project/kyma-environment-broker/internal/reconciler"
    11  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    12  	"github.com/sirupsen/logrus"
    13  )
    14  
    15  type DeregisterClusterStep struct {
    16  	operationManager   *process.OperationManager
    17  	reconcilerClient   reconciler.Client
    18  	provisionerTimeout time.Duration
    19  }
    20  
    21  func NewDeregisterClusterStep(os storage.Operations, cli reconciler.Client) *DeregisterClusterStep {
    22  	return &DeregisterClusterStep{
    23  		operationManager: process.NewOperationManager(os),
    24  		reconcilerClient: cli,
    25  	}
    26  }
    27  
    28  func (s *DeregisterClusterStep) Name() string {
    29  	return "Deregister_Cluster"
    30  }
    31  
    32  func (s *DeregisterClusterStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) {
    33  	if operation.ClusterConfigurationVersion == 0 {
    34  		log.Info("Cluster configuration was not created, skipping")
    35  		return operation, 0, nil
    36  	}
    37  	if operation.ClusterConfigurationDeleted {
    38  		log.Info("Cluster configuration was deleted, skipping")
    39  		return operation, 0, nil
    40  	}
    41  	err := s.reconcilerClient.DeleteCluster(operation.RuntimeID)
    42  	if err != nil {
    43  		return s.handleError(operation, err, log, "cannot remove DataTenant")
    44  	}
    45  
    46  	modifiedOp, d, _ := s.operationManager.UpdateOperation(operation, func(op *internal.Operation) {
    47  		op.ClusterConfigurationDeleted = true
    48  		op.ReconcilerDeregistrationAt = time.Now()
    49  	}, log)
    50  
    51  	return modifiedOp, d, nil
    52  }
    53  
    54  func (s *DeregisterClusterStep) handleError(operation internal.Operation, err error, log logrus.FieldLogger, msg string) (internal.Operation, time.Duration, error) {
    55  	log.Errorf("%s: %s", msg, err)
    56  
    57  	if kebErrors.IsTemporaryError(err) {
    58  		since := time.Since(operation.UpdatedAt)
    59  		if since < 30*time.Minute {
    60  			log.Errorf("request to the Reconciler failed: %s. Retry...", err)
    61  			return operation, 15 * time.Second, nil
    62  		}
    63  	}
    64  	errMsg := fmt.Sprintf("Reconciler cluster configuration have not been deleted in step %s.", s.Name())
    65  	operation, repeat, err := s.operationManager.MarkStepAsExcutedButNotCompleted(operation, s.Name(), errMsg, log)
    66  	if repeat != 0 {
    67  		return operation, repeat, err
    68  	}
    69  	return operation, 0, nil
    70  }