Skip to content

Commit

Permalink
👌 replace k3d getk3stoken command by --token flag onto command `k…
Browse files Browse the repository at this point in the history
…3d get cluster`
  • Loading branch information
RouxAntoine committed Jun 3, 2020
1 parent ad06e90 commit 0862f11
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 208 deletions.
1 change: 0 additions & 1 deletion cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func NewCmdGet() *cobra.Command {
cmd.AddCommand(NewCmdGetCluster())
cmd.AddCommand(NewCmdGetNode())
cmd.AddCommand(NewCmdGetKubeconfig())
cmd.AddCommand(NewCmdGetClusterToken())

// done
return cmd
Expand Down
96 changes: 45 additions & 51 deletions cmd/get/getCluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ THE SOFTWARE.
package get

import (
"context"
"fmt"
"os"
"sort"
"strings"

k3cluster "github.com/rancher/k3d/pkg/cluster"
Expand All @@ -37,96 +37,90 @@ import (
"github.com/liggitt/tabwriter"
)

// TODO : deal with --all flag to manage differentiate started cluster and stopped cluster like `docker ps` and `docker ps -a`
type clusterFlags struct {
noHeader bool
token bool
}

// NewCmdGetCluster returns a new cobra command
func NewCmdGetCluster() *cobra.Command {

clusterFlags := clusterFlags{}

// create new command
cmd := &cobra.Command{
Use: "cluster [NAME [NAME...]]",
Aliases: []string{"clusters"},
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 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
for _, cluster := range clusters {
found, err := k3cluster.GetCluster(cmd.Context(), runtimes.SelectedRuntime, cluster)
if err != nil {
log.Fatalln(err)
}
existingClusters = append(existingClusters, found)
}
}
// print existing clusters
printClusters(existingClusters, headersOff)
clusters := buildClusterList(cmd.Context(), args)
PrintClusters(clusters, clusterFlags)
},
}

// add flags
cmd.Flags().Bool("no-headers", false, "Disable headers")
cmd.Flags().BoolVar(&clusterFlags.noHeader, "no-headers", false, "Disable headers")
cmd.Flags().BoolVar(&clusterFlags.token, "token", false, "Print k3s cluster token")

// add subcommands

// done
return cmd
}

func parseGetClusterCmd(cmd *cobra.Command, args []string) ([]*k3d.Cluster, bool) {
func buildClusterList(ctx context.Context, args []string) []*k3d.Cluster {
var clusters []*k3d.Cluster
var err error

// --no-headers
headersOff, err := cmd.Flags().GetBool("no-headers")
if err != nil {
log.Fatalln(err)
}

// Args = cluster name
if len(args) == 0 {
return nil, headersOff
}

clusters := []*k3d.Cluster{}
for _, name := range args {
clusters = append(clusters, &k3d.Cluster{Name: name})
// cluster name not specified : get all clusters
clusters, err = k3cluster.GetClusters(ctx, runtimes.SelectedRuntime)
if err != nil {
log.Fatalln(err)
}
} else {
for _, clusterName := range args {
// cluster name specified : get specific cluster
retrievedCluster, err := k3cluster.GetCluster(ctx, runtimes.SelectedRuntime, &k3d.Cluster{Name: clusterName})
if err != nil {
log.Fatalln(err)
}
clusters = append(clusters, retrievedCluster)
}
}

return clusters, headersOff
return clusters
}

func printClusters(clusters []*k3d.Cluster, headersOff bool) {
// PrintPrintClusters : display list of cluster
func PrintClusters(clusters []*k3d.Cluster, flags clusterFlags) {

tabwriter := tabwriter.NewWriter(os.Stdout, 6, 4, 3, ' ', tabwriter.RememberWidths)
defer tabwriter.Flush()

if !headersOff {
if !flags.noHeader {
headers := []string{"NAME", "MASTERS", "WORKERS"} // TODO: getCluster: add status column
if flags.token {
headers = append(headers, "TOKEN")
}
_, err := fmt.Fprintf(tabwriter, "%s\n", strings.Join(headers, "\t"))
if err != nil {
log.Fatalln("Failed to print headers")
}
}

sort.Slice(clusters, func(i, j int) bool {
return clusters[i].Name < clusters[j].Name
})
k3cluster.SortClusters(clusters)

for _, cluster := range clusters {
masterCount := 0
workerCount := 0
for _, node := range cluster.Nodes {
if node.Role == k3d.MasterRole {
masterCount++
} else if node.Role == k3d.WorkerRole {
workerCount++
}
masterCount := cluster.MasterCount()
workerCount := cluster.WorkerCount()

if flags.token {
fmt.Fprintf(tabwriter, "%s\t%d\t%d\t%s\n", cluster.Name, masterCount, workerCount, cluster.Token)
} else {
fmt.Fprintf(tabwriter, "%s\t%d\t%d\n", cluster.Name, masterCount, workerCount)
}
fmt.Fprintf(tabwriter, "%s\t%d\t%d\n", cluster.Name, masterCount, workerCount)
}
}
111 changes: 0 additions & 111 deletions cmd/get/getClusterToken.go

This file was deleted.

44 changes: 0 additions & 44 deletions cmd/util/args.go

This file was deleted.

9 changes: 9 additions & 0 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"bytes"
"context"
"fmt"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -591,3 +592,11 @@ func StopCluster(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Cluste
}
return nil
}

// SortClusters : in place sort cluster list by cluster name alphabetical order
func SortClusters(clusters []*k3d.Cluster) []*k3d.Cluster {
sort.Slice(clusters, func(i, j int) bool {
return clusters[i].Name < clusters[j].Name
})
return clusters
}
21 changes: 21 additions & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,27 @@ type Cluster struct {
ImageVolume string `yaml:"image_volume" json:"imageVolume,omitempty"`
}

// MasterCount return number of master node into cluster
func (c *Cluster) MasterCount() int {
masterCount := 0
for _, node := range c.Nodes {
if node.Role == MasterRole {
masterCount++
}
}
return masterCount
}
// WorkerCount return number of worker node into cluster
func (c *Cluster) WorkerCount() int {
workerCount := 0
for _, node := range c.Nodes {
if node.Role == WorkerRole {
workerCount++
}
}
return workerCount
}

// Node describes a k3d node
type Node struct {
Name string `yaml:"name" json:"name,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion tests/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,5 @@ check_volume_exists() {

check_cluster_token_exist() {
[ -n "$EXE" ] || abort "EXE is not defined"
$EXE get k3stoken "$1" >/dev/null 2>&1
$EXE get cluster "$1" --token | grep "TOKEN" >/dev/null 2>&1
}

0 comments on commit 0862f11

Please sign in to comment.