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

feat: Adding option --use-kubeconfig in vcluster cli arguments, #1729

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion cmd/vclusterctl/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func wrapCompletionFuncWithTimeout(defaultDirective cobra.ShellCompDirective, co
// It takes into account the namespace if specified by the --namespace flag.
func newValidVClusterNameFunc(globalFlags *flags.GlobalFlags) completionFunc {
fn := func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
vclusters, err := find.ListVClusters(cmd.Context(), globalFlags.Context, "", globalFlags.Namespace, log.Default.ErrorStreamOnly())
vclusters, err := find.ListVClusters(cmd.Context(), globalFlags.UseKubeConfig, globalFlags.Context, "", globalFlags.Namespace, log.Default.ErrorStreamOnly())
if err != nil {
return []string{}, cobra.ShellCompDirectiveError | cobra.ShellCompDirectiveNoFileComp
}
Expand Down
1 change: 0 additions & 1 deletion cmd/vclusterctl/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ vcluster connect test -n test -- kubectl get ns

// platform
cobraCmd.Flags().StringVar(&cmd.Project, "project", "", "[PLATFORM] The platform project the vCluster is in")

return cobraCmd
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/cli/activate_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func ActivatePlatform(ctx context.Context, options *ActivateOptions, globalFlags
}

if globalFlags.Namespace == "" {
globalFlags.Namespace, err = GetVClusterNamespace(ctx, globalFlags.Context, vClusterName, globalFlags.Namespace, log)
globalFlags.Namespace, err = GetVClusterNamespace(ctx, globalFlags.UseKubeConfig, globalFlags.Context, vClusterName, globalFlags.Namespace, log)
if err != nil {
log.Warnf("Error retrieving vCluster namespace: %v", err)
}
Expand All @@ -68,13 +68,13 @@ func ActivatePlatform(ctx context.Context, options *ActivateOptions, globalFlags
return nil
}

func GetVClusterNamespace(ctx context.Context, context, name, namespace string, log log.Logger) (string, error) {
func GetVClusterNamespace(ctx context.Context, kubeconfigPath, context, name, namespace string, log log.Logger) (string, error) {
if name == "" {
return "", fmt.Errorf("please specify a name")
}

// list virtual clusters
ossVClusters, err := find.ListOSSVClusters(ctx, context, name, namespace)
ossVClusters, err := find.ListOSSVClusters(ctx, kubeconfigPath, context, name, namespace)
if err != nil {
log.Warnf("Error retrieving vclusters: %v", err)
return "", err
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/connect_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func ConnectHelm(ctx context.Context, options *ConnectOptions, globalFlags *flag
}

// retrieve the vcluster
vCluster, err := find.GetVCluster(ctx, cmd.Context, vClusterName, cmd.Namespace, cmd.Log)
vCluster, err := find.GetVCluster(ctx, cmd.UseKubeConfig, cmd.Context, vClusterName, cmd.Namespace, cmd.Log)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/delete_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func DeleteHelm(ctx context.Context, options *DeleteOptions, globalFlags *flags.
}

// find vcluster
vCluster, err := find.GetVCluster(ctx, cmd.Context, vClusterName, cmd.Namespace, cmd.log)
vCluster, err := find.GetVCluster(ctx, cmd.UseKubeConfig, cmd.Context, vClusterName, cmd.Namespace, cmd.log)
if err != nil {
if !cmd.IgnoreNotFound {
return err
Expand Down Expand Up @@ -159,7 +159,7 @@ func DeleteHelm(ctx context.Context, options *DeleteOptions, globalFlags *flags.
}

// check if there are any other vclusters in the namespace you are deleting vcluster in.
vClusters, err := find.ListVClusters(ctx, cmd.Context, "", cmd.Namespace, cmd.log)
vClusters, err := find.ListVClusters(ctx, cmd.UseKubeConfig, cmd.Context, "", cmd.Namespace, cmd.log)
if err != nil {
return err
}
Expand Down
55 changes: 38 additions & 17 deletions pkg/cli/find/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@ package find
import (
"context"
"fmt"
"strings"
"time"

"github.com/loft-sh/log"
"github.com/loft-sh/log/survey"
"github.com/loft-sh/log/terminal"
"github.com/loft-sh/vcluster/pkg/platform"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/loft-sh/vcluster/pkg/constants"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -21,6 +16,9 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"sigs.k8s.io/controller-runtime/pkg/client"
"strings"
"time"
)

const VirtualClusterSelector = "app=vcluster"
Expand Down Expand Up @@ -66,6 +64,23 @@ func CurrentContext() (string, *clientcmdapi.Config, error) {
return rawConfig.CurrentContext, &rawConfig, nil
}

func TargetContext(kubeconfigPath, context string) (string, *clientcmdapi.Config, error) {
rule := clientcmd.NewDefaultClientConfigLoadingRules()
rule.ExplicitPath = kubeconfigPath
rawConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rule, &clientcmd.ConfigOverrides{}).RawConfig()
if err != nil {
return "", nil, err
}
if context == "" {
return rawConfig.CurrentContext, &rawConfig, nil
}
_, ok := rawConfig.Contexts[context]
if !ok {
return "", nil, fmt.Errorf("context %s not found in kubeconfig %s", context, rule.GetDefaultFilename())
}
return context, &rawConfig, nil
}

func GetPlatformVCluster(ctx context.Context, platformClient platform.Client, name, project string, log log.Logger) (*platform.VirtualClusterInstanceProject, error) {
platformVClusters, err := platform.ListVClusters(ctx, platformClient, name, project)
if err != nil {
Expand Down Expand Up @@ -109,13 +124,13 @@ func GetPlatformVCluster(ctx context.Context, platformClient platform.Client, na
return nil, fmt.Errorf("unexpected error searching for selected virtual cluster")
}

func GetVCluster(ctx context.Context, context, name, namespace string, log log.Logger) (*VCluster, error) {
func GetVCluster(ctx context.Context, kubeconfigPath, context, name, namespace string, log log.Logger) (*VCluster, error) {
if name == "" {
return nil, fmt.Errorf("please specify a name")
}

// list virtual clusters
ossVClusters, err := ListVClusters(ctx, context, name, namespace, log)
ossVClusters, err := ListVClusters(ctx, kubeconfigPath, context, name, namespace, log)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -189,25 +204,29 @@ func FormatOptions(format string, options [][]string) []string {
return retOptions
}

func ListVClusters(ctx context.Context, context, name, namespace string, log log.Logger) ([]VCluster, error) {
func ListVClusters(ctx context.Context, kubeconfigPath, context, name, namespace string, log log.Logger) ([]VCluster, error) {
var err error
if context == "" {
var err error
if kubeconfigPath == "" && context == "" {
context, _, err = CurrentContext()
if err != nil {
return nil, err
}
} else {
context, _, err = TargetContext(kubeconfigPath, context)
if err != nil {
return nil, err
}
}

ossVClusters, err := ListOSSVClusters(ctx, context, name, namespace)
ossVClusters, err := ListOSSVClusters(ctx, kubeconfigPath, context, name, namespace)
if err != nil {
log.Warnf("Error retrieving vclusters: %v", err)
}

return ossVClusters, nil
}

func ListOSSVClusters(ctx context.Context, context, name, namespace string) ([]VCluster, error) {
func ListOSSVClusters(ctx context.Context, kubeconfigPath, context, name, namespace string) ([]VCluster, error) {
var err error

timeout := time.Minute
Expand All @@ -216,13 +235,13 @@ func ListOSSVClusters(ctx context.Context, context, name, namespace string) ([]V
timeout = time.Second * 5
}

vclusters, err := findInContext(ctx, context, name, namespace, timeout, false)
vclusters, err := findInContext(ctx, kubeconfigPath, context, name, namespace, timeout, false)
if err != nil && vClusterName == "" {
return nil, errors.Wrap(err, "find vcluster")
}

if vClusterName != "" {
parentContextVClusters, err := findInContext(ctx, vClusterContext, name, namespace, time.Minute, true)
parentContextVClusters, err := findInContext(ctx, kubeconfigPath, vClusterContext, name, namespace, time.Minute, true)
if err != nil {
return nil, errors.Wrap(err, "find vcluster")
}
Expand Down Expand Up @@ -275,9 +294,11 @@ func VClusterFromContext(originalContext string) (name string, namespace string,
return originalContext, "", ""
}

func findInContext(ctx context.Context, context, name, namespace string, timeout time.Duration, isParentContext bool) ([]VCluster, error) {
func findInContext(ctx context.Context, kubeconfigPath, context, name, namespace string, timeout time.Duration, isParentContext bool) ([]VCluster, error) {
vclusters := []VCluster{}
kubeClientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{
rules := clientcmd.NewDefaultClientConfigLoadingRules()
rules.ExplicitPath = kubeconfigPath
kubeClientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, &clientcmd.ConfigOverrides{
CurrentContext: context,
})
restConfig, err := kubeClientConfig.ClientConfig()
Expand Down
14 changes: 8 additions & 6 deletions pkg/cli/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import (

// GlobalFlags is the flags that contains the global flags
type GlobalFlags struct {
Silent bool
Debug bool
Config string
Context string
Namespace string
LogOutput string
Silent bool
Debug bool
Config string
UseKubeConfig string
Context string
Namespace string
LogOutput string
}

// SetGlobalFlags applies the global flags
func SetGlobalFlags(flags *flag.FlagSet) *GlobalFlags {
globalFlags := &GlobalFlags{}

flags.BoolVar(&globalFlags.Debug, "debug", false, "Prints the stack trace if an error occurs")
flags.StringVar(&globalFlags.UseKubeConfig, "use-kubeconfig", "", "The kubernetes config file path to use")
flags.StringVar(&globalFlags.Context, "context", "", "The kubernetes config context to use")
flags.StringVarP(&globalFlags.Namespace, "namespace", "n", "", "The kubernetes namespace to use")
flags.BoolVarP(&globalFlags.Silent, "silent", "s", false, "Run in silent mode and prevents any vcluster log output except panics & fatals")
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/list_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func ListHelm(ctx context.Context, options *ListOptions, globalFlags *flags.Glob
namespace = globalFlags.Namespace
}

vClusters, err := find.ListVClusters(ctx, globalFlags.Context, "", namespace, log.ErrorStreamOnly())
vClusters, err := find.ListVClusters(ctx, globalFlags.UseKubeConfig, globalFlags.Context, "", namespace, log.ErrorStreamOnly())
if err != nil {
return err
}
Expand Down Expand Up @@ -93,7 +93,7 @@ func printVClusters(ctx context.Context, options *ListOptions, output []ListVClu
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()

vClusters, _ := find.ListVClusters(ctx, globalFlags.Context, "", "", log.Discard)
vClusters, _ := find.ListVClusters(ctx, globalFlags.UseKubeConfig, globalFlags.Context, "", "", log.Discard)
if len(vClusters) > 0 {
logger.Infof("You also have %d virtual clusters in your current kube-context.", len(vClusters))
logger.Info("If you want to see them, run: 'vcluster list --manager helm' or 'vcluster use manager helm' to change the default")
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/pause_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type PauseOptions struct {

func PauseHelm(ctx context.Context, globalFlags *flags.GlobalFlags, vClusterName string, log log.Logger) error {
// find vcluster
vCluster, err := find.GetVCluster(ctx, globalFlags.Context, vClusterName, globalFlags.Namespace, log)
vCluster, err := find.GetVCluster(ctx, globalFlags.UseKubeConfig, globalFlags.Context, vClusterName, globalFlags.Namespace, log)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/resume_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type ResumeOptions struct {
}

func ResumeHelm(ctx context.Context, globalFlags *flags.GlobalFlags, vClusterName string, log log.Logger) error {
vCluster, err := find.GetVCluster(ctx, globalFlags.Context, vClusterName, globalFlags.Namespace, log)
vCluster, err := find.GetVCluster(ctx, globalFlags.UseKubeConfig, globalFlags.Context, vClusterName, globalFlags.Namespace, log)
if err != nil {
return err
}
Expand Down