github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/process/deprovisioning/remove_runtime.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/kyma-environment-broker/internal/broker"
    10  
    11  	"github.com/sirupsen/logrus"
    12  
    13  	"github.com/kyma-project/kyma-environment-broker/internal"
    14  	"github.com/kyma-project/kyma-environment-broker/internal/process"
    15  	"github.com/kyma-project/kyma-environment-broker/internal/provisioner"
    16  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    17  )
    18  
    19  type RemoveRuntimeStep struct {
    20  	operationManager   *process.OperationManager
    21  	instanceStorage    storage.Instances
    22  	provisionerClient  provisioner.Client
    23  	provisionerTimeout time.Duration
    24  }
    25  
    26  func NewRemoveRuntimeStep(os storage.Operations, is storage.Instances, cli provisioner.Client, provisionerTimeout time.Duration) *RemoveRuntimeStep {
    27  	return &RemoveRuntimeStep{
    28  		operationManager:   process.NewOperationManager(os),
    29  		instanceStorage:    is,
    30  		provisionerClient:  cli,
    31  		provisionerTimeout: provisionerTimeout,
    32  	}
    33  }
    34  
    35  func (s *RemoveRuntimeStep) Name() string {
    36  	return "Remove_Runtime"
    37  }
    38  
    39  func (s *RemoveRuntimeStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) {
    40  	if time.Since(operation.UpdatedAt) > s.provisionerTimeout {
    41  		log.Infof("operation has reached the time limit: updated operation time: %s", operation.UpdatedAt)
    42  		return s.operationManager.OperationFailed(operation, fmt.Sprintf("operation has reached the time limit: %s", s.provisionerTimeout), nil, log)
    43  	}
    44  
    45  	instance, err := s.instanceStorage.GetByID(operation.InstanceID)
    46  	switch {
    47  	case err == nil:
    48  	case dberr.IsNotFound(err):
    49  		log.Errorf("instance already deleted", err)
    50  		return operation, 0 * time.Second, nil
    51  	default:
    52  		log.Errorf("unable to get instance from storage: %s", err)
    53  		return operation, 1 * time.Second, nil
    54  	}
    55  	if instance.RuntimeID == "" || operation.ProvisioningParameters.PlanID == broker.OwnClusterPlanID {
    56  		// happens when provisioning process failed and Create_Runtime step was never reached
    57  		// It can also happen when the SKR is suspended (technically deprovisioned)
    58  		log.Infof("Runtime does not exist for instance id %q", operation.InstanceID)
    59  		return operation, 0 * time.Second, nil
    60  	}
    61  
    62  	if operation.ProvisionerOperationID == "" {
    63  		provisionerResponse, err := s.provisionerClient.DeprovisionRuntime(instance.GlobalAccountID, instance.RuntimeID)
    64  		if err != nil {
    65  			log.Errorf("unable to deprovision runtime: %s", err)
    66  			return s.operationManager.RetryOperationWithoutFail(operation, s.Name(), "unable to deprovision Runtime in Provisioner", 15*time.Second, 20*time.Minute, log)
    67  		}
    68  		log.Infof("fetched ProvisionerOperationID=%s", provisionerResponse)
    69  		repeat := time.Duration(0)
    70  		operation, repeat, _ = s.operationManager.UpdateOperation(operation, func(o *internal.Operation) {
    71  			o.ProvisionerOperationID = provisionerResponse
    72  		}, log)
    73  		if repeat != 0 {
    74  			return operation, 5 * time.Second, nil
    75  		}
    76  	}
    77  
    78  	log.Infof("runtime deletion process initiated successfully")
    79  	return operation, 0, nil
    80  }