package run import ( "context" "fmt" "log" "os" "path" "strconv" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/client" "github.com/mitchellh/go-homedir" "github.com/olekukonko/tablewriter" ) const ( defaultContainerNamePrefix = "k3d" ) type cluster struct { name string image string status string serverPorts []string server types.Container workers []types.Container } // GetContainerName generates the container names func GetContainerName(role, clusterName string, postfix int) string { if postfix >= 0 { return fmt.Sprintf("%s-%s-%s-%d", defaultContainerNamePrefix, clusterName, role, postfix) } return fmt.Sprintf("%s-%s-%s", defaultContainerNamePrefix, clusterName, role) } // GetAllContainerNames returns a list of all containernames that will be created func GetAllContainerNames(clusterName string, serverCount, workerCount int) []string { names := []string{} for postfix := 0; postfix < serverCount; postfix++ { names = append(names, GetContainerName("server", clusterName, postfix)) } for postfix := 0; postfix < workerCount; postfix++ { names = append(names, GetContainerName("worker", clusterName, postfix)) } return names } // createDirIfNotExists checks for the existence of a directory and creates it along with all required parents if not. // It returns an error if the directory (or parents) couldn't be created and nil if it worked fine or if the path already exists. func createDirIfNotExists(path string) error { if _, err := os.Stat(path); os.IsNotExist(err) { return os.MkdirAll(path, os.ModePerm) } return nil } // createClusterDir creates a directory with the cluster name under $HOME/.config/k3d/. // The cluster directory will be used e.g. to store the kubeconfig file. func createClusterDir(name string) { clusterPath, _ := getClusterDir(name) if err := createDirIfNotExists(clusterPath); err != nil { log.Fatalf("ERROR: couldn't create cluster directory [%s] -> %+v", clusterPath, err) } } // deleteClusterDir contrary to createClusterDir, this deletes the cluster directory under $HOME/.config/k3d/ func deleteClusterDir(name string) { clusterPath, _ := getClusterDir(name) if err := os.RemoveAll(clusterPath); err != nil { log.Printf("WARNING: couldn't delete cluster directory [%s]. You might want to delete it manually.", clusterPath) } } // getClusterDir returns the path to the cluster directory which is $HOME/.config/k3d/ func getClusterDir(name string) (string, error) { homeDir, err := homedir.Dir() if err != nil { log.Printf("ERROR: Couldn't get user's home directory") return "", err } return path.Join(homeDir, ".config", "k3d", name), nil } // printClusters prints the names of existing clusters func printClusters(all bool) { clusters, err := getClusters() if err != nil { log.Fatalf("ERROR: Couldn't list clusters\n%+v", err) } if len(clusters) == 0 { log.Printf("No clusters found!") return } table := tablewriter.NewWriter(os.Stdout) table.SetAlignment(tablewriter.ALIGN_CENTER) table.SetHeader([]string{"NAME", "IMAGE", "STATUS", "WORKERS"}) tableEmpty := true; for _, cluster := range clusters { workersRunning := 0 for _, worker := range cluster.workers { if worker.State == "running" { workersRunning++ } } workerData := fmt.Sprintf("%d/%d", workersRunning, len(cluster.workers)) clusterData := []string{cluster.name, cluster.image, cluster.status, workerData} if cluster.status == "running" || all { table.Append(clusterData) tableEmpty = false } } if !tableEmpty { table.Render() } } // Classify cluster state: Running, Stopped or Abnormal 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 { if w.State != server.State { return "unhealthy" } } switch server.State { case "exited": // All containers in this state are most likely // as the result of running the "k3d stop" command. 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) { ctx := context.Background() docker, err := client.NewEnvClient() if err != nil { return nil, fmt.Errorf("ERROR: couldn't create docker client\n%+v", err) } // Prepare docker label filters filters := filters.NewArgs() filters.Add("label", "app=k3d") filters.Add("label", "component=server") // get all servers created by k3d k3dServers, err := docker.ContainerList(ctx, types.ContainerListOptions{ All: true, Filters: filters, }) if err != nil { return nil, fmt.Errorf("WARNING: couldn't list server containers\n%+v", err) } clusters := make(map[string]cluster) // don't filter for servers but for workers now filters.Del("label", "component=server") filters.Add("label", "component=worker") // 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"] // get workers workers, err := docker.ContainerList(ctx, types.ContainerListOptions{ All: true, Filters: filters, }) if err != nil { log.Printf("WARNING: couldn't get worker containers for cluster %s\n%+v", clusterName, err) } // save cluster information serverPorts := []string{} for _, port := range server.Ports { serverPorts = append(serverPorts, strconv.Itoa(int(port.PublicPort))) } clusters[clusterName] = cluster{ name: clusterName, image: server.Image, status: getClusterStatus(server, workers), serverPorts: serverPorts, server: server, workers: workers, } // 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 }