From b56341c7f42c84a2edf8749759ef6d7e42929e67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bojanowski?= Date: Fri, 2 Aug 2024 14:39:13 +0200 Subject: [PATCH] bugfix: do not output logs if vcluster connect is invoked with --print flag --- cmd/vclusterctl/cmd/connect.go | 14 ++++++++++++++ test/e2e_cli/connect/connect.go | 24 +++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/cmd/vclusterctl/cmd/connect.go b/cmd/vclusterctl/cmd/connect.go index 2dd7873ee..f89412136 100644 --- a/cmd/vclusterctl/cmd/connect.go +++ b/cmd/vclusterctl/cmd/connect.go @@ -4,6 +4,10 @@ import ( "cmp" "context" "fmt" + "io" + "os" + + "github.com/sirupsen/logrus" "github.com/loft-sh/log" "github.com/loft-sh/vcluster/pkg/cli" @@ -77,6 +81,7 @@ func (cmd *ConnectCmd) Run(ctx context.Context, args []string) error { if err != nil { return err } + cmd.overrideLogStdoutIfNeeded() cfg := cmd.LoadedConfig(cmd.Log) @@ -100,3 +105,12 @@ func (cmd *ConnectCmd) validateFlags() error { return nil } + +// overrideLogStdoutIfNeeded +// If user specifies --print flag, we have to discard all the logs, otherwise +// we will get invalid kubeconfig. +func (cmd *ConnectCmd) overrideLogStdoutIfNeeded() { + if cmd.Print { + cmd.Log = log.NewStdoutLogger(os.Stdin, io.Discard, os.Stderr, logrus.InfoLevel) + } +} diff --git a/test/e2e_cli/connect/connect.go b/test/e2e_cli/connect/connect.go index 4f36b3128..5319b3e86 100644 --- a/test/e2e_cli/connect/connect.go +++ b/test/e2e_cli/connect/connect.go @@ -2,6 +2,7 @@ package connect import ( "os" + "os/exec" "strings" "github.com/loft-sh/vcluster/cmd/vclusterctl/cmd" @@ -12,7 +13,6 @@ import ( var _ = ginkgo.Describe("Connect to vCluster", func() { f := framework.DefaultFramework - ginkgo.BeforeEach(func() { disconnectCmd := cmd.NewDisconnectCmd(&flags.GlobalFlags{}) disconnectCmd.SetArgs([]string{}) @@ -38,6 +38,28 @@ var _ = ginkgo.Describe("Connect to vCluster", func() { framework.ExpectNoError(err) }) + ginkgo.It("should print connect config to a file and use it to connect to an OSS vcluster", func() { + kcfgFile, err := os.CreateTemp("", "kubeconfig") + framework.ExpectNoError(err) + // vcluster CLI has to be in $PATH + connectCmd := exec.Command("vcluster", "connect", "-n", f.VclusterNamespace, "--print", f.VclusterName) + kubeConfigBytes, err := connectCmd.Output() + framework.ExpectNoError(err) + _, err = kcfgFile.Write(kubeConfigBytes) + framework.ExpectNoError(err) + kubectlCmd := exec.Command("kubectl", "--kubeconfig", kcfgFile.Name(), "get", "pods", "-A") + err = kubectlCmd.Run() + framework.ExpectNoError(err) + }) + + ginkgo.It("should fail to print vcluster connect config to a file", func() { + notExistingVClusterName := "INVALID" + // vcluster CLI has to be in $PATH + connectCmd := exec.Command("vcluster", "connect", "-n", notExistingVClusterName, "--print", notExistingVClusterName) + err := connectCmd.Run() + framework.ExpectError(err) + }) + ginkgo.It("should connect to an OSS vcluster and execute a command", func() { connectCmd := cmd.NewConnectCmd(&flags.GlobalFlags{}) connectCmd.SetArgs([]string{f.VclusterName, "--", "kubectl", "get", "ns"})