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

     1  package update
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema"
     8  	"github.com/kyma-project/kyma-environment-broker/internal"
     9  	"github.com/kyma-project/kyma-environment-broker/internal/process"
    10  	"github.com/kyma-project/kyma-environment-broker/internal/provisioner"
    11  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    12  	"github.com/sirupsen/logrus"
    13  )
    14  
    15  const DryRunPrefix = "dry_run-"
    16  const retryDuration = 10 * time.Second
    17  
    18  type UpgradeShootStep struct {
    19  	operationManager    *process.OperationManager
    20  	provisionerClient   provisioner.Client
    21  	runtimeStateStorage storage.RuntimeStates
    22  }
    23  
    24  func NewUpgradeShootStep(
    25  	os storage.Operations,
    26  	runtimeStorage storage.RuntimeStates,
    27  	cli provisioner.Client) *UpgradeShootStep {
    28  
    29  	return &UpgradeShootStep{
    30  		operationManager:    process.NewOperationManager(os),
    31  		provisionerClient:   cli,
    32  		runtimeStateStorage: runtimeStorage,
    33  	}
    34  }
    35  
    36  func (s *UpgradeShootStep) Name() string {
    37  	return "Upgrade_Shoot"
    38  }
    39  
    40  func (s *UpgradeShootStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) {
    41  	if operation.RuntimeID == "" {
    42  		log.Infof("Runtime does not exists, skipping a call to Provisioner")
    43  		return operation, 0, nil
    44  	}
    45  	log = log.WithField("runtimeID", operation.RuntimeID)
    46  
    47  	latestRuntimeStateWithOIDC, err := s.runtimeStateStorage.GetLatestWithOIDCConfigByRuntimeID(operation.RuntimeID)
    48  	if err != nil {
    49  		return s.operationManager.RetryOperation(operation, err.Error(), err, 5*time.Second, 1*time.Minute, log)
    50  	}
    51  	operation.LastRuntimeState = latestRuntimeStateWithOIDC
    52  
    53  	input, err := s.createUpgradeShootInput(operation)
    54  	if err != nil {
    55  		return s.operationManager.OperationFailed(operation, "invalid operation data - cannot create upgradeShoot input", err, log)
    56  	}
    57  
    58  	var provisionerResponse gqlschema.OperationStatus
    59  	if operation.ProvisionerOperationID == "" {
    60  		// trigger upgradeRuntime mutation
    61  		provisionerResponse, err = s.provisionerClient.UpgradeShoot(operation.ProvisioningParameters.ErsContext.GlobalAccountID, operation.RuntimeID, input)
    62  		if err != nil {
    63  			log.Errorf("call to provisioner failed: %s", err)
    64  			return operation, retryDuration, nil
    65  		}
    66  
    67  		repeat := time.Duration(0)
    68  		operation, repeat, _ = s.operationManager.UpdateOperation(operation, func(op *internal.Operation) {
    69  			op.ProvisionerOperationID = *provisionerResponse.ID
    70  			op.Description = "update in progress"
    71  		}, log)
    72  		if repeat != 0 {
    73  			log.Errorf("cannot save operation ID from provisioner")
    74  			return operation, retryDuration, nil
    75  		}
    76  	}
    77  
    78  	log.Infof("call to provisioner succeeded for update, got operation ID %q", *provisionerResponse.ID)
    79  
    80  	rs := internal.NewRuntimeState(*provisionerResponse.RuntimeID, operation.ID, nil, gardenerUpgradeInputToConfigInput(input))
    81  	rs.KymaVersion = operation.RuntimeVersion.Version
    82  	err = s.runtimeStateStorage.Insert(rs)
    83  	if err != nil {
    84  		log.Errorf("cannot insert runtimeState: %s", err)
    85  		return operation, 10 * time.Second, nil
    86  	}
    87  	log.Infof("cluster upgrade process initiated successfully")
    88  
    89  	// return repeat mode to start the initialization step which will now check the runtime status
    90  	return operation, 0, nil
    91  
    92  }
    93  
    94  func (s *UpgradeShootStep) createUpgradeShootInput(operation internal.Operation) (gqlschema.UpgradeShootInput, error) {
    95  	operation.InputCreator.SetProvisioningParameters(operation.ProvisioningParameters)
    96  	if operation.LastRuntimeState.ClusterConfig.OidcConfig != nil {
    97  		operation.InputCreator.SetOIDCLastValues(*operation.LastRuntimeState.ClusterConfig.OidcConfig)
    98  	}
    99  	fullInput, err := operation.InputCreator.CreateUpgradeShootInput()
   100  	if err != nil {
   101  		return fullInput, fmt.Errorf("while building upgradeShootInput for provisioner: %w", err)
   102  	}
   103  
   104  	// modify configuration
   105  	result := gqlschema.UpgradeShootInput{
   106  		GardenerConfig: &gqlschema.GardenerUpgradeInput{
   107  			OidcConfig:     fullInput.GardenerConfig.OidcConfig,
   108  			AutoScalerMax:  operation.UpdatingParameters.AutoScalerMax,
   109  			AutoScalerMin:  operation.UpdatingParameters.AutoScalerMin,
   110  			MaxSurge:       operation.UpdatingParameters.MaxSurge,
   111  			MaxUnavailable: operation.UpdatingParameters.MaxUnavailable,
   112  			MachineType:    operation.UpdatingParameters.MachineType,
   113  		},
   114  		Administrators: fullInput.Administrators,
   115  	}
   116  	result.GardenerConfig.ShootNetworkingFilterDisabled = operation.ProvisioningParameters.ErsContext.DisableEnterprisePolicyFilter()
   117  
   118  	return result, nil
   119  }
   120  
   121  func gardenerUpgradeInputToConfigInput(input gqlschema.UpgradeShootInput) *gqlschema.GardenerConfigInput {
   122  	result := &gqlschema.GardenerConfigInput{
   123  		MachineImage:        input.GardenerConfig.MachineImage,
   124  		MachineImageVersion: input.GardenerConfig.MachineImageVersion,
   125  		DiskType:            input.GardenerConfig.DiskType,
   126  		VolumeSizeGb:        input.GardenerConfig.VolumeSizeGb,
   127  		Purpose:             input.GardenerConfig.Purpose,
   128  		OidcConfig:          input.GardenerConfig.OidcConfig,
   129  	}
   130  	if input.GardenerConfig.KubernetesVersion != nil {
   131  		result.KubernetesVersion = *input.GardenerConfig.KubernetesVersion
   132  	}
   133  	if input.GardenerConfig.MachineType != nil {
   134  		result.MachineType = *input.GardenerConfig.MachineType
   135  	}
   136  	if input.GardenerConfig.AutoScalerMin != nil {
   137  		result.AutoScalerMin = *input.GardenerConfig.AutoScalerMin
   138  	}
   139  	if input.GardenerConfig.AutoScalerMax != nil {
   140  		result.AutoScalerMax = *input.GardenerConfig.AutoScalerMax
   141  	}
   142  	if input.GardenerConfig.MaxSurge != nil {
   143  		result.MaxSurge = *input.GardenerConfig.MaxSurge
   144  	}
   145  	if input.GardenerConfig.MaxUnavailable != nil {
   146  		result.MaxUnavailable = *input.GardenerConfig.MaxUnavailable
   147  	}
   148  	if input.GardenerConfig.ShootNetworkingFilterDisabled != nil {
   149  		result.ShootNetworkingFilterDisabled = input.GardenerConfig.ShootNetworkingFilterDisabled
   150  	}
   151  
   152  	return result
   153  }