Skip to content

Commit

Permalink
add logic in connect command to check if vcluster can be connected us…
Browse files Browse the repository at this point in the history
…ing the kubeconfig
  • Loading branch information
kale-amruta committed Jan 17, 2025
1 parent a014dba commit faf56f1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
37 changes: 36 additions & 1 deletion pkg/cli/connect_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +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
}

// check if we should execute command
if len(command) > 0 {
if !cmd.portForwarding {
Expand Down Expand Up @@ -141,6 +147,33 @@ 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.Second*10, 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{})
return err == nil, nil
}
})
if werr != nil {
return fmt.Errorf("failed connecting to vcluster, verify connection arguments: %w %w", werr, err)
}
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 @@ -651,6 +684,8 @@ func getLocalVClusterConfig(vKubeConfig clientcmdapi.Config, options *ConnectOpt

// update vCluster server address in case of OSS vClusters only
if options.LocalPort != 0 {
fmt.Println("getvclusterconfig")
fmt.Println(options.LocalPort)
for _, cluster := range vKubeConfig.Clusters {
if cluster == nil {
continue
Expand Down Expand Up @@ -682,7 +717,7 @@ func (cmd *connectHelm) waitForVCluster(ctx context.Context, vKubeConfig clientc
return err
}

err = wait.PollUntilContextTimeout(ctx, time.Millisecond*200, time.Minute*3, true, func(ctx context.Context) (bool, error) {
err = wait.PollUntilContextTimeout(ctx, time.Millisecond*200, time.Minute*1, true, func(ctx context.Context) (bool, error) {
select {
case err := <-errorChan:
return false, err
Expand Down
12 changes: 12 additions & 0 deletions test/e2e_cli/connect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,16 @@ var _ = ginkgo.Describe("Connect to vCluster", func() {
err := connectCmd.Execute()
framework.ExpectNoError(err)
})

ginkgo.It("should fail saying the client timeout exceeded", func() {
kcfgFile, err := os.CreateTemp("", "kubeconfig")
framework.ExpectNoError(err)
connectCmd := cmd.NewConnectCmd(&flags.GlobalFlags{})
err = connectCmd.Flags().Set("kube-config", kcfgFile.Name())
framework.ExpectNoError(err)
err = connectCmd.Flags().Set("server", "testdomain.org")
framework.ExpectNoError(err)
err = connectCmd.Execute()
framework.ExpectError(err)
})
})

0 comments on commit faf56f1

Please sign in to comment.