From b03b1c0b44a74fc45e7c88d499bca6779ed3c738 Mon Sep 17 00:00:00 2001 From: Silvin Lubecki Date: Mon, 9 Apr 2018 15:13:16 +0200 Subject: [PATCH] Add --all-namespaces to docker stack ls command on Kubernetes orchestrator, to list all stacks in all namespaces. Signed-off-by: Silvin Lubecki --- cli/command/stack/kubernetes/client.go | 16 +++++++++++----- cli/command/stack/kubernetes/deploy.go | 2 +- cli/command/stack/kubernetes/list.go | 7 ++++--- cli/command/stack/kubernetes/ps.go | 2 +- cli/command/stack/kubernetes/remove.go | 2 +- cli/command/stack/kubernetes/services.go | 2 +- cli/command/stack/list.go | 2 ++ cli/command/stack/options/opts.go | 3 ++- 8 files changed, 23 insertions(+), 13 deletions(-) diff --git a/cli/command/stack/kubernetes/client.go b/cli/command/stack/kubernetes/client.go index 3f137ecf9296..d2b20f3d441e 100644 --- a/cli/command/stack/kubernetes/client.go +++ b/cli/command/stack/kubernetes/client.go @@ -3,6 +3,7 @@ package kubernetes import ( "github.com/docker/cli/kubernetes" "github.com/pkg/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" kubeclient "k8s.io/client-go/kubernetes" appsv1beta2 "k8s.io/client-go/kubernetes/typed/apps/v1beta2" typesappsv1beta2 "k8s.io/client-go/kubernetes/typed/apps/v1beta2" @@ -70,22 +71,27 @@ func (s *Factory) ReplicationControllers() corev1.ReplicationControllerInterface return s.coreClientSet.ReplicationControllers(s.namespace) } -// ReplicaSets return a client for kubernetes replace sets +// ReplicaSets returns a client for kubernetes replace sets func (s *Factory) ReplicaSets() typesappsv1beta2.ReplicaSetInterface { return s.appsClientSet.ReplicaSets(s.namespace) } -func (c *Factory) Stacks() (stackClient, error) { - version, err := kubernetes.GetStackAPIVersion(c.clientSet) +// Stacks returns a client for Docker's Stack on Kubernetes +func (s *Factory) Stacks(allNamespaces bool) (stackClient, error) { + version, err := kubernetes.GetStackAPIVersion(s.clientSet) if err != nil { return nil, err } + namespace := s.namespace + if allNamespaces { + namespace = metav1.NamespaceAll + } switch version { case kubernetes.StackAPIV1Beta1: - return newStackV1Beta1(c.config, c.namespace) + return newStackV1Beta1(s.config, namespace) case kubernetes.StackAPIV1Beta2: - return newStackV1Beta2(c.config, c.namespace) + return newStackV1Beta2(s.config, namespace) default: return nil, errors.Errorf("no supported Stack API version") } diff --git a/cli/command/stack/kubernetes/deploy.go b/cli/command/stack/kubernetes/deploy.go index 92b6de3e9f0a..48030a60f4c5 100644 --- a/cli/command/stack/kubernetes/deploy.go +++ b/cli/command/stack/kubernetes/deploy.go @@ -21,7 +21,7 @@ func RunDeploy(dockerCli *KubeCli, opts options.Deploy) error { if err != nil { return err } - stacks, err := composeClient.Stacks() + stacks, err := composeClient.Stacks(false) if err != nil { return err } diff --git a/cli/command/stack/kubernetes/list.go b/cli/command/stack/kubernetes/list.go index 8b02426320f3..8f9f47045e9a 100644 --- a/cli/command/stack/kubernetes/list.go +++ b/cli/command/stack/kubernetes/list.go @@ -11,7 +11,7 @@ import ( // RunList is the kubernetes implementation of docker stack ls func RunList(dockerCli *KubeCli, opts options.List) error { - stacks, err := getStacks(dockerCli) + stacks, err := getStacks(dockerCli, opts.AllNamespaces) if err != nil { return err } @@ -33,15 +33,16 @@ func (n byName) Len() int { return len(n) } func (n byName) Swap(i, j int) { n[i], n[j] = n[j], n[i] } func (n byName) Less(i, j int) bool { return sortorder.NaturalLess(n[i].Name, n[j].Name) } -func getStacks(kubeCli *KubeCli) ([]*formatter.Stack, error) { +func getStacks(kubeCli *KubeCli, allNamespaces bool) ([]*formatter.Stack, error) { composeClient, err := kubeCli.composeClient() if err != nil { return nil, err } - stackSvc, err := composeClient.Stacks() + stackSvc, err := composeClient.Stacks(allNamespaces) if err != nil { return nil, err } + stacks, err := stackSvc.List(metav1.ListOptions{}) if err != nil { return nil, err } diff --git a/cli/command/stack/kubernetes/ps.go b/cli/command/stack/kubernetes/ps.go index e0782bd4cef4..ded56db19d5c 100644 --- a/cli/command/stack/kubernetes/ps.go +++ b/cli/command/stack/kubernetes/ps.go @@ -25,7 +25,7 @@ func RunPS(dockerCli *KubeCli, options options.PS) error { if err != nil { return err } - stacks, err := client.Stacks() + stacks, err := client.Stacks(false) if err != nil { return err } diff --git a/cli/command/stack/kubernetes/remove.go b/cli/command/stack/kubernetes/remove.go index b402f040898d..af68db7f306e 100644 --- a/cli/command/stack/kubernetes/remove.go +++ b/cli/command/stack/kubernetes/remove.go @@ -12,7 +12,7 @@ func RunRemove(dockerCli *KubeCli, opts options.Remove) error { if err != nil { return err } - stacks, err := composeClient.Stacks() + stacks, err := composeClient.Stacks(false) if err != nil { return err } diff --git a/cli/command/stack/kubernetes/services.go b/cli/command/stack/kubernetes/services.go index 38d1b24b2ba2..b1a4a5f42f9d 100644 --- a/cli/command/stack/kubernetes/services.go +++ b/cli/command/stack/kubernetes/services.go @@ -16,7 +16,7 @@ func RunServices(dockerCli *KubeCli, opts options.Services) error { if err != nil { return nil } - stacks, err := client.Stacks() + stacks, err := client.Stacks(false) if err != nil { return err } diff --git a/cli/command/stack/list.go b/cli/command/stack/list.go index b153a8b2f62e..fc96fd679105 100644 --- a/cli/command/stack/list.go +++ b/cli/command/stack/list.go @@ -31,5 +31,7 @@ func newListCommand(dockerCli command.Cli) *cobra.Command { flags := cmd.Flags() flags.StringVar(&opts.Format, "format", "", "Pretty-print stacks using a Go template") + flags.BoolVarP(&opts.AllNamespaces, "all-namespaces", "", false, "List stacks among all Kubernetes namespaces") + flags.SetAnnotation("all-namespaces", "kubernetes", nil) return cmd } diff --git a/cli/command/stack/options/opts.go b/cli/command/stack/options/opts.go index 8c8a1acf261b..f620fe54a9d5 100644 --- a/cli/command/stack/options/opts.go +++ b/cli/command/stack/options/opts.go @@ -14,7 +14,8 @@ type Deploy struct { // List holds docker stack ls options type List struct { - Format string + Format string + AllNamespaces bool } // PS holds docker stack ps options