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 a17f07f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
5 changes: 3 additions & 2 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,7 +33,7 @@ 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
Expand All @@ -42,6 +42,7 @@ func getStacks(kubeCli *KubeCli) ([]*formatter.Stack, error) {
if err != nil {
return nil, err
}
stacks, err := stackSvc.List(metav1.ListOptions{}, allNamespaces)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions cli/command/stack/kubernetes/stackclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type stackClient interface {
CreateOrUpdate(s stack) error
Delete(name string) error
Get(name string) (stack, error)
List(opts metav1.ListOptions) ([]stack, error)
List(opts metav1.ListOptions, allNamespaces bool) ([]stack, error)
IsColliding(servicesClient corev1.ServiceInterface, s stack) error
FromCompose(stderr io.Writer, name string, cfg *composetypes.Config) (stack, error)
}
Expand Down Expand Up @@ -61,8 +61,8 @@ func (s *stackV1Beta1) Get(name string) (stack, error) {
return stackFromV1beta1(stackBeta1)
}

func (s *stackV1Beta1) List(opts metav1.ListOptions) ([]stack, error) {
list, err := s.stacks.List(opts)
func (s *stackV1Beta1) List(opts metav1.ListOptions, allNamespaces bool) ([]stack, error) {
list, err := s.stacks.List(opts, allNamespaces)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -154,8 +154,8 @@ func (s *stackV1Beta2) Get(name string) (stack, error) {
return stackFromV1beta2(stackBeta2), nil
}

func (s *stackV1Beta2) List(opts metav1.ListOptions) ([]stack, error) {
list, err := s.stacks.List(opts)
func (s *stackV1Beta2) List(opts metav1.ListOptions, allNamespaces bool) ([]stack, error) {
list, err := s.stacks.List(opts, allNamespaces)
if err != nil {
return nil, 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
10 changes: 7 additions & 3 deletions kubernetes/client/clientset/typed/compose/v1beta1/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type StackInterface interface {
Delete(name string, options *v1.DeleteOptions) error
DeleteCollection(options *v1.DeleteOptions, listOptions v1.ListOptions) error
Get(name string, options v1.GetOptions) (*v1beta1.Stack, error)
List(opts v1.ListOptions) (*v1beta1.StackList, error)
List(opts v1.ListOptions, allNamespaces bool) (*v1beta1.StackList, error)
Watch(opts v1.ListOptions) (watch.Interface, error)
Patch(name string, pt types.PatchType, data []byte, subresources ...string) (*v1beta1.Stack, error)
}
Expand Down Expand Up @@ -121,10 +121,14 @@ func (c *stacks) Get(name string, options v1.GetOptions) (*v1beta1.Stack, error)
}

// List takes label and field selectors, and returns the list of Stacks that match those selectors.
func (c *stacks) List(opts v1.ListOptions) (*v1beta1.StackList, error) {
func (c *stacks) List(opts v1.ListOptions, allNamespaces bool) (*v1beta1.StackList, error) {
result := &v1beta1.StackList{}
namespace := c.ns
if allNamespaces {
namespace = ""
}
err := c.client.Get().
Namespace(c.ns).
Namespace(namespace).
Resource("stacks").
VersionedParams(&opts, scheme.ParameterCodec).
Do().
Expand Down
10 changes: 7 additions & 3 deletions kubernetes/client/clientset/typed/compose/v1beta2/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type StackInterface interface {
Delete(name string, options *v1.DeleteOptions) error
DeleteCollection(options *v1.DeleteOptions, listOptions v1.ListOptions) error
Get(name string, options v1.GetOptions) (*v1beta2.Stack, error)
List(opts v1.ListOptions) (*v1beta2.StackList, error)
List(opts v1.ListOptions, allNamespaces bool) (*v1beta2.StackList, error)
Watch(opts v1.ListOptions) (watch.Interface, error)
Patch(name string, pt types.PatchType, data []byte, subresources ...string) (*v1beta2.Stack, error)
}
Expand Down Expand Up @@ -119,10 +119,14 @@ func (c *stacks) Get(name string, options v1.GetOptions) (*v1beta2.Stack, error)
}

// List takes label and field selectors, and returns the list of Stacks that match those selectors.
func (c *stacks) List(opts v1.ListOptions) (*v1beta2.StackList, error) {
func (c *stacks) List(opts v1.ListOptions, allNamespaces bool) (*v1beta2.StackList, error) {
result := &v1beta2.StackList{}
namespace := c.ns
if allNamespaces {
namespace = ""
}
err := c.client.Get().
Namespace(c.ns).
Namespace(namespace).
Resource("stacks").
VersionedParams(&opts, scheme.ParameterCodec).
Do().
Expand Down

0 comments on commit a17f07f

Please sign in to comment.