Skip to content
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
7 changes: 6 additions & 1 deletion cli/command/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"fmt"
"io"
"os"
)

Expand All @@ -19,6 +20,7 @@ const (

defaultOrchestrator = OrchestratorSwarm
envVarDockerStackOrchestrator = "DOCKER_STACK_ORCHESTRATOR"
envVarDockerOrchestrator = "DOCKER_ORCHESTRATOR"
)

// HasKubernetes returns true if defined orchestrator has Kubernetes capabilities.
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion cli/command/orchestrator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command

import (
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -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()))
Expand Down
22 changes: 8 additions & 14 deletions cli/command/stack/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package stack
import (
"errors"
"fmt"
"io"
"strings"

"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"
Expand All @@ -27,12 +27,12 @@ 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
}
opts.orchestrator = orchestrator
hideFlag(cmd, orchestrator)
hideOrchestrationFlags(cmd, orchestrator)
return checkSupportedFlag(cmd, orchestrator)
},

Expand All @@ -43,13 +43,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(
Expand Down Expand Up @@ -78,15 +72,15 @@ 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 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
Expand All @@ -96,7 +90,7 @@ func hideFlag(cmd *cobra.Command, orchestrator command.Orchestrator) {
}
})
for _, subcmd := range cmd.Commands() {
hideFlag(subcmd, orchestrator)
hideOrchestrationFlags(subcmd, orchestrator)
}
}

Expand Down
2 changes: 1 addition & 1 deletion cli/command/system/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
}
Expand Down