Add --all-namespaces to docker stack ls command on Kubernetes orchestrator, to list all stacks in all namespaces.

Signed-off-by: Silvin Lubecki <silvin.lubecki@docker.com>
This commit is contained in:
Silvin Lubecki 2018-04-09 15:13:16 +02:00 committed by Mathieu Champlon
parent 65526a201f
commit f097831eb3
9 changed files with 27 additions and 17 deletions

View File

@ -3,6 +3,7 @@ package kubernetes
import ( import (
"github.com/docker/cli/kubernetes" "github.com/docker/cli/kubernetes"
"github.com/pkg/errors" "github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubeclient "k8s.io/client-go/kubernetes" kubeclient "k8s.io/client-go/kubernetes"
appsv1beta2 "k8s.io/client-go/kubernetes/typed/apps/v1beta2" appsv1beta2 "k8s.io/client-go/kubernetes/typed/apps/v1beta2"
typesappsv1beta2 "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) 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 { func (s *Factory) ReplicaSets() typesappsv1beta2.ReplicaSetInterface {
return s.appsClientSet.ReplicaSets(s.namespace) return s.appsClientSet.ReplicaSets(s.namespace)
} }
func (c *Factory) Stacks() (stackClient, error) { // Stacks returns a client for Docker's Stack on Kubernetes
version, err := kubernetes.GetStackAPIVersion(c.clientSet) func (s *Factory) Stacks(allNamespaces bool) (StackClient, error) {
version, err := kubernetes.GetStackAPIVersion(s.clientSet)
if err != nil { if err != nil {
return nil, err return nil, err
} }
namespace := s.namespace
if allNamespaces {
namespace = metav1.NamespaceAll
}
switch version { switch version {
case kubernetes.StackAPIV1Beta1: case kubernetes.StackAPIV1Beta1:
return newStackV1Beta1(c.config, c.namespace) return newStackV1Beta1(s.config, namespace)
case kubernetes.StackAPIV1Beta2: case kubernetes.StackAPIV1Beta2:
return newStackV1Beta2(c.config, c.namespace) return newStackV1Beta2(s.config, namespace)
default: default:
return nil, errors.Errorf("no supported Stack API version") return nil, errors.Errorf("no supported Stack API version")
} }

View File

@ -21,7 +21,7 @@ func RunDeploy(dockerCli *KubeCli, opts options.Deploy) error {
if err != nil { if err != nil {
return err return err
} }
stacks, err := composeClient.Stacks() stacks, err := composeClient.Stacks(false)
if err != nil { if err != nil {
return err return err
} }

View File

@ -11,7 +11,7 @@ import (
// RunList is the kubernetes implementation of docker stack ls // RunList is the kubernetes implementation of docker stack ls
func RunList(dockerCli *KubeCli, opts options.List) error { func RunList(dockerCli *KubeCli, opts options.List) error {
stacks, err := getStacks(dockerCli) stacks, err := getStacks(dockerCli, opts.AllNamespaces)
if err != nil { if err != nil {
return err 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) 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 (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() composeClient, err := kubeCli.composeClient()
if err != nil { if err != nil {
return nil, err return nil, err
} }
stackSvc, err := composeClient.Stacks() stackSvc, err := composeClient.Stacks(allNamespaces)
if err != nil { if err != nil {
return nil, err return nil, err
} }
stacks, err := stackSvc.List(metav1.ListOptions{})
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -25,7 +25,7 @@ func RunPS(dockerCli *KubeCli, options options.PS) error {
if err != nil { if err != nil {
return err return err
} }
stacks, err := client.Stacks() stacks, err := client.Stacks(false)
if err != nil { if err != nil {
return err return err
} }

View File

@ -12,7 +12,7 @@ func RunRemove(dockerCli *KubeCli, opts options.Remove) error {
if err != nil { if err != nil {
return err return err
} }
stacks, err := composeClient.Stacks() stacks, err := composeClient.Stacks(false)
if err != nil { if err != nil {
return err return err
} }

View File

@ -16,7 +16,7 @@ func RunServices(dockerCli *KubeCli, opts options.Services) error {
if err != nil { if err != nil {
return nil return nil
} }
stacks, err := client.Stacks() stacks, err := client.Stacks(false)
if err != nil { if err != nil {
return err return err
} }

View File

@ -14,8 +14,8 @@ import (
"k8s.io/client-go/rest" "k8s.io/client-go/rest"
) )
// stackClient talks to a kubernetes compose component. // StackClient talks to a kubernetes compose component.
type stackClient interface { type StackClient interface {
CreateOrUpdate(s stack) error CreateOrUpdate(s stack) error
Delete(name string) error Delete(name string) error
Get(name string) (stack, error) Get(name string) (stack, error)
@ -29,7 +29,7 @@ type stackV1Beta1 struct {
stacks composev1beta1.StackInterface stacks composev1beta1.StackInterface
} }
func newStackV1Beta1(config *rest.Config, namespace string) (stackClient, error) { func newStackV1Beta1(config *rest.Config, namespace string) (StackClient, error) {
client, err := composev1beta1.NewForConfig(config) client, err := composev1beta1.NewForConfig(config)
if err != nil { if err != nil {
return nil, err return nil, err
@ -122,7 +122,7 @@ type stackV1Beta2 struct {
stacks composev1beta2.StackInterface stacks composev1beta2.StackInterface
} }
func newStackV1Beta2(config *rest.Config, namespace string) (stackClient, error) { func newStackV1Beta2(config *rest.Config, namespace string) (StackClient, error) {
client, err := composev1beta2.NewForConfig(config) client, err := composev1beta2.NewForConfig(config)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -31,5 +31,7 @@ func newListCommand(dockerCli command.Cli) *cobra.Command {
flags := cmd.Flags() flags := cmd.Flags()
flags.StringVar(&opts.Format, "format", "", "Pretty-print stacks using a Go template") 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 return cmd
} }

View File

@ -14,7 +14,8 @@ type Deploy struct {
// List holds docker stack ls options // List holds docker stack ls options
type List struct { type List struct {
Format string Format string
AllNamespaces bool
} }
// PS holds docker stack ps options // PS holds docker stack ps options