Skip to content

Commit

Permalink
Merge pull request #364 from replicatedhq/joshd/sc-99234/nodegroups-r…
Browse files Browse the repository at this point in the history
…eplicated-cluster-nodegroup-ls

Adding nodegroup ls command
  • Loading branch information
jdewinne authored Feb 16, 2024
2 parents ee99332 + dc0ba02 commit 30eba36
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 21 deletions.
14 changes: 14 additions & 0 deletions cli/cmd/cluster_nodegroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cmd

import (
"github.com/spf13/cobra"
)

func (r *runners) InitClusterNodeGroup(parent *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "nodegroup",
}
parent.AddCommand(cmd)

return cmd
}
39 changes: 39 additions & 0 deletions cli/cmd/cluster_nodegroup_ls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cmd

import (
"github.com/pkg/errors"
"github.com/replicatedhq/replicated/cli/print"
"github.com/replicatedhq/replicated/pkg/platformclient"
"github.com/spf13/cobra"
)

func (r *runners) InitClusterNodeGroupList(parent *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "ls [ID]",
Short: "List node groups for a cluster",
Long: `List node groups for a cluster`,
Args: cobra.ExactArgs(1),
RunE: r.listNodeGroups,
}
parent.AddCommand(cmd)

cmd.Flags().StringVar(&r.outputFormat, "output", "table", "The output format to use. One of: json|table|wide (default: table)")

return cmd
}

func (r *runners) listNodeGroups(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("cluster id is required")
}
clusterID := args[0]

cluster, err := r.kotsAPI.GetCluster(clusterID)
if errors.Cause(err) == platformclient.ErrForbidden {
return ErrCompatibilityMatrixTermsNotAccepted
} else if err != nil {
return errors.Wrap(err, "get cluster")
}

