github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/process/deprovisioning/init.go (about) 1 package deprovisioning 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/kyma-project/kyma-environment-broker/common/orchestration" 8 "github.com/kyma-project/kyma-environment-broker/internal" 9 "github.com/kyma-project/kyma-environment-broker/internal/process" 10 "github.com/kyma-project/kyma-environment-broker/internal/storage" 11 "github.com/kyma-project/kyma-environment-broker/internal/storage/dberr" 12 "github.com/pivotal-cf/brokerapi/v8/domain" 13 "github.com/sirupsen/logrus" 14 ) 15 16 type InitStep struct { 17 operationManager *process.OperationManager 18 operationTimeout time.Duration 19 operationStorage storage.Operations 20 instanceStorage storage.Instances 21 } 22 23 func NewInitStep(operations storage.Operations, instances storage.Instances, operationTimeout time.Duration) *InitStep { 24 return &InitStep{ 25 operationManager: process.NewOperationManager(operations), 26 operationTimeout: operationTimeout, 27 operationStorage: operations, 28 instanceStorage: instances, 29 } 30 } 31 32 func (s *InitStep) Name() string { 33 return "Initialisation" 34 } 35 36 func (s *InitStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) { 37 if time.Since(operation.CreatedAt) > s.operationTimeout { 38 log.Infof("operation has reached the time limit: operation was created at: %s", operation.CreatedAt) 39 return s.operationManager.OperationFailed(operation, fmt.Sprintf("operation has reached the time limit: %s", s.operationTimeout), nil, log) 40 } 41 42 if operation.State != orchestration.Pending { 43 return operation, 0, nil 44 } 45 // Check concurrent operation 46 lastOp, err := s.operationStorage.GetLastOperation(operation.InstanceID) 47 if err != nil { 48 return operation, time.Minute, nil 49 } 50 if !lastOp.IsFinished() { 51 log.Infof("waiting for %s operation (%s) to be finished", lastOp.Type, lastOp.ID) 52 return operation, time.Minute, nil 53 } 54 55 // read the instance details (it could happen that created deprovisioning operation has outdated one) 56 instance, err := s.instanceStorage.GetByID(operation.InstanceID) 57 if err != nil { 58 if dberr.IsNotFound(err) { 59 log.Warnf("the instance already deprovisioned") 60 return s.operationManager.OperationFailed(operation, "the instance was already deprovisioned", err, log) 61 } 62 return operation, time.Second, nil 63 } 64 65 log.Info("Setting state 'in progress' and refreshing instance details") 66 opr, delay, err := s.operationManager.UpdateOperation(operation, func(op *internal.Operation) { 67 op.State = domain.InProgress 68 op.InstanceDetails = instance.InstanceDetails 69 op.ProvisioningParameters.ErsContext = internal.InheritMissingERSContext(op.ProvisioningParameters.ErsContext, lastOp.ProvisioningParameters.ErsContext) 70 }, log) 71 if delay != 0 { 72 log.Errorf("unable to update the operation (move to 'in progress'), retrying") 73 return operation, delay, nil 74 } 75 76 return opr, 0, nil 77 }