Refactor getClusters() to make the API easier to use

Simplify the caller of this function in the code base. Also remove
getCluster() since there is no caller of this function any more.
pull/47/head
Andy Zhou 5 years ago
parent dbc93f6818
commit b48debb176
  1. 49
      cli/commands.go
  2. 27
      cli/config.go

@ -208,25 +208,11 @@ kubectl cluster-info`, os.Args[0], c.String("name"))
// DeleteCluster removes the containers belonging to a cluster and its local directory
func DeleteCluster(c *cli.Context) error {
clusters, err := getClusters(c.Bool("all"), c.String("name"))
// operate on one or all clusters
clusters := make(map[string]cluster)
if !c.Bool("all") {
cluster, err := getCluster(c.String("name"))
if err != nil {
return err
}
clusters[c.String("name")] = cluster
} else {
clusterMap, err := getClusters()
if err != nil {
return fmt.Errorf("ERROR: `--all` specified, but no clusters were found\n%+v", err)
}
// copy clusterMap
for k, v := range clusterMap {
clusters[k] = v
}
}
// remove clusters one by one instead of appending all names to the docker command
// this allows for more granular error handling and logging
@ -260,25 +246,11 @@ func DeleteCluster(c *cli.Context) error {
// StopCluster stops a running cluster container (restartable)
func StopCluster(c *cli.Context) error {
clusters, err := getClusters(c.Bool("all"), c.String("name"))
// operate on one or all clusters
clusters := make(map[string]cluster)
if !c.Bool("all") {
cluster, err := getCluster(c.String("name"))
if err != nil {
return err
}
clusters[c.String("name")] = cluster
} else {
clusterMap, err := getClusters()
if err != nil {
return fmt.Errorf("ERROR: `--all` specified, but no clusters were found\n%+v", err)
}
// copy clusterMap
for k, v := range clusterMap {
clusters[k] = v
}
}
ctx := context.Background()
docker, err := client.NewEnvClient()
@ -312,24 +284,11 @@ func StopCluster(c *cli.Context) error {
// StartCluster starts a stopped cluster container
func StartCluster(c *cli.Context) error {
// operate on one or all clusters
clusters := make(map[string]cluster)
if !c.Bool("all") {
cluster, err := getCluster(c.String("name"))
clusters, err := getClusters(c.Bool("all"), c.String("name"))
if err != nil {
return err
}
clusters[c.String("name")] = cluster
} else {
clusterMap, err := getClusters()
if err != nil {
return fmt.Errorf("ERROR: `--all` specified, but no clusters were found\n%+v", err)
}
// copy clusterMap
for k, v := range clusterMap {
clusters[k] = v
}
}
ctx := context.Background()
docker, err := client.NewEnvClient()

@ -87,7 +87,7 @@ func getClusterDir(name string) (string, error) {
// printClusters prints the names of existing clusters
func printClusters(all bool) {
clusters, err := getClusters()
clusters, err := getClusters(true, "")
if err != nil {
log.Fatalf("ERROR: Couldn't list clusters\n%+v", err)
}
@ -100,7 +100,7 @@ func printClusters(all bool) {
table.SetAlignment(tablewriter.ALIGN_CENTER)
table.SetHeader([]string{"NAME", "IMAGE", "STATUS", "WORKERS"})
tableEmpty := true;
tableEmpty := true
for _, cluster := range clusters {
workersRunning := 0
@ -123,7 +123,7 @@ func printClusters(all bool) {
}
// Classify cluster state: Running, Stopped or Abnormal
func getClusterStatus(server types.Container, workers []types.Container) (string) {
func getClusterStatus(server types.Container, workers []types.Container) string {
// The cluster is in the abnromal state when server state and the worker
// states don't agree.
for _, w := range workers {
@ -138,12 +138,14 @@ func getClusterStatus(server types.Container, workers []types.Container) (string
return "stopped"
}
return server.State
}
// getClusters uses the docker API to get existing clusters and compares that with the list of cluster directories
func getClusters() (map[string]cluster, error) {
// When 'all' is true, 'cluster' contains all clusters found from the docker daemon
// When 'all' is false, 'cluster' contains up to one cluster whose name matches 'name'. 'cluster' can
// be empty if no matching cluster is found.
func getClusters(all bool, name string) (map[string]cluster, error) {
ctx := context.Background()
docker, err := client.NewEnvClient()
if err != nil {
@ -172,9 +174,15 @@ func getClusters() (map[string]cluster, error) {
// for all servers created by k3d, get workers and cluster information
for _, server := range k3dServers {
filters.Add("label", fmt.Sprintf("cluster=%s", server.Labels["cluster"]))
clusterName := server.Labels["cluster"]
// Skip the cluster if we don't want all of them, and
// the cluster name does not match.
if all || name == clusterName {
// Add the cluster
filters.Add("label", fmt.Sprintf("cluster=%s", clusterName))
// get workers
workers, err := docker.ContainerList(ctx, types.ContainerListOptions{
All: true,
@ -200,12 +208,7 @@ func getClusters() (map[string]cluster, error) {
// clear label filters before searching for next cluster
filters.Del("label", fmt.Sprintf("cluster=%s", clusterName))
}
return clusters, nil
}
// getCluster creates a cluster struct with populated information fields
func getCluster(name string) (cluster, error) {
clusters, err := getClusters()
return clusters[name], err
return clusters, nil
}

Loading…
Cancel
Save