-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: jsonpath filter for talosctl get outputs
We add a filter to the `talosctl get` command that allows users to specify a jsonpath filter. Now they can reduce the information that is printed to only the parts they are interested in. Fixes #6109 Signed-off-by: Philipp Sauter <[email protected]>
- Loading branch information
Philipp Sauter
committed
Sep 27, 2022
1 parent
6bd3cca
commit f17cdee
Showing
9 changed files
with
319 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package output | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"reflect" | ||
|
||
"github.com/cosi-project/runtime/pkg/resource" | ||
"github.com/cosi-project/runtime/pkg/resource/meta" | ||
"github.com/cosi-project/runtime/pkg/state" | ||
"k8s.io/client-go/third_party/forked/golang/template" | ||
"k8s.io/client-go/util/jsonpath" | ||
) | ||
|
||
// JSONPath outputs resources in JSONPath format. | ||
type JSONPath struct { | ||
jsonPath *jsonpath.JSONPath | ||
json *JSON | ||
writer io.Writer | ||
} | ||
|
||
// NewJSONPath initializes JSONPath resource output. | ||
func NewJSONPath(writer io.Writer, jsonPath *jsonpath.JSONPath) *JSONPath { | ||
return &JSONPath{ | ||
jsonPath: jsonPath, | ||
json: NewJSON(writer), | ||
writer: writer, | ||
} | ||
} | ||
|
||
// WriteHeader implements output.Writer interface. | ||
func (j *JSONPath) WriteHeader(definition *meta.ResourceDefinition, withEvents bool) error { | ||
return j.json.WriteHeader(definition, withEvents) | ||
} | ||
|
||
// printResult prints a reflect.Value as JSON if it's a map, array, slice or struct. | ||
// But if it's just a 'scalar' type it prints it as a mere string. | ||
func printResult(wr io.Writer, result reflect.Value) error { | ||
kind := result.Kind() | ||
if kind == reflect.Interface { | ||
kind = result.Elem().Kind() | ||
} | ||
|
||
outputJSON := kind == reflect.Map || | ||
kind == reflect.Array || | ||
kind == reflect.Slice || | ||
kind == reflect.Struct | ||
|
||
var text []byte | ||
|
||
var err error | ||
|
||
if outputJSON { | ||
text, err = json.MarshalIndent(result.Interface(), "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
text, err = valueToText(result) | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
text = append(text, '\n') | ||
|
||
if _, err = wr.Write(text); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// valueToText translates reflect value to corresponding text. | ||
func valueToText(v reflect.Value) ([]byte, error) { | ||
iface, ok := template.PrintableValue(v) | ||
if !ok { | ||
return nil, fmt.Errorf("can't translate type %s to text", v.Type()) | ||
} | ||
|
||
var buffer bytes.Buffer | ||
|
||
fmt.Fprint(&buffer, iface) | ||
|
||
return buffer.Bytes(), nil | ||
} | ||
|
||
// WriteResource implements output.Writer interface. | ||
func (j *JSONPath) WriteResource(node string, r resource.Resource, event state.EventType) error { | ||
data, err := j.json.prepareEncodableData(node, r, event) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
results, err := j.jsonPath.FindResults(data) | ||
if err != nil { | ||
return fmt.Errorf("error finding result for jsonpath: %w", err) | ||
} | ||
|
||
j.jsonPath.EnableJSONOutput(true) | ||
|
||
for _, resultGroup := range results { | ||
for _, result := range resultGroup { | ||
err = printResult(j.writer, result) | ||
if err != nil { | ||
return fmt.Errorf("error generating jsonpath results: %w", err) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Flush implements output.Writer interface. | ||
func (j *JSONPath) Flush() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package output_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/cosi-project/runtime/pkg/state" | ||
"github.com/stretchr/testify/assert" | ||
"k8s.io/client-go/util/jsonpath" | ||
|
||
"github.com/talos-systems/talos/cmd/talosctl/cmd/talos/output" | ||
"github.com/talos-systems/talos/pkg/machinery/resources/hardware" | ||
) | ||
|
||
func TestWriteResource(t *testing.T) { | ||
node := "123.123.123.123" | ||
event := state.Created | ||
|
||
t.Run("prints scalar values on one line", func(tt *testing.T) { | ||
var buf bytes.Buffer | ||
|
||
// given | ||
expectedID := "myCPU" | ||
processorResource := hardware.NewProcessorInfo(expectedID) | ||
jsonPath := jsonpath.New("talos") | ||
assert.Nil(t, jsonPath.Parse("{.metadata.id}")) | ||
|
||
// when | ||
testObj := output.NewJSONPath(&buf, jsonPath) | ||
err := testObj.WriteResource(node, processorResource, event) | ||
|
||
// then | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, expectedID+"\n", buf.String()) | ||
}) | ||
|
||
t.Run("prints complex values as JSON", func(tt *testing.T) { | ||
var buf bytes.Buffer | ||
|
||
// given | ||
expectedMetadata := `{ | ||
"coreCount": 2 | ||
} | ||
` | ||
processorResource := hardware.NewProcessorInfo("myCPU") | ||
processorResource.TypedSpec().CoreCount = 2 | ||
jsonPath := jsonpath.New("talos") | ||
assert.Nil(t, jsonPath.Parse("{.spec}")) | ||
|
||
// when | ||
testObj := output.NewJSONPath(&buf, jsonPath) | ||
err := testObj.WriteResource(node, processorResource, event) | ||
|
||
// then | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, expectedMetadata, buf.String()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.