-
Notifications
You must be signed in to change notification settings - Fork 103
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
Refactoring of "get instances" CLI command #553
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
99dc398
refactoring get command
fabianbaier 5b5ca93
adding more unit tests for get.go and ListInstances to kudo.go includ…
fabianbaier a896702
make linters happy
fabianbaier 2bdb58e
addressed some review items
fabianbaier 17510eb
Merge branch 'master' into fb/get-instances-fix
fabianbaier ba70dd6
removing comment based on pr review
kensipe 9e64926
Merge branch 'master' into fb/get-instances-fix
kensipe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package get | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/kudobuilder/kudo/pkg/kudoctl/util/kudo" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/xlab/treeprint" | ||
"k8s.io/client-go/tools/clientcmd" | ||
|
||
"github.com/kudobuilder/kudo/pkg/kudoctl/util/check" | ||
) | ||
|
||
// Options defines configuration options for the get command | ||
type Options struct { | ||
KubeConfigPath string | ||
Namespace string | ||
} | ||
|
||
// DefaultOptions initializes the get command options to its defaults | ||
var DefaultOptions = &Options{ | ||
Namespace: "default", | ||
} | ||
|
||
// Run returns the errors associated with cmd env | ||
func Run(args []string, options *Options) error { | ||
|
||
err := validate(args, options) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
kc, err := kudo.NewClient(options.Namespace, options.KubeConfigPath) | ||
if err != nil { | ||
return errors.Wrap(err, "creating kudo client") | ||
} | ||
|
||
p, err := getInstances(kc, options) | ||
if err != nil { | ||
log.Printf("Error: %v", err) | ||
} | ||
tree := treeprint.New() | ||
|
||
for _, plan := range p { | ||
tree.AddBranch(plan) | ||
} | ||
fmt.Printf("List of current installed instances in namespace \"%s\":\n", options.Namespace) | ||
fmt.Println(tree.String()) | ||
return err | ||
} | ||
|
||
func validate(args []string, options *Options) error { | ||
if len(args) != 1 { | ||
return fmt.Errorf("expecting exactly one argument - \"instances\"") | ||
} | ||
|
||
if args[0] != "instances" { | ||
return fmt.Errorf("expecting \"instances\" and not \"%s\"", args[0]) | ||
} | ||
|
||
// If the $KUBECONFIG environment variable is set, use that | ||
if len(os.Getenv("KUBECONFIG")) > 0 { | ||
options.KubeConfigPath = os.Getenv("KUBECONFIG") | ||
} | ||
|
||
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") | ||
} | ||
_, err = clientcmd.BuildConfigFromFlags("", options.KubeConfigPath) | ||
if err != nil { | ||
return errors.Wrap(err, "getting config failed") | ||
} | ||
|
||
return nil | ||
|
||
} | ||
|
||
func getInstances(kc *kudo.Client, options *Options) ([]string, error) { | ||
|
||
instanceList, err := kc.ListInstances(options.Namespace) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "getting instances") | ||
} | ||
|
||
return instanceList, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package get | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/kudobuilder/kudo/pkg/apis/kudo/v1alpha1" | ||
"github.com/kudobuilder/kudo/pkg/client/clientset/versioned/fake" | ||
"github.com/kudobuilder/kudo/pkg/kudoctl/util/kudo" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestValidate(t *testing.T) { | ||
tests := []struct { | ||
arg []string | ||
opt Options | ||
err string | ||
}{ | ||
{nil, *DefaultOptions, "expecting exactly one argument - \"instances\""}, // 1 | ||
{[]string{"arg", "arg2"}, *DefaultOptions, "expecting exactly one argument - \"instances\""}, // 2 | ||
{[]string{}, *DefaultOptions, "expecting exactly one argument - \"instances\""}, // 3 | ||
{[]string{"somethingelse"}, *DefaultOptions, "expecting \"instances\" and not \"somethingelse\""}, // 4 | ||
} | ||
|
||
for _, tt := range tests { | ||
err := validate(tt.arg, DefaultOptions) | ||
if err != nil { | ||
if err.Error() != tt.err { | ||
t.Errorf("Expecting error message '%s' but got '%s'", tt.err, err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func newTestClient() *kudo.Client { | ||
return kudo.NewClientFromK8s(fake.NewSimpleClientset()) | ||
} | ||
|
||
func TestGetInstances(t *testing.T) { | ||
testInstance := &v1alpha1.Instance{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: "kudo.k8s.io/v1alpha1", | ||
Kind: "Instance", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{ | ||
"controller-tools.k8s.io": "1.0", | ||
"operator": "test", | ||
}, | ||
Name: "test", | ||
}, | ||
Spec: v1alpha1.InstanceSpec{ | ||
OperatorVersion: v1.ObjectReference{ | ||
Name: "test-1.0", | ||
}, | ||
}, | ||
} | ||
tests := []struct { | ||
arg []string | ||
opt Options | ||
err string | ||
instances []string | ||
}{ | ||
{nil, *DefaultOptions, "expecting exactly one argument - \"instances\"", nil}, // 1 | ||
{[]string{"arg", "arg2"}, *DefaultOptions, "expecting exactly one argument - \"instances\"", nil}, // 2 | ||
{[]string{}, *DefaultOptions, "expecting exactly one argument - \"instances\"", nil}, // 3 | ||
{[]string{"somethingelse"}, *DefaultOptions, "expecting \"instances\" and not \"somethingelse\"", nil}, // 4 | ||
{[]string{"instances"}, *DefaultOptions, "expecting \"instances\" and not \"somethingelse\"", []string{"test"}}, // 5 | ||
} | ||
|
||
for i, tt := range tests { | ||
kc := newTestClient() | ||
kc.InstallInstanceObjToCluster(testInstance, "default") | ||
instanceList, err := getInstances(kc, DefaultOptions) | ||
if err != nil { | ||
if err.Error() != tt.err { | ||
t.Errorf("%d: Expecting error message '%s' but got '%s'", i+1, tt.err, err) | ||
} | ||
} | ||
missing := compareSlice(tt.instances, instanceList) | ||
for _, m := range missing { | ||
t.Errorf("%d: Missed expected instance \"%v\"", i+1, m) | ||
} | ||
} | ||
} | ||
|
||
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) | ||
} | ||
} | ||
} | ||
return diff | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this copy-paste error? How is skipInstance relevant to get instances? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't call it that. More good intensions to make parts of the CLI following same patterns/look alike.