Set a global orchestrator flag

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
Vincent Demeester 2017-12-20 19:06:15 +01:00 committed by Silvin Lubecki
parent 12c0825a4c
commit 5d375b348a
10 changed files with 26 additions and 18 deletions

View File

@ -136,11 +136,12 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) error {
if err != nil {
return errors.Wrap(err, "Experimental field")
}
orchestrator := GetOrchestrator(cli.configFile.Orchestrator)
orchestrator := GetOrchestrator(opts.Common.Orchestrator, cli.configFile.Orchestrator)
cli.clientInfo = ClientInfo{
DefaultVersion: cli.client.ClientVersion(),
HasExperimental: hasExperimental,
HasKubernetes: orchestrator == OrchestratorKubernetes,
Orchestrator: orchestrator,
}
cli.initializeFromClient()
return nil
@ -207,6 +208,7 @@ type ClientInfo struct {
HasExperimental bool
HasKubernetes bool
DefaultVersion string
Orchestrator Orchestrator
}
// NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.

View File

@ -33,19 +33,23 @@ func normalize(flag string) Orchestrator {
// GetOrchestrator checks DOCKER_ORCHESTRATOR environment variable and configuration file
// orchestrator value and returns user defined Orchestrator.
func GetOrchestrator(orchestrator string) Orchestrator {
func GetOrchestrator(flagValue, value string) Orchestrator {
// Check flag
if o := normalize(flagValue); o != orchestratorUnset {
return o
}
// Check environment variable
env := os.Getenv(dockerOrchestrator)
if o := normalize(env); o != orchestratorUnset {
return o
}
// Check specified orchestrator
if o := normalize(orchestrator); o != orchestratorUnset {
if o := normalize(value); o != orchestratorUnset {
return o
}
if orchestrator != "" {
fmt.Fprintf(os.Stderr, "Specified orchestrator %q is invalid. Please use either kubernetes or swarm\n", orchestrator)
if value != "" {
fmt.Fprintf(os.Stderr, "Specified orchestrator %q is invalid. Please use either kubernetes or swarm\n", value)
}
// Nothing set, use default orchestrator
return defaultOrchestrator

View File

@ -19,7 +19,7 @@ func newDeployCommand(dockerCli command.Cli) *cobra.Command {
Args: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.Namespace = args[0]
if dockerCli.ClientInfo().HasKubernetes {
if dockerCli.ClientInfo().HasKubernetes() {
kli, err := kubernetes.WrapCli(dockerCli, cmd)
if err != nil {
return err

View File

@ -18,7 +18,7 @@ func newListCommand(dockerCli command.Cli) *cobra.Command {
Short: "List stacks",
Args: cli.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if dockerCli.ClientInfo().HasKubernetes {
if dockerCli.ClientInfo().HasKubernetes() {
kli, err := kubernetes.WrapCli(dockerCli, cmd)
if err != nil {
return err

View File

@ -19,7 +19,7 @@ func newPsCommand(dockerCli command.Cli) *cobra.Command {
Args: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.Namespace = args[0]
if dockerCli.ClientInfo().HasKubernetes {
if dockerCli.ClientInfo().HasKubernetes() {
kli, err := kubernetes.WrapCli(dockerCli, cmd)
if err != nil {
return err

View File

@ -19,7 +19,7 @@ func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.Namespaces = args
if dockerCli.ClientInfo().HasKubernetes {
if dockerCli.ClientInfo().HasKubernetes() {
kli, err := kubernetes.WrapCli(dockerCli, cmd)
if err != nil {
return err

View File

@ -19,7 +19,7 @@ func newServicesCommand(dockerCli command.Cli) *cobra.Command {
Args: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.Namespace = args[0]
if dockerCli.ClientInfo().HasKubernetes {
if dockerCli.ClientInfo().HasKubernetes() {
kli, err := kubernetes.WrapCli(dockerCli, cmd)
if err != nil {
return err

View File

@ -137,7 +137,7 @@ func runVersion(dockerCli command.Cli, opts *versionOptions) error {
Os: runtime.GOOS,
Arch: runtime.GOARCH,
Experimental: dockerCli.ClientInfo().HasExperimental,
Orchestrator: string(command.GetOrchestrator(dockerCli.ConfigFile().Orchestrator)),
Orchestrator: string(dockerCli.ClientInfo().Orchestrator),
},
}

View File

@ -32,6 +32,7 @@ var (
type CommonOptions struct {
Debug bool
Hosts []string
Orchestrator string
LogLevel string
TLS bool
TLSVerify bool
@ -53,6 +54,7 @@ func (commonOpts *CommonOptions) InstallFlags(flags *pflag.FlagSet) {
flags.StringVarP(&commonOpts.LogLevel, "log-level", "l", "info", `Set the logging level ("debug"|"info"|"warn"|"error"|"fatal")`)
flags.BoolVar(&commonOpts.TLS, "tls", false, "Use TLS; implied by --tlsverify")
flags.BoolVar(&commonOpts.TLSVerify, FlagTLSVerify, dockerTLSVerify, "Use TLS and verify the remote")
flags.StringVar(&commonOpts.Orchestrator, "orchestrator", "", "Which orchestrator to use with the docker cli (swarm|kubernetes)")
// TODO use flag flags.String("identity"}, "i", "", "Path to libtrust key file")

View File

@ -279,7 +279,7 @@ func areFlagsSupported(cmd *cobra.Command, details versionDetails) error {
clientVersion := details.Client().ClientVersion()
osType := details.ServerInfo().OSType
hasExperimental := details.ServerInfo().HasExperimental
hasKubernetes := details.ClientInfo().HasKubernetes
hasKubernetes := details.ClientInfo().HasKubernetes()
errs := []string{}