github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/process/deprovisioning/remove_instance_step.go (about) 1 package deprovisioning 2 3 import ( 4 "time" 5 6 "github.com/kyma-project/kyma-environment-broker/internal/storage/dberr" 7 8 "github.com/kyma-project/kyma-environment-broker/internal/process" 9 "github.com/kyma-project/kyma-environment-broker/internal/storage" 10 11 "github.com/sirupsen/logrus" 12 13 "github.com/kyma-project/kyma-environment-broker/internal" 14 ) 15 16 type RemoveInstanceStep struct { 17 operationManager *process.OperationManager 18 instanceStorage storage.Instances 19 operationStorage storage.Operations 20 } 21 22 var _ process.Step = &RemoveInstanceStep{} 23 24 func NewRemoveInstanceStep(instanceStorage storage.Instances, operationStorage storage.Operations) *RemoveInstanceStep { 25 return &RemoveInstanceStep{ 26 operationManager: process.NewOperationManager(operationStorage), 27 instanceStorage: instanceStorage, 28 operationStorage: operationStorage, 29 } 30 } 31 32 func (s *RemoveInstanceStep) Name() string { 33 return "Remove_Instance" 34 } 35 36 func (s *RemoveInstanceStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) { 37 var backoff time.Duration 38 39 _, err := s.instanceStorage.GetByID(operation.InstanceID) 40 switch { 41 case err == nil: 42 case dberr.IsNotFound(err): 43 log.Infof("instance already deleted", err) 44 return operation, 0 * time.Second, nil 45 default: 46 log.Errorf("unable to get instance from the storage: %s", err) 47 return operation, 1 * time.Second, nil 48 } 49 50 if operation.Temporary { 51 log.Info("Removing the RuntimeID field from the instance") 52 backoff = s.removeRuntimeIDFromInstance(operation.InstanceID, log) 53 if backoff != 0 { 54 return operation, backoff, nil 55 } 56 57 log.Info("Removing the RuntimeID field from the operation") 58 operation, backoff, _ = s.operationManager.UpdateOperation(operation, func(operation *internal.Operation) { 59 operation.RuntimeID = "" 60 }, log) 61 } else if operation.ExcutedButNotCompleted != nil { 62 log.Info("Marking the instance needs to retry some steps") 63 backoff = s.markInstanceNeedsRetrySomeSteps(operation.InstanceID, log) 64 if backoff != 0 { 65 return operation, backoff, nil 66 } 67 } else { 68 log.Info("Removing the instance permanently") 69 backoff = s.removeInstancePermanently(operation.InstanceID, log) 70 if backoff != 0 { 71 return operation, backoff, nil 72 } 73 74 log.Info("Removing the userID field from the operation") 75 operation, backoff, _ = s.operationManager.UpdateOperation(operation, func(operation *internal.Operation) { 76 operation.ProvisioningParameters.ErsContext.UserID = "" 77 }, log) 78 } 79 80 return operation, backoff, nil 81 } 82 83 func (s RemoveInstanceStep) removeRuntimeIDFromInstance(instanceID string, log logrus.FieldLogger) time.Duration { 84 backoff := time.Second 85 86 instance, err := s.instanceStorage.GetByID(instanceID) 87 if err != nil { 88 log.Errorf("unable to get instance %s from the storage: %s", instanceID, err) 89 return backoff 90 } 91 92 // empty RuntimeID means there is no runtime in the Provisioner Domain 93 instance.RuntimeID = "" 94 _, err = s.instanceStorage.Update(*instance) 95 if err != nil { 96 log.Errorf("unable to update instance %s in the storage: %s", instanceID, err) 97 return backoff 98 } 99 100 return 0 101 } 102 103 func (s RemoveInstanceStep) removeInstancePermanently(instanceID string, log logrus.FieldLogger) time.Duration { 104 err := s.instanceStorage.Delete(instanceID) 105 if err != nil { 106 log.Errorf("unable to remove instance %s from the storage: %s", instanceID, err) 107 return 10 * time.Second 108 } 109 110 return 0 111 } 112 113 func (s RemoveInstanceStep) markInstanceNeedsRetrySomeSteps(instanceID string, log logrus.FieldLogger) time.Duration { 114 backoff := time.Second 115 116 instance, err := s.instanceStorage.GetByID(instanceID) 117 if dberr.IsNotFound(err) { 118 log.Warnf("instance %s not found", instanceID) 119 return 0 120 } 121 if err != nil { 122 log.Errorf("unable to get instance %s from the storage: %s", instanceID, err) 123 return backoff 124 } 125 126 instance.DeletedAt = time.Now() 127 _, err = s.instanceStorage.Update(*instance) 128 if err != nil { 129 log.Errorf("unable to update instance %s in the storage: %s", instanceID, err) 130 return backoff 131 } 132 133 return 0 134 }