From 8eac7d6b4d25fb9f0befda44a0e8ef9c85de1ede Mon Sep 17 00:00:00 2001 From: Philip Laine Date: Fri, 9 Oct 2020 21:41:09 +0200 Subject: [PATCH] Implement table output --- cmd/gotk/create_alert.go | 2 +- cmd/gotk/create_receiver.go | 22 ++++++------- cmd/gotk/get_alert.go | 58 ++++++++++++++++++++++------------- cmd/gotk/get_alertprovider.go | 48 +++++++++++++++++++---------- cmd/gotk/get_receiver.go | 53 +++++++++++++++++++------------- cmd/gotk/suspend_alert.go | 34 ++++++++++---------- cmd/gotk/suspend_receiver.go | 34 ++++++++++---------- 7 files changed, 144 insertions(+), 107 deletions(-) diff --git a/cmd/gotk/create_alert.go b/cmd/gotk/create_alert.go index a5bb341d..4df61b87 100644 --- a/cmd/gotk/create_alert.go +++ b/cmd/gotk/create_alert.go @@ -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 `, diff --git a/cmd/gotk/create_receiver.go b/cmd/gotk/create_receiver.go index 1a1a12bf..7488cbbc 100644 --- a/cmd/gotk/create_receiver.go +++ b/cmd/gotk/create_receiver.go @@ -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) } diff --git a/cmd/gotk/get_alert.go b/cmd/gotk/get_alert.go index cf6cce83..0158bdc7 100644 --- a/cmd/gotk/get_alert.go +++ b/cmd/gotk/get_alert.go @@ -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 } - for _, alert := range list.Items { - if alert.Spec.Suspend { - logger.Successf("%s is suspended", alert.GetName()) - continue - } - isInitialized := false - 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) - } - isInitialized = true - } - if !isInitialized { - logger.Failuref("%s is not ready", alert.GetName()) - } + header := []string{"Name", "Suspended", "Ready", "Message"} + if allNamespaces { + header = append([]string{"Namespace"}, header...) } + var rows [][]string + for _, alert := range list.Items { + row := []string{} + if c := meta.GetCondition(alert.Status.Conditions, meta.ReadyCondition); c != nil { + 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", + } + } + if allNamespaces { + row = append([]string{alert.Namespace}, row...) + } + rows = append(rows, row) + } + utils.printTable(os.Stdout, header, rows) return nil } diff --git a/cmd/gotk/get_alertprovider.go b/cmd/gotk/get_alertprovider.go index 4b3a892e..65ca975b 100644 --- a/cmd/gotk/get_alertprovider.go +++ b/cmd/gotk/get_alertprovider.go @@ -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 } - for _, provider := range list.Items { - isInitialized := false - 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) - } - isInitialized = true - } - if !isInitialized { - logger.Failuref("%s is not ready", provider.GetName()) - } + header := []string{"Name", "Ready", "Message"} + if allNamespaces { + header = append([]string{"Namespace"}, header...) } + var rows [][]string + for _, provider := range list.Items { + row := []string{} + if c := meta.GetCondition(provider.Status.Conditions, meta.ReadyCondition); c != nil { + row = []string{ + provider.GetName(), + string(c.Status), + c.Message, + } + } else { + row = []string{ + provider.GetName(), + string(corev1.ConditionFalse), + "waiting to be reconciled", + } + } + if allNamespaces { + row = append([]string{provider.Namespace}, row...) + } + rows = append(rows, row) + } + utils.printTable(os.Stdout, header, rows) return nil } diff --git a/cmd/gotk/get_receiver.go b/cmd/gotk/get_receiver.go index a9aa680b..a01a7d82 100644 --- a/cmd/gotk/get_receiver.go +++ b/cmd/gotk/get_receiver.go @@ -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 } - for _, receiver := range list.Items { - if receiver.Spec.Suspend { - logger.Successf("%s is suspended", receiver.GetName()) - continue - } - isInitialized := false - 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) - } - isInitialized = true - } - if !isInitialized { - logger.Failuref("%s is not ready", receiver.GetName()) - } + header := []string{"Name", "Suspended", "Ready", "Message"} + if allNamespaces { + header = append([]string{"Namespace"}, header...) } + var rows [][]string + for _, receiver := range list.Items { + row := []string{} + if c := meta.GetCondition(receiver.Status.Conditions, meta.ReadyCondition); c != nil { + 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", + } + } + rows = append(rows, row) + } + utils.printTable(os.Stdout, header, rows) return nil } diff --git a/cmd/gotk/suspend_alert.go b/cmd/gotk/suspend_alert.go index 8cfb8dc4..555f9e3b 100644 --- a/cmd/gotk/suspend_alert.go +++ b/cmd/gotk/suspend_alert.go @@ -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 } diff --git a/cmd/gotk/suspend_receiver.go b/cmd/gotk/suspend_receiver.go index 555f9e3b..8cfb8dc4 100644 --- a/cmd/gotk/suspend_receiver.go +++ b/cmd/gotk/suspend_receiver.go @@ -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 }