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

     1  package update
     2  
     3  import (
     4  	"time"
     5  
     6  	reconcilerApi "github.com/kyma-incubator/reconciler/pkg/keb"
     7  	"github.com/kyma-project/kyma-environment-broker/internal"
     8  	"github.com/kyma-project/kyma-environment-broker/internal/process"
     9  	"github.com/kyma-project/kyma-environment-broker/internal/process/input"
    10  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    11  	"github.com/sirupsen/logrus"
    12  )
    13  
    14  const BTPOperatorComponentName = "btp-operator"
    15  
    16  var ConfigMapGetter internal.ClusterIDGetter = internal.GetClusterIDWithKubeconfig
    17  
    18  type BTPOperatorOverridesStep struct {
    19  	operationManager *process.OperationManager
    20  	components       input.ComponentListProvider
    21  }
    22  
    23  func NewBTPOperatorOverridesStep(os storage.Operations, components input.ComponentListProvider) *BTPOperatorOverridesStep {
    24  	return &BTPOperatorOverridesStep{
    25  		operationManager: process.NewOperationManager(os),
    26  		components:       components,
    27  	}
    28  }
    29  
    30  func (s *BTPOperatorOverridesStep) Name() string {
    31  	return "BTPOperatorOverrides"
    32  }
    33  
    34  func (s *BTPOperatorOverridesStep) Run(operation internal.Operation, logger logrus.FieldLogger) (internal.Operation, time.Duration, error) {
    35  	if operation.LastRuntimeState.ClusterSetup == nil {
    36  		logger.Infof("no last runtime state found, skipping")
    37  		return operation, 0, nil
    38  	}
    39  	if !operation.InputCreator.Configuration().ContainsAdditionalComponent(internal.BTPOperatorComponentName) {
    40  		logger.Infof("BTP operator is not in the list of additional components, skipping")
    41  		return operation, 0, nil
    42  	}
    43  	// get btp-operator component input and calculate overrides
    44  	ci, err := getComponentInput(s.components, BTPOperatorComponentName, operation.RuntimeVersion, operation.InputCreator.Configuration())
    45  	if err != nil {
    46  		return s.operationManager.RetryOperation(operation, "failed to get components", err, 5*time.Second, 30*time.Second, logger)
    47  	}
    48  	if err := s.setBTPOperatorOverrides(&ci, operation, logger); err != nil {
    49  		return s.operationManager.RetryOperation(operation, "failed to create BTP Operator input", err, 5*time.Second, 30*time.Second, logger)
    50  	}
    51  
    52  	// find last btp-operator config if any
    53  	last := -1
    54  	for i, c := range operation.LastRuntimeState.ClusterSetup.KymaConfig.Components {
    55  		if c.Component == BTPOperatorComponentName {
    56  			last = i
    57  			break
    58  		}
    59  	}
    60  
    61  	// didn't find btp-operator in last runtime state, append components
    62  	if last == -1 {
    63  		operation.LastRuntimeState.ClusterSetup.KymaConfig.Components = append(operation.LastRuntimeState.ClusterSetup.KymaConfig.Components, ci)
    64  		operation.RequiresReconcilerUpdate = true
    65  		return operation, 0, nil
    66  	}
    67  
    68  	// found btp-operator in last runtime state but config isn't matching
    69  	l := operation.LastRuntimeState.ClusterSetup.KymaConfig.Components[last]
    70  	if !internal.CheckBTPCredsMatching(l, ci) {
    71  		operation.RequiresReconcilerUpdate = true
    72  		operation.LastRuntimeState.ClusterSetup.KymaConfig.Components[last] = ci
    73  	}
    74  	return operation, 0, nil
    75  }
    76  
    77  func (s *BTPOperatorOverridesStep) setBTPOperatorOverrides(c *reconcilerApi.Component, operation internal.Operation, logger logrus.FieldLogger) error {
    78  	clusterID := operation.InstanceDetails.ServiceManagerClusterID
    79  	if clusterID == "" {
    80  		var err error
    81  		if clusterID, err = ConfigMapGetter(operation.InstanceDetails.Kubeconfig); err != nil {
    82  			return err
    83  		}
    84  	}
    85  
    86  	creds := operation.ProvisioningParameters.ErsContext.SMOperatorCredentials
    87  	c.Configuration = internal.GetBTPOperatorReconcilerOverrides(creds, clusterID)
    88  	if clusterID != operation.InstanceDetails.ServiceManagerClusterID {
    89  		f := func(op *internal.Operation) {
    90  			op.InstanceDetails.ServiceManagerClusterID = clusterID
    91  		}
    92  		_, _, err := s.operationManager.UpdateOperation(operation, f, logger)
    93  		return err
    94  	}
    95  	return nil
    96  }