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

     1  package deprovisioning
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/kyma-project/kyma-environment-broker/internal/storage/dberr"
     8  
     9  	"github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema"
    10  	"github.com/kyma-project/kyma-environment-broker/internal"
    11  	"github.com/kyma-project/kyma-environment-broker/internal/process"
    12  	"github.com/kyma-project/kyma-environment-broker/internal/provisioner"
    13  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    14  	"github.com/sirupsen/logrus"
    15  )
    16  
    17  type CheckRuntimeRemovalStep struct {
    18  	operationManager  *process.OperationManager
    19  	provisionerClient provisioner.Client
    20  	instanceStorage   storage.Instances
    21  	timeout           time.Duration
    22  }
    23  
    24  var _ process.Step = &CheckRuntimeRemovalStep{}
    25  
    26  func NewCheckRuntimeRemovalStep(operations storage.Operations, instances storage.Instances,
    27  	provisionerClient provisioner.Client, timeout time.Duration) *CheckRuntimeRemovalStep {
    28  	return &CheckRuntimeRemovalStep{
    29  		operationManager:  process.NewOperationManager(operations),
    30  		provisionerClient: provisionerClient,
    31  		instanceStorage:   instances,
    32  		timeout:           timeout,
    33  	}
    34  }
    35  
    36  func (s *CheckRuntimeRemovalStep) Name() string {
    37  	return "Check_Runtime_Removal"
    38  }
    39  
    40  func (s *CheckRuntimeRemovalStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) {
    41  	if time.Since(operation.UpdatedAt) > s.timeout {
    42  		log.Infof("operation has reached the time limit: %s updated operation time: %s", s.timeout, operation.UpdatedAt)
    43  		return s.operationManager.OperationFailed(operation, fmt.Sprintf("CheckRuntimeRemovalStep operation has reached the time limit: %s", s.timeout), nil, log)
    44  	}
    45  	if operation.ProvisionerOperationID == "" {
    46  		log.Infof("ProvisionerOperationID is empty, skipping (there is no runtime)")
    47  		return operation, 0, nil
    48  	}
    49  
    50  	instance, err := s.instanceStorage.GetByID(operation.InstanceID)
    51  	switch {
    52  	case err == nil:
    53  	case dberr.IsNotFound(err):
    54  		log.Infof("instance already deleted", err)
    55  		return operation, 0 * time.Second, nil
    56  	default:
    57  		log.Errorf("unable to get instance from storage: %s", err)
    58  		return operation, 1 * time.Second, nil
    59  	}
    60  
    61  	status, err := s.provisionerClient.RuntimeOperationStatus(instance.GlobalAccountID, operation.ProvisionerOperationID)
    62  	if err != nil {
    63  		log.Errorf("call to provisioner RuntimeOperationStatus failed: %s, GlobalAccountID=%s, Provisioner OperationID=%s", err.Error(), instance.GlobalAccountID, operation.ProvisionerOperationID)
    64  		return operation, 1 * time.Minute, nil
    65  	}
    66  	var msg string
    67  	if status.Message != nil {
    68  		msg = *status.Message
    69  	}
    70  	log.Infof("call to provisioner returned %s status: %s", status.State.String(), msg)
    71  
    72  	switch status.State {
    73  	case gqlschema.OperationStateSucceeded:
    74  		msg := fmt.Sprintf("Provisioner succeeded in %s.", time.Since(operation.UpdatedAt))
    75  		log.Info(msg)
    76  		operation.EventInfof(msg)
    77  		return operation, 0, nil
    78  	case gqlschema.OperationStateInProgress:
    79  		return operation, 30 * time.Second, nil
    80  	case gqlschema.OperationStatePending:
    81  		return operation, 30 * time.Second, nil
    82  	case gqlschema.OperationStateFailed:
    83  		lastErr := provisioner.OperationStatusLastError(status.LastError)
    84  		return s.operationManager.OperationFailed(operation, "provisioner client returns failed status", lastErr, log)
    85  	}
    86  
    87  	lastErr := provisioner.OperationStatusLastError(status.LastError)
    88  	return s.operationManager.OperationFailed(operation, fmt.Sprintf("unsupported provisioner client status: %s", status.State.String()), lastErr, log)
    89  }