Expose all stack commands to be used by downstream projects.

Factorize orchestrator switch among stack commands.

Signed-off-by: Silvin Lubecki <silvin.lubecki@docker.com>
This commit is contained in:
Silvin Lubecki 2019-01-14 18:00:14 +01:00
parent 48bd4c6deb
commit cf0271ace4
7 changed files with 60 additions and 54 deletions

View File

@ -17,6 +17,13 @@ type commonOptions struct {
orchestrator command.Orchestrator orchestrator command.Orchestrator
} }
func (o *commonOptions) Orchestrator() command.Orchestrator {
if o == nil {
return command.OrchestratorSwarm
}
return o.orchestrator
}
// NewStackCommand returns a cobra command for `stack` subcommands // NewStackCommand returns a cobra command for `stack` subcommands
func NewStackCommand(dockerCli command.Cli) *cobra.Command { func NewStackCommand(dockerCli command.Cli) *cobra.Command {
var opts commonOptions var opts commonOptions

View File

@ -4,6 +4,10 @@ import (
"fmt" "fmt"
"strings" "strings"
"unicode" "unicode"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/stack/kubernetes"
"github.com/spf13/pflag"
) )
// validateStackName checks if the provided string is a valid stack name (namespace). // validateStackName checks if the provided string is a valid stack name (namespace).
@ -29,3 +33,18 @@ func validateStackNames(namespaces []string) error {
func quotesOrWhitespace(r rune) bool { func quotesOrWhitespace(r rune) bool {
return unicode.IsSpace(r) || r == '"' || r == '\'' return unicode.IsSpace(r) || r == '"' || r == '\''
} }
func runOrchestratedCommand(dockerCli command.Cli, flags *pflag.FlagSet, commonOrchestrator command.Orchestrator, swarmCmd func() error, kubernetesCmd func(*kubernetes.KubeCli) error) error {
switch {
case commonOrchestrator.HasAll():
return errUnsupportedAllOrchestrator
case commonOrchestrator.HasKubernetes():
kli, err := kubernetes.WrapCli(dockerCli, kubernetes.NewOptions(flags, commonOrchestrator))
if err != nil {
return err
}
return kubernetesCmd(kli)
default:
return swarmCmd()
}
}

View File

@ -50,7 +50,7 @@ func newDeployCommand(dockerCli command.Cli, common *commonOptions) *cobra.Comma
if err != nil { if err != nil {
return err return err
} }
return RunDeploy(dockerCli, cmd.Flags(), config, commonOrchestrator, opts) return RunDeploy(dockerCli, cmd.Flags(), config, common.Orchestrator(), opts)
}, },
} }
@ -75,16 +75,7 @@ func newDeployCommand(dockerCli command.Cli, common *commonOptions) *cobra.Comma
// RunDeploy performs a stack deploy against the specified orchestrator // RunDeploy performs a stack deploy against the specified orchestrator
func RunDeploy(dockerCli command.Cli, flags *pflag.FlagSet, config *composetypes.Config, commonOrchestrator command.Orchestrator, opts options.Deploy) error { func RunDeploy(dockerCli command.Cli, flags *pflag.FlagSet, config *composetypes.Config, commonOrchestrator command.Orchestrator, opts options.Deploy) error {
switch { return runOrchestratedCommand(dockerCli, flags, commonOrchestrator,
case commonOrchestrator.HasAll(): func() error { return swarm.RunDeploy(dockerCli, opts, config) },
return errUnsupportedAllOrchestrator func(kli *kubernetes.KubeCli) error { return kubernetes.RunDeploy(kli, opts, config) })
case commonOrchestrator.HasKubernetes():
kli, err := kubernetes.WrapCli(dockerCli, kubernetes.NewOptions(flags, commonOrchestrator))
if err != nil {
return errors.Wrap(err, "unable to deploy to Kubernetes")
}
return kubernetes.RunDeploy(kli, opts, config)
default:
return swarm.RunDeploy(dockerCli, opts, config)
}
} }

View File

