|
|
|
@ -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 |
|
|
|
|
} |
|
|
|
|