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

     1  package provisioning
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/kyma-project/kyma-environment-broker/common/hyperscaler"
     8  	"github.com/kyma-project/kyma-environment-broker/internal"
     9  	"github.com/kyma-project/kyma-environment-broker/internal/broker"
    10  	"github.com/kyma-project/kyma-environment-broker/internal/process"
    11  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    12  	"github.com/sirupsen/logrus"
    13  )
    14  
    15  type ResolveCredentialsStep struct {
    16  	operationManager *process.OperationManager
    17  	accountProvider  hyperscaler.AccountProvider
    18  	opStorage        storage.Operations
    19  	tenant           string
    20  }
    21  
    22  func NewResolveCredentialsStep(os storage.Operations, accountProvider hyperscaler.AccountProvider) *ResolveCredentialsStep {
    23  	return &ResolveCredentialsStep{
    24  		operationManager: process.NewOperationManager(os),
    25  		opStorage:        os,
    26  		accountProvider:  accountProvider,
    27  	}
    28  }
    29  
    30  func (s *ResolveCredentialsStep) Name() string {
    31  	return "Resolve_Target_Secret"
    32  }
    33  
    34  func (s *ResolveCredentialsStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) {
    35  	if operation.ProvisioningParameters.Parameters.TargetSecret != nil {
    36  		return operation, 0, nil
    37  	}
    38  
    39  	hypType, err := hyperscaler.FromCloudProvider(operation.InputCreator.Provider())
    40  	if err != nil {
    41  		msg := fmt.Sprintf("failing to determine the type of Hyperscaler to use for planID: %s", operation.ProvisioningParameters.PlanID)
    42  		log.Errorf("Aborting after %s", msg)
    43  		return s.operationManager.OperationFailed(operation, msg, err, log)
    44  	}
    45  
    46  	euAccess := internal.IsEuAccess(operation.ProvisioningParameters.PlatformRegion)
    47  	log.Infof("HAP lookup for credentials secret binding to provision cluster for global account ID %s on Hyperscaler %s, euAccess %v", operation.ProvisioningParameters.ErsContext.GlobalAccountID, hypType, euAccess)
    48  
    49  	var secretName string
    50  	if !broker.IsTrialPlan(operation.ProvisioningParameters.PlanID) {
    51  		secretName, err = s.accountProvider.GardenerSecretName(hypType, operation.ProvisioningParameters.ErsContext.GlobalAccountID, euAccess)
    52  	} else {
    53  		log.Infof("HAP lookup for shared secret binding")
    54  		secretName, err = s.accountProvider.GardenerSharedSecretName(hypType, euAccess)
    55  	}
    56  	if err != nil {
    57  		msg := fmt.Sprintf("HAP lookup for secret binding to provision cluster for global account ID %s on Hyperscaler %s has failed", operation.ProvisioningParameters.ErsContext.GlobalAccountID, hypType)
    58  		errMsg := fmt.Sprintf("%s: %s", msg, err)
    59  		log.Info(errMsg)
    60  
    61  		// if failed retry step every 10s by next 10min
    62  		dur := time.Since(operation.UpdatedAt).Round(time.Minute)
    63  
    64  		if dur < 10*time.Minute {
    65  			return operation, 10 * time.Second, nil
    66  		}
    67  
    68  		log.Errorf("Aborting after 10 minutes of failing to resolve provisioning secret binding for global account ID %s on Hyperscaler %s", operation.ProvisioningParameters.ErsContext.GlobalAccountID, hypType)
    69  
    70  		return s.operationManager.OperationFailed(operation, msg, err, log)
    71  	}
    72  	operation.ProvisioningParameters.Parameters.TargetSecret = &secretName
    73  
    74  	updatedOperation, err := s.opStorage.UpdateOperation(operation)
    75  	if err != nil {
    76  		return operation, 1 * time.Minute, nil
    77  	}
    78  
    79  	log.Infof("Resolved %s as target secret name to use for cluster provisioning for global account ID %s on Hyperscaler %s", *operation.ProvisioningParameters.Parameters.TargetSecret, operation.ProvisioningParameters.ErsContext.GlobalAccountID, hypType)
    80  
    81  	return *updatedOperation, 0, nil
    82  }