Zipzap
Zipzap

Reputation: 121

Customizing kubectl output with go templates using custom function

I'm trying to add a custom function In Go template for parsing the time in PodStatus and getting the absolute time for it.

Example for the custom function:

PodScheduled, _ := time.Parse(time.RFC3339, "2021-12- 23T20:20:36Z")
Ready, _ := time.Parse(time.RFC3339, "2021-12-31T07:36:11Z")

difference := Ready.Sub(PodScheduled)
fmt.Printf("difference = %v\n", difference)

I can use the built-in functions.

How I can use a custom function with the kubectl?

For example this lib: https://github.com/Masterminds/sprig

Thanks :)

Upvotes: 1

Views: 5311

Answers (1)

DazWilkin
DazWilkin

Reputation: 40296

IIUC you have (at least) 3 options:

  1. Discouraged: Write your own client (instead of kubectl) that provides the functionality;
  2. Encouraged: Use the shell to post-process the output from e.g. kubectl get pods --output=json) by piping the result through:
    • Either your Golang binary that reads from standard input
    • Or better a general-purpose JSON processing tool like jq that would do this (and much more!)
  3. For completeness, kubectl supports output formatting (--output=jsonpath...); although JSONPath may be insufficient for this need;

See jq's documentation for Dates

Upvotes: 2

Related Questions