From aa8c0cd471c1bbcd5857f4d6c2c3ec9398f1a842 Mon Sep 17 00:00:00 2001 From: Nolan Brubaker Date: Wed, 17 Jul 2019 17:19:14 -0400 Subject: [PATCH] Honor kube client flags in install command (#1656) Flags specifying the kubeconfig or kubecontext to use weren't actually being used by the install command. Fixes #1651 Signed-off-by: Nolan Brubaker --- changelogs/unreleased/1656-nrb | 1 + pkg/client/factory.go | 17 +++++++++++++++++ pkg/cmd/cli/install/install.go | 12 +++--------- 3 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 changelogs/unreleased/1656-nrb diff --git a/changelogs/unreleased/1656-nrb b/changelogs/unreleased/1656-nrb new file mode 100644 index 000000000..c2cee2492 --- /dev/null +++ b/changelogs/unreleased/1656-nrb @@ -0,0 +1 @@ +Respect the --kubecontext and --kubeconfig arugments for `velero install`. diff --git a/pkg/client/factory.go b/pkg/client/factory.go index 9ecad61d0..fa70a8eaf 100644 --- a/pkg/client/factory.go +++ b/pkg/client/factory.go @@ -22,6 +22,7 @@ import ( "github.com/pkg/errors" "github.com/spf13/pflag" + "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" v1 "github.com/heptio/velero/pkg/apis/velero/v1" @@ -38,6 +39,9 @@ type Factory interface { // KubeClient returns a Kubernetes client. It uses the following priority to specify the cluster // configuration: --kubeconfig flag, KUBECONFIG environment variable, in-cluster configuration. KubeClient() (kubernetes.Interface, error) + // DynamicClient returns a Kubernetes dynamic client. It uses the following priority to specify the cluster + // configuration: --kubeconfig flag, KUBECONFIG environment variable, in-cluster configuration. + DynamicClient() (dynamic.Interface, error) Namespace() string } @@ -103,6 +107,19 @@ func (f *factory) KubeClient() (kubernetes.Interface, error) { return kubeClient, nil } +func (f *factory) DynamicClient() (dynamic.Interface, error) { + clientConfig, err := Config(f.kubeconfig, f.kubecontext, f.baseName) + if err != nil { + return nil, err + } + + dynamicClient, err := dynamic.NewForConfig(clientConfig) + if err != nil { + return nil, errors.WithStack(err) + } + return dynamicClient, nil +} + func (f *factory) Namespace() string { return f.namespace } diff --git a/pkg/cmd/cli/install/install.go b/pkg/cmd/cli/install/install.go index 97d07e63a..f78f3d53f 100644 --- a/pkg/cmd/cli/install/install.go +++ b/pkg/cmd/cli/install/install.go @@ -26,7 +26,6 @@ import ( "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" - "k8s.io/client-go/dynamic" api "github.com/heptio/velero/pkg/apis/velero/v1" "github.com/heptio/velero/pkg/client" @@ -147,7 +146,7 @@ This is useful as a starting point for more customized installations. Run: func(c *cobra.Command, args []string) { cmd.CheckError(o.Validate(c, args, f)) cmd.CheckError(o.Complete(args, f)) - cmd.CheckError(o.Run(c)) + cmd.CheckError(o.Run(c, f)) }, } @@ -159,7 +158,7 @@ This is useful as a starting point for more customized installations. } // Run executes a command in the context of the provided arguments. -func (o *InstallOptions) Run(c *cobra.Command) error { +func (o *InstallOptions) Run(c *cobra.Command, f client.Factory) error { vo, err := o.AsVeleroOptions() if err != nil { return err @@ -177,12 +176,7 @@ func (o *InstallOptions) Run(c *cobra.Command) error { if o.DryRun { return nil } - - clientConfig, err := client.Config("", "", fmt.Sprintf("%s-%s", c.Parent().Name(), c.Name())) - if err != nil { - return err - } - dynamicClient, err := dynamic.NewForConfig(clientConfig) + dynamicClient, err := f.DynamicClient() if err != nil { return err }