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

     1  package provisioning
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/kyma-project/kyma-environment-broker/internal"
     8  	kebError "github.com/kyma-project/kyma-environment-broker/internal/error"
     9  	"github.com/kyma-project/kyma-environment-broker/internal/process"
    10  	"github.com/kyma-project/kyma-environment-broker/internal/process/input"
    11  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    12  
    13  	"github.com/sirupsen/logrus"
    14  )
    15  
    16  const (
    17  	// label key used to send to director
    18  	grafanaURLLabel = "operator_grafanaUrl"
    19  )
    20  
    21  //go:generate mockery --name=DirectorClient --output=automock --outpkg=automock --case=underscore
    22  
    23  type DirectorClient interface {
    24  	SetLabel(accountID, runtimeID, key, value string) error
    25  }
    26  
    27  type KymaVersionConfigurator interface {
    28  	ForGlobalAccount(string) (string, bool, error)
    29  }
    30  
    31  type InitialisationStep struct {
    32  	operationManager       *process.OperationManager
    33  	inputBuilder           input.CreatorForPlan
    34  	runtimeVerConfigurator RuntimeVersionConfiguratorForProvisioning
    35  	instanceStorage        storage.Instances
    36  }
    37  
    38  func NewInitialisationStep(os storage.Operations, is storage.Instances, b input.CreatorForPlan, rvc RuntimeVersionConfiguratorForProvisioning) *InitialisationStep {
    39  	return &InitialisationStep{
    40  		operationManager:       process.NewOperationManager(os),
    41  		inputBuilder:           b,
    42  		runtimeVerConfigurator: rvc,
    43  		instanceStorage:        is,
    44  	}
    45  }
    46  
    47  func (s *InitialisationStep) Name() string {
    48  	return "Provision_Initialization"
    49  }
    50  
    51  func (s *InitialisationStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) {
    52  	// configure the Kyma version to use
    53  	err := s.configureKymaVersion(&operation, log)
    54  	if err != nil {
    55  		return s.operationManager.RetryOperation(operation, "error while configuring kyma version", err, 5*time.Second, 5*time.Minute, log)
    56  	}
    57  
    58  	// create Provisioner InputCreator
    59  	log.Infof("create provisioner input creator for %q plan ID", operation.ProvisioningParameters.PlanID)
    60  	creator, err := s.inputBuilder.CreateProvisionInput(operation.ProvisioningParameters, operation.RuntimeVersion)
    61  
    62  	switch {
    63  	case err == nil:
    64  		operation.InputCreator = creator
    65  		operation.InputCreator.DisableOptionalComponent(internal.BTPOperatorComponentName)
    66  		err := s.updateInstance(operation.InstanceID, creator.Provider())
    67  		if err != nil {
    68  			return s.operationManager.RetryOperation(operation, "error while creating provisioning input creator", err, 1*time.Second, 5*time.Second, log)
    69  		}
    70  
    71  		return operation, 0, nil
    72  	case kebError.IsTemporaryError(err):
    73  		log.Errorf("cannot create input creator at the moment for plan %s and version %s: %s", operation.ProvisioningParameters.PlanID, operation.ProvisioningParameters.Parameters.KymaVersion, err)
    74  		return s.operationManager.RetryOperation(operation, "error while creating provisioning input creator", err, 5*time.Second, 5*time.Minute, log)
    75  	default:
    76  		log.Errorf("cannot create input creator for plan %s: %s", operation.ProvisioningParameters.PlanID, err)
    77  		return s.operationManager.OperationFailed(operation, "cannot create provisioning input creator", err, log)
    78  	}
    79  }
    80  
    81  func (s *InitialisationStep) configureKymaVersion(operation *internal.Operation, log logrus.FieldLogger) error {
    82  	if !operation.RuntimeVersion.IsEmpty() {
    83  		return nil
    84  	}
    85  	version, err := s.runtimeVerConfigurator.ForProvisioning(*operation)
    86  	if err != nil {
    87  		return fmt.Errorf("while getting the runtime version: %w", err)
    88  	}
    89  
    90  	var repeat time.Duration
    91  	if *operation, repeat, err = s.operationManager.UpdateOperation(*operation, func(operation *internal.Operation) {
    92  		operation.RuntimeVersion = *version
    93  	}, log); repeat != 0 {
    94  		return fmt.Errorf("while updating operation with RuntimeVersion property: %w", err)
    95  	}
    96  	return nil
    97  }
    98  
    99  func (s *InitialisationStep) updateInstance(id string, provider internal.CloudProvider) error {
   100  	instance, err := s.instanceStorage.GetByID(id)
   101  	if err != nil {
   102  		return fmt.Errorf("while getting instance: %w", err)
   103  	}
   104  	instance.Provider = provider
   105  	_, err = s.instanceStorage.Update(*instance)
   106  	if err != nil {
   107  		return fmt.Errorf("while updating instance: %w", err)
   108  	}
   109  
   110  	return nil
   111  }