From aaa7faa117813c35f1841a35335b8efcfed779b9 Mon Sep 17 00:00:00 2001 From: Alena Varkockova Date: Tue, 11 Jun 2019 09:31:51 +0200 Subject: [PATCH] Remove vars, pass params explicitly (#320) --- pkg/kudoctl/cmd/install.go | 28 +++--- pkg/kudoctl/cmd/install/install.go | 127 ++++++++++++++---------- pkg/kudoctl/cmd/install/install_test.go | 60 +---------- pkg/kudoctl/cmd/install_test.go | 15 +-- pkg/kudoctl/util/check/check.go | 22 ++-- pkg/kudoctl/util/check/check_test.go | 16 ++- pkg/kudoctl/util/kudo/kudo.go | 51 +++++----- pkg/kudoctl/util/kudo/kudo_test.go | 30 ++---- pkg/kudoctl/util/vars/vars.go | 14 --- 9 files changed, 148 insertions(+), 215 deletions(-) delete mode 100644 pkg/kudoctl/util/vars/vars.go diff --git a/pkg/kudoctl/cmd/install.go b/pkg/kudoctl/cmd/install.go index ea6cb404b..891e519ec 100644 --- a/pkg/kudoctl/cmd/install.go +++ b/pkg/kudoctl/cmd/install.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/kudobuilder/kudo/pkg/kudoctl/cmd/install" - "github.com/kudobuilder/kudo/pkg/kudoctl/util/vars" "github.com/spf13/cobra" ) @@ -22,22 +21,25 @@ var ( // newInstallCmd creates the install command for the CLI func newInstallCmd() *cobra.Command { + options := install.DefaultOptions installCmd := &cobra.Command{ - Use: "install ", - Short: "-> Install an official KUDO package.", - Long: `Install a KUDO package from the official GitHub repo.`, - Example: installExample, - RunE: install.RunInstall, + Use: "install ", + Short: "-> Install an official KUDO package.", + Long: `Install a KUDO package from the official GitHub repo.`, + Example: installExample, + RunE: func(cmd *cobra.Command, args []string) error { + return install.RunInstall(cmd, args, options) + }, SilenceUsage: true, } - installCmd.Flags().BoolVar(&vars.AllDependencies, "all-dependencies", false, "Installs all dependency packages. (default \"false\")") - installCmd.Flags().BoolVar(&vars.AutoApprove, "auto-approve", false, "Skip interactive approval when existing version found. (default \"false\")") - installCmd.Flags().StringVar(&vars.KubeConfigPath, "kubeconfig", "", "The file path to Kubernetes configuration file. (default \"$HOME/.kube/config\")") - installCmd.Flags().StringVar(&vars.Instance, "instance", "", "The instance name. (default to Framework name)") - installCmd.Flags().StringVar(&vars.Namespace, "namespace", "default", "The namespace used for the framework installation. (default \"default\"") - installCmd.Flags().StringArrayVarP(&vars.Parameter, "parameter", "p", nil, "The parameter name and value separated by '='") - installCmd.Flags().StringVar(&vars.PackageVersion, "package-version", "", "A specific package version on the official GitHub repo. (default to the most recent)") + installCmd.Flags().BoolVar(&options.AllDependencies, "all-dependencies", false, "Installs all dependency packages. (default \"false\")") + installCmd.Flags().BoolVar(&options.AutoApprove, "auto-approve", false, "Skip interactive approval when existing version found. (default \"false\")") + installCmd.Flags().StringVar(&options.KubeConfigPath, "kubeconfig", "", "The file path to Kubernetes configuration file. (default \"$HOME/.kube/config\")") + installCmd.Flags().StringVar(&options.InstanceName, "instance", "", "The instance name. (default to Framework name)") + installCmd.Flags().StringVar(&options.Namespace, "namespace", "default", "The namespace used for the package installation. (default \"default\"") + installCmd.Flags().StringArrayVarP(&options.Parameters, "parameter", "p", nil, "The parameter name and value separated by '='") + installCmd.Flags().StringVar(&options.PackageVersion, "package-version", "", "A specific package version on the official GitHub repo. (default to the most recent)") const usageFmt = "Usage:\n %s\n\nFlags:\n%s" installCmd.SetUsageFunc(func(cmd *cobra.Command) error { diff --git a/pkg/kudoctl/cmd/install/install.go b/pkg/kudoctl/cmd/install/install.go index bef33906f..74e9c5688 100644 --- a/pkg/kudoctl/cmd/install/install.go +++ b/pkg/kudoctl/cmd/install/install.go @@ -9,56 +9,73 @@ import ( "github.com/kudobuilder/kudo/pkg/kudoctl/util/helpers" "github.com/kudobuilder/kudo/pkg/kudoctl/util/kudo" "github.com/kudobuilder/kudo/pkg/kudoctl/util/repo" - "github.com/kudobuilder/kudo/pkg/kudoctl/util/vars" "github.com/pkg/errors" "github.com/spf13/cobra" "k8s.io/client-go/tools/clientcmd" ) +// Options defines configuration options for the install command +type Options struct { + AllDependencies bool + AutoApprove bool + InstanceName string + Namespace string + Parameters []string + PackageVersion string + KubeConfigPath string +} + +// NewOptions initializes the install command options to its defaults +var DefaultOptions = &Options{ + Namespace: "default", +} + // RunInstall returns the errors associated with cmd env -func RunInstall(cmd *cobra.Command, args []string) error { +func RunInstall(cmd *cobra.Command, args []string, options *Options) error { // This makes --kubeconfig flag optional if _, err := cmd.Flags().GetString("kubeconfig"); err != nil { return fmt.Errorf("get flag: %+v", err) } - if err := check.ValidateKubeConfigPath(); err != nil { + configPath, err := check.KubeConfigLocationOrDefault(options.KubeConfigPath) + if err != nil { + return fmt.Errorf("error when getting default kubeconfig path: %+v", err) + } + options.KubeConfigPath = configPath + if err := check.ValidateKubeConfigPath(options.KubeConfigPath); err != nil { return errors.WithMessage(err, "could not check kubeconfig path") } // Validate install parameters - if err := validateInstallParameters(); err != nil { + if err := validateInstallParameters(options.Parameters); err != nil { return errors.WithMessage(err, "could not parse parameters") } - if err := installFrameworks(args); err != nil { + if err := installFrameworks(args, options); err != nil { return errors.WithMessage(err, "could not install framework(s)") } return nil } -func validateInstallParameters() error { +func validateInstallParameters(parameters []string) error { var errs []string - if vars.Parameter != nil { - - for _, a := range vars.Parameter { - // Using '=' as the delimiter. Split after the first delimiter to support using '=' in values - s := strings.SplitN(a, "=", 2) - if len(s) < 2 { - errs = append(errs, fmt.Sprintf("parameter not set: %+v", a)) - continue - } - if s[0] == "" { - errs = append(errs, fmt.Sprintf("parameter name can not be empty: %+v", a)) - continue - } - if s[1] == "" { - errs = append(errs, fmt.Sprintf("parameter value can not be empty: %+v", a)) - continue - } + for _, a := range parameters { + // Using '=' as the delimiter. Split after the first delimiter to support using '=' in values + s := strings.SplitN(a, "=", 2) + if len(s) < 2 { + errs = append(errs, fmt.Sprintf("parameter not set: %+v", a)) + continue + } + if s[0] == "" { + errs = append(errs, fmt.Sprintf("parameter name can not be empty: %+v", a)) + continue + } + if s[1] == "" { + errs = append(errs, fmt.Sprintf("parameter value can not be empty: %+v", a)) + continue } } @@ -70,13 +87,13 @@ func validateInstallParameters() error { } // installFrameworks installs all frameworks specified as arguments into the cluster -func installFrameworks(args []string) error { +func installFrameworks(args []string, options *Options) error { if len(args) < 1 { return fmt.Errorf("no argument provided") } - if len(args) > 1 && vars.PackageVersion != "" { + if len(args) > 1 && options.PackageVersion != "" { return fmt.Errorf("--package-version not supported in multi framework install") } repoConfig := repo.Default @@ -93,18 +110,18 @@ func installFrameworks(args []string) error { return errors.WithMessage(err, "could not download index file") } - _, err = clientcmd.BuildConfigFromFlags("", vars.KubeConfigPath) + _, err = clientcmd.BuildConfigFromFlags("", options.KubeConfigPath) if err != nil { return errors.Wrap(err, "getting config failed") } - kc, err := kudo.NewKudoClient() + kc, err := kudo.NewKudoClient(options.Namespace, options.KubeConfigPath) if err != nil { return errors.Wrap(err, "creating kudo client") } for _, name := range args { - err := installFramework(name, "", *r, indexFile, kc) + err := installFramework(name, "", *r, indexFile, kc, options) if err != nil { return err } @@ -115,17 +132,17 @@ func installFrameworks(args []string) error { // Todo: needs testing // installFramework is the umbrella for a single framework installation that gathers the business logic // for a cluster and returns an error in case there is a problem -func installFramework(name, previous string, repository repo.FrameworkRepository, indexFile *repo.IndexFile, kc *kudo.Client) error { +func installFramework(name, previous string, repository repo.FrameworkRepository, indexFile *repo.IndexFile, kc *kudo.Client, options *Options) error { var bundleVersion *repo.BundleVersion - if vars.PackageVersion == "" { + if options.PackageVersion == "" { bv, err := indexFile.GetByName(name) if err != nil { return errors.Wrapf(err, "getting %s in index file", name) } bundleVersion = bv } else { - bv, err := indexFile.GetByNameAndVersion(name, vars.PackageVersion) + bv, err := indexFile.GetByNameAndVersion(name, options.PackageVersion) if err != nil { return errors.Wrapf(err, "getting %s in index file", name) } @@ -142,8 +159,8 @@ func installFramework(name, previous string, repository repo.FrameworkRepository // Framework part // Check if Framework exists - if !kc.FrameworkExistsInCluster(name) { - if err := installSingleFrameworkToCluster(name, bundle.Framework, kc); err != nil { + if !kc.FrameworkExistsInCluster(name, options.Namespace) { + if err := installSingleFrameworkToCluster(name, options.Namespace, bundle.Framework, kc); err != nil { return errors.Wrap(err, "installing single Framework") } } @@ -151,27 +168,27 @@ func installFramework(name, previous string, repository repo.FrameworkRepository // FrameworkVersion part // Check if AnyFrameworkVersion for Framework exists - if !kc.AnyFrameworkVersionExistsInCluster(name) { + if !kc.AnyFrameworkVersionExistsInCluster(name, options.Namespace) { // FrameworkVersion CRD for Framework does not exist - if err := installSingleFrameworkVersionToCluster(name, kc, bundle.FrameworkVersion); err != nil { + if err := installSingleFrameworkVersionToCluster(name, options.Namespace, kc, bundle.FrameworkVersion); err != nil { return errors.Wrapf(err, "installing FrameworkVersion CRD for framework %s", name) } } // Check if FrameworkVersion is out of sync with official FrameworkVersion for this Framework - if !kc.FrameworkVersionInClusterOutOfSync(name, bundle.FrameworkVersion.Spec.Version) { + if !kc.FrameworkVersionInClusterOutOfSync(name, bundle.FrameworkVersion.Spec.Version, options.Namespace) { // This happens when the given FrameworkVersion is not existing. E.g. // when a version has been installed that is not part of the official kudobuilder/frameworks repo. - if !vars.AutoApprove { + if !options.AutoApprove { fmt.Printf("No official FrameworkVersion has been found for \"%s\". "+ "Do you want to install one? (Yes/no) ", name) if helpers.AskForConfirmation() { - if err := installSingleFrameworkVersionToCluster(name, kc, bundle.FrameworkVersion); err != nil { + if err := installSingleFrameworkVersionToCluster(name, options.Namespace, kc, bundle.FrameworkVersion); err != nil { return errors.Wrapf(err, "installing FrameworkVersion CRD for framework %s", name) } } } else { - if err := installSingleFrameworkVersionToCluster(name, kc, bundle.FrameworkVersion); err != nil { + if err := installSingleFrameworkVersionToCluster(name, options.Namespace, kc, bundle.FrameworkVersion); err != nil { return errors.Wrapf(err, "installing FrameworkVersion CRD for framework %s", name) } } @@ -179,7 +196,7 @@ func installFramework(name, previous string, repository repo.FrameworkRepository } // Dependencies of the particular FrameworkVersion - if vars.AllDependencies { + if options.AllDependencies { dependencyFrameworks, err := repository.GetFrameworkVersionDependencies(name, bundle.FrameworkVersion) if err != nil { return errors.Wrap(err, "getting Framework dependencies") @@ -189,7 +206,7 @@ func installFramework(name, previous string, repository repo.FrameworkRepository // Dependencies should not be as big as that they will have an overflow in the function stack frame // installFramework makes sure that dependency Frameworks are created before the Framework itself // and it allows to inherit dependencies. - if err := installFramework(v, name, repository, indexFile, kc); err != nil { + if err := installFramework(v, name, repository, indexFile, kc, options); err != nil { return errors.Wrapf(err, "installing dependency Framework %s", v) } } @@ -203,21 +220,21 @@ func installFramework(name, previous string, repository repo.FrameworkRepository // Check if Instance exists in cluster // It won't create the Instance if any in combination with given Framework Name and FrameworkVersion exists - if !kc.AnyInstanceExistsInCluster(name, bundle.FrameworkVersion.Spec.Version) { + if !kc.AnyInstanceExistsInCluster(name, options.Namespace, bundle.FrameworkVersion.Spec.Version) { // This happens when the given FrameworkVersion is not existing. E.g. // when a version has been installed that is not part of the official kudobuilder/frameworks repo. - if !vars.AutoApprove { + if !options.AutoApprove { fmt.Printf("No Instance tied to this \"%s\" version has been found. "+ "Do you want to create one? (Yes/no) ", name) if helpers.AskForConfirmation() { // If Instance is a dependency we need to make sure installSingleInstanceToCluster is aware of it. // By having the previous string set we can make this distinction. - if err := installSingleInstanceToCluster(name, previous, bundle.Instance, kc); err != nil { + if err := installSingleInstanceToCluster(name, previous, bundle.Instance, kc, options); err != nil { return errors.Wrap(err, "installing single Instance") } } } else { - if err := installSingleInstanceToCluster(name, previous, bundle.Instance, kc); err != nil { + if err := installSingleInstanceToCluster(name, previous, bundle.Instance, kc, options); err != nil { return errors.Wrap(err, "installing single Instance") } } @@ -228,8 +245,8 @@ func installFramework(name, previous string, repository repo.FrameworkRepository // Todo: needs testing // installSingleFrameworkToCluster installs a given Framework to the cluster -func installSingleFrameworkToCluster(name string, f *v1alpha1.Framework, kc *kudo.Client) error { - if _, err := kc.InstallFrameworkObjToCluster(f); err != nil { +func installSingleFrameworkToCluster(name, namespace string, f *v1alpha1.Framework, kc *kudo.Client) error { + if _, err := kc.InstallFrameworkObjToCluster(f, namespace); err != nil { return errors.Wrapf(err, "installing %s-framework.yaml", name) } fmt.Printf("framework.%s/%s created\n", f.APIVersion, f.Name) @@ -238,8 +255,8 @@ func installSingleFrameworkToCluster(name string, f *v1alpha1.Framework, kc *kud // Todo: needs testing // installSingleFrameworkVersionToCluster installs a given FrameworkVersion to the cluster -func installSingleFrameworkVersionToCluster(name string, kc *kudo.Client, fv *v1alpha1.FrameworkVersion) error { - if _, err := kc.InstallFrameworkVersionObjToCluster(fv); err != nil { +func installSingleFrameworkVersionToCluster(name, namespace string, kc *kudo.Client, fv *v1alpha1.FrameworkVersion) error { + if _, err := kc.InstallFrameworkVersionObjToCluster(fv, namespace); err != nil { return errors.Wrapf(err, "installing %s-frameworkversion.yaml", name) } fmt.Printf("frameworkversion.%s/%s created\n", fv.APIVersion, fv.Name) @@ -248,7 +265,7 @@ func installSingleFrameworkVersionToCluster(name string, kc *kudo.Client, fv *v1 // Todo: needs more testing // installSingleInstanceToCluster installs a given Instance to the cluster -func installSingleInstanceToCluster(name, previous string, instance *v1alpha1.Instance, kc *kudo.Client) error { +func installSingleInstanceToCluster(name, previous string, instance *v1alpha1.Instance, kc *kudo.Client, options *Options) error { // Customizing Instance // TODO: traversing, e.g. check function that looksup if key exists in the current FrameworkVersion // That way just Parameters will be applied if they exist in the matching FrameworkVersion @@ -256,18 +273,18 @@ func installSingleInstanceToCluster(name, previous string, instance *v1alpha1.In // E.g. when installing with flag --all-dependencies to prevent overwriting dependency Instance name // This checks if flag --instance was set with a name and it is the not a dependency Instance - if vars.Instance != "" && previous == "" { - instance.ObjectMeta.SetName(vars.Instance) + if options.InstanceName != "" && previous == "" { + instance.ObjectMeta.SetName(options.InstanceName) } - if vars.Parameter != nil { + if options.Parameters != nil { p := make(map[string]string) - for _, a := range vars.Parameter { + for _, a := range options.Parameters { s := strings.SplitN(a, "=", 2) p[s[0]] = s[1] } instance.Spec.Parameters = p } - if _, err := kc.InstallInstanceObjToCluster(instance); err != nil { + if _, err := kc.InstallInstanceObjToCluster(instance, options.Namespace); err != nil { return errors.Wrapf(err, "installing %s-instance.yaml", name) } fmt.Printf("instance.%s/%s created\n", instance.APIVersion, instance.Name) diff --git a/pkg/kudoctl/cmd/install/install_test.go b/pkg/kudoctl/cmd/install/install_test.go index b8d110215..951e21f99 100644 --- a/pkg/kudoctl/cmd/install/install_test.go +++ b/pkg/kudoctl/cmd/install/install_test.go @@ -2,62 +2,8 @@ package install import ( "testing" - - "github.com/kudobuilder/kudo/pkg/kudoctl/util/vars" - "github.com/spf13/cobra" ) -func TestInstallCmd(t *testing.T) { - - // Default for test case #1 - cmdDefault := &cobra.Command{} - - expectedDefaultErrors := []string{ - "get flag: flag accessed but not defined: kubeconfig", - } - - // For test case #2 - cmdNoKubeconfigFlagDefined := &cobra.Command{} - expectedNoKubeconfigFlagDefinedErrors := []string{ - "get flag: flag accessed but not defined: kubeconfig", - } - - // For test case #3 - cmdWrongDirKubeConfigFlag := &cobra.Command{} - cmdWrongDirKubeConfigFlag.Flags().StringVar(&vars.KubeConfigPath, "kubeconfig", "", "Usage") - vars.KubeConfigPath = "/tmp" - installCmdArgs := []string{"test", "--kubeconfig=" + vars.KubeConfigPath} - expectedEmptyKubeConfigFlagErrors := []string{ - "could not check kubeconfig path: getting config failed: /tmp is a directory", - } - - tests := []struct { - cmd *cobra.Command - args []string - err []string - }{ - {cmdDefault, nil, expectedDefaultErrors}, // 1 - {cmdNoKubeconfigFlagDefined, nil, expectedNoKubeconfigFlagDefinedErrors}, // 2 - {cmdWrongDirKubeConfigFlag, installCmdArgs, expectedEmptyKubeConfigFlagErrors}, // 3 - } - - for i, tt := range tests { - err := RunInstall(tt.cmd, tt.args) - if err != nil { - receivedErrorList := []string{err.Error()} - diff := compareSlice(receivedErrorList, tt.err) - for _, err := range diff { - t.Errorf("%d: Found unexpected error: %v", i+1, err) - } - - missing := compareSlice(tt.err, receivedErrorList) - for _, err := range missing { - t.Errorf("%d: Missed expected error: %v", i+1, err) - } - } - } -} - func TestInstallFrameworks(t *testing.T) { // For test case #1 @@ -66,8 +12,8 @@ func TestInstallFrameworks(t *testing.T) { } // For test case #2 - vars.KubeConfigPath = "" - vars.PackageVersion = "0.0" + options := DefaultOptions + options.PackageVersion = "0.0" installCmdPackageVersionArgs := []string{"one", "two"} expectedPackageVersionFlagErrors := []string{ "--package-version not supported in multi framework install", @@ -82,7 +28,7 @@ func TestInstallFrameworks(t *testing.T) { } for i, tt := range tests { - err := installFrameworks(tt.args) + err := installFrameworks(tt.args, options) if err != nil { receivedErrorList := []string{err.Error()} diff := compareSlice(receivedErrorList, tt.err) diff --git a/pkg/kudoctl/cmd/install_test.go b/pkg/kudoctl/cmd/install_test.go index 12ba15872..cfbdd71bf 100644 --- a/pkg/kudoctl/cmd/install_test.go +++ b/pkg/kudoctl/cmd/install_test.go @@ -27,19 +27,22 @@ func TestNewCmdInstallReturnsCmd(t *testing.T) { } var parameterTests = []struct { - flags []string + flags map[string]string + parameters []string errorMessage string }{ - {[]string{"foo"}, "a parameter without value worked"}, // 1 - {[]string{"bar="}, "a parameter with empty value worked"}, // 2 - {[]string{"foo=bar", "fiz="}, "one of many parameters with empty value worked"}, // 3 - {[]string{"foo", "bar"}, "multiple empty parameters worked"}, // 4 + {map[string]string{}, []string{"foo"}, "a parameter without value worked"}, // 1 + {map[string]string{}, []string{"bar="}, "a parameter with empty value worked"}, // 2 + {map[string]string{}, []string{"foo=bar", "fiz="}, "one of many parameters with empty value worked"}, // 3 + {map[string]string{}, []string{"foo", "bar"}, "multiple empty parameters worked"}, // 4 + {map[string]string{}, []string{}, "get flag: flag accessed but not defined: kubeconfig"}, // 5 + {map[string]string{"kubeconfig": "/tmp"}, []string{}, "could not check kubeconfig path: getting config failed: /tmp is a directory"}, // 6 } func TestTableNewInstallCmd_WithParameters(t *testing.T) { for _, test := range parameterTests { newCmdInstall := newInstallCmd() - for _, flag := range test.flags { + for _, flag := range test.parameters { newCmdInstall.Flags().Set("parameter", flag) } err := newCmdInstall.RunE(newCmdInstall, []string{}) diff --git a/pkg/kudoctl/util/check/check.go b/pkg/kudoctl/util/check/check.go index 7b9397df0..c66661451 100644 --- a/pkg/kudoctl/util/check/check.go +++ b/pkg/kudoctl/util/check/check.go @@ -6,7 +6,6 @@ import ( "os/user" "path/filepath" - "github.com/kudobuilder/kudo/pkg/kudoctl/util/vars" "github.com/pkg/errors" ) @@ -15,30 +14,25 @@ const ( ) // ValidateKubeConfigPath checks if the kubeconfig file exists. -func ValidateKubeConfigPath() error { - path, err := getKubeConfigLocation() - if err != nil { - return err - } - - vars.KubeConfigPath = path - stat, err := os.Stat(vars.KubeConfigPath) +func ValidateKubeConfigPath(path string) error { + stat, err := os.Stat(path) if os.IsNotExist(err) { return errors.Wrap(err, "failed to find kubeconfig file") } else if stat.IsDir() { - return errors.Wrap(fmt.Errorf("%v is a directory", vars.KubeConfigPath), "getting config failed") + return errors.Wrap(fmt.Errorf("%v is a directory", path), "getting config failed") } return nil } -func getKubeConfigLocation() (string, error) { - // if vars.KubeConfigPath is not specified, search for the default kubeconfig file under the $HOME/.kube/config. - if len(vars.KubeConfigPath) == 0 { +// KubeConfigLocationOrDefault returns provided kubeconfig location or default if empty +func KubeConfigLocationOrDefault(location string) (string, error) { + // if location is not specified, set the default kubeconfig file to $HOME/.kube/config. + if len(location) == 0 { usr, err := user.Current() if err != nil { return "", errors.Wrap(err, "failed to determine user's home dir") } return filepath.Join(usr.HomeDir, defaultKubeConfigPath), nil } - return vars.KubeConfigPath, nil + return location, nil } diff --git a/pkg/kudoctl/util/check/check_test.go b/pkg/kudoctl/util/check/check_test.go index 909cd8788..3aae39606 100644 --- a/pkg/kudoctl/util/check/check_test.go +++ b/pkg/kudoctl/util/check/check_test.go @@ -4,26 +4,24 @@ import ( "os/user" "path/filepath" "testing" - - "github.com/kudobuilder/kudo/pkg/kudoctl/util/vars" ) func TestKubeConfigPath(t *testing.T) { // first we test that the vars.KubeConfigPath is propagated correctly when resolving the path - vars.KubeConfigPath = "/tmp/;" - location, err := getKubeConfigLocation() + kubeConfigPath := "/tmp/;" + location, err := KubeConfigLocationOrDefault(kubeConfigPath) if err != nil { - t.Errorf("expected kubeconfig path '%v' to be propagated from vars, got error instead %v", vars.KubeConfigPath, err) + t.Errorf("expected kubeconfig path '%v' to be propagated from vars, got error instead %v", kubeConfigPath, err) } - if location != vars.KubeConfigPath { - t.Errorf("expected kubeconfig path '%v' to be propagated from vars, kubeconfig path instead resolved as %v", vars.KubeConfigPath, location) + if location != kubeConfigPath { + t.Errorf("expected kubeconfig path '%v' to be propagated from vars, kubeconfig path instead resolved as %v", kubeConfigPath, location) } // then we test that default is used when no path is provided in vars - vars.KubeConfigPath = "" + kubeConfigPath = "" usr, _ := user.Current() expectedPath := filepath.Join(usr.HomeDir, defaultKubeConfigPath) - location, err = getKubeConfigLocation() + location, err = KubeConfigLocationOrDefault(kubeConfigPath) if err != nil { t.Errorf("expected kubeconfig path '%v', got error instead %v", expectedPath, err) } diff --git a/pkg/kudoctl/util/kudo/kudo.go b/pkg/kudoctl/util/kudo/kudo.go index f327141c1..46b24fa6f 100644 --- a/pkg/kudoctl/util/kudo/kudo.go +++ b/pkg/kudoctl/util/kudo/kudo.go @@ -7,7 +7,6 @@ import ( "github.com/kudobuilder/kudo/pkg/apis/kudo/v1alpha1" "github.com/kudobuilder/kudo/pkg/client/clientset/versioned" - "github.com/kudobuilder/kudo/pkg/kudoctl/util/vars" "github.com/pkg/errors" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/tools/clientcmd" @@ -19,10 +18,10 @@ type Client struct { } // NewKudoClient creates new KUDO Client -func NewKudoClient() (*Client, error) { +func NewKudoClient(namespace, kubeConfigPath string) (*Client, error) { // use the current context in kubeconfig - config, err := clientcmd.BuildConfigFromFlags("", vars.KubeConfigPath) + config, err := clientcmd.BuildConfigFromFlags("", kubeConfigPath) if err != nil { return nil, err } @@ -36,19 +35,19 @@ func NewKudoClient() (*Client, error) { return nil, err } - _, err = kudoClientset.KudoV1alpha1().Frameworks(vars.Namespace).List(v1.ListOptions{}) + _, err = kudoClientset.KudoV1alpha1().Frameworks(namespace).List(v1.ListOptions{}) if err != nil { return nil, errors.WithMessage(err, "frameworks") } - _, err = kudoClientset.KudoV1alpha1().FrameworkVersions(vars.Namespace).List(v1.ListOptions{}) + _, err = kudoClientset.KudoV1alpha1().FrameworkVersions(namespace).List(v1.ListOptions{}) if err != nil { return nil, errors.WithMessage(err, "frameworkversions") } - _, err = kudoClientset.KudoV1alpha1().Instances(vars.Namespace).List(v1.ListOptions{}) + _, err = kudoClientset.KudoV1alpha1().Instances(namespace).List(v1.ListOptions{}) if err != nil { return nil, errors.WithMessage(err, "instances") } - _, err = kudoClientset.KudoV1alpha1().PlanExecutions(vars.Namespace).List(v1.ListOptions{}) + _, err = kudoClientset.KudoV1alpha1().PlanExecutions(namespace).List(v1.ListOptions{}) if err != nil { return nil, errors.WithMessage(err, "planexecutions") } @@ -59,20 +58,20 @@ func NewKudoClient() (*Client, error) { } // CRDsInstalled checks for essential CRDs of KUDO to be installed -func (c *Client) CRDsInstalled() error { - _, err := c.clientset.KudoV1alpha1().Frameworks(vars.Namespace).List(v1.ListOptions{}) +func (c *Client) CRDsInstalled(namespace string) error { + _, err := c.clientset.KudoV1alpha1().Frameworks(namespace).List(v1.ListOptions{}) if err != nil { return errors.WithMessage(err, "frameworks") } - _, err = c.clientset.KudoV1alpha1().FrameworkVersions(vars.Namespace).List(v1.ListOptions{}) + _, err = c.clientset.KudoV1alpha1().FrameworkVersions(namespace).List(v1.ListOptions{}) if err != nil { return errors.WithMessage(err, "frameworkversions") } - _, err = c.clientset.KudoV1alpha1().Instances(vars.Namespace).List(v1.ListOptions{}) + _, err = c.clientset.KudoV1alpha1().Instances(namespace).List(v1.ListOptions{}) if err != nil { return errors.WithMessage(err, "instances") } - _, err = c.clientset.KudoV1alpha1().PlanExecutions(vars.Namespace).List(v1.ListOptions{}) + _, err = c.clientset.KudoV1alpha1().PlanExecutions(namespace).List(v1.ListOptions{}) if err != nil { return errors.WithMessage(err, "planexecutions") } @@ -80,8 +79,8 @@ func (c *Client) CRDsInstalled() error { } // FrameworkExistsInCluster checks if a given Framework object is installed on the current k8s cluster -func (c *Client) FrameworkExistsInCluster(name string) bool { - framework, err := c.clientset.KudoV1alpha1().Frameworks(vars.Namespace).Get(name, v1.GetOptions{}) +func (c *Client) FrameworkExistsInCluster(name, namespace string) bool { + framework, err := c.clientset.KudoV1alpha1().Frameworks(namespace).Get(name, v1.GetOptions{}) if err != nil { return false } @@ -91,8 +90,8 @@ func (c *Client) FrameworkExistsInCluster(name string) bool { // AnyFrameworkVersionExistsInCluster checks if any FrameworkVersion object matches to the given Framework name // in the cluster -func (c *Client) AnyFrameworkVersionExistsInCluster(framework string) bool { - fv, err := c.clientset.KudoV1alpha1().FrameworkVersions(vars.Namespace).List(v1.ListOptions{}) +func (c *Client) AnyFrameworkVersionExistsInCluster(framework string, namespace string) bool { + fv, err := c.clientset.KudoV1alpha1().FrameworkVersions(namespace).List(v1.ListOptions{}) if err != nil { return false } @@ -128,8 +127,8 @@ func (c *Client) AnyFrameworkVersionExistsInCluster(framework string) bool { // controller-tools.k8s.io: "1.0" // framework: kafka // This function also just returns true if the Instance matches a specific FrameworkVersion of a Framework -func (c *Client) AnyInstanceExistsInCluster(name, version string) bool { - instances, err := c.clientset.KudoV1alpha1().Instances(vars.Namespace).List(v1.ListOptions{LabelSelector: "framework=" + name}) +func (c *Client) AnyInstanceExistsInCluster(name, namespace, version string) bool { + instances, err := c.clientset.KudoV1alpha1().Instances(namespace).List(v1.ListOptions{LabelSelector: "framework=" + name}) if err != nil { return false } @@ -156,8 +155,8 @@ func (c *Client) AnyInstanceExistsInCluster(name, version string) bool { // FrameworkVersionInClusterOutOfSync checks if any FrameworkVersion object matches a given Framework name and // if not it returns false. False means that for the given Framework the most recent official FrameworkVersion // is not installed in the cluster or an error occurred. -func (c *Client) FrameworkVersionInClusterOutOfSync(framework, mostRecentVersion string) bool { - fv, err := c.clientset.KudoV1alpha1().FrameworkVersions(vars.Namespace).List(v1.ListOptions{}) +func (c *Client) FrameworkVersionInClusterOutOfSync(framework, mostRecentVersion, namespace string) bool { + fv, err := c.clientset.KudoV1alpha1().FrameworkVersions(namespace).List(v1.ListOptions{}) if err != nil { return false } @@ -177,8 +176,8 @@ func (c *Client) FrameworkVersionInClusterOutOfSync(framework, mostRecentVersion } // InstallFrameworkObjToCluster expects a valid Framework obj to install -func (c *Client) InstallFrameworkObjToCluster(obj *v1alpha1.Framework) (*v1alpha1.Framework, error) { - createdObj, err := c.clientset.KudoV1alpha1().Frameworks(vars.Namespace).Create(obj) +func (c *Client) InstallFrameworkObjToCluster(obj *v1alpha1.Framework, namespace string) (*v1alpha1.Framework, error) { + createdObj, err := c.clientset.KudoV1alpha1().Frameworks(namespace).Create(obj) if err != nil { return nil, errors.WithMessage(err, "installing Framework") } @@ -186,8 +185,8 @@ func (c *Client) InstallFrameworkObjToCluster(obj *v1alpha1.Framework) (*v1alpha } // InstallFrameworkVersionObjToCluster expects a valid Framework obj to install -func (c *Client) InstallFrameworkVersionObjToCluster(obj *v1alpha1.FrameworkVersion) (*v1alpha1.FrameworkVersion, error) { - createdObj, err := c.clientset.KudoV1alpha1().FrameworkVersions(vars.Namespace).Create(obj) +func (c *Client) InstallFrameworkVersionObjToCluster(obj *v1alpha1.FrameworkVersion, namespace string) (*v1alpha1.FrameworkVersion, error) { + createdObj, err := c.clientset.KudoV1alpha1().FrameworkVersions(namespace).Create(obj) if err != nil { return nil, errors.WithMessage(err, "installing FrameworkVersion") } @@ -195,8 +194,8 @@ func (c *Client) InstallFrameworkVersionObjToCluster(obj *v1alpha1.FrameworkVers } // InstallInstanceObjToCluster expects a valid Instance obj to install -func (c *Client) InstallInstanceObjToCluster(obj *v1alpha1.Instance) (*v1alpha1.Instance, error) { - createdObj, err := c.clientset.KudoV1alpha1().Instances(vars.Namespace).Create(obj) +func (c *Client) InstallInstanceObjToCluster(obj *v1alpha1.Instance, namespace string) (*v1alpha1.Instance, error) { + createdObj, err := c.clientset.KudoV1alpha1().Instances(namespace).Create(obj) if err != nil { return nil, errors.WithMessage(err, "installing Instance") } diff --git a/pkg/kudoctl/util/kudo/kudo_test.go b/pkg/kudoctl/util/kudo/kudo_test.go index 2d409f013..45b1bf292 100644 --- a/pkg/kudoctl/util/kudo/kudo_test.go +++ b/pkg/kudoctl/util/kudo/kudo_test.go @@ -5,7 +5,6 @@ import ( "github.com/kudobuilder/kudo/pkg/apis/kudo/v1alpha1" "github.com/kudobuilder/kudo/pkg/client/clientset/versioned/fake" - "github.com/kudobuilder/kudo/pkg/kudoctl/util/vars" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -17,10 +16,6 @@ func newTestSimpleK2o() *Client { } func TestNewK2oClient(t *testing.T) { - - // For test case #1 - vars.KubeConfigPath = "" - tests := []struct { err string }{ @@ -29,7 +24,7 @@ func TestNewK2oClient(t *testing.T) { for _, tt := range tests { // Just interested in errors - _, err := NewKudoClient() + _, err := NewKudoClient("default", "") if err.Error() != tt.err { t.Errorf("non existing test:\nexpected: %v\n got: %v", tt.err, err.Error()) } @@ -38,7 +33,7 @@ func TestNewK2oClient(t *testing.T) { func TestK2oClient_CRDsInstalled(t *testing.T) { k2o := newTestSimpleK2o() - err := k2o.CRDsInstalled() + err := k2o.CRDsInstalled("default") if err != nil { t.Errorf("\nexpected: \n got: %v", err) } @@ -86,8 +81,7 @@ func TestK2oClient_FrameworkExistsInCluster(t *testing.T) { } // test if Framework exists in namespace - vars.Namespace = tt.getns - exist := k2o.FrameworkExistsInCluster("test") + exist := k2o.FrameworkExistsInCluster("test", tt.getns) if tt.bool != exist { t.Errorf("%d:\nexpected: %v\n got: %v", i+1, tt.bool, exist) @@ -131,8 +125,7 @@ func TestK2oClient_AnyFrameworkVersionExistsInCluster(t *testing.T) { k2o.clientset.KudoV1alpha1().FrameworkVersions(tt.createns).Create(tt.obj) // test if FrameworkVersion exists in namespace - vars.Namespace = tt.getns - exist := k2o.AnyFrameworkVersionExistsInCluster("test") + exist := k2o.AnyFrameworkVersionExistsInCluster("test", tt.getns) if tt.bool != exist { t.Errorf("%d:\nexpected: %v\n got: %v", i+1, tt.bool, exist) } @@ -202,8 +195,7 @@ func TestK2oClient_AnyInstanceExistsInCluster(t *testing.T) { k2o.clientset.KudoV1alpha1().Instances(tt.createns).Create(tt.obj) // test if FrameworkVersion exists in namespace - vars.Namespace = tt.getns - exist := k2o.AnyInstanceExistsInCluster("test", "1.0") + exist := k2o.AnyInstanceExistsInCluster("test", tt.getns, "1.0") if tt.bool != exist { t.Errorf("%d:\nexpected: %v\n got: %v", i+1, tt.bool, exist) } @@ -267,8 +259,7 @@ func TestK2oClient_FrameworkVersionInClusterOutOfSync(t *testing.T) { k2o.clientset.KudoV1alpha1().FrameworkVersions(tt.createns).Create(tt.obj) // test if FrameworkVersion exists in namespace - vars.Namespace = tt.getns - exist := k2o.FrameworkVersionInClusterOutOfSync("test", "1.0") + exist := k2o.FrameworkVersionInClusterOutOfSync("test", "1.0", tt.getns) if tt.bool != exist { t.Errorf("%d:\nexpected: %v\n got: %v", i+1, tt.bool, exist) } @@ -310,8 +301,7 @@ func TestK2oClient_InstallFrameworkObjToCluster(t *testing.T) { k2o.clientset.KudoV1alpha1().Frameworks(tt.createns).Create(tt.obj) // test if Framework exists in namespace - vars.Namespace = tt.createns - k2o.InstallFrameworkObjToCluster(tt.obj) + k2o.InstallFrameworkObjToCluster(tt.obj, tt.createns) _, err := k2o.clientset.KudoV1alpha1().Frameworks(tt.createns).Get(tt.name, metav1.GetOptions{}) if err != nil { @@ -358,8 +348,7 @@ func TestK2oClient_InstallFrameworkVersionObjToCluster(t *testing.T) { k2o.clientset.KudoV1alpha1().FrameworkVersions(tt.createns).Create(tt.obj) // test if Framework exists in namespace - vars.Namespace = tt.createns - k2o.InstallFrameworkVersionObjToCluster(tt.obj) + k2o.InstallFrameworkVersionObjToCluster(tt.obj, tt.createns) _, err := k2o.clientset.KudoV1alpha1().FrameworkVersions(tt.createns).Get(tt.name, metav1.GetOptions{}) if err != nil { @@ -406,8 +395,7 @@ func TestK2oClient_InstallInstanceObjToCluster(t *testing.T) { k2o.clientset.KudoV1alpha1().Instances(tt.createns).Create(tt.obj) // test if Framework exists in namespace - vars.Namespace = tt.createns - k2o.InstallInstanceObjToCluster(tt.obj) + k2o.InstallInstanceObjToCluster(tt.obj, tt.createns) _, err := k2o.clientset.KudoV1alpha1().Instances(tt.createns).Get(tt.name, metav1.GetOptions{}) if err != nil { diff --git a/pkg/kudoctl/util/vars/vars.go b/pkg/kudoctl/util/vars/vars.go deleted file mode 100644 index 8a1daa7dc..000000000 --- a/pkg/kudoctl/util/vars/vars.go +++ /dev/null @@ -1,14 +0,0 @@ -package vars - -// Variables for flags -var ( - AllDependencies bool - AutoApprove bool - Instance string - KubeConfigPath string - Namespace string - Parameter []string - PackageVersion string - StorageBucket string - StoragePrefix string -)