Skip to content

Commit

Permalink
Add --all-namespaces to docker stack ls command on Kubernetes orchest…
Browse files Browse the repository at this point in the history
…rator, to list all stacks in all namespaces.

Signed-off-by: Silvin Lubecki <[email protected]>
  • Loading branch information
silvin-lubecki committed Apr 9, 2018
1 parent c8f4053 commit b03b1c0
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 13 deletions.
16 changes: 11 additions & 5 deletions cli/command/stack/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion cli/command/stack/kubernetes/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions cli/command/stack/kubernetes/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cli/command/stack/kubernetes/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cli/command/stack/kubernetes/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cli/command/stack/kubernetes/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions cli/command/stack/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion cli/command/stack/options/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b03b1c0

Please sign in to comment.