@ -22,7 +22,7 @@ func newListCommand(dockerCli command.Cli, common *commonOptions) *cobra.Command
Short: "List stacks", Short: "List stacks",
Args: cli.NoArgs, Args: cli.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return runList(cmd, dockerCli, opts, common.orchestrator) return RunList(cmd, dockerCli, opts, common.orchestrator)
}, },
} }
@ -35,7 +35,8 @@ func newListCommand(dockerCli command.Cli, common *commonOptions) *cobra.Command
return cmd return cmd
} }
func runList(cmd *cobra.Command, dockerCli command.Cli, opts options.List, orchestrator command.Orchestrator) error { // RunList performs a stack list against the specified orchestrator
func RunList(cmd *cobra.Command, dockerCli command.Cli, opts options.List, orchestrator command.Orchestrator) error {
stacks := []*formatter.Stack{} stacks := []*formatter.Stack{}
if orchestrator.HasSwarm() { if orchestrator.HasSwarm() {
ss, err := swarm.GetStacks(dockerCli) ss, err := swarm.GetStacks(dockerCli)

View File

@ -8,6 +8,7 @@ import (
"github.com/docker/cli/cli/command/stack/swarm" "github.com/docker/cli/cli/command/stack/swarm"
cliopts "github.com/docker/cli/opts" cliopts "github.com/docker/cli/opts"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag"
) )
func newPsCommand(dockerCli command.Cli, common *commonOptions) *cobra.Command { func newPsCommand(dockerCli command.Cli, common *commonOptions) *cobra.Command {
@ -22,19 +23,7 @@ func newPsCommand(dockerCli command.Cli, common *commonOptions) *cobra.Command {
if err := validateStackName(opts.Namespace); err != nil { if err := validateStackName(opts.Namespace); err != nil {
return err return err
} }
return RunPs(dockerCli, cmd.Flags(), common.Orchestrator(), opts)
switch {
case common.orchestrator.HasAll():
return errUnsupportedAllOrchestrator
case common.orchestrator.HasKubernetes():
kli, err := kubernetes.WrapCli(dockerCli, kubernetes.NewOptions(cmd.Flags(), common.orchestrator))
if err != nil {
return err
}
return kubernetes.RunPS(kli, opts)
default:
return swarm.RunPS(dockerCli, opts)
}
}, },
} }
flags := cmd.Flags() flags := cmd.Flags()
@ -46,3 +35,10 @@ func newPsCommand(dockerCli command.Cli, common *commonOptions) *cobra.Command {
kubernetes.AddNamespaceFlag(flags) kubernetes.AddNamespaceFlag(flags)
return cmd return cmd
} }
// RunPs performs a stack ps against the specified orchestrator
func RunPs(dockerCli command.Cli, flags *pflag.FlagSet, commonOrchestrator command.Orchestrator, opts options.PS) error {
return runOrchestratedCommand(dockerCli, flags, commonOrchestrator,
func() error { return swarm.RunPS(dockerCli, opts) },
func(kli *kubernetes.KubeCli) error { return kubernetes.RunPS(kli, opts) })
}

View File

@ -7,6 +7,7 @@ import (
"github.com/docker/cli/cli/command/stack/options" "github.com/docker/cli/cli/command/stack/options"
"github.com/docker/cli/cli/command/stack/swarm" "github.com/docker/cli/cli/command/stack/swarm"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag"
) )
func newRemoveCommand(dockerCli command.Cli, common *commonOptions) *cobra.Command { func newRemoveCommand(dockerCli command.Cli, common *commonOptions) *cobra.Command {
@ -22,22 +23,17 @@ func newRemoveCommand(dockerCli command.Cli, common *commonOptions) *cobra.Comma
if err := validateStackNames(opts.Namespaces); err != nil { if err := validateStackNames(opts.Namespaces); err != nil {
return err return err
} }
return RunRemove(dockerCli, cmd.Flags(), common.Orchestrator(), opts)
switch {
case common.orchestrator.HasAll():
return errUnsupportedAllOrchestrator
case common.orchestrator.HasKubernetes():
kli, err := kubernetes.WrapCli(dockerCli, kubernetes.NewOptions(cmd.Flags(), common.orchestrator))
if err != nil {
return err
}
return kubernetes.RunRemove(kli, opts)
default:
return swarm.RunRemove(dockerCli, opts)
}
}, },
} }
flags := cmd.Flags() flags := cmd.Flags()
kubernetes.AddNamespaceFlag(flags) kubernetes.AddNamespaceFlag(flags)
return cmd return cmd
} }
// RunRemove performs a stack remove against the specified orchestrator
func RunRemove(dockerCli command.Cli, flags *pflag.FlagSet, commonOrchestrator command.Orchestrator, opts options.Remove) error {
return runOrchestratedCommand(dockerCli, flags, commonOrchestrator,
func() error { return swarm.RunRemove(dockerCli, opts) },
func(kli *kubernetes.KubeCli) error { return kubernetes.RunRemove(kli, opts) })
}

View File

@ -8,6 +8,7 @@ import (
"github.com/docker/cli/cli/command/stack/swarm" "github.com/docker/cli/cli/command/stack/swarm"
cliopts "github.com/docker/cli/opts" cliopts "github.com/docker/cli/opts"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag"
) )
func newServicesCommand(dockerCli command.Cli, common *commonOptions) *cobra.Command { func newServicesCommand(dockerCli command.Cli, common *commonOptions) *cobra.Command {
@ -22,19 +23,7 @@ func newServicesCommand(dockerCli command.Cli, common *commonOptions) *cobra.Com
if err := validateStackName(opts.Namespace); err != nil { if err := validateStackName(opts.Namespace); err != nil {
return err return err
} }
return RunServices(dockerCli, cmd.Flags(), common.Orchestrator(), opts)
switch {
case common.orchestrator.HasAll():
return errUnsupportedAllOrchestrator
case common.orchestrator.HasKubernetes():
kli, err := kubernetes.WrapCli(dockerCli, kubernetes.NewOptions(cmd.Flags(), common.orchestrator))
if err != nil {
return err
}
return kubernetes.RunServices(kli, opts)
default:
return swarm.RunServices(dockerCli, opts)
}
}, },
} }
flags := cmd.Flags() flags := cmd.Flags()
@ -44,3 +33,10 @@ func newServicesCommand(dockerCli command.Cli, common *commonOptions) *cobra.Com
kubernetes.AddNamespaceFlag(flags) kubernetes.AddNamespaceFlag(flags)
return cmd return cmd
} }
// RunServices performs a stack services against the specified orchestrator
func RunServices(dockerCli command.Cli, flags *pflag.FlagSet, commonOrchestrator command.Orchestrator, opts options.Services) error {
return runOrchestratedCommand(dockerCli, flags, commonOrchestrator,
func() error { return swarm.RunServices(dockerCli, opts) },
func(kli *kubernetes.KubeCli) error { return kubernetes.RunServices(kli, opts) })
}