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

     1  package update
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/sirupsen/logrus"
     7  
     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/provisioner"
    12  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    13  	"sigs.k8s.io/controller-runtime/pkg/client"
    14  )
    15  
    16  type GetKubeconfigStep struct {
    17  	provisionerClient   provisioner.Client
    18  	operationManager    *process.OperationManager
    19  	provisioningTimeout time.Duration
    20  	k8sClientProvider   func(kcfg string) (client.Client, error)
    21  }
    22  
    23  func NewGetKubeconfigStep(os storage.Operations, provisionerClient provisioner.Client, k8sClientProvider func(kcfg string) (client.Client, error)) *GetKubeconfigStep {
    24  	return &GetKubeconfigStep{
    25  		provisionerClient: provisionerClient,
    26  		operationManager:  process.NewOperationManager(os),
    27  		k8sClientProvider: k8sClientProvider,
    28  	}
    29  }
    30  
    31  var _ process.Step = (*GetKubeconfigStep)(nil)
    32  
    33  func (s *GetKubeconfigStep) Name() string {
    34  	return "Get_Kubeconfig"
    35  }
    36  
    37  func (s *GetKubeconfigStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) {
    38  	if broker.IsOwnClusterPlan(operation.ProvisioningParameters.PlanID) {
    39  		operation.Kubeconfig = operation.ProvisioningParameters.Parameters.Kubeconfig
    40  	}
    41  
    42  	if operation.Kubeconfig != "" {
    43  		cli, err := s.k8sClientProvider(operation.Kubeconfig)
    44  		if err != nil {
    45  			log.Errorf("Unable to create k8s client from the kubeconfig")
    46  			return s.operationManager.RetryOperation(operation, "unable to create k8s client from the kubeconfig", err, 5*time.Second, 1*time.Minute, log)
    47  		}
    48  		operation.K8sClient = cli
    49  		return operation, 0, nil
    50  	}
    51  	if operation.RuntimeID == "" {
    52  		log.Errorf("Runtime ID is empty")
    53  		return s.operationManager.OperationFailed(operation, "Runtime ID is empty", nil, log)
    54  	}
    55  
    56  	status, err := s.provisionerClient.RuntimeStatus(operation.ProvisioningParameters.ErsContext.GlobalAccountID, operation.RuntimeID)
    57  	if err != nil {
    58  		log.Errorf("call to provisioner RuntimeStatus failed: %s", err.Error())
    59  		return operation, 1 * time.Minute, nil
    60  	}
    61  
    62  	if status.RuntimeConfiguration.Kubeconfig == nil {
    63  		log.Errorf("kubeconfig is not provided")
    64  		return operation, 1 * time.Minute, nil
    65  	}
    66  	k := *status.RuntimeConfiguration.Kubeconfig
    67  	log.Infof("kubeconfig details length: %v", len(k))
    68  	if len(k) < 10 {
    69  		log.Errorf("kubeconfig suspiciously small, requeueing after 30s")
    70  		return operation, 30 * time.Second, nil
    71  	}
    72  	cli, err := s.k8sClientProvider(*status.RuntimeConfiguration.Kubeconfig)
    73  	if err != nil {
    74  		log.Errorf("Unable to create k8s client from the kubeconfig")
    75  		return s.operationManager.RetryOperation(operation, "unable to create k8s client from the kubeconfig", err, 5*time.Second, 1*time.Minute, log)
    76  
    77  	}
    78  	operation.Kubeconfig = *status.RuntimeConfiguration.Kubeconfig
    79  	operation.K8sClient = cli
    80  
    81  	return operation, 0, nil
    82  }