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

     1  package provisioning
     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 CreateClusterConfigurationStep struct {
    18  	reconcilerClient    reconciler.Client
    19  	operationManager    *process.OperationManager
    20  	provisioningTimeout time.Duration
    21  	runtimeStateStorage storage.RuntimeStates
    22  }
    23  
    24  func NewCreateClusterConfiguration(os storage.Operations, runtimeStorage storage.RuntimeStates, reconcilerClient reconciler.Client) *CreateClusterConfigurationStep {
    25  	return &CreateClusterConfigurationStep{
    26  		reconcilerClient:    reconcilerClient,
    27  		operationManager:    process.NewOperationManager(os),
    28  		runtimeStateStorage: runtimeStorage,
    29  	}
    30  }
    31  
    32  var _ process.Step = (*CreateClusterConfigurationStep)(nil)
    33  
    34  func (s *CreateClusterConfigurationStep) Name() string {
    35  	return "Create_Cluster_Configuration"
    36  }
    37  
    38  func (s *CreateClusterConfigurationStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) {
    39  
    40  	if operation.ClusterConfigurationVersion != 0 {
    41  		log.Debugf("Cluster configuration already created, skipping")
    42  		return operation, 0, nil
    43  	}
    44  	log.Infof("Runtime id is %v", operation.RuntimeID)
    45  	operation.InputCreator.SetRuntimeID(operation.RuntimeID).
    46  		SetInstanceID(operation.InstanceID).
    47  		SetKubeconfig(operation.Kubeconfig).
    48  		SetShootName(operation.ShootName).
    49  		SetShootDomain(operation.ShootDomain).
    50  		SetShootDNSProviders(operation.ShootDNSProviders).
    51  		SetProvisioningParameters(operation.ProvisioningParameters)
    52  
    53  	clusterConfiguration, err := operation.InputCreator.CreateClusterConfiguration()
    54  	if err != nil {
    55  		log.Errorf("Unable to create cluster configuration: %s", err.Error())
    56  		return s.operationManager.OperationFailed(operation, "invalid operation data - cannot create cluster configuration", err, log)
    57  	}
    58  	if err := internal.CheckBTPCredsValid(clusterConfiguration); err != nil {
    59  		log.Errorf("Sanity check for BTP operator configuration failed: %s", err.Error())
    60  		return s.operationManager.OperationFailed(operation, "invalid BTP Operator configuration", err, log)
    61  	}
    62  
    63  	err = s.runtimeStateStorage.Insert(
    64  		internal.NewRuntimeStateWithReconcilerInput(clusterConfiguration.RuntimeID, operation.ID, &clusterConfiguration))
    65  	if err != nil {
    66  		log.Errorf("cannot insert runtimeState with reconciler payload: %s", err)
    67  		return operation, 10 * time.Second, nil
    68  	}
    69  
    70  	log.Infof("Creating Cluster Configuration: cluster(runtimeID)=%s, kymaVersion=%s, kymaProfile=%s, components=[%s], name=%s",
    71  		clusterConfiguration.RuntimeID,
    72  		clusterConfiguration.KymaConfig.Version,
    73  		clusterConfiguration.KymaConfig.Profile,
    74  		s.componentList(clusterConfiguration),
    75  		clusterConfiguration.RuntimeInput.Name)
    76  
    77  	state, err := s.reconcilerClient.ApplyClusterConfig(clusterConfiguration)
    78  	switch {
    79  	case kebError.IsTemporaryError(err):
    80  		msg := fmt.Sprintf("Request to Reconciler failed: %s", err.Error())
    81  		log.Error(msg)
    82  		return operation, 5 * time.Second, nil
    83  	case err != nil:
    84  		msg := fmt.Sprintf("Request to Reconciler failed: %s", err.Error())
    85  		log.Error(msg)
    86  		return s.operationManager.OperationFailed(operation, "Request to Reconciler failed", err, log)
    87  	}
    88  	log.Infof("Cluster configuration version %d", state.ConfigurationVersion)
    89  
    90  	updatedOperation, repeat, _ := s.operationManager.UpdateOperation(operation, func(operation *internal.Operation) {
    91  		operation.ClusterConfigurationVersion = state.ConfigurationVersion
    92  		operation.ClusterName = clusterConfiguration.RuntimeInput.Name
    93  	}, log)
    94  	if repeat != 0 {
    95  		log.Errorf("cannot save cluster configuration version")
    96  		return operation, 5 * time.Second, nil
    97  	}
    98  
    99  	return updatedOperation, 0, nil
   100  }
   101  
   102  func (s *CreateClusterConfigurationStep) componentList(cluster reconcilerApi.Cluster) string {
   103  	vals := []string{}
   104  	for _, c := range cluster.KymaConfig.Components {
   105  		vals = append(vals, c.Component)
   106  	}
   107  	return strings.Join(vals, ", ")
   108  }