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 (
"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")
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -14,8 +14,8 @@ import (
"k8s.io/client-go/rest"
)
// stackClient talks to a kubernetes compose component.
type stackClient interface {
// StackClient talks to a kubernetes compose component.
type StackClient interface {
CreateOrUpdate(s stack) error
Delete(name string) error
Get(name string) (stack, error)
@ -29,7 +29,7 @@ type stackV1Beta1 struct {
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)
if err != nil {
return nil, err
@ -122,7 +122,7 @@ type stackV1Beta2 struct {
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)
if err != nil {
return nil, err

View File

@ -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
}

View File

@ -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