Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove possibility to install multiple packages #479

Merged
merged 5 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 28 additions & 41 deletions pkg/kudoctl/cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,18 @@ var DefaultOptions = &Options{
// Run returns the errors associated with cmd env
func Run(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)
err := validate(args, options)
if err != nil {
return err
}

err = installOperator(args[0], options)
return err
}

func validate(args []string, options *Options) error {
if len(args) != 1 {
return fmt.Errorf("expecting exactly one argument - name of the package or path to install")
}

// If the $KUBECONFIG environment variable is set, use that
Expand All @@ -51,48 +60,11 @@ func Run(cmd *cobra.Command, args []string, options *Options) error {
if err := check.ValidateKubeConfigPath(options.KubeConfigPath); err != nil {
return errors.WithMessage(err, "could not check kubeconfig path")
}

if err := installOperators(args, options); err != nil {
return errors.WithMessage(err, "could not install operator(s)")
}

return nil
}

// installOperators installs all operators specified as arguments into the cluster
func installOperators(args []string, options *Options) error {

if len(args) < 1 {
return fmt.Errorf("no argument provided")
}

if len(args) > 1 && options.PackageVersion != "" {
return fmt.Errorf("--package-version not supported in multi operator install")
}
repoConfig := repo.Default

// Initializing empty repo with given variables
r, err := repo.NewOperatorRepository(repoConfig)
if err != nil {
return errors.WithMessage(err, "could not build operator repository")
}

_, err = clientcmd.BuildConfigFromFlags("", options.KubeConfigPath)
if err != nil {
return errors.Wrap(err, "getting config failed")
}

kc, err := kudo.NewClient(options.Namespace, options.KubeConfigPath)
if err != nil {
return errors.Wrap(err, "creating kudo client")
}

for _, operatorName := range args {
err := installOperator(operatorName, r, kc, options)
if err != nil {
return err
}
}
return nil
}

Expand Down Expand Up @@ -122,7 +94,22 @@ func getPackageCRDs(name string, options *Options, repository repo.Repository) (
// installOperator is the umbrella for a single operator installation that gathers the business logic
// for a cluster and returns an error in case there is a problem
// TODO: needs testing
func installOperator(operatorArgument string, repository repo.Repository, kc *kudo.Client, options *Options) error {
func installOperator(operatorArgument string, options *Options) error {
repository, err := repo.NewOperatorRepository(repo.Default)
if err != nil {
return errors.WithMessage(err, "could not build operator repository")
}

_, err = clientcmd.BuildConfigFromFlags("", options.KubeConfigPath)
if err != nil {
return errors.Wrap(err, "getting config failed")
}

kc, err := kudo.NewClient(options.Namespace, options.KubeConfigPath)
if err != nil {
return errors.Wrap(err, "creating kudo client")
}

crds, err := getPackageCRDs(operatorArgument, options, repository)
if err != nil {
return errors.Wrapf(err, "failed to resolve package CRDs for operator: %s", operatorArgument)
Expand Down
63 changes: 10 additions & 53 deletions pkg/kudoctl/cmd/install/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,23 @@ import (
"testing"
)

func TestInstallOperators(t *testing.T) {

// For test case #1
expectedNoArgumentErrors := []string{
"no argument provided",
}

// For test case #2
options := DefaultOptions
options.PackageVersion = "0.0"
installCmdPackageVersionArgs := []string{"one", "two"}
expectedPackageVersionFlagErrors := []string{
"--package-version not supported in multi operator install",
}
func TestValidate(t *testing.T) {

tests := []struct {
args []string
err []string
arg []string
err string
}{
{nil, expectedNoArgumentErrors}, // 1
{installCmdPackageVersionArgs, expectedPackageVersionFlagErrors}, // 2
{nil, "expecting exactly one argument - name of the package or path to install"}, // 1
{[]string{"arg", "arg2"}, "expecting exactly one argument - name of the package or path to install"}, // 2
{[]string{}, "expecting exactly one argument - name of the package or path to install"}, // 3
}

for i, tt := range tests {
err := installOperators(tt.args, options)
for _, tt := range tests {
err := validate(tt.arg, DefaultOptions)
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 compareSlice(real, mock []string) []string {
lm := len(mock)

var diff []string

for _, rv := range real {
i := 0
j := 0
for _, mv := range mock {
i++
if rv == mv {
continue
}
if rv != mv {
j++
}
if lm <= j {
diff = append(diff, rv)
if err.Error() != tt.err {
t.Errorf("Expecting error message '%s' but got '%s'", tt.err, err)
}
}
}
return diff
}