From 31dccfffb5fb9f40009d4246ee1a25e23a3853c9 Mon Sep 17 00:00:00 2001 From: Silvin Lubecki Date: Mon, 9 Apr 2018 15:07:11 +0200 Subject: [PATCH] Move stack client creation from KubeCli to client factory, where it belongs Signed-off-by: Silvin Lubecki --- cli/command/stack/kubernetes/cli.go | 19 +------------------ cli/command/stack/kubernetes/client.go | 23 ++++++++++++++++++++++- cli/command/stack/kubernetes/deploy.go | 4 ++-- cli/command/stack/kubernetes/list.go | 8 +++++--- cli/command/stack/kubernetes/ps.go | 2 +- cli/command/stack/kubernetes/remove.go | 6 +++++- cli/command/stack/kubernetes/services.go | 2 +- 7 files changed, 37 insertions(+), 27 deletions(-) diff --git a/cli/command/stack/kubernetes/cli.go b/cli/command/stack/kubernetes/cli.go index 4984f2b0e0..b9396bcf79 100644 --- a/cli/command/stack/kubernetes/cli.go +++ b/cli/command/stack/kubernetes/cli.go @@ -7,7 +7,6 @@ import ( "github.com/docker/cli/cli/command" "github.com/docker/cli/kubernetes" "github.com/docker/docker/pkg/homedir" - "github.com/pkg/errors" flag "github.com/spf13/pflag" kubeclient "k8s.io/client-go/kubernetes" restclient "k8s.io/client-go/rest" @@ -81,21 +80,5 @@ func WrapCli(dockerCli command.Cli, opts Options) (*KubeCli, error) { } func (c *KubeCli) composeClient() (*Factory, error) { - return NewFactory(c.kubeNamespace, c.kubeConfig) -} - -func (c *KubeCli) stacks() (stackClient, error) { - version, err := kubernetes.GetStackAPIVersion(c.clientSet) - if err != nil { - return nil, err - } - - switch version { - case kubernetes.StackAPIV1Beta1: - return newStackV1Beta1(c.kubeConfig, c.kubeNamespace) - case kubernetes.StackAPIV1Beta2: - return newStackV1Beta2(c.kubeConfig, c.kubeNamespace) - default: - return nil, errors.Errorf("no supported Stack API version") - } + return NewFactory(c.kubeNamespace, c.kubeConfig, c.clientSet) } diff --git a/cli/command/stack/kubernetes/client.go b/cli/command/stack/kubernetes/client.go index a44bf44590..3f137ecf92 100644 --- a/cli/command/stack/kubernetes/client.go +++ b/cli/command/stack/kubernetes/client.go @@ -1,6 +1,9 @@ package kubernetes import ( + "github.com/docker/cli/kubernetes" + "github.com/pkg/errors" + kubeclient "k8s.io/client-go/kubernetes" appsv1beta2 "k8s.io/client-go/kubernetes/typed/apps/v1beta2" typesappsv1beta2 "k8s.io/client-go/kubernetes/typed/apps/v1beta2" corev1 "k8s.io/client-go/kubernetes/typed/core/v1" @@ -13,10 +16,11 @@ type Factory struct { config *restclient.Config coreClientSet *corev1.CoreV1Client appsClientSet *appsv1beta2.AppsV1beta2Client + clientSet *kubeclient.Clientset } // NewFactory creates a kubernetes client factory -func NewFactory(namespace string, config *restclient.Config) (*Factory, error) { +func NewFactory(namespace string, config *restclient.Config, clientSet *kubeclient.Clientset) (*Factory, error) { coreClientSet, err := corev1.NewForConfig(config) if err != nil { return nil, err @@ -32,6 +36,7 @@ func NewFactory(namespace string, config *restclient.Config) (*Factory, error) { config: config, coreClientSet: coreClientSet, appsClientSet: appsClientSet, + clientSet: clientSet, }, nil } @@ -69,3 +74,19 @@ func (s *Factory) ReplicationControllers() corev1.ReplicationControllerInterface func (s *Factory) ReplicaSets() typesappsv1beta2.ReplicaSetInterface { return s.appsClientSet.ReplicaSets(s.namespace) } + +func (c *Factory) Stacks() (stackClient, error) { + version, err := kubernetes.GetStackAPIVersion(c.clientSet) + if err != nil { + return nil, err + } + + switch version { + case kubernetes.StackAPIV1Beta1: + return newStackV1Beta1(c.config, c.namespace) + case kubernetes.StackAPIV1Beta2: + return newStackV1Beta2(c.config, c.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 b708b9f4e6..92b6de3e9f 100644 --- a/cli/command/stack/kubernetes/deploy.go +++ b/cli/command/stack/kubernetes/deploy.go @@ -17,11 +17,11 @@ func RunDeploy(dockerCli *KubeCli, opts options.Deploy) error { } // Initialize clients - stacks, err := dockerCli.stacks() + composeClient, err := dockerCli.composeClient() if err != nil { return err } - composeClient, err := dockerCli.composeClient() + stacks, err := composeClient.Stacks() if err != nil { return err } diff --git a/cli/command/stack/kubernetes/list.go b/cli/command/stack/kubernetes/list.go index d513872a6d..915fb8b925 100644 --- a/cli/command/stack/kubernetes/list.go +++ b/cli/command/stack/kubernetes/list.go @@ -34,12 +34,14 @@ 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) { - stackSvc, err := kubeCli.stacks() + composeClient, err := kubeCli.composeClient() + if err != nil { + return nil, err + } + stackSvc, err := composeClient.Stacks() 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 60fdb32e5d..e0782bd4ce 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 := dockerCli.stacks() + stacks, err := client.Stacks() if err != nil { return err } diff --git a/cli/command/stack/kubernetes/remove.go b/cli/command/stack/kubernetes/remove.go index bf3654a7a4..b402f04089 100644 --- a/cli/command/stack/kubernetes/remove.go +++ b/cli/command/stack/kubernetes/remove.go @@ -8,7 +8,11 @@ import ( // RunRemove is the kubernetes implementation of docker stack remove func RunRemove(dockerCli *KubeCli, opts options.Remove) error { - stacks, err := dockerCli.stacks() + composeClient, err := dockerCli.composeClient() + if err != nil { + return err + } + stacks, err := composeClient.Stacks() if err != nil { return err } diff --git a/cli/command/stack/kubernetes/services.go b/cli/command/stack/kubernetes/services.go index 1c98c4fd6c..38d1b24b2b 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 := dockerCli.stacks() + stacks, err := client.Stacks() if err != nil { return err }