diff --git a/pkg/cli/connect_helm.go b/pkg/cli/connect_helm.go index 9d7bc29186..131c700688 100644 --- a/pkg/cli/connect_helm.go +++ b/pkg/cli/connect_helm.go @@ -105,6 +105,14 @@ func (cmd *connectHelm) connect(ctx context.Context, vCluster *find.VCluster, co 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 if len(command) > 0 { if !cmd.portForwarding { @@ -650,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) } } @@ -689,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 } }) diff --git a/test/e2e_cli/connect/connect.go b/test/e2e_cli/connect/connect.go index 5319b3e86e..69a9904957 100644 --- a/test/e2e_cli/connect/connect.go +++ b/test/e2e_cli/connect/connect.go @@ -67,4 +67,15 @@ var _ = ginkgo.Describe("Connect to vCluster", func() { err := connectCmd.Execute() framework.ExpectNoError(err) }) + + ginkgo.It("should fail saying the client timeout exceeded", func() { + connectCmd := cmd.NewConnectCmd(&flags.GlobalFlags{}) + connectCmd.SetArgs([]string{f.VclusterName}) + err := connectCmd.Flags().Set("kube-config", f.VClusterKubeConfigFile.Name()) + framework.ExpectNoError(err) + err = connectCmd.Flags().Set("server", "testdomain.org") + framework.ExpectNoError(err) + err = connectCmd.Execute() + framework.ExpectError(err) + }) })