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

     1  package provisioning
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/sirupsen/logrus"
     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  )
    15  
    16  // CheckRuntimeStep checks if the SKR is provisioned
    17  type CheckRuntimeStep struct {
    18  	provisionerClient   provisioner.Client
    19  	operationManager    *process.OperationManager
    20  	provisioningTimeout time.Duration
    21  }
    22  
    23  func NewCheckRuntimeStep(os storage.Operations,
    24  	provisionerClient provisioner.Client,
    25  	provisioningTimeout time.Duration) *CheckRuntimeStep {
    26  	return &CheckRuntimeStep{
    27  		provisionerClient:   provisionerClient,
    28  		operationManager:    process.NewOperationManager(os),
    29  		provisioningTimeout: provisioningTimeout,
    30  	}
    31  }
    32  
    33  var _ process.Step = (*CheckRuntimeStep)(nil)
    34  
    35  func (s *CheckRuntimeStep) Name() string {
    36  	return "Check_Runtime"
    37  }
    38  
    39  func (s *CheckRuntimeStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) {
    40  	if operation.RuntimeID == "" {
    41  		log.Errorf("Runtime ID is empty")
    42  		return s.operationManager.OperationFailed(operation, "Runtime ID is empty", nil, log)
    43  	}
    44  	return s.checkRuntimeStatus(operation, log.WithField("runtimeID", operation.RuntimeID))
    45  }
    46  
    47  func (s *CheckRuntimeStep) checkRuntimeStatus(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) {
    48  	if time.Since(operation.UpdatedAt) > s.provisioningTimeout {
    49  		log.Infof("operation has reached the time limit: updated operation time: %s", operation.UpdatedAt)
    50  		return s.operationManager.OperationFailed(operation, fmt.Sprintf("operation has reached the time limit: %s", s.provisioningTimeout), nil, log)
    51  	}
    52  
    53  	if operation.ProvisionerOperationID == "" {
    54  		msg := "Operation does not contain Provisioner Operation ID"
    55  		log.Error(msg)
    56  		return s.operationManager.OperationFailed(operation, msg, nil, log)
    57  	}
    58  
    59  	status, err := s.provisionerClient.RuntimeOperationStatus(operation.ProvisioningParameters.ErsContext.GlobalAccountID, operation.ProvisionerOperationID)
    60  	if err != nil {
    61  		log.Errorf("call to provisioner RuntimeOperationStatus failed: %s", err.Error())
    62  		return operation, 1 * time.Minute, nil
    63  	}
    64  	log.Infof("call to provisioner returned %s status", status.State.String())
    65  
    66  	switch status.State {
    67  	case gqlschema.OperationStateSucceeded:
    68  		return operation, 0, nil
    69  	case gqlschema.OperationStateInProgress:
    70  		return operation, 20 * time.Second, nil
    71  	case gqlschema.OperationStatePending:
    72  		return operation, 20 * time.Second, nil
    73  	case gqlschema.OperationStateFailed:
    74  		lastErr := provisioner.OperationStatusLastError(status.LastError)
    75  		return s.operationManager.OperationFailed(operation, "provisioner client returns failed status", lastErr, log)
    76  	}
    77  
    78  	lastErr := provisioner.OperationStatusLastError(status.LastError)
    79  	return s.operationManager.OperationFailed(operation, fmt.Sprintf("unsupported provisioner client status: %s", status.State.String()), lastErr, log)
    80  }