Skip to content

Commit

Permalink
cmd/observe: add flag to allow specifying a time format
Browse files Browse the repository at this point in the history
The default behavior is unchanged as time.StampMilli is used by default.

Signed-off-by: Robin Hahling <robin.hahling@gw-computing.net>
  • Loading branch information
rolinh committed Mar 12, 2021
1 parent f7d7a54 commit 18a7aef
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion cmd/observe/observe.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"os/signal"
"strings"
"time"

pb "github.com/cilium/cilium/api/v1/flow"
"github.com/cilium/cilium/api/v1/observer"
Expand Down Expand Up @@ -56,6 +57,8 @@ var (
dictOutput bool
output string

timeFormat string

enableIPTranslation bool
nodeName bool
numeric bool
Expand All @@ -81,6 +84,25 @@ func eventTypes() (l []string) {
return
}

func timeFormatNameToLayout(name string) string {
switch strings.ToLower(name) {
case "rfc3339":
return time.RFC3339
case "rfc3339milli":
return hubtime.RFC3339Milli
case "rfc3339micro":
return hubtime.RFC3339Micro
case "rfc3339nano":
return time.RFC3339Nano
case "rfc1123z":
return time.RFC1123Z
case "stampmilli":
fallthrough
default:
return time.StampMilli
}
}

// New observer command.
func New(vp *viper.Viper) *cobra.Command {
return newObserveCmd(vp, newObserveFilter())
Expand Down Expand Up @@ -256,6 +278,7 @@ more.`,
"Show all flows terminating at an endpoint with the given security identity"))
observeCmd.Flags().AddFlagSet(filterFlags)

// formatting flags
formattingFlags := pflag.NewFlagSet("Formatting", pflag.ContinueOnError)
formattingFlags.BoolVarP(
&formattingOpts.jsonOutput, "json", "j", false, "Deprecated. Use '--output json' instead.",
Expand Down Expand Up @@ -290,6 +313,17 @@ more.`,
"Translate IP addresses to logical names such as pod name, FQDN, ...",
)
formattingFlags.BoolVarP(&formattingOpts.nodeName, "print-node-name", "", false, "Print node name in output")
formattingFlags.StringVar(
&formattingOpts.timeFormat, "time-format", "StampMilli",
fmt.Sprintf(`Specify the time format for printing. This option does not apply to the json and jsonpb output type. One of:
StampMilli: %s
RFC3339: %s
RFC3339Milli: %s
RFC3339Micro: %s
RFC3339Nano: %s
RFC1123Z: %s
`, time.StampMilli, time.RFC3339, hubtime.RFC3339Milli, hubtime.RFC3339Micro, time.RFC3339Nano, time.RFC1123Z),
)
observeCmd.Flags().AddFlagSet(formattingFlags)

// other flags
Expand Down Expand Up @@ -346,6 +380,16 @@ more.`,
"table",
}, cobra.ShellCompDirectiveDefault
})
observeCmd.RegisterFlagCompletionFunc("time-format", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{
"StampMilli",
"RFC3339",
"RFC3339Milli",
"RFC3339Micro",
"RFC3339Nano",
"RFC1123Z",
}, cobra.ShellCompDirectiveDefault
})

// default value for when the flag is on the command line without any options
observeCmd.Flags().Lookup("not").NoOptDefVal = "true"
Expand All @@ -361,7 +405,9 @@ func handleArgs(ofilter *observeFilter, debug bool) (err error) {
}

// initialize the printer with any options that were passed in
var opts []hubprinter.Option
var opts = []hubprinter.Option{
hubprinter.WithTimeFormat(timeFormatNameToLayout(formattingOpts.timeFormat)),
}

if formattingOpts.output == "" { // support deprecated output flags if provided
if formattingOpts.jsonOutput {
Expand Down

0 comments on commit 18a7aef

Please sign in to comment.