github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/process/provisioning/create_runtime_for_own_cluster_step.go (about) 1 package provisioning 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/google/uuid" 8 9 "github.com/kyma-project/kyma-environment-broker/internal" 10 "github.com/kyma-project/kyma-environment-broker/internal/process" 11 "github.com/kyma-project/kyma-environment-broker/internal/storage" 12 "github.com/kyma-project/kyma-environment-broker/internal/storage/dberr" 13 "github.com/sirupsen/logrus" 14 ) 15 16 type CreateRuntimeForOwnCluster struct { 17 operationManager *process.OperationManager 18 instanceStorage storage.Instances 19 } 20 21 func NewCreateRuntimeForOwnClusterStep(os storage.Operations, is storage.Instances) *CreateRuntimeForOwnCluster { 22 return &CreateRuntimeForOwnCluster{ 23 operationManager: process.NewOperationManager(os), 24 instanceStorage: is, 25 } 26 } 27 28 func (s *CreateRuntimeForOwnCluster) Name() string { 29 return "Create_Runtime_For_Own_Cluster" 30 } 31 32 func (s *CreateRuntimeForOwnCluster) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) { 33 if operation.RuntimeID != "" { 34 log.Infof("RuntimeID already set %s, skipping", operation.RuntimeID) 35 return operation, 0, nil 36 } 37 if time.Since(operation.UpdatedAt) > CreateRuntimeTimeout { 38 log.Infof("operation has reached the time limit: updated operation time: %s", operation.UpdatedAt) 39 return s.operationManager.OperationFailed(operation, fmt.Sprintf("operation has reached the time limit: %s", CreateRuntimeTimeout), nil, log) 40 } 41 42 runtimeID := uuid.New().String() 43 44 operation, repeat, _ := s.operationManager.UpdateOperation(operation, func(operation *internal.Operation) { 45 operation.ProvisionerOperationID = "" 46 operation.RuntimeID = runtimeID 47 }, log) 48 if repeat != 0 { 49 log.Errorf("cannot save neither runtimeID nor empty provider operation ID") 50 return operation, 5 * time.Second, nil 51 } 52 53 err := s.updateInstance(operation.InstanceID, 54 runtimeID) 55 56 switch { 57 case err == nil: 58 case dberr.IsConflict(err): 59 err := s.updateInstance(operation.InstanceID, runtimeID) 60 if err != nil { 61 log.Errorf("cannot update instance: %s", err) 62 return operation, 1 * time.Minute, nil 63 } 64 } 65 66 log.Info("runtime created for own cluster plan") 67 return operation, 0, nil 68 } 69 70 func (s *CreateRuntimeForOwnCluster) updateInstance(id, runtimeID string) error { 71 instance, err := s.instanceStorage.GetByID(id) 72 if err != nil { 73 return fmt.Errorf("while getting instance: %w", err) 74 } 75 instance.RuntimeID = runtimeID 76 instance.Provider = "custom" 77 instance.ProviderRegion = "custom" 78 _, err = s.instanceStorage.Update(*instance) 79 if err != nil { 80 return fmt.Errorf("while updating instance: %w", err) 81 } 82 83 return nil 84 }