Переглянути джерело

list namespaces to test cluster connection

Alexander Belanger 5 роки тому
батько
коміт
cb9954097a
3 змінених файлів з 128 додано та 16 видалено
  1. 91 0
      cli/cmd/cluster.go
  2. 37 0
      cli/cmd/config.go
  3. 0 16
      cli/cmd/connect/kubeconfig.go

+ 91 - 0
cli/cmd/cluster.go

@@ -0,0 +1,91 @@
+package cmd
+
+import (
+	"context"
+	"fmt"
+	"os"
+	"text/tabwriter"
+
+	"github.com/porter-dev/porter/cli/cmd/api"
+	"github.com/spf13/cobra"
+)
+
+// clusterCmd represents the "porter cluster" base command when called
+// without any subcommands
+var clusterCmd = &cobra.Command{
+	Use:   "cluster",
+	Short: "Commands that read from a connected cluster",
+}
+
+var listClusterNSCmd = &cobra.Command{
+	Use:   "namespace list",
+	Short: "Lists the namespaces in a cluster (used for testing connection)",
+	Run: func(cmd *cobra.Command, args []string) {
+		err := checkLoginAndRun(args, listNamespaces)
+
+		if err != nil {
+			os.Exit(1)
+		}
+	},
+}
+
+func init() {
+	rootCmd.AddCommand(clusterCmd)
+
+	clusterCmd.PersistentFlags().UintVar(
+		&clusterID,
+		"cluster-id",
+		getClusterID(),
+		"id of the cluster",
+	)
+
+	clusterCmd.AddCommand(listClusterNSCmd)
+}
+
+func listNamespaces(user *api.AuthCheckResponse, client *api.Client, args []string) error {
+	pID := getProjectID()
+	clusters, err := client.ListProjectClusters(context.Background(), pID)
+
+	if err != nil {
+		return err
+	}
+
+	// get the service account based on the cluster id
+	cID := getClusterID()
+	var saID uint = 0
+
+	for _, cluster := range clusters {
+		if cluster.ID == cID {
+			saID = cluster.ServiceAccountID
+		}
+	}
+
+	if saID == 0 {
+		return fmt.Errorf("could not find cluster with id %d", cID)
+	}
+
+	// get the list of namespaces
+	namespaces, err := client.GetK8sNamespaces(
+		context.Background(),
+		pID,
+		saID,
+		cID,
+	)
+
+	if err != nil {
+		return err
+	}
+
+	w := new(tabwriter.Writer)
+	w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
+
+	fmt.Fprintf(w, "%s\t%s\n", "NAME", "STATUS")
+
+	for _, namespace := range namespaces.Items {
+		fmt.Fprintf(w, "%s\t%s\n", namespace.Name, namespace.Status.Phase)
+	}
+
+	w.Flush()
+
+	return nil
+}

+ 37 - 0
cli/cmd/config.go

@@ -13,6 +13,7 @@ import (
 var (
 	host      string
 	projectID uint
+	clusterID uint
 )
 
 var configCmd = &cobra.Command{
@@ -41,6 +42,27 @@ var setProjectCmd = &cobra.Command{
 	},
 }
 
+var setClusterCmd = &cobra.Command{
+	Use:   "set-cluster [id]",
+	Args:  cobra.ExactArgs(1),
+	Short: "Saves the cluster id in the default configuration",
+	Run: func(cmd *cobra.Command, args []string) {
+		clusterID, err := strconv.ParseUint(args[0], 10, 64)
+
+		if err != nil {
+			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
+			os.Exit(1)
+		}
+
+		err = setCluster(uint(clusterID))
+
+		if err != nil {
+			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
+			os.Exit(1)
+		}
+	},
+}
+
 var setHostCmd = &cobra.Command{
 	Use:   "set-host [host]",
 	Args:  cobra.ExactArgs(1),
@@ -59,6 +81,7 @@ func init() {
 	rootCmd.AddCommand(configCmd)
 
 	configCmd.AddCommand(setProjectCmd)
+	configCmd.AddCommand(setClusterCmd)
 	configCmd.AddCommand(setHostCmd)
 }
 
@@ -68,6 +91,12 @@ func setProject(id uint) error {
 	return viper.WriteConfig()
 }
 
+func setCluster(id uint) error {
+	viper.Set("cluster", id)
+	color.New(color.FgGreen).Printf("Set the current cluster id as %d\n", id)
+	return viper.WriteConfig()
+}
+
 func setHost(host string) error {
 	viper.Set("host", host)
 	err := viper.WriteConfig()
@@ -83,6 +112,14 @@ func getHost() string {
 	return viper.GetString("host")
 }
 
+func getClusterID() uint {
+	if clusterID != 0 {
+		return clusterID
+	}
+
+	return viper.GetUint("cluster")
+}
+
 func getProjectID() uint {
 	if projectID != 0 {
 		return projectID

+ 0 - 16
cli/cmd/connect/kubeconfig.go

@@ -156,22 +156,6 @@ func Kubeconfig(
 
 		for _, cluster := range clusters {
 			color.New(color.FgGreen).Printf("created service account for cluster %s with id %d\n", cluster.Name, saID)
-
-			// sanity check to ensure it's working
-			// namespaces, err := client.GetK8sNamespaces(
-			// 	context.Background(),
-			// 	projectID,
-			// 	saID,
-			// 	cluster.ID,
-			// )
-
-			// if err != nil {
-			// 	return err
-			// }
-
-			// for _, ns := range namespaces.Items {
-			// 	fmt.Println(ns.ObjectMeta.GetName())
-			// }
 		}
 	}