Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid unnecessary proxy container creation for already connected vCluster #2341

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
FabianKramm marked this conversation as resolved.
Show resolved Hide resolved
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
}
Loading