Skip to content
Closed
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
50 changes: 41 additions & 9 deletions cli/cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/hashicorp/consul-k8s/cli/helm"
"github.com/hashicorp/consul-k8s/cli/release"
"github.com/hashicorp/consul-k8s/cli/validation"
"github.com/posener/complete"
"helm.sh/helm/v3/pkg/action"
helmCLI "helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/cli/values"
Expand Down Expand Up @@ -52,6 +53,9 @@ const (

flagNameWait = "wait"
defaultWait = true

flagNameContext = "context"
flagNameKubeconfig = "kubeconfig"
)

type Command struct {
Expand Down Expand Up @@ -158,14 +162,14 @@ func (c *Command) init() {

f = c.set.NewSet("Global Options")
f.StringVar(&flag.StringVar{
Name: "kubeconfig",
Name: flagNameKubeconfig,
Aliases: []string{"c"},
Target: &c.flagKubeConfig,
Default: "",
Usage: "Set the path to kubeconfig file.",
})
f.StringVar(&flag.StringVar{
Name: "context",
Name: flagNameContext,
Target: &c.flagKubeContext,
Default: "",
Usage: "Set the Kubernetes context to use.",
Expand Down Expand Up @@ -265,29 +269,29 @@ func (c *Command) Run(args []string) int {
return 1
}

var values helm.Values
err = yaml.Unmarshal(valuesYaml, &values)
var helmVals helm.Values
err = yaml.Unmarshal(valuesYaml, &helmVals)
if err != nil {
c.UI.Output(err.Error(), terminal.WithErrorStyle())
return 1
}

release := release.Release{
rel := release.Release{
Name: "consul",
Namespace: c.flagNamespace,
Configuration: values,
Configuration: helmVals,
}

msg, err := c.checkForPreviousSecrets(release)
msg, err := c.checkForPreviousSecrets(rel)
if err != nil {
c.UI.Output(err.Error(), terminal.WithErrorStyle())
return 1
}
c.UI.Output(msg, terminal.WithSuccessStyle())

// If an enterprise license secret was provided, check that the secret exists and that the enterprise Consul image is set.
if values.Global.EnterpriseLicense.SecretName != "" {
if err := c.checkValidEnterprise(release.Configuration.Global.EnterpriseLicense.SecretName); err != nil {
if helmVals.Global.EnterpriseLicense.SecretName != "" {
if err := c.checkValidEnterprise(rel.Configuration.Global.EnterpriseLicense.SecretName); err != nil {
c.UI.Output(err.Error(), terminal.WithErrorStyle())
return 1
}
Expand Down Expand Up @@ -383,6 +387,34 @@ func (c *Command) Synopsis() string {
return "Install Consul on Kubernetes."
}

// AutocompleteFlags returns a mapping of supported flags and autocomplete
// options for this command. The map key for the Flags map should be the
// complete flag such as "-foo" or "--foo".
func (c *Command) AutocompleteFlags() complete.Flags {
return complete.Flags{
fmt.Sprintf("-%s", flagNamePreset): complete.PredictNothing,
fmt.Sprintf("-%s", flagNameNamespace): complete.PredictNothing,
fmt.Sprintf("-%s", flagNameDryRun): complete.PredictNothing,
fmt.Sprintf("-%s", flagNameAutoApprove): complete.PredictNothing,
fmt.Sprintf("-%s", flagNameConfigFile): complete.PredictFiles("*"),
fmt.Sprintf("-%s", flagNameSetStringValues): complete.PredictNothing,
fmt.Sprintf("-%s", flagNameSetValues): complete.PredictNothing,
fmt.Sprintf("-%s", flagNameFileValues): complete.PredictFiles("*"),
fmt.Sprintf("-%s", flagNameTimeout): complete.PredictNothing,
fmt.Sprintf("-%s", flagNameVerbose): complete.PredictNothing,
fmt.Sprintf("-%s", flagNameWait): complete.PredictNothing,
fmt.Sprintf("-%s", flagNameContext): complete.PredictNothing,
fmt.Sprintf("-%s", flagNameKubeconfig): complete.PredictNothing,
}
}

// AutocompleteArgs returns the argument predictor for this command.
// Since argument completion is not supported, this will return
// complete.PredictNothing.
func (c *Command) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}

// checkForPreviousPVCs checks for existing Kubernetes persistent volume claims with a name containing "consul-server"
// and returns an error with a list of PVCs it finds if any match.
func (c *Command) checkForPreviousPVCs() error {
Expand Down
35 changes: 35 additions & 0 deletions cli/cmd/install/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package install

import (
"context"
"flag"
"fmt"
"os"
"testing"

"github.com/hashicorp/consul-k8s/cli/common"
cmnFlag "github.com/hashicorp/consul-k8s/cli/common/flag"
"github.com/hashicorp/consul-k8s/cli/helm"
"github.com/hashicorp/consul-k8s/cli/release"
"github.com/hashicorp/go-hclog"
"github.com/posener/complete"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -248,3 +253,33 @@ func TestCheckValidEnterprise(t *testing.T) {
require.Error(t, err)
require.Contains(t, err.Error(), "please make sure that the secret exists")
}

func TestTaskCreateCommand_AutocompleteFlags(t *testing.T) {
t.Parallel()
cmd := getInitializedCommand(t)

predictor := cmd.AutocompleteFlags()

// Test that we get the expected number of predictions
args := complete.Args{Last: "-"}
res := predictor.Predict(args)

// Grab the list of flags from the Flag object
flags := make([]string, 0)
cmd.set.VisitSets(func(name string, set *cmnFlag.Set) {
set.VisitAll(func(flag *flag.Flag) {
flags = append(flags, fmt.Sprintf("-%s", flag.Name))
})
})

// Verify that there is a prediction for each flag associated with the command
assert.Equal(t, len(flags), len(res))
assert.ElementsMatch(t, flags, res, "flags and predictions didn't match, make sure to add "+
"new flags to the command AutoCompleteFlags function")
}

func TestTaskCreateCommand_AutocompleteArgs(t *testing.T) {
cmd := getInitializedCommand(t)
c := cmd.AutocompleteArgs()
assert.Equal(t, complete.PredictNothing, c)
}
2 changes: 1 addition & 1 deletion cli/cmd/proxy/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type ProxyCommand struct {
}

// Run prints out information about the subcommands.
func (c *ProxyCommand) Run(args []string) int {
func (c *ProxyCommand) Run([]string) int {
return cli.RunResultHelp
}

Expand Down
36 changes: 32 additions & 4 deletions cli/cmd/proxy/list/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ import (
"github.com/hashicorp/consul-k8s/cli/common"
"github.com/hashicorp/consul-k8s/cli/common/flag"
"github.com/hashicorp/consul-k8s/cli/common/terminal"
"github.com/posener/complete"
helmCLI "helm.sh/helm/v3/pkg/cli"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/validation"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

const (
flagNameNamespace = "namespace"
flagNameAllNamespaces = "all-namespaces"
flagNameKubeConfig = "kubeconfig"
flagNameKubeContext = "context"
)

// ListCommand is the command struct for the proxy list command.
type ListCommand struct {
*common.BaseCommand
Expand All @@ -40,13 +48,13 @@ func (c *ListCommand) init() {

f := c.set.NewSet("Command Options")
f.StringVar(&flag.StringVar{
Name: "namespace",
Name: flagNameNamespace,
Target: &c.flagNamespace,
Usage: "The namespace to list proxies in.",
Aliases: []string{"n"},
})
f.BoolVar(&flag.BoolVar{
Name: "all-namespaces",
Name: flagNameAllNamespaces,
Target: &c.flagAllNamespaces,
Default: false,
Usage: "List pods in all namespaces.",
Expand All @@ -55,14 +63,14 @@ func (c *ListCommand) init() {

f = c.set.NewSet("Global Options")
f.StringVar(&flag.StringVar{
Name: "kubeconfig",
Name: flagNameKubeConfig,
Aliases: []string{"c"},
Target: &c.flagKubeConfig,
Default: "",
Usage: "Set the path to kubeconfig file.",
})
f.StringVar(&flag.StringVar{
Name: "context",
Name: flagNameKubeContext,
Target: &c.flagKubeContext,
Default: "",
Usage: "Set the Kubernetes context to use.",
Expand Down Expand Up @@ -117,6 +125,25 @@ func (c *ListCommand) Synopsis() string {
return "List all Pods running proxies managed by Consul."
}

// AutocompleteFlags returns a mapping of supported flags and autocomplete
// options for this command. The map key for the Flags map should be the
// complete flag such as "-foo" or "--foo".
func (c *ListCommand) AutocompleteFlags() complete.Flags {
return complete.Flags{
fmt.Sprintf("-%s", flagNameNamespace): complete.PredictNothing,
fmt.Sprintf("-%s", flagNameAllNamespaces): complete.PredictNothing,
fmt.Sprintf("-%s", flagNameKubeConfig): complete.PredictFiles("*"),
fmt.Sprintf("-%s", flagNameKubeContext): complete.PredictNothing,
}
}

// AutocompleteArgs returns the argument predictor for this command.
// Since argument completion is not supported, this will return
// complete.PredictNothing.
func (c *ListCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}

// validateFlags ensures that the flags passed in by the can be used.
func (c *ListCommand) validateFlags() error {
if len(c.set.Args()) > 0 {
Expand All @@ -125,6 +152,7 @@ func (c *ListCommand) validateFlags() error {
if errs := validation.ValidateNamespaceName(c.flagNamespace, false); c.flagNamespace != "" && len(errs) > 0 {
return fmt.Errorf("invalid namespace name passed for -namespace/-n: %v", strings.Join(errs, "; "))
}

return nil
}

Expand Down
37 changes: 37 additions & 0 deletions cli/cmd/proxy/list/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ package list
import (
"bytes"
"context"
"flag"
"fmt"
"io"
"os"
"testing"

"github.com/hashicorp/consul-k8s/cli/common"
cmnFlag "github.com/hashicorp/consul-k8s/cli/common/flag"
"github.com/hashicorp/consul-k8s/cli/common/terminal"
"github.com/hashicorp/go-hclog"
"github.com/posener/complete"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -359,3 +364,35 @@ func setupCommand(buf io.Writer) *ListCommand {

return command
}

func TestTaskCreateCommand_AutocompleteFlags(t *testing.T) {
t.Parallel()
buf := new(bytes.Buffer)
cmd := setupCommand(buf)

predictor := cmd.AutocompleteFlags()

// Test that we get the expected number of predictions
args := complete.Args{Last: "-"}
res := predictor.Predict(args)

// Grab the list of flags from the Flag object
flags := make([]string, 0)
cmd.set.VisitSets(func(name string, set *cmnFlag.Set) {
set.VisitAll(func(flag *flag.Flag) {
flags = append(flags, fmt.Sprintf("-%s", flag.Name))
})
})

// Verify that there is a prediction for each flag associated with the command
assert.Equal(t, len(flags), len(res))
assert.ElementsMatch(t, flags, res, "flags and predictions didn't match, make sure to add "+
"new flags to the command AutoCompleteFlags function")
}

func TestTaskCreateCommand_AutocompleteArgs(t *testing.T) {
buf := new(bytes.Buffer)
cmd := setupCommand(buf)
c := cmd.AutocompleteArgs()
assert.Equal(t, complete.PredictNothing, c)
}
Loading