Skip to content

Commit

Permalink
add table view for list cmd
Browse files Browse the repository at this point in the history
Signed-off-by: sh2 <[email protected]>
  • Loading branch information
shawnh2 committed Aug 25, 2023
1 parent 17e4e6d commit bd49a55
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 24 deletions.
85 changes: 64 additions & 21 deletions pkg/cmd/gtctl/cluster/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,92 @@ 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
},
}

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
}
6 changes: 3 additions & 3 deletions pkg/deployer/k8s/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,16 @@ 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 {
d.logger.V(0).Info(string(manifests))
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)
Expand Down

0 comments on commit bd49a55

Please sign in to comment.