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

bugfix: do not output logs in vcluster connect if --print is set #2014

Merged
Merged
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
14 changes: 14 additions & 0 deletions cmd/vclusterctl/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand All @@ -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)
}
}
24 changes: 23 additions & 1 deletion test/e2e_cli/connect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package connect

import (
"os"
"os/exec"
"strings"

"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd"
Expand All @@ -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{})
Expand All @@ -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"})
Expand Down
Loading