Skip to content

Commit

Permalink
re-use waitForVcluster function and remove duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
kale-amruta committed Jan 28, 2025
1 parent c8658e1 commit eb9e6aa
Showing 1 changed file with 16 additions and 39 deletions.
55 changes: 16 additions & 39 deletions pkg/cli/connect_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@ func (cmd *connectHelm) connect(ctx context.Context, vCluster *find.VCluster, co
return err
}

// check if vcluster is ready
err = cmd.isVclusterReady(ctx, *kubeConfig, cmd.errorChan)
if err != nil {
return err
if !cmd.ConnectOptions.Print {
// check if vcluster is ready
err = cmd.waitForVCluster(ctx, *kubeConfig, cmd.errorChan)
if err != nil {
return fmt.Errorf("failed connecting to vcluster, verify connection arguments: %w ", err)
}
}

// check if we should execute command
Expand Down Expand Up @@ -147,36 +149,6 @@ func (cmd *connectHelm) connect(ctx context.Context, vCluster *find.VCluster, co
return nil
}

func (cmd *connectHelm) isVclusterReady(ctx context.Context, vKubeConfig clientcmdapi.Config, errorChan chan error) error {
vRestConfig, err := clientcmd.NewDefaultClientConfig(vKubeConfig, &clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return fmt.Errorf("create virtual rest config: %w", err)
}

vKubeClient, err := kubernetes.NewForConfig(vRestConfig)
if err != nil {
return fmt.Errorf("create virtual kube client: %w", err)
}

werr := wait.PollUntilContextTimeout(ctx, time.Millisecond*200, time.Minute*2, true, func(ctx context.Context) (bool, error) {
select {
case err := <-errorChan:
return false, err
default:
// check if service account exists
_, err := vKubeClient.CoreV1().ServiceAccounts("default").Get(ctx, "default", metav1.GetOptions{})
if err != nil {
cmd.Log.Debugf("failed to list default service account %v", err)
}
return err == nil, nil
}
})
if werr != nil {
return fmt.Errorf("failed connecting to vcluster, verify connection arguments: %w ", werr)
}
return nil
}

func writeKubeConfig(kubeConfig *clientcmdapi.Config, vClusterName string, options *ConnectOptions, globalFlags *flags.GlobalFlags, portForwarding bool, log log.Logger) error {
if kubeConfig == nil {
return errors.New("nil kubeconfig")
Expand Down Expand Up @@ -686,12 +658,14 @@ func getLocalVClusterConfig(vKubeConfig clientcmdapi.Config, options *ConnectOpt
vKubeConfig = *vKubeConfig.DeepCopy()

// update vCluster server address in case of OSS vClusters only
if options.LocalPort != 0 {
for _, cluster := range vKubeConfig.Clusters {
if cluster == nil {
continue
if options.Server == "" {
if options.LocalPort != 0 {
for _, cluster := range vKubeConfig.Clusters {
if cluster == nil {
continue
}
cluster.Server = "https://localhost:" + strconv.Itoa(options.LocalPort)
}
cluster.Server = "https://localhost:" + strconv.Itoa(options.LocalPort)
}
}

Expand Down Expand Up @@ -725,6 +699,9 @@ func (cmd *connectHelm) waitForVCluster(ctx context.Context, vKubeConfig clientc
default:
// check if service account exists
_, err = vKubeClient.CoreV1().ServiceAccounts("default").Get(ctx, "default", metav1.GetOptions{})
if err != nil {
cmd.Log.Debugf("failed to list default service account %v", err)
}
return err == nil, nil
}
})
Expand Down

0 comments on commit eb9e6aa

Please sign in to comment.