Skip to content

Commit 0e358b6

Browse files
* Renaming DOCKER_ORCHESTRATOR to DOCKER_STACK_ORCHESTRATOR
* Renaming config file option "orchestrator" to "stackOrchestrator" * "--orchestrator" flag is no more global but local to stack command and subcommands * Cleaning all global orchestrator code * Replicating Hidden flags in help and Supported flags from root command to stack command Signed-off-by: Silvin Lubecki <silvin.lubecki@docker.com>
1 parent 8de0753 commit 0e358b6

File tree

22 files changed

+317
-265
lines changed

22 files changed

+317
-265
lines changed

cli/command/cli.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,9 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) error {
166166
if err != nil {
167167
return errors.Wrap(err, "Experimental field")
168168
}
169-
orchestrator, err := GetOrchestrator(opts.Common.Orchestrator, cli.configFile.Orchestrator)
170-
if err != nil {
171-
return err
172-
}
173169
cli.clientInfo = ClientInfo{
174170
DefaultVersion: cli.client.ClientVersion(),
175171
HasExperimental: hasExperimental,
176-
Orchestrator: orchestrator,
177172
}
178173
cli.initializeFromClient()
179174
return nil
@@ -239,22 +234,6 @@ type ServerInfo struct {
239234
type ClientInfo struct {
240235
HasExperimental bool
241236
DefaultVersion string
242-
Orchestrator Orchestrator
243-
}
244-
245-
// HasKubernetes checks if kubernetes orchestrator is enabled
246-
func (c ClientInfo) HasKubernetes() bool {
247-
return c.Orchestrator == OrchestratorKubernetes || c.Orchestrator == OrchestratorAll
248-
}
249-
250-
// HasSwarm checks if swarm orchestrator is enabled
251-
func (c ClientInfo) HasSwarm() bool {
252-
return c.Orchestrator == OrchestratorSwarm || c.Orchestrator == OrchestratorAll
253-
}
254-
255-
// HasAll checks if all orchestrator is enabled
256-
func (c ClientInfo) HasAll() bool {
257-
return c.Orchestrator == OrchestratorAll
258237
}
259238

260239
// NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.

cli/command/cli_test.go

Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -161,110 +161,6 @@ func TestExperimentalCLI(t *testing.T) {
161161
}
162162
}
163163

164-
func TestOrchestratorSwitch(t *testing.T) {
165-
defaultVersion := "v0.00"
166-
167-
var testcases = []struct {
168-
doc string
169-
configfile string
170-
envOrchestrator string
171-
flagOrchestrator string
172-
expectedOrchestrator string
173-
expectedKubernetes bool
174-
expectedSwarm bool
175-
}{
176-
{
177-
doc: "default",
178-
configfile: `{
179-
}`,
180-
expectedOrchestrator: "swarm",
181-
expectedKubernetes: false,
182-
expectedSwarm: true,
183-
},
184-
{
185-
doc: "kubernetesConfigFile",
186-
configfile: `{
187-
"orchestrator": "kubernetes"
188-
}`,
189-
expectedOrchestrator: "kubernetes",
190-
expectedKubernetes: true,
191-
expectedSwarm: false,
192-
},
193-
{
194-
doc: "kubernetesEnv",
195-
configfile: `{
196-
}`,
197-
envOrchestrator: "kubernetes",
198-
expectedOrchestrator: "kubernetes",
199-
expectedKubernetes: true,
200-
expectedSwarm: false,
201-
},
202-
{
203-
doc: "kubernetesFlag",
204-
configfile: `{
205-
}`,
206-
flagOrchestrator: "kubernetes",
207-
expectedOrchestrator: "kubernetes",
208-
expectedKubernetes: true,
209-
expectedSwarm: false,
210-
},
211-
{
212-
doc: "allOrchestratorFlag",
213-
configfile: `{
214-
}`,
215-
flagOrchestrator: "all",
216-
expectedOrchestrator: "all",
217-
expectedKubernetes: true,
218-
expectedSwarm: true,
219-
},
220-
{
221-
doc: "envOverridesConfigFile",
222-
configfile: `{
223-
"orchestrator": "kubernetes"
224-
}`,
225-
envOrchestrator: "swarm",
226-
expectedOrchestrator: "swarm",
227-
expectedKubernetes: false,
228-
expectedSwarm: true,
229-
},
230-
{
231-
doc: "flagOverridesEnv",
232-
configfile: `{
233-
}`,
234-
envOrchestrator: "kubernetes",
235-
flagOrchestrator: "swarm",
236-
expectedOrchestrator: "swarm",
237-
expectedKubernetes: false,
238-
expectedSwarm: true,
239-
},
240-
}
241-
242-
for _, testcase := range testcases {
243-
t.Run(testcase.doc, func(t *testing.T) {
244-
dir := fs.NewDir(t, testcase.doc, fs.WithFile("config.json", testcase.configfile))
245-
defer dir.Remove()
246-
apiclient := &fakeClient{
247-
version: defaultVersion,
248-
}
249-
if testcase.envOrchestrator != "" {
250-
defer env.Patch(t, "DOCKER_ORCHESTRATOR", testcase.envOrchestrator)()
251-
}
252-
253-
cli := &DockerCli{client: apiclient, err: os.Stderr}
254-
cliconfig.SetDir(dir.Path())
255-
options := flags.NewClientOptions()
256-
if testcase.flagOrchestrator != "" {
257-
options.Common.Orchestrator = testcase.flagOrchestrator
258-
}
259-
err := cli.Initialize(options)
260-
assert.NilError(t, err)
261-
assert.Check(t, is.Equal(testcase.expectedKubernetes, cli.ClientInfo().HasKubernetes()))
262-
assert.Check(t, is.Equal(testcase.expectedSwarm, cli.ClientInfo().HasSwarm()))
263-
assert.Check(t, is.Equal(testcase.expectedOrchestrator, string(cli.ClientInfo().Orchestrator)))
264-
})
265-
}
266-
}
267-
268164
func TestGetClientWithPassword(t *testing.T) {
269165
expected := "password"
270166

cli/command/orchestrator.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,25 @@ const (
1717
OrchestratorAll = Orchestrator("all")
1818
orchestratorUnset = Orchestrator("unset")
1919

20-
defaultOrchestrator = OrchestratorSwarm
21-
envVarDockerOrchestrator = "DOCKER_ORCHESTRATOR"
20+
defaultOrchestrator = OrchestratorSwarm
21+
envVarDockerStackOrchestrator = "DOCKER_STACK_ORCHESTRATOR"
2222
)
2323

24+
// HasKubernetes returns true if defined orchestrator has Kubernetes capabilities.
25+
func (o Orchestrator) HasKubernetes() bool {
26+
return o == OrchestratorKubernetes || o == OrchestratorAll
27+
}
28+
29+
// HasSwarm returns true if defined orchestrator has Swarm capabilities.
30+
func (o Orchestrator) HasSwarm() bool {
31+
return o == OrchestratorSwarm || o == OrchestratorAll
32+
}
33+
34+
// HasAll returns true if defined orchestrator has both Swarm and Kubernetes capabilities.
35+
func (o Orchestrator) HasAll() bool {
36+
return o == OrchestratorAll
37+
}
38+
2439
func normalize(value string) (Orchestrator, error) {
2540
switch value {
2641
case "kubernetes":
@@ -36,15 +51,15 @@ func normalize(value string) (Orchestrator, error) {
3651
}
3752
}
3853

39-
// GetOrchestrator checks DOCKER_ORCHESTRATOR environment variable and configuration file
54+
// GetStackOrchestrator checks DOCKER_STACK_ORCHESTRATOR environment variable and configuration file
4055
// orchestrator value and returns user defined Orchestrator.
41-
func GetOrchestrator(flagValue, value string) (Orchestrator, error) {
56+
func GetStackOrchestrator(flagValue, value string) (Orchestrator, error) {
4257
// Check flag
4358
if o, err := normalize(flagValue); o != orchestratorUnset {
4459
return o, err
4560
}
4661
// Check environment variable
47-
env := os.Getenv(envVarDockerOrchestrator)
62+
env := os.Getenv(envVarDockerStackOrchestrator)
4863
if o, err := normalize(env); o != orchestratorUnset {
4964
return o, err
5065
}

cli/command/orchestrator_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package command
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
cliconfig "github.com/docker/cli/cli/config"
8+
"github.com/docker/cli/cli/flags"
9+
"gotest.tools/assert"
10+
is "gotest.tools/assert/cmp"
11+
"gotest.tools/env"
12+
"gotest.tools/fs"
13+
)
14+
15+
func TestOrchestratorSwitch(t *testing.T) {
16+
defaultVersion := "v0.00"
17+
18+
var testcases = []struct {
19+
doc string
20+
configfile string
21+
envOrchestrator string
22+
flagOrchestrator string
23+
expectedOrchestrator string
24+
expectedKubernetes bool
25+
expectedSwarm bool
26+
}{
27+
{
28+
doc: "default",
29+
configfile: `{
30+
}`,
31+
expectedOrchestrator: "swarm",
32+
expectedKubernetes: false,
33+
expectedSwarm: true,
34+
},
35+
{
36+
doc: "kubernetesConfigFile",
37+
configfile: `{
38+
"stackOrchestrator": "kubernetes"
39+
}`,
40+
expectedOrchestrator: "kubernetes",
41+
expectedKubernetes: true,
42+
expectedSwarm: false,
43+
},
44+
{
45+
doc: "kubernetesEnv",
46+
configfile: `{
47+
}`,
48+
envOrchestrator: "kubernetes",
49+
expectedOrchestrator: "kubernetes",
50+
expectedKubernetes: true,
51+
expectedSwarm: false,
52+
},
53+
{
54+
doc: "kubernetesFlag",
55+
configfile: `{
56+
}`,
57+
flagOrchestrator: "kubernetes",
58+
expectedOrchestrator: "kubernetes",
59+
expectedKubernetes: true,
60+
expectedSwarm: false,
61+
},
62+
{
63+
doc: "allOrchestratorFlag",
64+
configfile: `{
65+
}`,
66+
flagOrchestrator: "all",
67+
expectedOrchestrator: "all",
68+
expectedKubernetes: true,
69+
expectedSwarm: true,
70+
},
71+
{
72+
doc: "envOverridesConfigFile",
73+
configfile: `{
74+
"stackOrchestrator": "kubernetes"
75+
}`,
76+
envOrchestrator: "swarm",
77+
expectedOrchestrator: "swarm",
78+
expectedKubernetes: false,
79+
expectedSwarm: true,
80+
},
81+
{
82+
doc: "flagOverridesEnv",
83+
configfile: `{
84+
}`,
85+
envOrchestrator: "kubernetes",
86+
flagOrchestrator: "swarm",
87+
expectedOrchestrator: "swarm",
88+
expectedKubernetes: false,
89+
expectedSwarm: true,
90+
},
91+
}
92+
93+
for _, testcase := range testcases {
94+
t.Run(testcase.doc, func(t *testing.T) {
95+
dir := fs.NewDir(t, testcase.doc, fs.WithFile("config.json", testcase.configfile))
96+
defer dir.Remove()
97+
apiclient := &fakeClient{
98+
version: defaultVersion,
99+
}
100+
if testcase.envOrchestrator != "" {
101+
defer env.Patch(t, "DOCKER_STACK_ORCHESTRATOR", testcase.envOrchestrator)()
102+
}
103+
104+
cli := &DockerCli{client: apiclient, err: os.Stderr}
105+
cliconfig.SetDir(dir.Path())
106+
options := flags.NewClientOptions()
107+
if testcase.flagOrchestrator != "" {
108+
options.Common.StackOrchestrator = testcase.flagOrchestrator
109+
}
110+
err := cli.Initialize(options)
111+
assert.NilError(t, err)
112+
113+
orchestrator, err := GetStackOrchestrator(testcase.flagOrchestrator, cli.ConfigFile().StackOrchestrator)
114+
assert.NilError(t, err)
115+
assert.Check(t, is.Equal(testcase.expectedKubernetes, orchestrator.HasKubernetes()))
116+
assert.Check(t, is.Equal(testcase.expectedSwarm, orchestrator.HasSwarm()))
117+
assert.Check(t, is.Equal(testcase.expectedOrchestrator, string(orchestrator)))
118+
})
119+
}
120+
}

0 commit comments

Comments
 (0)