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