-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix broken swarm commands with Kubernetes defined as orchestrator #1137
Changes from all commits
71272dd
a63252b
0f07b9f
f0a8598
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,25 @@ const ( | |
OrchestratorAll = Orchestrator("all") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like these constants can be un-exported? (and the GoDoc removed) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nevermind; I see they're used in a test; can be fixed, but no urgency |
||
orchestratorUnset = Orchestrator("unset") | ||
|
||
defaultOrchestrator = OrchestratorSwarm | ||
envVarDockerOrchestrator = "DOCKER_ORCHESTRATOR" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may have to think about printing a warning if someone has this variable set. I realize so far it's been experimental, so we're allowed to change things, but it's definitely possible people had this option set, and now it's being ignored. Of course, to further complicate stuff; some day it may find its way back, once k8s integration on the cli is complete 😅 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just added the warning! |
||
defaultOrchestrator = OrchestratorSwarm | ||
envVarDockerStackOrchestrator = "DOCKER_STACK_ORCHESTRATOR" | ||
) | ||
|
||
// HasKubernetes returns true if defined orchestrator has Kubernetes capabilities. | ||
func (o Orchestrator) HasKubernetes() bool { | ||
return o == OrchestratorKubernetes || o == OrchestratorAll | ||
} | ||
|
||
// HasSwarm returns true if defined orchestrator has Swarm capabilities. | ||
func (o Orchestrator) HasSwarm() bool { | ||
return o == OrchestratorSwarm || o == OrchestratorAll | ||
} | ||
|
||
// HasAll returns true if defined orchestrator has both Swarm and Kubernetes capabilities. | ||
func (o Orchestrator) HasAll() bool { | ||
return o == OrchestratorAll | ||
} | ||
|
||
func normalize(value string) (Orchestrator, error) { | ||
switch value { | ||
case "kubernetes": | ||
|
@@ -36,15 +51,15 @@ func normalize(value string) (Orchestrator, error) { | |
} | ||
} | ||
|
||
// GetOrchestrator checks DOCKER_ORCHESTRATOR environment variable and configuration file | ||
// GetStackOrchestrator checks DOCKER_STACK_ORCHESTRATOR environment variable and configuration file | ||
// orchestrator value and returns user defined Orchestrator. | ||
func GetOrchestrator(flagValue, value string) (Orchestrator, error) { | ||
func GetStackOrchestrator(flagValue, value string) (Orchestrator, error) { | ||
// Check flag | ||
if o, err := normalize(flagValue); o != orchestratorUnset { | ||
return o, err | ||
} | ||
// Check environment variable | ||
env := os.Getenv(envVarDockerOrchestrator) | ||
env := os.Getenv(envVarDockerStackOrchestrator) | ||
if o, err := normalize(env); o != orchestratorUnset { | ||
return o, err | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package command | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
cliconfig "github.com/docker/cli/cli/config" | ||
"github.com/docker/cli/cli/flags" | ||
"gotest.tools/assert" | ||
is "gotest.tools/assert/cmp" | ||
"gotest.tools/env" | ||
"gotest.tools/fs" | ||
) | ||
|
||
func TestOrchestratorSwitch(t *testing.T) { | ||
defaultVersion := "v0.00" | ||
|
||
var testcases = []struct { | ||
doc string | ||
configfile string | ||
envOrchestrator string | ||
flagOrchestrator string | ||
expectedOrchestrator string | ||
expectedKubernetes bool | ||
expectedSwarm bool | ||
}{ | ||
{ | ||
doc: "default", | ||
configfile: `{ | ||
}`, | ||
expectedOrchestrator: "swarm", | ||
expectedKubernetes: false, | ||
expectedSwarm: true, | ||
}, | ||
{ | ||
doc: "kubernetesConfigFile", | ||
configfile: `{ | ||
"stackOrchestrator": "kubernetes" | ||
}`, | ||
expectedOrchestrator: "kubernetes", | ||
expectedKubernetes: true, | ||
expectedSwarm: false, | ||
}, | ||
{ | ||
doc: "kubernetesEnv", | ||
configfile: `{ | ||
}`, | ||
envOrchestrator: "kubernetes", | ||
expectedOrchestrator: "kubernetes", | ||
expectedKubernetes: true, | ||
expectedSwarm: false, | ||
}, | ||
{ | ||
doc: "kubernetesFlag", | ||
configfile: `{ | ||
}`, | ||
flagOrchestrator: "kubernetes", | ||
expectedOrchestrator: "kubernetes", | ||
expectedKubernetes: true, | ||
expectedSwarm: false, | ||
}, | ||
{ | ||
doc: "allOrchestratorFlag", | ||
configfile: `{ | ||
}`, | ||
flagOrchestrator: "all", | ||
expectedOrchestrator: "all", | ||
expectedKubernetes: true, | ||
expectedSwarm: true, | ||
}, | ||
{ | ||
doc: "envOverridesConfigFile", | ||
configfile: `{ | ||
"stackOrchestrator": "kubernetes" | ||
}`, | ||
envOrchestrator: "swarm", | ||
expectedOrchestrator: "swarm", | ||
expectedKubernetes: false, | ||
expectedSwarm: true, | ||
}, | ||
{ | ||
doc: "flagOverridesEnv", | ||
configfile: `{ | ||
}`, | ||
envOrchestrator: "kubernetes", | ||
flagOrchestrator: "swarm", | ||
expectedOrchestrator: "swarm", | ||
expectedKubernetes: false, | ||
expectedSwarm: true, | ||
}, | ||
} | ||
|
||
for _, testcase := range testcases { | ||
t.Run(testcase.doc, func(t *testing.T) { | ||
dir := fs.NewDir(t, testcase.doc, fs.WithFile("config.json", testcase.configfile)) | ||
defer dir.Remove() | ||
apiclient := &fakeClient{ | ||
version: defaultVersion, | ||
} | ||
if testcase.envOrchestrator != "" { | ||
defer env.Patch(t, "DOCKER_STACK_ORCHESTRATOR", testcase.envOrchestrator)() | ||
} | ||
|
||
cli := &DockerCli{client: apiclient, err: os.Stderr} | ||
cliconfig.SetDir(dir.Path()) | ||
options := flags.NewClientOptions() | ||
err := cli.Initialize(options) | ||
assert.NilError(t, err) | ||
|
||
orchestrator, err := GetStackOrchestrator(testcase.flagOrchestrator, cli.ConfigFile().StackOrchestrator) | ||
assert.NilError(t, err) | ||
assert.Check(t, is.Equal(testcase.expectedKubernetes, orchestrator.HasKubernetes())) | ||
assert.Check(t, is.Equal(testcase.expectedSwarm, orchestrator.HasSwarm())) | ||
assert.Check(t, is.Equal(testcase.expectedOrchestrator, string(orchestrator))) | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Isn't this change unrelated ? 😝
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
docker stack
command itself now has options, but there was no[OPTIONS]
printed 😞