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

     1  package provisioning
     2  
     3  import (
     4  	"time"
     5  
     6  	btpmanagercredentials "github.com/kyma-project/kyma-environment-broker/internal/btpmanager/credentials"
     7  
     8  	"github.com/google/uuid"
     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/storage"
    13  	"github.com/sirupsen/logrus"
    14  	"sigs.k8s.io/controller-runtime/pkg/client"
    15  )
    16  
    17  const (
    18  	updateSecretBackoff = 10 * time.Second
    19  )
    20  
    21  type InjectBTPOperatorCredentialsStep struct {
    22  	operationManager  *process.OperationManager
    23  	k8sClientProvider func(kubeconfig string) (client.Client, error)
    24  }
    25  
    26  func NewInjectBTPOperatorCredentialsStep(os storage.Operations, k8sClientProvider func(kcfg string) (client.Client, error)) *InjectBTPOperatorCredentialsStep {
    27  	return &InjectBTPOperatorCredentialsStep{
    28  		operationManager:  process.NewOperationManager(os),
    29  		k8sClientProvider: k8sClientProvider,
    30  	}
    31  }
    32  
    33  func (s *InjectBTPOperatorCredentialsStep) Name() string {
    34  	return "Inject_BTP_Operator_Credentials"
    35  }
    36  
    37  func (s *InjectBTPOperatorCredentialsStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) {
    38  
    39  	if operation.RuntimeID == "" {
    40  		log.Error("Runtime ID is empty")
    41  		return s.operationManager.OperationFailed(operation, "Runtime ID is empty", nil, log)
    42  	}
    43  
    44  	if operation.K8sClient == nil {
    45  		log.Error("kubernetes client not set")
    46  		return s.operationManager.OperationFailed(operation, "kubernetes client not set", nil, log)
    47  	}
    48  
    49  	clusterID := operation.InstanceDetails.ServiceManagerClusterID
    50  	if clusterID == "" {
    51  		clusterID = uuid.NewString()
    52  		updatedOperation, backoff, err := s.operationManager.UpdateOperation(operation, func(op *internal.Operation) {
    53  			op.InstanceDetails.ServiceManagerClusterID = clusterID
    54  		}, log)
    55  		if err != nil {
    56  			log.Errorf("failed to update operation: %s", err)
    57  		}
    58  		if backoff != 0 {
    59  			log.Error("cannot save cluster ID")
    60  			return updatedOperation, backoff, nil
    61  		}
    62  	}
    63  
    64  	secret, err := btpmanagercredentials.PrepareSecret(operation.ProvisioningParameters.ErsContext.SMOperatorCredentials, clusterID)
    65  	if err != nil {
    66  		return s.operationManager.OperationFailed(operation, "secret preparation failed", err, log)
    67  	}
    68  
    69  	if err := btpmanagercredentials.CreateOrUpdateSecret(operation.K8sClient, secret, log); err != nil {
    70  		err = kebError.AsTemporaryError(err, "failed create/update of the secret")
    71  		return operation, updateSecretBackoff, nil
    72  	}
    73  	return operation, 0, nil
    74  }