1
0
mirror of synced 2026-02-06 19:05:55 +00:00

Switch get commands to use tables for output

Signed-off-by: circa10a <caleblemoine@gmail.com>
This commit is contained in:
circa10a
2020-10-03 15:26:27 -05:00
parent 5efa1ebe88
commit 3619cb8bd1
17 changed files with 226 additions and 106 deletions

View File

@@ -26,6 +26,10 @@ var getCmd = &cobra.Command{
Long: "The get sub-commands print the statuses of sources and resources.",
}
var allNamespaces bool
func init() {
getCmd.PersistentFlags().BoolVarP(&allNamespaces, "all-namespaces", "A", false,
"list the requested object(s) across all namespaces")
rootCmd.AddCommand(getCmd)
}

View File

@@ -18,6 +18,10 @@ package main
import (
"context"
"os"
"strconv"
"strings"
"github.com/fluxcd/pkg/apis/meta"
"github.com/spf13/cobra"
@@ -51,8 +55,12 @@ func getHelmReleaseCmdRun(cmd *cobra.Command, args []string) error {
return err
}
var listOpts []client.ListOption
if !allNamespaces {
listOpts = append(listOpts, client.InNamespace(namespace))
}
var list helmv2.HelmReleaseList
err = kubeClient.List(ctx, &list, client.InNamespace(namespace))
err = kubeClient.List(ctx, &list, listOpts...)
if err != nil {
return err
}
@@ -62,26 +70,35 @@ func getHelmReleaseCmdRun(cmd *cobra.Command, args []string) error {
return nil
}
for _, helmRelease := range list.Items {
if helmRelease.Spec.Suspend {
logger.Successf("%s is suspended", helmRelease.GetName())
continue
}
isInitialized := false
if c := meta.GetCondition(helmRelease.Status.Conditions, meta.ReadyCondition); c != nil {
switch c.Status {
case corev1.ConditionTrue:
logger.Successf("%s last applied revision %s", helmRelease.GetName(), helmRelease.Status.LastAppliedRevision)
case corev1.ConditionUnknown:
logger.Successf("%s reconciling", helmRelease.GetName())
default:
logger.Failuref("%s %s", helmRelease.GetName(), c.Message)
}
isInitialized = true
}
if !isInitialized {
logger.Failuref("%s is not ready", helmRelease.GetName())
}
header := []string{"Name", "Revision", "Suspended", "Ready", "Message"}
if allNamespaces {
header = append([]string{"Namespace"}, header...)
}
var rows [][]string
for _, helmRelease := range list.Items {
row := []string{}
if c := meta.GetCondition(helmRelease.Status.Conditions, meta.ReadyCondition); c != nil {
row = []string{
helmRelease.GetName(),
helmRelease.Status.LastAppliedRevision,
strings.Title(strconv.FormatBool(helmRelease.Spec.Suspend)),
string(c.Status),
c.Message,
}
} else {
row = []string{
helmRelease.GetName(),
helmRelease.Status.LastAppliedRevision,
strings.Title(strconv.FormatBool(helmRelease.Spec.Suspend)),
string(corev1.ConditionFalse),
"waiting to be reconciled",
}
}
if allNamespaces {
row = append([]string{helmRelease.Namespace}, row...)
}
rows = append(rows, row)
}
utils.printTable(os.Stdout, header, rows)
return nil
}

View File

@@ -18,6 +18,10 @@ package main
import (
"context"
"os"
"strconv"
"strings"
"github.com/fluxcd/pkg/apis/meta"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1"
@@ -50,8 +54,12 @@ func getKsCmdRun(cmd *cobra.Command, args []string) error {
return err
}
var listOpts []client.ListOption
if !allNamespaces {
listOpts = append(listOpts, client.InNamespace(namespace))
}
var list kustomizev1.KustomizationList
err = kubeClient.List(ctx, &list, client.InNamespace(namespace))
err = kubeClient.List(ctx, &list, listOpts...)
if err != nil {
return err
}
@@ -61,26 +69,35 @@ func getKsCmdRun(cmd *cobra.Command, args []string) error {
return nil
}
for _, kustomization := range list.Items {
if kustomization.Spec.Suspend {
logger.Successf("%s is suspended", kustomization.GetName())
continue
}
isInitialized := false
if c := meta.GetCondition(kustomization.Status.Conditions, meta.ReadyCondition); c != nil {
switch c.Status {
case corev1.ConditionTrue:
logger.Successf("%s last applied revision %s", kustomization.GetName(), kustomization.Status.LastAppliedRevision)
case corev1.ConditionUnknown:
logger.Successf("%s reconciling", kustomization.GetName())
default:
logger.Failuref("%s %s", kustomization.GetName(), c.Message)
}
isInitialized = true
}
if !isInitialized {
logger.Failuref("%s is not ready", kustomization.GetName())
}
header := []string{"Name", "Revision", "Suspended", "Ready", "Message"}
if allNamespaces {
header = append([]string{"Namespace"}, header...)
}
var rows [][]string
for _, kustomization := range list.Items {
row := []string{}
if c := meta.GetCondition(kustomization.Status.Conditions, meta.ReadyCondition); c != nil {
row = []string{
kustomization.GetName(),
kustomization.Status.LastAppliedRevision,
strings.Title(strconv.FormatBool(kustomization.Spec.Suspend)),
string(c.Status),
c.Message,
}
} else {
row = []string{
kustomization.GetName(),
kustomization.Status.LastAppliedRevision,
strings.Title(strconv.FormatBool(kustomization.Spec.Suspend)),
string(corev1.ConditionFalse),
"waiting to be reconciled",
}
}
if allNamespaces {
row = append([]string{kustomization.Namespace}, row...)
}
rows = append(rows, row)
}
utils.printTable(os.Stdout, header, rows)
return nil
}

View File

@@ -18,6 +18,8 @@ package main
import (
"context"
"os"
"github.com/fluxcd/pkg/apis/meta"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
@@ -49,36 +51,48 @@ func getSourceBucketCmdRun(cmd *cobra.Command, args []string) error {
return err
}
var listOpts []client.ListOption
if !allNamespaces {
listOpts = append(listOpts, client.InNamespace(namespace))
}
var list sourcev1.BucketList
err = kubeClient.List(ctx, &list, client.InNamespace(namespace))
err = kubeClient.List(ctx, &list, listOpts...)
if err != nil {
return err
}
if len(list.Items) == 0 {
logger.Failuref("no sources found in %s namespace", namespace)
logger.Failuref("no bucket sources found in %s namespace", namespace)
return nil
}
// TODO(hidde): this should print a table, and should produce better output
// for items that have an artifact attached while they are in a reconciling
// 'Unknown' state.
for _, source := range list.Items {
isInitialized := false
if c := meta.GetCondition(source.Status.Conditions, meta.ReadyCondition); c != nil {
switch c.Status {
case corev1.ConditionTrue:
logger.Successf("%s last fetched revision: %s", source.GetName(), source.GetArtifact().Revision)
case corev1.ConditionUnknown:
logger.Successf("%s reconciling", source.GetName())
default:
logger.Failuref("%s %s", source.GetName(), c.Message)
}
isInitialized = true
}
if !isInitialized {
logger.Failuref("%s is not ready", source.GetName())
}
header := []string{"Name", "Revision", "Ready", "Message"}
if allNamespaces {
header = append([]string{"Namespace"}, header...)
}
var rows [][]string
for _, source := range list.Items {
row := []string{}
if c := meta.GetCondition(source.Status.Conditions, meta.ReadyCondition); c != nil {
row = []string{
source.GetName(),
source.GetArtifact().Revision,
string(c.Status),
c.Message,
}
} else {
row = []string{
source.GetName(),
source.GetArtifact().Revision,
string(corev1.ConditionFalse),
"waiting to be reconciled",
}
}
if allNamespaces {
row = append([]string{source.Namespace}, row...)
}
rows = append(rows, row)
}
utils.printTable(os.Stdout, header, rows)
return nil
}

View File

@@ -18,6 +18,8 @@ package main
import (
"context"
"os"
"github.com/fluxcd/pkg/apis/meta"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
@@ -49,36 +51,48 @@ func getSourceGitCmdRun(cmd *cobra.Command, args []string) error {
return err
}
var listOpts []client.ListOption
if !allNamespaces {
listOpts = append(listOpts, client.InNamespace(namespace))
}
var list sourcev1.GitRepositoryList
err = kubeClient.List(ctx, &list, client.InNamespace(namespace))
err = kubeClient.List(ctx, &list, listOpts...)
if err != nil {
return err
}
if len(list.Items) == 0 {
logger.Failuref("no sources found in %s namespace", namespace)
logger.Failuref("no git sources found in %s namespace", namespace)
return nil
}
// TODO(hidde): this should print a table, and should produce better output
// for items that have an artifact attached while they are in a reconciling
// 'Unknown' state.
for _, source := range list.Items {
isInitialized := false
if c := meta.GetCondition(source.Status.Conditions, meta.ReadyCondition); c != nil {
switch c.Status {
case corev1.ConditionTrue:
logger.Successf("%s last fetched revision: %s", source.GetName(), source.GetArtifact().Revision)
case corev1.ConditionUnknown:
logger.Successf("%s reconciling", source.GetName())
default:
logger.Failuref("%s %s", source.GetName(), c.Message)
}
isInitialized = true
}
if !isInitialized {
logger.Failuref("%s is not ready", source.GetName())
}
header := []string{"Name", "Revision", "Ready", "Message"}
if allNamespaces {
header = append([]string{"Namespace"}, header...)
}
var rows [][]string
for _, source := range list.Items {
row := []string{}
if c := meta.GetCondition(source.Status.Conditions, meta.ReadyCondition); c != nil {
row = []string{
source.GetName(),
"unknown",
string(c.Status),
c.Message,
}
} else {
row = []string{
source.GetName(),
source.GetArtifact().Revision,
string(corev1.ConditionFalse),
"waiting to be reconciled",
}
}
if allNamespaces {
row = append([]string{source.Namespace}, row...)
}
rows = append(rows, row)
}
utils.printTable(os.Stdout, header, rows)
return nil
}

View File

@@ -18,6 +18,8 @@ package main
import (
"context"
"os"
"github.com/fluxcd/pkg/apis/meta"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
@@ -49,36 +51,48 @@ func getSourceHelmCmdRun(cmd *cobra.Command, args []string) error {
return err
}
var listOpts []client.ListOption
if !allNamespaces {
listOpts = append(listOpts, client.InNamespace(namespace))
}
var list sourcev1.HelmRepositoryList
err = kubeClient.List(ctx, &list, client.InNamespace(namespace))
err = kubeClient.List(ctx, &list, listOpts...)
if err != nil {
return err
}
if len(list.Items) == 0 {
logger.Failuref("no sources found in %s namespace", namespace)
logger.Failuref("no helm sources found in %s namespace", namespace)
return nil
}
// TODO(hidde): this should print a table, and should produce better output
// for items that have an artifact attached while they are in a reconciling
// 'Unknown' state.
for _, source := range list.Items {
isInitialized := false
if c := meta.GetCondition(source.Status.Conditions, meta.ReadyCondition); c != nil {
switch c.Status {
case corev1.ConditionTrue:
logger.Successf("%s last fetched revision: %s", source.GetName(), source.GetArtifact().Revision)
case corev1.ConditionUnknown:
logger.Successf("%s reconciling", source.GetName())
default:
logger.Failuref("%s %s", source.GetName(), c.Message)
}
isInitialized = true
}
if !isInitialized {
logger.Failuref("%s is not ready", source.GetName())
}
header := []string{"Name", "Revision", "Ready", "Message"}
if allNamespaces {
header = append([]string{"Namespace"}, header...)
}
var rows [][]string
for _, source := range list.Items {
row := []string{}
if c := meta.GetCondition(source.Status.Conditions, meta.ReadyCondition); c != nil {
row = []string{
source.GetName(),
source.GetArtifact().Revision,
string(c.Status),
c.Message,
}
} else {
row = []string{
source.GetName(),
source.GetArtifact().Revision,
string(corev1.ConditionFalse),
"waiting to be reconciled",
}
}
if allNamespaces {
row = append([]string{source.Namespace}, row...)
}
rows = append(rows, row)
}
utils.printTable(os.Stdout, header, rows)
return nil
}

View File

@@ -44,6 +44,7 @@ import (
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1"
"github.com/fluxcd/pkg/runtime/dependency"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
"github.com/olekukonko/tablewriter"
)
type Utils struct {
@@ -297,3 +298,21 @@ func (*Utils) generateKustomizationYaml(dirPath string) error {
}
return nil
}
func (*Utils) printTable(writer io.Writer, header []string, rows [][]string) {
table := tablewriter.NewWriter(writer)
table.SetHeader(header)
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)
table.AppendBulk(rows)
table.Render()
}