mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 12:43:23 +00:00
Merge pull request #102991 from soltysh/column_printer
Extract columnPrinter interface for printing the values behind additional columns
This commit is contained in:
commit
eda1298c47
@ -21,6 +21,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
||||||
@ -69,9 +70,14 @@ func New(crdColumns []apiextensionsv1.CustomResourceColumnDefinition) (rest.Tabl
|
|||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type columnPrinter interface {
|
||||||
|
FindResults(data interface{}) ([][]reflect.Value, error)
|
||||||
|
PrintResults(w io.Writer, results []reflect.Value) error
|
||||||
|
}
|
||||||
|
|
||||||
type convertor struct {
|
type convertor struct {
|
||||||
headers []metav1.TableColumnDefinition
|
headers []metav1.TableColumnDefinition
|
||||||
additionalColumns []*jsonpath.JSONPath
|
additionalColumns []columnPrinter
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *convertor) ConvertToTable(ctx context.Context, obj runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
|
func (c *convertor) ConvertToTable(ctx context.Context, obj runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
|
||||||
|
@ -78,7 +78,7 @@ func Test_cellForJSONValue(t *testing.T) {
|
|||||||
func Test_convertor_ConvertToTable(t *testing.T) {
|
func Test_convertor_ConvertToTable(t *testing.T) {
|
||||||
type fields struct {
|
type fields struct {
|
||||||
headers []metav1.TableColumnDefinition
|
headers []metav1.TableColumnDefinition
|
||||||
additionalColumns []*jsonpath.JSONPath
|
additionalColumns []columnPrinter
|
||||||
}
|
}
|
||||||
type args struct {
|
type args struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
@ -212,7 +212,7 @@ func Test_convertor_ConvertToTable(t *testing.T) {
|
|||||||
{Name: "single2", Type: "string"},
|
{Name: "single2", Type: "string"},
|
||||||
{Name: "multi", Type: "string"},
|
{Name: "multi", Type: "string"},
|
||||||
},
|
},
|
||||||
additionalColumns: []*jsonpath.JSONPath{
|
additionalColumns: []columnPrinter{
|
||||||
newJSONPath("valueOnly", "{.spec.servers[0].hosts[0]}"),
|
newJSONPath("valueOnly", "{.spec.servers[0].hosts[0]}"),
|
||||||
newJSONPath("single1", "{.spec.servers[0].hosts}"),
|
newJSONPath("single1", "{.spec.servers[0].hosts}"),
|
||||||
newJSONPath("single2", "{.spec.servers[1].hosts}"),
|
newJSONPath("single2", "{.spec.servers[1].hosts}"),
|
||||||
@ -285,7 +285,7 @@ func Test_convertor_ConvertToTable(t *testing.T) {
|
|||||||
{Name: "single2", Type: "string"},
|
{Name: "single2", Type: "string"},
|
||||||
{Name: "multi", Type: "string"},
|
{Name: "multi", Type: "string"},
|
||||||
},
|
},
|
||||||
additionalColumns: []*jsonpath.JSONPath{
|
additionalColumns: []columnPrinter{
|
||||||
newJSONPath("valueOnly", "{.spec.foo[0].bar[0]}"),
|
newJSONPath("valueOnly", "{.spec.foo[0].bar[0]}"),
|
||||||
newJSONPath("single1", "{.spec.foo[0].bar}"),
|
newJSONPath("single1", "{.spec.foo[0].bar}"),
|
||||||
newJSONPath("single2", "{.spec.foo[1].bar}"),
|
newJSONPath("single2", "{.spec.foo[1].bar}"),
|
||||||
@ -358,7 +358,7 @@ func Test_convertor_ConvertToTable(t *testing.T) {
|
|||||||
{Name: "single2", Type: "integer"},
|
{Name: "single2", Type: "integer"},
|
||||||
{Name: "multi", Type: "integer"},
|
{Name: "multi", Type: "integer"},
|
||||||
},
|
},
|
||||||
additionalColumns: []*jsonpath.JSONPath{
|
additionalColumns: []columnPrinter{
|
||||||
newJSONPath("valueOnly", "{.spec.foo[0].bar[0]}"),
|
newJSONPath("valueOnly", "{.spec.foo[0].bar[0]}"),
|
||||||
newJSONPath("single1", "{.spec.foo[0].bar}"),
|
newJSONPath("single1", "{.spec.foo[0].bar}"),
|
||||||
newJSONPath("single2", "{.spec.foo[1].bar}"),
|
newJSONPath("single2", "{.spec.foo[1].bar}"),
|
||||||
@ -440,7 +440,7 @@ func Test_convertor_ConvertToTable(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newJSONPath(name string, jsonPathExpression string) *jsonpath.JSONPath {
|
func newJSONPath(name string, jsonPathExpression string) columnPrinter {
|
||||||
jp := jsonpath.New(name)
|
jp := jsonpath.New(name)
|
||||||
_ = jp.Parse(jsonPathExpression)
|
_ = jp.Parse(jsonPathExpression)
|
||||||
return jp
|
return jp
|
||||||
|
Loading…
Reference in New Issue
Block a user