diff --git a/pkg/cmd/gtctl/cluster/list/list.go b/pkg/cmd/gtctl/cluster/list/list.go index b9519197..007e1913 100644 --- a/pkg/cmd/gtctl/cluster/list/list.go +++ b/pkg/cmd/gtctl/cluster/list/list.go @@ -17,45 +17,32 @@ package list import ( "context" "fmt" + "os" greptimedbclusterv1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1" + "github.com/olekukonko/tablewriter" "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/api/errors" + "github.com/GreptimeTeam/gtctl/pkg/deployer" "github.com/GreptimeTeam/gtctl/pkg/deployer/k8s" "github.com/GreptimeTeam/gtctl/pkg/logger" ) func NewListClustersCommand(l logger.Logger) *cobra.Command { + table := tablewriter.NewWriter(os.Stdout) + configClustersTableView(table) + cmd := &cobra.Command{ Use: "list", Short: "List all GreptimeDB clusters", Long: `List all GreptimeDB clusters`, RunE: func(cmd *cobra.Command, args []string) error { - k8sDeployer, err := k8s.NewDeployer(l) - if err != nil { - return err - } + ctx := context.Background() - ctx := context.TODO() - clusters, err := k8sDeployer.ListGreptimeDBClusters(ctx, nil) - if err != nil && !errors.IsNotFound(err) { + if err := listClustersFromKubernetes(ctx, l, table); err != nil { return err } - if errors.IsNotFound(err) || (clusters != nil && len(clusters) == 0) { - l.Error("clusters not found\n") - return nil - } - - // TODO(zyy17): more human friendly output format. - for _, cluster := range clusters { - rawCluster, ok := cluster.Raw.(*greptimedbclusterv1alpha1.GreptimeDBCluster) - if !ok { - return fmt.Errorf("invalid cluster type") - } - l.V(0).Infof("Cluster '%s' in '%s' namespace is running, create at %s\n", - rawCluster.Name, rawCluster.Namespace, rawCluster.CreationTimestamp) - } return nil }, @@ -63,3 +50,59 @@ func NewListClustersCommand(l logger.Logger) *cobra.Command { return cmd } + +func listClustersFromKubernetes(ctx context.Context, l logger.Logger, table *tablewriter.Table) error { + k8sDeployer, err := k8s.NewDeployer(l) + if err != nil { + return err + } + + clusters, err := k8sDeployer.ListGreptimeDBClusters(ctx, nil) + if err != nil && !errors.IsNotFound(err) { + return err + } + if errors.IsNotFound(err) || (clusters != nil && len(clusters) == 0) { + l.Error("clusters not found\n") + return nil + } + + if err := renderClustersTableView(table, clusters); err != nil { + return err + } + + return nil +} + +func configClustersTableView(table *tablewriter.Table) { + table.SetAutoWrapText(false) + table.SetAutoFormatHeaders(true) + table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) + table.SetAlignment(tablewriter.ALIGN_LEFT) + table.SetCenterSeparator("") + table.SetColumnSeparator("") + table.SetRowSeparator("") + table.SetHeaderLine(false) + table.SetBorder(false) + table.SetTablePadding("\t") + table.SetNoWhiteSpace(true) +} + +func renderClustersTableView(table *tablewriter.Table, clusters []*deployer.GreptimeDBCluster) error { + table.SetHeader([]string{"Name", "Namespace", "Creation Date"}) + + for _, cluster := range clusters { + rawCluster, ok := cluster.Raw.(*greptimedbclusterv1alpha1.GreptimeDBCluster) + if !ok { + return fmt.Errorf("invalid cluster type") + } + table.Append([]string{ + rawCluster.Name, + rawCluster.Namespace, + rawCluster.CreationTimestamp.String(), + }) + } + + table.Render() + + return nil +} diff --git a/pkg/deployer/k8s/deployer.go b/pkg/deployer/k8s/deployer.go index f19835c0..371cc295 100644 --- a/pkg/deployer/k8s/deployer.go +++ b/pkg/deployer/k8s/deployer.go @@ -175,7 +175,7 @@ func (d *deployer) CreateEtcdCluster(ctx context.Context, name string, options * manifests, err := d.helmManager.LoadAndRenderChart(ctx, resourceName, resourceNamespace, helm.EtcdBitnamiOCIRegistry, helm.DefaultEtcdChartVersion, *options) if err != nil { - return err + return fmt.Errorf("error while loading helm chart: %v", err) } if d.dryRun { @@ -183,8 +183,8 @@ func (d *deployer) CreateEtcdCluster(ctx context.Context, name string, options * return nil } - if err := d.client.Apply(ctx, manifests); err != nil { - return err + if err = d.client.Apply(ctx, manifests); err != nil { + return fmt.Errorf("error while applying helm chart: %v", err) } return d.client.WaitForEtcdReady(ctx, resourceName, resourceNamespace, d.timeout)