From e02c28f40a49dd81e6551991c77608921dd8a288 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 22 Jun 2018 11:54:07 -0700 Subject: [PATCH 1/2] Remove duplicated getOrchestrator(), and rename hideFlag() Signed-off-by: Sebastiaan van Stijn --- cli/command/stack/cmd.go | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/cli/command/stack/cmd.go b/cli/command/stack/cmd.go index c9feb99b61..f19b5bfded 100644 --- a/cli/command/stack/cmd.go +++ b/cli/command/stack/cmd.go @@ -7,7 +7,6 @@ import ( "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" - cliconfig "github.com/docker/cli/cli/config" "github.com/docker/cli/cli/config/configfile" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -32,7 +31,7 @@ func NewStackCommand(dockerCli command.Cli) *cobra.Command { return err } opts.orchestrator = orchestrator - hideFlag(cmd, orchestrator) + hideOrchestrationFlags(cmd, orchestrator) return checkSupportedFlag(cmd, orchestrator) }, @@ -43,13 +42,7 @@ func NewStackCommand(dockerCli command.Cli) *cobra.Command { } defaultHelpFunc := cmd.HelpFunc() cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) { - config := cliconfig.LoadDefaultConfigFile(dockerCli.Err()) // dockerCli is not yet initialized, but we only need config file here - o, err := getOrchestrator(config, cmd) - if err != nil { - fmt.Fprint(dockerCli.Err(), err) - return - } - hideFlag(cmd, o) + hideOrchestrationFlags(cmd, opts.orchestrator) defaultHelpFunc(cmd, args) }) cmd.AddCommand( @@ -86,7 +79,7 @@ func getOrchestrator(config *configfile.ConfigFile, cmd *cobra.Command) (command return command.GetStackOrchestrator(orchestratorFlag, config.StackOrchestrator) } -func hideFlag(cmd *cobra.Command, orchestrator command.Orchestrator) { +func hideOrchestrationFlags(cmd *cobra.Command, orchestrator command.Orchestrator) { cmd.Flags().VisitAll(func(f *pflag.Flag) { if _, ok := f.Annotations["kubernetes"]; ok && !orchestrator.HasKubernetes() { f.Hidden = true @@ -96,7 +89,7 @@ func hideFlag(cmd *cobra.Command, orchestrator command.Orchestrator) { } }) for _, subcmd := range cmd.Commands() { - hideFlag(subcmd, orchestrator) + hideOrchestrationFlags(subcmd, orchestrator) } } From e8c87f7cb36a339163d1e5af1336e4719d7c10df Mon Sep 17 00:00:00 2001 From: Silvin Lubecki Date: Fri, 22 Jun 2018 16:26:18 +0200 Subject: [PATCH 2/2] Warn if DOCKER_ORCHESTRATOR is still used but not DOCKER_STACK_ORCHESTRATOR Signed-off-by: Silvin Lubecki Signed-off-by: Sebastiaan van Stijn --- cli/command/orchestrator.go | 7 ++++++- cli/command/orchestrator_test.go | 3 ++- cli/command/stack/cmd.go | 7 ++++--- cli/command/system/version.go | 2 +- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/cli/command/orchestrator.go b/cli/command/orchestrator.go index 5ffd0c0a84..5f3e446205 100644 --- a/cli/command/orchestrator.go +++ b/cli/command/orchestrator.go @@ -2,6 +2,7 @@ package command import ( "fmt" + "io" "os" ) @@ -19,6 +20,7 @@ const ( defaultOrchestrator = OrchestratorSwarm envVarDockerStackOrchestrator = "DOCKER_STACK_ORCHESTRATOR" + envVarDockerOrchestrator = "DOCKER_ORCHESTRATOR" ) // HasKubernetes returns true if defined orchestrator has Kubernetes capabilities. @@ -53,13 +55,16 @@ func normalize(value string) (Orchestrator, error) { // GetStackOrchestrator checks DOCKER_STACK_ORCHESTRATOR environment variable and configuration file // orchestrator value and returns user defined Orchestrator. -func GetStackOrchestrator(flagValue, value string) (Orchestrator, error) { +func GetStackOrchestrator(flagValue, value string, stderr io.Writer) (Orchestrator, error) { // Check flag if o, err := normalize(flagValue); o != orchestratorUnset { return o, err } // Check environment variable env := os.Getenv(envVarDockerStackOrchestrator) + if env == "" && os.Getenv(envVarDockerOrchestrator) != "" { + fmt.Fprintf(stderr, "WARNING: experimental environment variable %s is set. Please use %s instead\n", envVarDockerOrchestrator, envVarDockerStackOrchestrator) + } if o, err := normalize(env); o != orchestratorUnset { return o, err } diff --git a/cli/command/orchestrator_test.go b/cli/command/orchestrator_test.go index 01f50acd92..322e8a9169 100644 --- a/cli/command/orchestrator_test.go +++ b/cli/command/orchestrator_test.go @@ -1,6 +1,7 @@ package command import ( + "io/ioutil" "os" "testing" @@ -107,7 +108,7 @@ func TestOrchestratorSwitch(t *testing.T) { err := cli.Initialize(options) assert.NilError(t, err) - orchestrator, err := GetStackOrchestrator(testcase.flagOrchestrator, cli.ConfigFile().StackOrchestrator) + orchestrator, err := GetStackOrchestrator(testcase.flagOrchestrator, cli.ConfigFile().StackOrchestrator, ioutil.Discard) assert.NilError(t, err) assert.Check(t, is.Equal(testcase.expectedKubernetes, orchestrator.HasKubernetes())) assert.Check(t, is.Equal(testcase.expectedSwarm, orchestrator.HasSwarm())) diff --git a/cli/command/stack/cmd.go b/cli/command/stack/cmd.go index f19b5bfded..0416885ee1 100644 --- a/cli/command/stack/cmd.go +++ b/cli/command/stack/cmd.go @@ -3,6 +3,7 @@ package stack import ( "errors" "fmt" + "io" "strings" "github.com/docker/cli/cli" @@ -26,7 +27,7 @@ func NewStackCommand(dockerCli command.Cli) *cobra.Command { Short: "Manage Docker stacks", Args: cli.NoArgs, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - orchestrator, err := getOrchestrator(dockerCli.ConfigFile(), cmd) + orchestrator, err := getOrchestrator(dockerCli.ConfigFile(), cmd, dockerCli.Err()) if err != nil { return err } @@ -71,12 +72,12 @@ func NewTopLevelDeployCommand(dockerCli command.Cli) *cobra.Command { return cmd } -func getOrchestrator(config *configfile.ConfigFile, cmd *cobra.Command) (command.Orchestrator, error) { +func getOrchestrator(config *configfile.ConfigFile, cmd *cobra.Command, stderr io.Writer) (command.Orchestrator, error) { var orchestratorFlag string if o, err := cmd.Flags().GetString("orchestrator"); err == nil { orchestratorFlag = o } - return command.GetStackOrchestrator(orchestratorFlag, config.StackOrchestrator) + return command.GetStackOrchestrator(orchestratorFlag, config.StackOrchestrator, stderr) } func hideOrchestrationFlags(cmd *cobra.Command, orchestrator command.Orchestrator) { diff --git a/cli/command/system/version.go b/cli/command/system/version.go index c8a1471720..2a8fa5e599 100644 --- a/cli/command/system/version.go +++ b/cli/command/system/version.go @@ -126,7 +126,7 @@ func runVersion(dockerCli command.Cli, opts *versionOptions) error { return cli.StatusError{StatusCode: 64, Status: err.Error()} } - orchestrator, err := command.GetStackOrchestrator("", dockerCli.ConfigFile().StackOrchestrator) + orchestrator, err := command.GetStackOrchestrator("", dockerCli.ConfigFile().StackOrchestrator, dockerCli.Err()) if err != nil { return cli.StatusError{StatusCode: 64, Status: err.Error()} }