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

     1  package runtimeversion
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  
     8  	"github.com/kyma-project/kyma-environment-broker/internal/storage/dberr"
     9  
    10  	"github.com/kyma-project/kyma-environment-broker/internal"
    11  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    12  )
    13  
    14  type RuntimeVersionConfigurator struct {
    15  	defaultVersion string
    16  	accountMapping *AccountVersionMapping
    17  	runtimeStateDB storage.RuntimeStates
    18  }
    19  
    20  func NewRuntimeVersionConfigurator(defaultVersion string, accountMapping *AccountVersionMapping, runtimeStates storage.RuntimeStates) *RuntimeVersionConfigurator {
    21  	if defaultVersion == "" {
    22  		panic("Default version not provided")
    23  	}
    24  
    25  	return &RuntimeVersionConfigurator{
    26  		defaultVersion: defaultVersion,
    27  		accountMapping: accountMapping,
    28  		runtimeStateDB: runtimeStates,
    29  	}
    30  }
    31  
    32  func (rvc *RuntimeVersionConfigurator) ForUpdating(op internal.Operation) (*internal.RuntimeVersionData, error) {
    33  	r, err := rvc.runtimeStateDB.GetLatestWithKymaVersionByRuntimeID(op.RuntimeID)
    34  	if dberr.IsNotFound(err) {
    35  		return internal.NewEmptyRuntimeVersion(), nil
    36  	}
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	return internal.NewRuntimeVersionFromDefaults(r.GetKymaVersion()), nil
    42  }
    43  
    44  func (rvc *RuntimeVersionConfigurator) ForProvisioning(op internal.Operation) (*internal.RuntimeVersionData, error) {
    45  
    46  	pp := op.ProvisioningParameters
    47  
    48  	if pp.Parameters.KymaVersion == "" {
    49  		version, found, err := rvc.accountMapping.Get(pp.ErsContext.GlobalAccountID, pp.ErsContext.SubAccountID)
    50  		if err != nil {
    51  			return nil, err
    52  		}
    53  		if found {
    54  			majorVer, err := determineMajorVersion(version, rvc.defaultVersion)
    55  			if err != nil {
    56  				return nil, fmt.Errorf("while determining Kyma's major version: %w", err)
    57  			}
    58  			return internal.NewRuntimeVersionFromAccountMapping(version, majorVer), nil
    59  		}
    60  		return internal.NewRuntimeVersionFromDefaults(rvc.defaultVersion), nil
    61  	}
    62  
    63  	majorVer, err := determineMajorVersion(pp.Parameters.KymaVersion, rvc.defaultVersion)
    64  	if err != nil {
    65  		return nil, fmt.Errorf("while determining Kyma's major version: %w", err)
    66  	}
    67  	return internal.NewRuntimeVersionFromParameters(pp.Parameters.KymaVersion, majorVer), nil
    68  }
    69  
    70  func determineMajorVersion(version, defaultVersion string) (int, error) {
    71  	if isCustomVersion(version) {
    72  		return extractMajorVersionNumberFromVersionString(defaultVersion)
    73  	}
    74  	return extractMajorVersionNumberFromVersionString(version)
    75  }
    76  
    77  func isCustomVersion(version string) bool {
    78  	return strings.HasPrefix(version, "PR-") || strings.HasPrefix(version, "main-")
    79  }
    80  
    81  func extractMajorVersionNumberFromVersionString(version string) (int, error) {
    82  	splitVer := strings.Split(version, ".")
    83  	majorVerNum, err := strconv.Atoi(splitVer[0])
    84  	if err != nil {
    85  		return 0, fmt.Errorf("cannot convert major version (version: \"%s\") to int", version)
    86  	}
    87  	return majorVerNum, nil
    88  }
    89  
    90  func (rvc *RuntimeVersionConfigurator) ForUpgrade(op internal.UpgradeKymaOperation) (*internal.RuntimeVersionData, error) {
    91  	version, found, err := rvc.accountMapping.Get(op.GlobalAccountID, op.RuntimeOperation.SubAccountID)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	if found {
    96  		majorVer, err := determineMajorVersion(version, rvc.defaultVersion)
    97  		if err != nil {
    98  			return nil, fmt.Errorf("while determining Kyma's major version: %w", err)
    99  		}
   100  		return internal.NewRuntimeVersionFromAccountMapping(version, majorVer), nil
   101  	}
   102  
   103  	return internal.NewRuntimeVersionFromDefaults(rvc.defaultVersion), nil
   104  }