Scope orchestration selection to stack commands only
* 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>
This commit is contained in:
committed by
Sebastiaan van Stijn
parent
8de0753869
commit
71272dd203
@ -1,50 +1,125 @@
|
||||
package stack
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"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"
|
||||
)
|
||||
|
||||
var errUnsupportedAllOrchestrator = fmt.Errorf(`no orchestrator specified: use either "kubernetes" or "swarm"`)
|
||||
|
||||
type commonOptions struct {
|
||||
orchestrator command.Orchestrator
|
||||
}
|
||||
|
||||
// NewStackCommand returns a cobra command for `stack` subcommands
|
||||
func NewStackCommand(dockerCli command.Cli) *cobra.Command {
|
||||
var opts commonOptions
|
||||
cmd := &cobra.Command{
|
||||
Use: "stack",
|
||||
Short: "Manage Docker stacks",
|
||||
Args: cli.NoArgs,
|
||||
RunE: command.ShowHelp(dockerCli.Err()),
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
orchestrator, err := getOrchestrator(dockerCli.ConfigFile(), cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts.orchestrator = orchestrator
|
||||
hideFlag(cmd, orchestrator)
|
||||
return checkSupportedFlag(cmd, orchestrator)
|
||||
},
|
||||
|
||||
RunE: command.ShowHelp(dockerCli.Err()),
|
||||
Annotations: map[string]string{
|
||||
"kubernetes": "",
|
||||
"swarm": "",
|
||||
"version": "1.25",
|
||||
"version": "1.25",
|
||||
},
|
||||
}
|
||||
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)
|
||||
defaultHelpFunc(cmd, args)
|
||||
})
|
||||
cmd.AddCommand(
|
||||
newDeployCommand(dockerCli),
|
||||
newListCommand(dockerCli),
|
||||
newPsCommand(dockerCli),
|
||||
newRemoveCommand(dockerCli),
|
||||
newServicesCommand(dockerCli),
|
||||
newDeployCommand(dockerCli, &opts),
|
||||
newListCommand(dockerCli, &opts),
|
||||
newPsCommand(dockerCli, &opts),
|
||||
newRemoveCommand(dockerCli, &opts),
|
||||
newServicesCommand(dockerCli, &opts),
|
||||
)
|
||||
flags := cmd.PersistentFlags()
|
||||
flags.String("kubeconfig", "", "Kubernetes config file")
|
||||
flags.SetAnnotation("kubeconfig", "kubernetes", nil)
|
||||
flags.String("orchestrator", "", "Orchestrator to use (swarm|kubernetes|all)")
|
||||
return cmd
|
||||
}
|
||||
|
||||
// NewTopLevelDeployCommand returns a command for `docker deploy`
|
||||
func NewTopLevelDeployCommand(dockerCli command.Cli) *cobra.Command {
|
||||
cmd := newDeployCommand(dockerCli)
|
||||
cmd := newDeployCommand(dockerCli, nil)
|
||||
// Remove the aliases at the top level
|
||||
cmd.Aliases = []string{}
|
||||
cmd.Annotations = map[string]string{
|
||||
"experimental": "",
|
||||
"swarm": "",
|
||||
"version": "1.25",
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
func getOrchestrator(config *configfile.ConfigFile, cmd *cobra.Command) (command.Orchestrator, error) {
|
||||
var orchestratorFlag string
|
||||
if o, err := cmd.Flags().GetString("orchestrator"); err == nil {
|
||||
orchestratorFlag = o
|
||||
}
|
||||
return command.GetStackOrchestrator(orchestratorFlag, config.StackOrchestrator)
|
||||
}
|
||||
|
||||
func hideFlag(cmd *cobra.Command, orchestrator command.Orchestrator) {
|
||||
cmd.Flags().VisitAll(func(f *pflag.Flag) {
|
||||
if _, ok := f.Annotations["kubernetes"]; ok && !orchestrator.HasKubernetes() {
|
||||
f.Hidden = true
|
||||
}
|
||||
if _, ok := f.Annotations["swarm"]; ok && !orchestrator.HasSwarm() {
|
||||
f.Hidden = true
|
||||
}
|
||||
})
|
||||
for _, subcmd := range cmd.Commands() {
|
||||
hideFlag(subcmd, orchestrator)
|
||||
}
|
||||
}
|
||||
|
||||
func checkSupportedFlag(cmd *cobra.Command, orchestrator command.Orchestrator) error {
|
||||
errs := []string{}
|
||||
cmd.Flags().VisitAll(func(f *pflag.Flag) {
|
||||
if !f.Changed {
|
||||
return
|
||||
}
|
||||
if _, ok := f.Annotations["kubernetes"]; ok && !orchestrator.HasKubernetes() {
|
||||
errs = append(errs, fmt.Sprintf(`"--%s" is only supported on a Docker cli with kubernetes features enabled`, f.Name))
|
||||
}
|
||||
if _, ok := f.Annotations["swarm"]; ok && !orchestrator.HasSwarm() {
|
||||
errs = append(errs, fmt.Sprintf(`"--%s" is only supported on a Docker cli with swarm features enabled`, f.Name))
|
||||
}
|
||||
})
|
||||
for _, subcmd := range cmd.Commands() {
|
||||
if err := checkSupportedFlag(subcmd, orchestrator); err != nil {
|
||||
errs = append(errs, err.Error())
|
||||
}
|
||||
}
|
||||
if len(errs) > 0 {
|
||||
return errors.New(strings.Join(errs, "\n"))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user