return print.NodeGroups(r.outputFormat, r.w, cluster.NodeGroups)
}
3 changes: 3 additions & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ func Execute(rootCmd *cobra.Command, stdin io.Reader, stdout io.Writer, stderr i
runCmds.InitClusterVersions(clusterCmd)
runCmds.InitClusterShell(clusterCmd)

clusterNodeGroupCmd := runCmds.InitClusterNodeGroup(clusterCmd)
runCmds.InitClusterNodeGroupList(clusterNodeGroupCmd)

clusterAddOnCmd := runCmds.InitClusterAddOn(clusterCmd)
runCmds.InitClusterAddOnRm(clusterAddOnCmd)
runCmds.InitClusterAddOnLs(clusterAddOnCmd)
Expand Down
24 changes: 18 additions & 6 deletions cli/print/cluster_addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var addOnsTmpl = template.Must(template.New("ingresses").Funcs(funcs).Parse(addO
var addOnsTmplNoHeader = template.Must(template.New("ingresses").Funcs(funcs).Parse(addOnsTmplRowSrc))

func AddOns(outputFormat string, w *tabwriter.Writer, addOns []*types.ClusterAddOn, header bool) error {
if outputFormat == "table" {
switch outputFormat {
case "table":
if header {
if err := addOnsTmpl.Execute(w, addOns); err != nil {
return err
Expand All @@ -28,25 +29,36 @@ func AddOns(outputFormat string, w *tabwriter.Writer, addOns []*types.ClusterAdd
return err
}
}
} else if outputFormat == "json" {
cAsByte, _ := json.MarshalIndent(addOns, "", " ")
case "json":
cAsByte, err := json.MarshalIndent(addOns, "", " ")
if err != nil {
return err
}
if _, err := fmt.Fprintln(w, string(cAsByte)); err != nil {
return err
}
default:
return fmt.Errorf("unsupported output format: %s", outputFormat)
}
return w.Flush()
}

func AddOn(outputFormat string, w *tabwriter.Writer, addOn *types.ClusterAddOn) error {
if outputFormat == "table" {
switch outputFormat {
case "table":
if err := addOnsTmpl.Execute(w, []*types.ClusterAddOn{addOn}); err != nil {
return err
}
} else if outputFormat == "json" {
cAsByte, _ := json.MarshalIndent(addOn, "", " ")
case "json":
cAsByte, err := json.MarshalIndent(addOn, "", " ")
if err != nil {
return err
}
if _, err := fmt.Fprintln(w, string(cAsByte)); err != nil {
return err
}
default:
return fmt.Errorf("unsupported output format: %s", outputFormat)
}
return w.Flush()
}
37 changes: 37 additions & 0 deletions cli/print/cluster_nodegroups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package print

import (
"encoding/json"
"fmt"
"text/tabwriter"
"text/template"

"github.com/replicatedhq/replicated/pkg/types"
)

var nodeGroupsTmplSrc = `ID NAME DEFAULT INSTANCE TYPE NODES DISK
{{ range . -}}
{{ .ID }} {{ .Name }} {{ .IsDefault }} {{ .InstanceType }} {{.NodeCount}} {{.DiskGiB}}
{{ end }}`

var nodeGroupsTmpl = template.Must(template.New("nodegroups").Parse(nodeGroupsTmplSrc))

func NodeGroups(outputFormat string, w *tabwriter.Writer, addOns []*types.NodeGroup) error {
switch outputFormat {
case "table", "wide":
if err := nodeGroupsTmpl.Execute(w, addOns); err != nil {
return err
}
case "json":
cAsByte, err := json.MarshalIndent(addOns, "", " ")
if err != nil {
return err
}
if _, err := fmt.Fprintln(w, string(cAsByte)); err != nil {
return err
}
default:
return fmt.Errorf("unsupported output format: %s", outputFormat)
}
return w.Flush()
}
54 changes: 39 additions & 15 deletions cli/print/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ DISTRIBUTION: {{ $d.Name }}
var clusterVersionsTmpl = template.Must(template.New("clusterVersions").Funcs(funcs).Parse(clusterVersionsTmplSrc))

func Clusters(outputFormat string, w *tabwriter.Writer, clusters []*types.Cluster, header bool) error {
if outputFormat == "table" {
switch outputFormat {
case "table":
if header {
if err := clustersTmplTable.Execute(w, clusters); err != nil {
return err
Expand All @@ -52,7 +53,7 @@ func Clusters(outputFormat string, w *tabwriter.Writer, clusters []*types.Cluste
return err
}
}
} else if outputFormat == "wide" {
case "wide":
if header {
if err := clustersTmplWide.Execute(w, clusters); err != nil {
return err
Expand All @@ -62,71 +63,94 @@ func Clusters(outputFormat string, w *tabwriter.Writer, clusters []*types.Cluste
return err
}
}
} else if outputFormat == "json" {
cAsByte, _ := json.MarshalIndent(clusters, "", " ")
case "json":
cAsByte, err := json.MarshalIndent(clusters, "", " ")
if err != nil {
return err
}
if _, err := fmt.Fprintln(w, string(cAsByte)); err != nil {
return err
}
default:
return fmt.Errorf("invalid output format: %s", outputFormat)
}
return w.Flush()
}

func NoClusters(outputFormat string, w *tabwriter.Writer) error {
if outputFormat == "table" || outputFormat == "wide" {
switch outputFormat {
case "table", "wide":
_, err := fmt.Fprintln(w, "No clusters found. Use the `replicated cluster create` command to create a new cluster.")
if err != nil {
return err
}
} else if outputFormat == "json" {
case "json":
if _, err := fmt.Fprintln(w, "[]"); err != nil {
return err
}
default:
return fmt.Errorf("invalid output format: %s", outputFormat)
}
return w.Flush()
}

func Cluster(outputFormat string, w *tabwriter.Writer, cluster *types.Cluster) error {
if outputFormat == "table" {
switch outputFormat {
case "table":
if err := clustersTmplTable.Execute(w, []*types.Cluster{cluster}); err != nil {
return err
}
} else if outputFormat == "wide" {
case "wide":
if err := clustersTmplWide.Execute(w, []*types.Cluster{cluster}); err != nil {
return err
}
} else if outputFormat == "json" {
cAsByte, _ := json.MarshalIndent(cluster, "", " ")
case "json":
cAsByte, err := json.MarshalIndent(cluster, "", " ")
if err != nil {
return err
}
if _, err := fmt.Fprintln(w, string(cAsByte)); err != nil {
return err
}
default:
return fmt.Errorf("invalid output format: %s", outputFormat)
}
return w.Flush()
}

func NoClusterVersions(outputFormat string, w *tabwriter.Writer) error {
if outputFormat == "table" {
switch outputFormat {
case "table":
_, err := fmt.Fprintln(w, "No cluster versions found.")
if err != nil {
return err
}
} else if outputFormat == "json" {
case "json":
if _, err := fmt.Fprintln(w, "[]"); err != nil {
return err
}
default:
return fmt.Errorf("invalid output format: %s", outputFormat)
}
return w.Flush()
}

func ClusterVersions(outputFormat string, w *tabwriter.Writer, clusters []*types.ClusterVersion) error {
if outputFormat == "table" {
switch outputFormat {
case "table":
if err := clusterVersionsTmpl.Execute(w, clusters); err != nil {
return err
}
} else if outputFormat == "json" {
cAsByte, _ := json.MarshalIndent(clusters, "", " ")
case "json":
cAsByte, err := json.MarshalIndent(clusters, "", " ")
if err != nil {
return err
}
if _, err := fmt.Fprintln(w, string(cAsByte)); err != nil {
return err
}
default:
return fmt.Errorf("invalid output format: %s", outputFormat)
}
return w.Flush()
}

0 comments on commit 30eba36

Please sign in to comment.