add jsonpath to kubectl

This commit is contained in:
Dai Zuozhuo
2015-08-04 16:14:31 +08:00
parent 8a43bd621e
commit b61a905b19
23 changed files with 250 additions and 144 deletions

View File

@@ -69,7 +69,7 @@ func NewCmdGet(f *cmdutil.Factory, out io.Writer) *cobra.Command {
validArgs := p.HandledResources()
cmd := &cobra.Command{
Use: "get [(-o|--output=)json|yaml|template|wide|...] (TYPE [(NAME | -l label] | TYPE/NAME ...) [flags]",
Use: "get [(-o|--output=)json|yaml|template|templatefile|wide|jsonpath|...] (TYPE [NAME | -l label] | TYPE/NAME ...)",
Short: "Display one or many resources",
Long: get_long,
Example: get_example,

View File

@@ -28,10 +28,10 @@ import (
// AddPrinterFlags adds printing related flags to a command (e.g. output format, no headers, template path)
func AddPrinterFlags(cmd *cobra.Command) {
cmd.Flags().StringP("output", "o", "", "Output format. One of: json|yaml|template|templatefile|wide.")
cmd.Flags().StringP("output", "o", "", "Output format. One of: json|yaml|template|templatefile|wide|jsonpath.")
cmd.Flags().String("output-version", "", "Output the formatted object with the given version (default api-version).")
cmd.Flags().Bool("no-headers", false, "When using the default output, don't print headers.")
cmd.Flags().StringP("template", "t", "", "Template string or path to template file to use when -o=template or -o=templatefile. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]")
cmd.Flags().StringP("template", "t", "", "Template string or path to template file to use when -o=template, -o=templatefile or -o=jsonpath. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]. The jsonpath template is composed of jsonpath expressions enclosed by {}")
cmd.Flags().String("sort-by", "", "If non-empty, sort list types using this field specification. The field specification is expressed as a JSONPath expression (e.g. 'ObjectMeta.Name'). The field in the API resource specified by this JSONPath expression must be an integer or a string.")
cmd.Flags().BoolP("show-all", "a", false, "When printing, show all resources (default hide terminated pods.)")
}

View File

@@ -35,6 +35,7 @@ import (
"k8s.io/kubernetes/pkg/conversion"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util"
"k8s.io/kubernetes/pkg/util/jsonpath"
"k8s.io/kubernetes/pkg/volume"
)
@@ -71,6 +72,15 @@ func GetPrinter(format, formatArgument string) (ResourcePrinter, bool, error) {
if err != nil {
return nil, false, fmt.Errorf("error parsing template %s, %v\n", string(data), err)
}
case "jsonpath":
if len(formatArgument) == 0 {
return nil, false, fmt.Errorf("jsonpath format specified but no jsonpath template given")
}
var err error
printer, err = NewJSONPathPrinter(formatArgument)
if err != nil {
return nil, false, fmt.Errorf("error parsing jsonpath %s, %v\n", formatArgument, err)
}
case "wide":
fallthrough
case "":
@@ -1205,3 +1215,37 @@ func indirect(v reflect.Value) (rv reflect.Value, isNil bool) {
}
return v, false
}
// JSONPathPrinter is an implementation of ResourcePrinter which formats data with jsonpath expression.
type JSONPathPrinter struct {
rawTemplate string
*jsonpath.JSONPath
}
func NewJSONPathPrinter(tmpl string) (*JSONPathPrinter, error) {
j := jsonpath.New("out")
if err := j.Parse(tmpl); err != nil {
return nil, err
}
return &JSONPathPrinter{tmpl, j}, nil
}
// PrintObj formats the obj with the JSONPath Template.
func (j *JSONPathPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
data, err := json.Marshal(obj)
if err != nil {
return err
}
out := map[string]interface{}{}
if err := json.Unmarshal(data, &out); err != nil {
return err
}
if err = j.JSONPath.Execute(w, out); err != nil {
fmt.Fprintf(w, "Error executing template: %v\n", err)
fmt.Fprintf(w, "template was:\n\t%v\n", j.rawTemplate)
fmt.Fprintf(w, "raw data was:\n\t%v\n", string(data))
fmt.Fprintf(w, "object given to template engine was:\n\t%+v\n", out)
return fmt.Errorf("error executing jsonpath '%v': '%v'\n----data----\n%+v\n", j.rawTemplate, err, out)
}
return nil
}

View File

@@ -164,6 +164,23 @@ func TestPrintTemplate(t *testing.T) {
}
}
func TestPrintJSONPath(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
printer, found, err := GetPrinter("jsonpath", "{.metadata.name}")
if err != nil || !found {
t.Fatalf("unexpected error: %#v", err)
}
unversionedPod := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
obj, err := api.Scheme.ConvertToVersion(unversionedPod, testapi.Version())
err = printer.PrintObj(obj, buf)
if err != nil {
t.Fatalf("unexpected error: %s %#v", buf, err)
}
if buf.String() != "foo" {
t.Errorf("unexpected output: %s", buf.String())
}
}
func TestPrintEmptyTemplate(t *testing.T) {
if _, _, err := GetPrinter("template", ""); err == nil {
t.Errorf("unexpected non-error")
@@ -450,6 +467,10 @@ func TestPrinters(t *testing.T) {
if err != nil {
t.Fatal(err)
}
jsonpathPrinter, err := NewJSONPathPrinter("{.metadata.name}")
if err != nil {
t.Fatal(err)
}
printers := map[string]ResourcePrinter{
"humanReadable": NewHumanReadablePrinter(true, false, false, false, []string{}),
"humanReadableHeaders": NewHumanReadablePrinter(false, false, false, false, []string{}),
@@ -457,6 +478,7 @@ func TestPrinters(t *testing.T) {
"yaml": &YAMLPrinter{},
"template": templatePrinter,
"template2": templatePrinter2,
"jsonpath": jsonpathPrinter,
}
objects := map[string]runtime.Object{
"pod": &api.Pod{ObjectMeta: om("pod")},
@@ -471,6 +493,7 @@ func TestPrinters(t *testing.T) {
// map of printer name to set of objects it should fail on.
expectedErrors := map[string]util.StringSet{
"template2": util.NewStringSet("pod", "emptyPodList", "endpoints"),
"jsonpath": util.NewStringSet("emptyPodList", "nonEmptyPodList", "endpoints"),
}
for pName, p := range printers {