Move stack client creation from KubeCli to client factory, where it belongs

Signed-off-by: Silvin Lubecki <silvin.lubecki@docker.com>
This commit is contained in:
Silvin Lubecki 2018-04-09 15:07:11 +02:00 committed by Mathieu Champlon
parent c23c1482c7
commit 31dccfffb5
7 changed files with 37 additions and 27 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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