Skip to content

Commit 8fb2e7d

Browse files
Warn if DOCKER_ORCHESTRATOR is still used but not DOCKER_STACK_ORCHESTRATOR
Signed-off-by: Silvin Lubecki <[email protected]>
1 parent f0a8598 commit 8fb2e7d

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

cli/command/orchestrator.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package command
22

33
import (
44
"fmt"
5+
"io"
56
"os"
67
)
78

@@ -19,6 +20,7 @@ const (
1920

2021
defaultOrchestrator = OrchestratorSwarm
2122
envVarDockerStackOrchestrator = "DOCKER_STACK_ORCHESTRATOR"
23+
envVarDockerOrchestrator = "DOCKER_ORCHESTRATOR"
2224
)
2325

2426
// HasKubernetes returns true if defined orchestrator has Kubernetes capabilities.
@@ -53,13 +55,16 @@ func normalize(value string) (Orchestrator, error) {
5355

5456
// GetStackOrchestrator checks DOCKER_STACK_ORCHESTRATOR environment variable and configuration file
5557
// orchestrator value and returns user defined Orchestrator.
56-
func GetStackOrchestrator(flagValue, value string) (Orchestrator, error) {
58+
func GetStackOrchestrator(flagValue, value string, stderr io.Writer) (Orchestrator, error) {
5759
// Check flag
5860
if o, err := normalize(flagValue); o != orchestratorUnset {
5961
return o, err
6062
}
6163
// Check environment variable
6264
env := os.Getenv(envVarDockerStackOrchestrator)
65+
if env == "" && os.Getenv(envVarDockerOrchestrator) != "" {
66+
fmt.Fprintf(stderr, "WARNING: experimental environment variable %s is set. Please use %s instead\n", envVarDockerOrchestrator, envVarDockerStackOrchestrator)
67+
}
6368
if o, err := normalize(env); o != orchestratorUnset {
6469
return o, err
6570
}

cli/command/orchestrator_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package command
22

33
import (
4+
"io/ioutil"
45
"os"
56
"testing"
67

@@ -107,7 +108,7 @@ func TestOrchestratorSwitch(t *testing.T) {
107108
err := cli.Initialize(options)
108109
assert.NilError(t, err)
109110

110-
orchestrator, err := GetStackOrchestrator(testcase.flagOrchestrator, cli.ConfigFile().StackOrchestrator)
111+
orchestrator, err := GetStackOrchestrator(testcase.flagOrchestrator, cli.ConfigFile().StackOrchestrator, ioutil.Discard)
111112
assert.NilError(t, err)
112113
assert.Check(t, is.Equal(testcase.expectedKubernetes, orchestrator.HasKubernetes()))
113114
assert.Check(t, is.Equal(testcase.expectedSwarm, orchestrator.HasSwarm()))

cli/command/stack/cmd.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package stack
33
import (
44
"errors"
55
"fmt"
6+
"io"
67
"strings"
78

89
"github.com/docker/cli/cli"
910
"github.com/docker/cli/cli/command"
10-
cliconfig "github.com/docker/cli/cli/config"
1111
"github.com/docker/cli/cli/config/configfile"
1212
"github.com/spf13/cobra"
1313
"github.com/spf13/pflag"
@@ -27,12 +27,12 @@ func NewStackCommand(dockerCli command.Cli) *cobra.Command {
2727
Short: "Manage Docker stacks",
2828
Args: cli.NoArgs,
2929
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
30-
orchestrator, err := getOrchestrator(dockerCli.ConfigFile(), cmd)
30+
orchestrator, err := getOrchestrator(dockerCli.ConfigFile(), cmd, dockerCli.Err())
3131
if err != nil {
3232
return err
3333
}
3434
opts.orchestrator = orchestrator
35-
hideFlag(cmd, orchestrator)
35+
hideOrchestrationFlags(cmd, orchestrator)
3636
return checkSupportedFlag(cmd, orchestrator)
3737
},
3838

@@ -43,13 +43,7 @@ func NewStackCommand(dockerCli command.Cli) *cobra.Command {
4343
}
4444
defaultHelpFunc := cmd.HelpFunc()
4545
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
46-
config := cliconfig.LoadDefaultConfigFile(dockerCli.Err()) // dockerCli is not yet initialized, but we only need config file here
47-
o, err := getOrchestrator(config, cmd)
48-
if err != nil {
49-
fmt.Fprint(dockerCli.Err(), err)
50-
return
51-
}
52-
hideFlag(cmd, o)
46+
hideOrchestrationFlags(cmd, opts.orchestrator)
5347
defaultHelpFunc(cmd, args)
5448
})
5549
cmd.AddCommand(
@@ -78,15 +72,15 @@ func NewTopLevelDeployCommand(dockerCli command.Cli) *cobra.Command {
7872
return cmd
7973
}
8074

81-
func getOrchestrator(config *configfile.ConfigFile, cmd *cobra.Command) (command.Orchestrator, error) {
75+
func getOrchestrator(config *configfile.ConfigFile, cmd *cobra.Command, stderr io.Writer) (command.Orchestrator, error) {
8276
var orchestratorFlag string
8377
if o, err := cmd.Flags().GetString("orchestrator"); err == nil {
8478
orchestratorFlag = o
8579
}
86-
return command.GetStackOrchestrator(orchestratorFlag, config.StackOrchestrator)
80+
return command.GetStackOrchestrator(orchestratorFlag, config.StackOrchestrator, stderr)
8781
}
8882

89-
func hideFlag(cmd *cobra.Command, orchestrator command.Orchestrator) {
83+
func hideOrchestrationFlags(cmd *cobra.Command, orchestrator command.Orchestrator) {
9084
cmd.Flags().VisitAll(func(f *pflag.Flag) {
9185
if _, ok := f.Annotations["kubernetes"]; ok && !orchestrator.HasKubernetes() {
9286
f.Hidden = true
@@ -96,7 +90,7 @@ func hideFlag(cmd *cobra.Command, orchestrator command.Orchestrator) {
9690
}
9791
})
9892
for _, subcmd := range cmd.Commands() {
99-
hideFlag(subcmd, orchestrator)
93+
hideOrchestrationFlags(subcmd, orchestrator)
10094
}
10195
}
10296

cli/command/system/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func runVersion(dockerCli command.Cli, opts *versionOptions) error {
126126
return cli.StatusError{StatusCode: 64, Status: err.Error()}
127127
}
128128

129-
orchestrator, err := command.GetStackOrchestrator("", dockerCli.ConfigFile().StackOrchestrator)
129+
orchestrator, err := command.GetStackOrchestrator("", dockerCli.ConfigFile().StackOrchestrator, dockerCli.Err())
130130
if err != nil {
131131
return cli.StatusError{StatusCode: 64, Status: err.Error()}
132132
}

0 commit comments

Comments
 (0)