Skip to content

Commit

Permalink
Fix context handling for the CLI (#1406)
Browse files Browse the repository at this point in the history
* Use context as set by user for Kubernetes

* Pass in context using Kube options to CLI run commands
  • Loading branch information
Thomas Eckert authored Aug 11, 2022
1 parent e403f35 commit f8331da
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
16 changes: 15 additions & 1 deletion acceptance/framework/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package cli
import (
"fmt"
"os/exec"
"strings"
"testing"

"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/hashicorp/consul-k8s/acceptance/framework/config"
"github.com/hashicorp/consul-k8s/acceptance/framework/logger"
)

// CLI provides access to compile and execute commands with the `consul-k8s` CLI.
Expand All @@ -22,10 +26,20 @@ func NewCLI() (*CLI, error) {
}

// Run runs the CLI with the given args.
func (c *CLI) Run(args ...string) ([]byte, error) {
func (c *CLI) Run(t *testing.T, options *k8s.KubectlOptions, args ...string) ([]byte, error) {
if !c.initialized {
return nil, fmt.Errorf("CLI must be initialized before calling Run, use `cli.NewCLI()` to initialize.")
}

// Append configuration from `options` to the command.
if options.ConfigPath != "" {
args = append(args, "-config", options.ConfigPath)
}
if options.ContextName != "" {
args = append(args, "-context", options.ContextName)
}

logger.Logf(t, "Running `consul-k8s %s`", strings.Join(args, " "))
cmd := exec.Command("cli", args...)
return cmd.Output()
}
8 changes: 3 additions & 5 deletions acceptance/framework/consul/cli_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ func (c *CLICluster) Create(t *testing.T) {

// Set the args for running the install command.
args := []string{"install"}
args = c.setKube(args)

for k, v := range c.values {
args = append(args, "-set", fmt.Sprintf("%s=%s", k, v))
Expand All @@ -134,7 +133,7 @@ func (c *CLICluster) Create(t *testing.T) {
args = append(args, "-timeout", "15m")
args = append(args, "-auto-approve")

out, err := c.cli.Run(args...)
out, err := c.cli.Run(t, c.kubectlOptions, args...)
if err != nil {
c.logger.Logf(t, "error running command `consul-k8s %s`: %s", strings.Join(args, " "), err.Error())
c.logger.Logf(t, "command stdout: %s", string(out))
Expand Down Expand Up @@ -167,7 +166,7 @@ func (c *CLICluster) Upgrade(t *testing.T, helmValues map[string]string) {
args = append(args, "-timeout", "15m")
args = append(args, "-auto-approve")

out, err := c.cli.Run(args...)
out, err := c.cli.Run(t, c.kubectlOptions, args...)
if err != nil {
c.logger.Logf(t, "error running command `consul-k8s %s`: %s", strings.Join(args, " "), err.Error())
c.logger.Logf(t, "command stdout: %s", string(out))
Expand All @@ -185,13 +184,12 @@ func (c *CLICluster) Destroy(t *testing.T) {

// Set the args for running the uninstall command.
args := []string{"uninstall"}
args = c.setKube(args)
args = append(args, "-auto-approve", "-wipe-data")

// Use `go run` so that the CLI is recompiled and therefore uses the local
// charts directory rather than the directory from whenever it was last
// compiled.
out, err := c.cli.Run(args...)
out, err := c.cli.Run(t, c.kubectlOptions, args...)
if err != nil {
c.logger.Logf(t, "error running command `consul-k8s %s`: %s", strings.Join(args, " "), err.Error())
c.logger.Logf(t, "command stdout: %s", string(out))
Expand Down
4 changes: 2 additions & 2 deletions acceptance/tests/connect/connect_inject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestConnectInject(t *testing.T) {
}

// Run proxy list and get the two results.
listOut, err := cli.Run("proxy", "list")
listOut, err := cli.Run(t, ctx.KubectlOptions(t), "proxy", "list")
require.NoError(t, err)
logger.Log(t, string(listOut))
list := translateListOutput(listOut)
Expand All @@ -101,7 +101,7 @@ func TestConnectInject(t *testing.T) {
retrier := &retry.Timer{Timeout: 160 * time.Second, Wait: 2 * time.Second}
retry.RunWith(retrier, t, func(r *retry.R) {
for podName := range list {
out, err := cli.Run("proxy", "read", podName)
out, err := cli.Run(t, ctx.KubectlOptions(t), "proxy", "read", podName)
require.NoError(t, err)

output := string(out)
Expand Down
8 changes: 8 additions & 0 deletions cli/cmd/proxy/list/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ func (c *ListCommand) validateFlags() error {
func (c *ListCommand) initKubernetes() error {
settings := helmCLI.New()

if c.flagKubeConfig != "" {
settings.KubeConfig = c.flagKubeConfig
}

if c.flagKubeContext != "" {
settings.KubeContext = c.flagKubeContext
}

restConfig, err := settings.RESTClientGetter().ToRESTConfig()
if err != nil {
return fmt.Errorf("error retrieving Kubernetes authentication %v", err)
Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/proxy/read/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,11 @@ func (c *ReadCommand) validateFlags() error {
func (c *ReadCommand) initKubernetes() (err error) {
settings := helmCLI.New()

if c.flagKubeConfig == "" {
if c.flagKubeConfig != "" {
settings.KubeConfig = c.flagKubeConfig
}

if c.flagKubeContext == "" {
if c.flagKubeContext != "" {
settings.KubeContext = c.flagKubeContext
}

Expand Down

0 comments on commit f8331da

Please sign in to comment.