diff --git a/internal/cluster/cluster.go b/internal/cluster/cluster.go index 83b220adf..9c4674b19 100644 --- a/internal/cluster/cluster.go +++ b/internal/cluster/cluster.go @@ -155,3 +155,43 @@ func GetCluster(clusterName string) (apiTypes.Cluster, error) { return cluster, nil } + +func GetClusters() ([]apiTypes.Cluster, error) { + customTransport := http.DefaultTransport.(*http.Transport).Clone() + customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} + httpClient := http.Client{Transport: customTransport} + + clusters := []apiTypes.Cluster{} + req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/api/proxy?url=/cluster", ConsoleIngresUrl), nil) + if err != nil { + log.Info().Msgf("error %s", err) + return clusters, err + } + req.Header.Add("Content-Type", "application/json") + req.Header.Add("Accept", "application/json") + + res, err := httpClient.Do(req) + if err != nil { + log.Info().Msgf("error %s", err) + return clusters, err + } + + if res.StatusCode != http.StatusOK { + log.Info().Msgf("unable to get clusters %s, continuing", res.Status) + return clusters, err + } + + body, err := io.ReadAll(res.Body) + if err != nil { + log.Info().Msgf("unable to get clusters %s", err) + return clusters, err + } + + err = json.Unmarshal(body, &clusters) + if err != nil { + log.Info().Msgf("unable to cast clusters object %s", err) + return clusters, err + } + + return clusters, nil +} diff --git a/internal/launch/cmd.go b/internal/launch/cmd.go index 7159fa45b..09577b69d 100644 --- a/internal/launch/cmd.go +++ b/internal/launch/cmd.go @@ -18,6 +18,7 @@ import ( "strings" "time" + "github.com/kubefirst/kubefirst/internal/cluster" "github.com/kubefirst/kubefirst/internal/helm" k3dint "github.com/kubefirst/kubefirst/internal/k3d" "github.com/kubefirst/kubefirst/internal/progress" @@ -623,72 +624,11 @@ func Down(inCluster bool) { // ListClusters makes a request to the console API to list created clusters func ListClusters() { - homeDir, err := os.UserHomeDir() - if err != nil { - log.Fatal().Msgf("something went wrong getting home path: %s", err) - } - - dir := fmt.Sprintf("%s/.k1/%s", homeDir, consoleClusterName) - if _, err := os.Stat(dir); os.IsNotExist(err) { - log.Info().Msgf(fmt.Sprintf("unable to list clusters - cluster %s directory does not exist", dir)) - } - - // Port forward to API - kubeconfigPath := fmt.Sprintf("%s/.k1/%s/kubeconfig", homeDir, consoleClusterName) - if _, err := os.Stat(dir); os.IsNotExist(err) { - log.Info().Msgf("unable to list clusters - kubeconfig file does not exist") - } - - kcfg := k8s.CreateKubeConfig(false, kubeconfigPath) - pods, err := kcfg.Clientset.CoreV1().Pods("").List(context.Background(), metav1.ListOptions{ - LabelSelector: "app.kubernetes.io/name=kubefirst-api", - Limit: 1, - }) - if err != nil { - log.Fatal().Msgf("could not find api pod: %s", err) - } - - randPort := rand.Intn(65535-65000) + 65000 - apiStopChannel := make(chan struct{}, 1) - defer func() { - close(apiStopChannel) - }() - k8s.OpenPortForwardPodWrapper( - kcfg.Clientset, - kcfg.RestConfig, - pods.Items[0].ObjectMeta.Name, - "kubefirst", - 8081, - randPort, - apiStopChannel, - ) - - // Get lister of clusters from API - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://localhost:%v/api/v1/cluster", randPort), nil) - if err != nil { - log.Fatal().Msgf("error creating request to api: %s", err) - } - req.Header.Set("Content-Type", "application/json") - res, getErr := httpClient.Do(req) - if getErr != nil { - log.Fatal().Msgf("error during api get call: %s", getErr) - } - if res.Body != nil { - defer res.Body.Close() - } - body, err := ioutil.ReadAll(res.Body) - if err != nil { - log.Fatal().Msgf("error reading api response: %s", err) - } - - var objMap []map[string]interface{} - if err := json.Unmarshal(body, &objMap); err != nil { - log.Fatal().Msgf("error unmarshaling api response: %s", err) - } + clusters, err := cluster.GetClusters() - err = displayFormattedClusterInfo(objMap) + err = displayFormattedClusterInfo(clusters) if err != nil { - log.Fatal().Msgf("error printing cluster list: %s", err) + progress.Error(fmt.Sprintf("error printing cluster list: %s", err)) } } diff --git a/internal/launch/utils.go b/internal/launch/utils.go index e7b13dc00..464d12803 100644 --- a/internal/launch/utils.go +++ b/internal/launch/utils.go @@ -8,29 +8,30 @@ package launch import ( "fmt" - "os" - "text/tabwriter" + + "github.com/kubefirst/kubefirst-api/pkg/types" + "github.com/kubefirst/kubefirst/internal/progress" ) // displayFormattedClusterInfo uses tabwriter to pretty print information on clusters using // the specified formatting -func displayFormattedClusterInfo(clusters []map[string]interface{}) error { - // A friendly warning before we proceed - writer := tabwriter.NewWriter(os.Stdout, 0, 8, 2, '\t', tabwriter.AlignRight) - fmt.Fprintln(writer, "NAME\tCREATED AT\tSTATUS\tTYPE\tPROVIDER") +func displayFormattedClusterInfo(clusters []types.Cluster) error { + header := ` +| NAME | CREATED AT | STATUS | TYPE | PROVIDER | +| --- | --- | --- | --- | --- | + ` + content := "" for _, cluster := range clusters { - fmt.Fprintf(writer, "%s\t%s\t%s\t%s\t%s\n", - cluster["cluster_name"], - cluster["creation_timestamp"], - cluster["status"], - cluster["cluster_type"], - cluster["cloud_provider"], + content = content + fmt.Sprintf("|%s|%s|%s|%s|%s\n", + cluster.ClusterName, + cluster.CreationTimestamp, + cluster.Status, + cluster.ClusterType, + cluster.CloudProvider, ) } - err := writer.Flush() - if err != nil { - return fmt.Errorf("error closing buffer: %s", err) - } + + progress.Success(header + content) return nil }