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

     1  package update
     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  // CheckStep checks if the SKR is updated
    17  type CheckStep struct {
    18  	provisionerClient   provisioner.Client
    19  	operationManager    *process.OperationManager
    20  	provisioningTimeout time.Duration
    21  }
    22  
    23  func NewCheckStep(os storage.Operations,
    24  	provisionerClient provisioner.Client,
    25  	provisioningTimeout time.Duration) *CheckStep {
    26  	return &CheckStep{
    27  		provisionerClient:   provisionerClient,
    28  		operationManager:    process.NewOperationManager(os),
    29  		provisioningTimeout: provisioningTimeout,
    30  	}
    31  }
    32  
    33  var _ process.Step = (*CheckStep)(nil)
    34  
    35  func (s *CheckStep) Name() string {
    36  	return "Check_Runtime"
    37  }
    38  
    39  func (s *CheckStep) 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 *CheckStep) 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  	var msg string
    67  	if status.Message != nil {
    68  		msg = *status.Message
    69  	}
    70  
    71  	switch status.State {
    72  	case gqlschema.OperationStateSucceeded:
    73  		return operation, 0, nil
    74  	case gqlschema.OperationStateInProgress:
    75  		return operation, time.Minute, nil
    76  	case gqlschema.OperationStatePending:
    77  		return operation, time.Minute, nil
    78  	case gqlschema.OperationStateFailed:
    79  		return s.operationManager.OperationFailed(operation, fmt.Sprintf("provisioner client returns failed status: %s", msg), nil, log)
    80  	}
    81  
    82  	return s.operationManager.OperationFailed(operation, fmt.Sprintf("unsupported provisioner client status: %s", status.State.String()), nil, log)
    83  }