Implement table output

pull/302/head
Philip Laine 4 years ago committed by Philip Laine
parent 7ebb34de80
commit 8eac7d6b4d

@ -40,7 +40,7 @@ var createAlertCmd = &cobra.Command{
Example: ` # Create an Alert for kustomization events
gotk create alert \
--event-severity info \
--event-source kustomization/gotk-system \
--event-source Kustomization/gotk-system \
--provider-ref slack \
gotk-system
`,

@ -37,18 +37,14 @@ var createReceiverCmd = &cobra.Command{
Aliases: []string{"rcv"},
Short: "Create or update a Receiver resource",
Long: "The create receiver command generates a Receiver resource.",
Example: ` # Create a Provider for a Slack channel
gotk create ap slack \
--type slack \
--channel general \
--address https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK \
--secret-ref webhook-url
# Create a Provider for a Github repository
gotk create ap github-podinfo \
--type github \
--address https://github.com/stefanprodan/podinfo \
--secret-ref github-token
Example: ` # Create a Receiver
gotk create rcv github-receiver \
--type github \
--event ping \
--event push \
--secret-ref webhook-token \
--resource GitRepository/webapp \
--resource HelmRepository/webapp
`,
RunE: createReceiverCmdRun,
}
@ -63,7 +59,7 @@ var (
func init() {
createReceiverCmd.Flags().StringVar(&rcvType, "type", "", "")
createReceiverCmd.Flags().StringVar(&rcvSecretRef, "secret-ref", "", "")
createReceiverCmd.Flags().StringArrayVar(&rcvEvents, "events", []string{}, "")
createReceiverCmd.Flags().StringArrayVar(&rcvEvents, "event", []string{}, "")
createReceiverCmd.Flags().StringArrayVar(&rcvResources, "resource", []string{}, "")
createCmd.AddCommand(createReceiverCmd)
}

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

@ -18,6 +18,7 @@ package main
import (
"context"
"os"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
@ -51,8 +52,12 @@ func getAlertProviderCmdRun(cmd *cobra.Command, args []string) error {
return err
}
var listOpts []client.ListOption
if !allNamespaces {
listOpts = append(listOpts, client.InNamespace(namespace))
}
var list notificationv1.ProviderList
err = kubeClient.List(ctx, &list, client.InNamespace(namespace))
err = kubeClient.List(ctx, &list, listOpts...)
if err != nil {
return err
}
@ -62,22 +67,31 @@ func getAlertProviderCmdRun(cmd *cobra.Command, args []string) error {
return nil
}
header := []string{"Name", "Ready", "Message"}
if allNamespaces {
header = append([]string{"Namespace"}, header...)
}
var rows [][]string
for _, provider := range list.Items {
isInitialized := false
row := []string{}
if c := meta.GetCondition(provider.Status.Conditions, meta.ReadyCondition); c != nil {
switch c.Status {
case corev1.ConditionTrue:
logger.Successf("%s is ready", provider.GetName())
case corev1.ConditionUnknown:
logger.Successf("%s reconciling", provider.GetName())
default:
logger.Failuref("%s %s", provider.GetName(), c.Message)
row = []string{
provider.GetName(),
string(c.Status),
c.Message,
}
} else {
row = []string{
provider.GetName(),
string(corev1.ConditionFalse),
"waiting to be reconciled",
}
isInitialized = true
}
if !isInitialized {
logger.Failuref("%s is not ready", provider.GetName())
if allNamespaces {
row = append([]string{provider.Namespace}, row...)
}
rows = append(rows, row)
}
utils.printTable(os.Stdout, header, rows)
return nil
}

@ -18,6 +18,9 @@ package main
import (
"context"
"os"
"strconv"
"strings"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
@ -51,8 +54,12 @@ func getReceiverCmdRun(cmd *cobra.Command, args []string) error {
return err
}
var listOpts []client.ListOption
if !allNamespaces {
listOpts = append(listOpts, client.InNamespace(namespace))
}
var list notificationv1.ReceiverList
err = kubeClient.List(ctx, &list, client.InNamespace(namespace))
err = kubeClient.List(ctx, &list, listOpts...)
if err != nil {
return err
}
@ -62,26 +69,30 @@ func getReceiverCmdRun(cmd *cobra.Command, args []string) error {
return nil
}
header := []string{"Name", "Suspended", "Ready", "Message"}
if allNamespaces {
header = append([]string{"Namespace"}, header...)
}
var rows [][]string
for _, receiver := range list.Items {
if receiver.Spec.Suspend {
logger.Successf("%s is suspended", receiver.GetName())
continue
}
isInitialized := false
row := []string{}
if c := meta.GetCondition(receiver.Status.Conditions, meta.ReadyCondition); c != nil {
switch c.Status {
case corev1.ConditionTrue:
logger.Successf("%s is ready", receiver.GetName())
case corev1.ConditionUnknown:
logger.Successf("%s reconciling", receiver.GetName())
default:
logger.Failuref("%s %s", receiver.GetName(), c.Message)
row = []string{
receiver.GetName(),
strings.Title(strconv.FormatBool(receiver.Spec.Suspend)),
string(c.Status),
c.Message,
}
} else {
row = []string{
receiver.GetName(),
strings.Title(strconv.FormatBool(receiver.Spec.Suspend)),
string(corev1.ConditionFalse),
"waiting to be reconciled",
}
isInitialized = true
}
if !isInitialized {
logger.Failuref("%s is not ready", receiver.GetName())
}
rows = append(rows, row)
}
utils.printTable(os.Stdout, header, rows)
return nil
}

@ -26,24 +26,24 @@ import (
notificationv1 "github.com/fluxcd/notification-controller/api/v1beta1"
)
var suspendReceiverCmd = &cobra.Command{
Use: "receiver [name]",
Aliases: []string{"rcv"},
Short: "Suspend reconciliation of Receiver",
Long: "The suspend command disables the reconciliation of a Receiver resource.",
Example: ` # Suspend reconciliation for an existing Receiver
gotk suspend receiver main
var suspendAlertCmd = &cobra.Command{
Use: "alert [name]",
Aliases: []string{},
Short: "Suspend reconciliation of Alert",
Long: "The suspend command disables the reconciliation of a Alert resource.",
Example: ` # Suspend reconciliation for an existing Alert
gotk suspend alert main
`,
RunE: suspendReceiverCmdRun,
RunE: suspendAlertCmdRun,
}
func init() {
suspendCmd.AddCommand(suspendReceiverCmd)
suspendCmd.AddCommand(suspendAlertCmd)
}
func suspendReceiverCmdRun(cmd *cobra.Command, args []string) error {
func suspendAlertCmdRun(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("Receiver name is required")
return fmt.Errorf("Alert name is required")
}
name := args[0]
@ -59,18 +59,18 @@ func suspendReceiverCmdRun(cmd *cobra.Command, args []string) error {
Namespace: namespace,
Name: name,
}
var receiver notificationv1.Receiver
err = kubeClient.Get(ctx, namespacedName, &receiver)
var alert notificationv1.Alert
err = kubeClient.Get(ctx, namespacedName, &alert)
if err != nil {
return err
}
logger.Actionf("suspending Receiver %s in %s namespace", name, namespace)
receiver.Spec.Suspend = true
if err := kubeClient.Update(ctx, &receiver); err != nil {
logger.Actionf("suspending Alert %s in %s namespace", name, namespace)
alert.Spec.Suspend = true
if err := kubeClient.Update(ctx, &alert); err != nil {
return err
}
logger.Successf("Receiver suspended")
logger.Successf("Alert suspended")
return nil
}

@ -26,24 +26,24 @@ import (
notificationv1 "github.com/fluxcd/notification-controller/api/v1beta1"
)
var suspendAlertCmd = &cobra.Command{
Use: "alert [name]",
Aliases: []string{},
Short: "Suspend reconciliation of Alert",
Long: "The suspend command disables the reconciliation of a Alert resource.",
Example: ` # Suspend reconciliation for an existing Alert
gotk suspend alert main
var suspendReceiverCmd = &cobra.Command{
Use: "receiver [name]",
Aliases: []string{"rcv"},
Short: "Suspend reconciliation of Receiver",
Long: "The suspend command disables the reconciliation of a Receiver resource.",
Example: ` # Suspend reconciliation for an existing Receiver
gotk suspend receiver main
`,
RunE: suspendAlertCmdRun,
RunE: suspendReceiverCmdRun,
}
func init() {
suspendCmd.AddCommand(suspendAlertCmd)
suspendCmd.AddCommand(suspendReceiverCmd)
}
func suspendAlertCmdRun(cmd *cobra.Command, args []string) error {
func suspendReceiverCmdRun(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("Alert name is required")
return fmt.Errorf("Receiver name is required")
}
name := args[0]
@ -59,18 +59,18 @@ func suspendAlertCmdRun(cmd *cobra.Command, args []string) error {
Namespace: namespace,
Name: name,
}
var alert notificationv1.Alert
err = kubeClient.Get(ctx, namespacedName, &alert)
var receiver notificationv1.Receiver
err = kubeClient.Get(ctx, namespacedName, &receiver)
if err != nil {
return err
}
logger.Actionf("suspending Alert %s in %s namespace", name, namespace)
alert.Spec.Suspend = true
if err := kubeClient.Update(ctx, &alert); err != nil {
logger.Actionf("suspending Receiver %s in %s namespace", name, namespace)
receiver.Spec.Suspend = true
if err := kubeClient.Update(ctx, &receiver); err != nil {
return err
}
logger.Successf("Alert suspended")
logger.Successf("Receiver suspended")
return nil
}

Loading…
Cancel
Save