Support multiple namespaces for docker stack ls

Signed-off-by: Mathieu Champlon <mathieu.champlon@docker.com>
This commit is contained in:
Mathieu Champlon
2018-04-26 11:13:14 +02:00
parent 4d947de292
commit 84241cc393
9 changed files with 64 additions and 15 deletions

View File

@ -34,9 +34,15 @@ func NewOptions(flags *flag.FlagSet) Options {
return opts
}
// AddNamespaceFlag adds the namespace flag to the given flag set
func AddNamespaceFlag(flags *flag.FlagSet) {
flags.String("namespace", "", "Kubernetes namespace to use")
flags.SetAnnotation("namespace", "kubernetes", nil)
flags.SetAnnotation("namespace", "experimentalCLI", nil)
}
// WrapCli wraps command.Cli with kubernetes specifics
func WrapCli(dockerCli command.Cli, opts Options) (*KubeCli, error) {
var err error
cli := &KubeCli{
Cli: dockerCli,
}

View File

@ -1,13 +1,25 @@
package kubernetes
import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/cli/command/stack/options"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// GetStacks lists the kubernetes stacks.
func GetStacks(kubeCli *KubeCli, opts options.List) ([]*formatter.Stack, error) {
// GetStacks lists the kubernetes stacks
func GetStacks(dockerCli command.Cli, opts options.List, kopts Options) ([]*formatter.Stack, error) {
kubeCli, err := WrapCli(dockerCli, kopts)
if err != nil {
return nil, err
}
if opts.AllNamespaces || len(opts.Namespaces) == 0 {
return getStacks(kubeCli, opts)
}
return getStacksWithNamespaces(kubeCli, opts)
}
func getStacks(kubeCli *KubeCli, opts options.List) ([]*formatter.Stack, error) {
composeClient, err := kubeCli.composeClient()
if err != nil {
return nil, err
@ -31,3 +43,28 @@ func GetStacks(kubeCli *KubeCli, opts options.List) ([]*formatter.Stack, error)
}
return formattedStacks, nil
}
func getStacksWithNamespaces(kubeCli *KubeCli, opts options.List) ([]*formatter.Stack, error) {
stacks := []*formatter.Stack{}
for _, namespace := range removeDuplicates(opts.Namespaces) {
kubeCli.kubeNamespace = namespace
ss, err := getStacks(kubeCli, opts)
if err != nil {
return nil, err
}
stacks = append(stacks, ss...)
}
return stacks, nil
}
func removeDuplicates(namespaces []string) []string {
found := make(map[string]bool)
results := namespaces[:0]
for _, n := range namespaces {
if !found[n] {
results = append(results, n)
found[n] = true
}
}
return results
}