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

     1  package update
     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 ApplyReconcilerConfigurationStep struct {
    18  	reconcilerClient    reconciler.Client
    19  	operationManager    *process.OperationManager
    20  	runtimeStateStorage storage.RuntimeStates
    21  }
    22  
    23  func NewApplyReconcilerConfigurationStep(os storage.Operations, runtimeStorage storage.RuntimeStates, reconcilerClient reconciler.Client) *ApplyReconcilerConfigurationStep {
    24  	return &ApplyReconcilerConfigurationStep{
    25  		reconcilerClient:    reconcilerClient,
    26  		operationManager:    process.NewOperationManager(os),
    27  		runtimeStateStorage: runtimeStorage,
    28  	}
    29  }
    30  
    31  func (s *ApplyReconcilerConfigurationStep) Name() string {
    32  	return "Apply_Reconciler_Configuration"
    33  }
    34  
    35  func (s *ApplyReconcilerConfigurationStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) {
    36  	if err := internal.CheckBTPCredsValid(*operation.LastRuntimeState.ClusterSetup); err != nil {
    37  		log.Errorf("Sanity check for BTP operator configuration failed: %s", err.Error())
    38  		return s.operationManager.OperationFailed(operation, "invalid BTP Operator configuration", err, log)
    39  	}
    40  	cluster := operation.LastRuntimeState.ClusterSetup
    41  	if err := s.runtimeStateStorage.Insert(internal.NewRuntimeStateWithReconcilerInput(cluster.RuntimeID, operation.ID, cluster)); err != nil {
    42  		log.Errorf("cannot insert runtimeState with reconciler payload: %s", err)
    43  		return operation, 10 * time.Second, nil
    44  	}
    45  
    46  	log.Infof("Applying Cluster Configuration: cluster(runtimeID)=%s, kymaVersion=%s, kymaProfile=%s, components=[%s]",
    47  		cluster.RuntimeID, cluster.KymaConfig.Version, cluster.KymaConfig.Profile, s.componentList(*cluster))
    48  	state, err := s.reconcilerClient.ApplyClusterConfig(*cluster)
    49  	switch {
    50  	case kebError.IsTemporaryError(err):
    51  		msg := fmt.Sprintf("Request to Reconciler failed: %s", err.Error())
    52  		log.Error(msg)
    53  		return operation, 5 * time.Second, nil
    54  	case err != nil:
    55  		return s.operationManager.OperationFailed(operation, "Request to Reconciler failed", err, log)
    56  	}
    57  
    58  	log.Infof("Reconciler configuration version %d", state.ConfigurationVersion)
    59  	updatedOperation, repeat, _ := s.operationManager.UpdateOperation(operation, func(op *internal.Operation) {
    60  		op.ClusterConfigurationVersion = state.ConfigurationVersion
    61  		op.CheckReconcilerStatus = true
    62  	}, log)
    63  	if repeat != 0 {
    64  		log.Errorf("cannot save cluster configuration version")
    65  		return operation, repeat, nil
    66  	}
    67  	return updatedOperation, 0, nil
    68  }
    69  
    70  func (s *ApplyReconcilerConfigurationStep) componentList(cluster reconcilerApi.Cluster) string {
    71  	vals := []string{}
    72  	for _, c := range cluster.KymaConfig.Components {
    73  		vals = append(vals, c.Component)
    74  	}
    75  	return strings.Join(vals, ", ")
    76  }