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 Example: ` # Create an Alert for kustomization events
gotk create alert \ gotk create alert \
--event-severity info \ --event-severity info \
--event-source kustomization/gotk-system \ --event-source Kustomization/gotk-system \
--provider-ref slack \ --provider-ref slack \
gotk-system gotk-system
`, `,

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

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

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

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

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

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

Loading…
Cancel
Save