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

     1  package provisioning
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/kyma-project/kyma-environment-broker/internal/process"
     8  	"github.com/kyma-project/kyma-environment-broker/internal/runtimeoverrides"
     9  
    10  	"github.com/kyma-project/kyma-environment-broker/internal"
    11  	"github.com/kyma-project/kyma-environment-broker/internal/broker"
    12  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    13  
    14  	"github.com/sirupsen/logrus"
    15  )
    16  
    17  type RuntimeOverridesAppender interface {
    18  	Append(input runtimeoverrides.InputAppender, planName, overridesVersion string) error
    19  }
    20  
    21  //go:generate mockery --name=RuntimeVersionConfiguratorForProvisioning --output=automock --outpkg=automock --case=underscore
    22  type RuntimeVersionConfiguratorForProvisioning interface {
    23  	ForProvisioning(op internal.Operation) (*internal.RuntimeVersionData, error)
    24  }
    25  
    26  type OverridesFromSecretsAndConfigStep struct {
    27  	operationManager       *process.OperationManager
    28  	runtimeOverrides       RuntimeOverridesAppender
    29  	runtimeVerConfigurator RuntimeVersionConfiguratorForProvisioning
    30  }
    31  
    32  func NewOverridesFromSecretsAndConfigStep(os storage.Operations, runtimeOverrides RuntimeOverridesAppender,
    33  	rvc RuntimeVersionConfiguratorForProvisioning) *OverridesFromSecretsAndConfigStep {
    34  	return &OverridesFromSecretsAndConfigStep{
    35  		operationManager:       process.NewOperationManager(os),
    36  		runtimeOverrides:       runtimeOverrides,
    37  		runtimeVerConfigurator: rvc,
    38  	}
    39  }
    40  
    41  func (s *OverridesFromSecretsAndConfigStep) Name() string {
    42  	return "Overrides_From_Secrets_And_Config_Step"
    43  }
    44  
    45  func (s *OverridesFromSecretsAndConfigStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) {
    46  	planName, exists := broker.PlanNamesMapping[operation.ProvisioningParameters.PlanID]
    47  	if !exists {
    48  		log.Errorf("cannot map planID '%s' to planName", operation.ProvisioningParameters.PlanID)
    49  		return s.operationManager.OperationFailed(operation, "invalid operation provisioning parameters", nil, log)
    50  	}
    51  
    52  	overridesVersion := s.getOverridesVersion(operation)
    53  
    54  	if overridesVersion == "" { // if no overrides version number specified explicitly we read the RuntimeVersion
    55  		runtimeVersion, err := s.getRuntimeVersion(operation)
    56  		if err != nil {
    57  			errMsg := fmt.Sprintf("error while getting the runtime version for operation %s", operation.ID)
    58  			log.Error(errMsg)
    59  			return s.operationManager.OperationFailed(operation, errMsg, err, log)
    60  		}
    61  
    62  		overridesVersion = runtimeVersion.Version
    63  	}
    64  
    65  	log.Infof("runtime overrides version: %s", overridesVersion)
    66  
    67  	if err := s.runtimeOverrides.Append(operation.InputCreator, planName, overridesVersion); err != nil {
    68  		errMsg := fmt.Sprintf("error when appending overrides for operation %s", operation.ID)
    69  		log.Error(fmt.Sprintf("%s: %s", errMsg, err.Error()))
    70  		return s.operationManager.OperationFailed(operation, errMsg, err, log)
    71  	}
    72  
    73  	return operation, 0, nil
    74  }
    75  
    76  func (s *OverridesFromSecretsAndConfigStep) getRuntimeVersion(op internal.Operation) (*internal.RuntimeVersionData, error) {
    77  	// for some previously stored operations the RuntimeVersion property may not be initialized
    78  	if op.RuntimeVersion.Version != "" {
    79  		return &op.RuntimeVersion, nil
    80  	}
    81  
    82  	// if so, we manually compute the correct version using the same algorithm as when preparing
    83  	// the provisioning operation. The following code can be removed after all operations will use
    84  	// new approach for setting up runtime version in operation struct
    85  	return s.runtimeVerConfigurator.ForProvisioning(op)
    86  }
    87  
    88  func (s *OverridesFromSecretsAndConfigStep) getOverridesVersion(op internal.Operation) string {
    89  	if op.ProvisioningParameters.Parameters.OverridesVersion != "" {
    90  		return op.ProvisioningParameters.Parameters.OverridesVersion
    91  	}
    92  
    93  	return ""
    94  }