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

     1  package upgrade_kyma
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  
     8  	reconcilerApi "github.com/kyma-incubator/reconciler/pkg/keb"
     9  	"github.com/kyma-project/kyma-environment-broker/internal"
    10  	kebError "github.com/kyma-project/kyma-environment-broker/internal/error"
    11  	"github.com/kyma-project/kyma-environment-broker/internal/process"
    12  	"github.com/kyma-project/kyma-environment-broker/internal/reconciler"
    13  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    14  	"github.com/sirupsen/logrus"
    15  )
    16  
    17  type ApplyClusterConfigurationStep struct {
    18  	operationManager    *process.UpgradeKymaOperationManager
    19  	reconcilerClient    reconciler.Client
    20  	runtimeStateStorage storage.RuntimeStates
    21  }
    22  
    23  func NewApplyClusterConfigurationStep(os storage.Operations, rs storage.RuntimeStates, reconcilerClient reconciler.Client) *ApplyClusterConfigurationStep {
    24  	return &ApplyClusterConfigurationStep{
    25  		operationManager:    process.NewUpgradeKymaOperationManager(os),
    26  		reconcilerClient:    reconcilerClient,
    27  		runtimeStateStorage: rs,
    28  	}
    29  }
    30  
    31  func (s *ApplyClusterConfigurationStep) Name() string {
    32  	return "Apply_Cluster_Configuration"
    33  }
    34  
    35  func (s *ApplyClusterConfigurationStep) Run(operation internal.UpgradeKymaOperation, log logrus.FieldLogger) (internal.UpgradeKymaOperation, time.Duration, error) {
    36  	if operation.ClusterConfigurationApplied {
    37  		log.Infof("Cluster configuration already applied")
    38  		return operation, 0, nil
    39  	}
    40  	operation.InputCreator.SetRuntimeID(operation.InstanceDetails.RuntimeID).
    41  		SetInstanceID(operation.InstanceID).
    42  		SetShootName(operation.InstanceDetails.ShootName).
    43  		SetShootDomain(operation.ShootDomain).
    44  		SetProvisioningParameters(operation.ProvisioningParameters).
    45  		SetClusterName(operation.ClusterName)
    46  
    47  	clusterConfiguration, err := operation.InputCreator.CreateClusterConfiguration()
    48  	if err != nil {
    49  		log.Errorf("Unable to apply cluster configuration: %s", err.Error())
    50  		return s.operationManager.OperationFailed(operation, "invalid operation data - cannot create cluster configuration", err, log)
    51  	}
    52  
    53  	if err := internal.CheckBTPCredsValid(clusterConfiguration); err != nil {
    54  		log.Errorf("Sanity check for BTP operator configuration failed: %s", err.Error())
    55  		return s.operationManager.OperationFailed(operation, "invalid BTP Operator configuration", err, log)
    56  	}
    57  
    58  	err = s.runtimeStateStorage.Insert(
    59  		internal.NewRuntimeStateWithReconcilerInput(clusterConfiguration.RuntimeID, operation.Operation.ID, &clusterConfiguration))
    60  	if err != nil {
    61  		log.Errorf("cannot insert runtimeState with reconciler payload: %s", err)
    62  		return operation, 10 * time.Second, nil
    63  	}
    64  
    65  	log.Infof("Apply Cluster Configuration: cluster(runtimeID)=%s, kymaVersion=%s, kymaProfile=%s, components=[%s], name=%s",
    66  		clusterConfiguration.RuntimeID,
    67  		clusterConfiguration.KymaConfig.Version,
    68  		clusterConfiguration.KymaConfig.Profile,
    69  		s.componentList(clusterConfiguration),
    70  		clusterConfiguration.RuntimeInput.Name)
    71  	state, err := s.reconcilerClient.ApplyClusterConfig(clusterConfiguration)
    72  	switch {
    73  	case kebError.IsTemporaryError(err):
    74  		msg := fmt.Sprintf("Request to Reconciler failed: %s", err.Error())
    75  		log.Error(msg)
    76  		return operation, 5 * time.Second, nil
    77  	case err != nil:
    78  		msg := fmt.Sprintf("Request to Reconciler failed: %s", err.Error())
    79  		log.Error(msg)
    80  		return s.operationManager.OperationFailed(operation, "Request to Reconciler failed", err, log)
    81  	}
    82  	log.Infof("Cluster configuration version %d", state.ConfigurationVersion)
    83  
    84  	updatedOperation, repeat, _ := s.operationManager.UpdateOperation(operation, func(operation *internal.UpgradeKymaOperation) {
    85  		operation.ClusterConfigurationVersion = state.ConfigurationVersion
    86  		operation.ClusterConfigurationApplied = true
    87  		operation.ClusterName = clusterConfiguration.RuntimeInput.Name
    88  	}, log)
    89  	if repeat != 0 {
    90  		log.Errorf("cannot save cluster configuration version")
    91  		return operation, 5 * time.Second, nil
    92  	}
    93  
    94  	// return some retry value to get back to initialisation step
    95  	return updatedOperation, 5 * time.Second, nil
    96  
    97  }
    98  
    99  func (s *ApplyClusterConfigurationStep) componentList(cluster reconcilerApi.Cluster) string {
   100  	vals := []string{}
   101  	for _, c := range cluster.KymaConfig.Components {
   102  		vals = append(vals, c.Component)
   103  	}
   104  	return strings.Join(vals, ", ")
   105  }