Skip to content

Commit

Permalink
fix: avoid unnecessary proxy container creation for already connected…
Browse files Browse the repository at this point in the history
… vCluster (#2341)

* fix: avoid unnecessary proxy container creation for already connected vCluster

Signed-off-by: Rokibul Hasan <[email protected]>

* Log info instead of returning an error when already connected to vcluster

Signed-off-by: Rokibul Hasan <[email protected]>

* Check if vCluster is ready

Signed-off-by: Rokibul Hasan <[email protected]>

* Use timeout ctx to get the serviceaccount

Signed-off-by: Rokibul Hasan <[email protected]>

---------

Signed-off-by: Rokibul Hasan <[email protected]>
  • Loading branch information
RokibulHasan7 authored Dec 19, 2024
1 parent ecc7bcf commit a8f1b6e
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions pkg/cli/connect_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ func ConnectHelm(ctx context.Context, options *ConnectOptions, globalFlags *flag
}

func (cmd *connectHelm) connect(ctx context.Context, vCluster *find.VCluster, command []string) error {
if connected, _ := checkIfAlreadyConnected(ctx, vCluster); connected {
cmd.Log.Infof("already connected to vcluster %q", vCluster.Name)
return nil
}

// prepare clients and find vcluster
err := cmd.prepare(ctx, vCluster)
if err != nil {
Expand Down Expand Up @@ -693,3 +698,32 @@ func (cmd *connectHelm) waitForVCluster(ctx context.Context, vKubeConfig clientc

return nil
}

func checkIfAlreadyConnected(ctx context.Context, vCluster *find.VCluster) (bool, error) {
currentContext, _, err := find.CurrentContext()
if err != nil {
return false, err
}
if currentContext == find.VClusterContextName(vCluster.Name, vCluster.Namespace, vCluster.Context) {
kubeConfig, err := vCluster.ClientFactory.ClientConfig()
if err != nil {
return false, err
}

vKubeClient, err := kubernetes.NewForConfig(kubeConfig)
if err != nil {
return false, err
}

timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

// Use the timeout context in the Get call
_, err = vKubeClient.CoreV1().ServiceAccounts("default").Get(timeoutCtx, "default", metav1.GetOptions{})
if err != nil {
return false, err
}
return true, nil
}
return false, nil
}

0 comments on commit a8f1b6e

Please sign in to comment.