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

[Enhancement] get: allow multiple or no names for getCluster/getNodes #260

Merged
merged 1 commit into from
Jun 3, 2020
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
29 changes: 17 additions & 12 deletions cmd/get/getCluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"sort"
"strings"

"github.com/rancher/k3d/pkg/cluster"
k3cluster "github.com/rancher/k3d/pkg/cluster"
"github.com/rancher/k3d/pkg/runtimes"
k3d "github.com/rancher/k3d/pkg/types"
"github.com/spf13/cobra"
Expand All @@ -44,24 +44,26 @@ func NewCmdGetCluster() *cobra.Command {
cmd := &cobra.Command{
Use: "cluster [NAME [NAME...]]",
Aliases: []string{"clusters"},
Short: "Get cluster",
Long: `Get cluster.`,
Short: "Get cluster(s)",
Long: `Get cluster(s).`,
Args: cobra.MinimumNArgs(0), // 0 or more; 0 = all
Run: func(cmd *cobra.Command, args []string) {
clusters, headersOff := parseGetClusterCmd(cmd, args)
var existingClusters []*k3d.Cluster
if clusters == nil { // Option a) no cluster name specified -> get all clusters
found, err := cluster.GetClusters(cmd.Context(), runtimes.SelectedRuntime)
if len(clusters) == 0 { // Option a) no cluster name specified -> get all clusters
found, err := k3cluster.GetClusters(cmd.Context(), runtimes.SelectedRuntime)
if err != nil {
log.Fatalln(err)
}
existingClusters = append(existingClusters, found...)
} else { // Option b) cluster name specified -> get specific cluster
found, err := cluster.GetCluster(cmd.Context(), runtimes.SelectedRuntime, clusters)
if err != nil {
log.Fatalln(err)
for _, cluster := range clusters {
found, err := k3cluster.GetCluster(cmd.Context(), runtimes.SelectedRuntime, cluster)
if err != nil {
log.Fatalln(err)
}
existingClusters = append(existingClusters, found)
}
existingClusters = append(existingClusters, found)
}
// print existing clusters
printClusters(existingClusters, headersOff)
Expand All @@ -77,7 +79,7 @@ func NewCmdGetCluster() *cobra.Command {
return cmd
}

func parseGetClusterCmd(cmd *cobra.Command, args []string) (*k3d.Cluster, bool) {
func parseGetClusterCmd(cmd *cobra.Command, args []string) ([]*k3d.Cluster, bool) {

// --no-headers
headersOff, err := cmd.Flags().GetBool("no-headers")
Expand All @@ -90,9 +92,12 @@ func parseGetClusterCmd(cmd *cobra.Command, args []string) (*k3d.Cluster, bool)
return nil, headersOff
}

cluster := &k3d.Cluster{Name: args[0]}
clusters := []*k3d.Cluster{}
for _, name := range args {
clusters = append(clusters, &k3d.Cluster{Name: name})
}

return cluster, headersOff
return clusters, headersOff
}

func printClusters(clusters []*k3d.Cluster, headersOff bool) {
Expand Down
30 changes: 18 additions & 12 deletions cmd/get/getNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,28 @@ func NewCmdGetNode() *cobra.Command {

// create new command
cmd := &cobra.Command{
Use: "node NAME", // TODO: getNode: allow one or more names or --all flag
Short: "Get node",
Use: "node [NAME [NAME...]]",
Aliases: []string{"nodes"},
Long: `Get node.`,
Short: "Get node(s)",
Long: `Get node(s).`,
Args: cobra.MinimumNArgs(0), // 0 or more; 0 = all
Run: func(cmd *cobra.Command, args []string) {
node, headersOff := parseGetNodeCmd(cmd, args)
nodes, headersOff := parseGetNodeCmd(cmd, args)
var existingNodes []*k3d.Node
if node == nil { // Option a) no name specified -> get all nodes
if len(nodes) == 0 { // Option a) no name specified -> get all nodes
found, err := cluster.GetNodes(cmd.Context(), runtimes.SelectedRuntime)
if err != nil {
log.Fatalln(err)
}
existingNodes = append(existingNodes, found...)
} else { // Option b) cluster name specified -> get specific cluster
found, err := cluster.GetNode(cmd.Context(), runtimes.SelectedRuntime, node)
if err != nil {
log.Fatalln(err)
for _, node := range nodes {
found, err := cluster.GetNode(cmd.Context(), runtimes.SelectedRuntime, node)
if err != nil {
log.Fatalln(err)
}
existingNodes = append(existingNodes, found)
}
existingNodes = append(existingNodes, found)
}
// print existing clusters
printNodes(existingNodes, headersOff)
Expand All @@ -75,7 +78,7 @@ func NewCmdGetNode() *cobra.Command {
return cmd
}

func parseGetNodeCmd(cmd *cobra.Command, args []string) (*k3d.Node, bool) {
func parseGetNodeCmd(cmd *cobra.Command, args []string) ([]*k3d.Node, bool) {
// --no-headers
headersOff, err := cmd.Flags().GetBool("no-headers")
if err != nil {
Expand All @@ -87,9 +90,12 @@ func parseGetNodeCmd(cmd *cobra.Command, args []string) (*k3d.Node, bool) {
return nil, headersOff
}

node := &k3d.Node{Name: args[0]} // TODO: validate name first?
nodes := []*k3d.Node{}
for _, name := range args {
nodes = append(nodes, &k3d.Node{Name: name})
}

return node, headersOff
return nodes, headersOff
}

func printNodes(nodes []*k3d.Node, headersOff bool) {
Expand Down
10 changes: 10 additions & 0 deletions tests/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ check_clusters() {
return 0
}

check_cluster_count() {
expectedClusterCount=$1
actualClusterCount=$($EXE get clusters --no-headers | wc -l)
if [[ $actualClusterCount != $expectedClusterCount ]]; then
failed "incorrect number of clusters available: $actualClusterCount != $expectedClusterCount"
return 1
fi
return 0
}

# check_multi_node verifies that a cluster runs with an expected number of nodes
check_multi_node() {
cluster=$1
Expand Down
3 changes: 3 additions & 0 deletions tests/test_basic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ info "Creating two clusters..."
$EXE create cluster c1 --wait --timeout 60s --api-port 6443 || failed "could not create cluster c1"
$EXE create cluster c2 --wait --timeout 60s --api-port 6444 || failed "could not create cluster c2"

info "Checking that we can get both clusters..."
check_cluster_count 2

info "Checking we have access to both clusters..."
check_clusters "c1" "c2" || failed "error checking cluster"

Expand Down