mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 10:51:29 +00:00
Remove internal object printing from kubectl
This commit is contained in:
parent
ce9d4a947e
commit
cf3e75de70
@ -384,7 +384,6 @@ package_group(
|
||||
name = "pkg_kubectl_testing_CONSUMERS",
|
||||
packages = [
|
||||
"//pkg/kubectl",
|
||||
"//pkg/printers/internalversion",
|
||||
],
|
||||
)
|
||||
|
||||
|
@ -59,7 +59,6 @@
|
||||
"k8s.io/kubernetes/pkg/features",
|
||||
"k8s.io/kubernetes/pkg/kubectl",
|
||||
"k8s.io/kubernetes/pkg/printers",
|
||||
"k8s.io/kubernetes/pkg/printers/internalversion",
|
||||
"k8s.io/kubernetes/pkg/registry/rbac/reconciliation",
|
||||
"k8s.io/kubernetes/pkg/registry/rbac/validation",
|
||||
"k8s.io/kubernetes/pkg/util/interrupt",
|
||||
|
@ -29,17 +29,14 @@ go_library(
|
||||
importpath = "k8s.io/kubernetes/pkg/kubectl/cmd/get",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//pkg/api/legacyscheme:go_default_library",
|
||||
"//pkg/kubectl/cmd/util:go_default_library",
|
||||
"//pkg/kubectl/cmd/util/openapi:go_default_library",
|
||||
"//pkg/kubectl/scheme:go_default_library",
|
||||
"//pkg/kubectl/util/i18n:go_default_library",
|
||||
"//pkg/printers:go_default_library",
|
||||
"//pkg/printers/internalversion:go_default_library",
|
||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/meta:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/internalversion:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1:go_default_library",
|
||||
@ -51,6 +48,7 @@ go_library(
|
||||
"//staging/src/k8s.io/cli-runtime/pkg/genericclioptions:go_default_library",
|
||||
"//staging/src/k8s.io/cli-runtime/pkg/printers:go_default_library",
|
||||
"//staging/src/k8s.io/cli-runtime/pkg/resource:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/kubernetes/scheme:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/rest:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/tools/watch:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/util/jsonpath:go_default_library",
|
||||
@ -82,7 +80,6 @@ go_test(
|
||||
],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//pkg/apis/core:go_default_library",
|
||||
"//pkg/kubectl/cmd/testing:go_default_library",
|
||||
"//pkg/kubectl/cmd/util/openapi:go_default_library",
|
||||
"//pkg/kubectl/cmd/util/openapi/testing:go_default_library",
|
||||
|
@ -24,29 +24,26 @@ import (
|
||||
"net/url"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"k8s.io/klog"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
kapierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
metainternal "k8s.io/apimachinery/pkg/apis/meta/internalversion"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||
"k8s.io/cli-runtime/pkg/printers"
|
||||
"k8s.io/cli-runtime/pkg/resource"
|
||||
kubernetesscheme "k8s.io/client-go/kubernetes/scheme"
|
||||
"k8s.io/client-go/rest"
|
||||
watchtools "k8s.io/client-go/tools/watch"
|
||||
"k8s.io/kubectl/pkg/util/interrupt"
|
||||
utilprinters "k8s.io/kubectl/pkg/util/printers"
|
||||
"k8s.io/kubectl/pkg/util/templates"
|
||||
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
||||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
||||
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
|
||||
utilpointer "k8s.io/utils/pointer"
|
||||
@ -412,7 +409,7 @@ func NewRuntimeSorter(objects []runtime.Object, sortBy string) *RuntimeSorter {
|
||||
|
||||
return &RuntimeSorter{
|
||||
field: parsedField,
|
||||
decoder: legacyscheme.Codecs.UniversalDecoder(),
|
||||
decoder: kubernetesscheme.Codecs.UniversalDecoder(),
|
||||
objects: objects,
|
||||
}
|
||||
}
|
||||
@ -562,14 +559,7 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
|
||||
continue
|
||||
}
|
||||
|
||||
internalObj, err := legacyscheme.Scheme.ConvertToVersion(info.Object, info.Mapping.GroupVersionKind.GroupKind().WithVersion(runtime.APIVersionInternal).GroupVersion())
|
||||
if err != nil {
|
||||
// if there's an error, try to print what you have (mirrors old behavior).
|
||||
klog.V(1).Info(err)
|
||||
printer.PrintObj(info.Object, w)
|
||||
} else {
|
||||
printer.PrintObj(internalObj, w)
|
||||
}
|
||||
printer.PrintObj(info.Object, w)
|
||||
}
|
||||
w.Flush()
|
||||
if trackingWriter.Written == 0 && !o.IgnoreNotFound && len(allErrs) == 0 {
|
||||
@ -666,8 +656,6 @@ func (o *GetOptions) watch(f cmdutil.Factory, cmd *cobra.Command, args []string)
|
||||
|
||||
writer := utilprinters.GetNewTabWriter(o.Out)
|
||||
|
||||
tableGK := metainternal.SchemeGroupVersion.WithKind("Table").GroupKind()
|
||||
|
||||
// print the current object
|
||||
var objsToPrint []runtime.Object
|
||||
if isList {
|
||||
@ -676,11 +664,6 @@ func (o *GetOptions) watch(f cmdutil.Factory, cmd *cobra.Command, args []string)
|
||||
objsToPrint = append(objsToPrint, obj)
|
||||
}
|
||||
for _, objToPrint := range objsToPrint {
|
||||
if o.IsHumanReadablePrinter && objToPrint.GetObjectKind().GroupVersionKind().GroupKind() != tableGK {
|
||||
// printing anything other than tables always takes the internal version, but the watch event uses externals
|
||||
internalGV := mapping.GroupVersionKind.GroupKind().WithVersion(runtime.APIVersionInternal).GroupVersion()
|
||||
objToPrint = attemptToConvertToInternal(objToPrint, legacyscheme.Scheme, internalGV)
|
||||
}
|
||||
if err := printer.PrintObj(objToPrint, writer); err != nil {
|
||||
return fmt.Errorf("unable to output the provided object: %v", err)
|
||||
}
|
||||
@ -705,14 +688,7 @@ func (o *GetOptions) watch(f cmdutil.Factory, cmd *cobra.Command, args []string)
|
||||
intr := interrupt.New(nil, cancel)
|
||||
intr.Run(func() error {
|
||||
_, err := watchtools.UntilWithoutRetry(ctx, w, func(e watch.Event) (bool, error) {
|
||||
// printing always takes the internal version, but the watch event uses externals
|
||||
// TODO fix printing to use server-side or be version agnostic
|
||||
objToPrint := e.Object
|
||||
if o.IsHumanReadablePrinter && objToPrint.GetObjectKind().GroupVersionKind().GroupKind() != tableGK {
|
||||
internalGV := mapping.GroupVersionKind.GroupKind().WithVersion(runtime.APIVersionInternal).GroupVersion()
|
||||
objToPrint = attemptToConvertToInternal(e.Object, legacyscheme.Scheme, internalGV)
|
||||
}
|
||||
if err := printer.PrintObj(objToPrint, writer); err != nil {
|
||||
if err := printer.PrintObj(e.Object, writer); err != nil {
|
||||
return false, err
|
||||
}
|
||||
writer.Flush()
|
||||
@ -725,16 +701,6 @@ func (o *GetOptions) watch(f cmdutil.Factory, cmd *cobra.Command, args []string)
|
||||
return nil
|
||||
}
|
||||
|
||||
// attemptToConvertToInternal tries to convert to an internal type, but returns the original if it can't
|
||||
func attemptToConvertToInternal(obj runtime.Object, converter runtime.ObjectConvertor, targetVersion schema.GroupVersion) runtime.Object {
|
||||
internalObject, err := converter.ConvertToVersion(obj, targetVersion)
|
||||
if err != nil {
|
||||
klog.V(1).Infof("Unable to convert %T to %v: %v", obj, targetVersion, err)
|
||||
return obj
|
||||
}
|
||||
return internalObject
|
||||
}
|
||||
|
||||
func (o *GetOptions) printGeneric(r *resource.Result) error {
|
||||
// we flattened the data from the builder, so we have individual items, but now we'd like to either:
|
||||
// 1. if there is more than one item, combine them all into a single list
|
||||
|
@ -235,8 +235,8 @@ func TestGetObjects(t *testing.T) {
|
||||
cmd.SetOutput(buf)
|
||||
cmd.Run(cmd, []string{"pods", "foo"})
|
||||
|
||||
expected := `NAME READY STATUS RESTARTS AGE
|
||||
foo 0/0 0 <unknown>
|
||||
expected := `NAME AGE
|
||||
foo <unknown>
|
||||
`
|
||||
if e, a := expected, buf.String(); e != a {
|
||||
t.Errorf("expected\n%v\ngot\n%v", e, a)
|
||||
@ -286,8 +286,8 @@ func TestGetObjectsShowKind(t *testing.T) {
|
||||
cmd.Flags().Set("show-kind", "true")
|
||||
cmd.Run(cmd, []string{"pods", "foo"})
|
||||
|
||||
expected := `NAME READY STATUS RESTARTS AGE
|
||||
pod/foo 0/0 0 <unknown>
|
||||
expected := `NAME AGE
|
||||
pod/foo <unknown>
|
||||
`
|
||||
if e, a := expected, buf.String(); e != a {
|
||||
t.Errorf("expected\n%v\ngot\n%v", e, a)
|
||||
@ -364,11 +364,11 @@ func TestGetMultipleResourceTypesShowKinds(t *testing.T) {
|
||||
cmd.SetOutput(buf)
|
||||
cmd.Run(cmd, []string{"all"})
|
||||
|
||||
expected := `NAME READY STATUS RESTARTS AGE
|
||||
pod/foo 0/0 0 <unknown>
|
||||
pod/bar 0/0 0 <unknown>
|
||||
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
|
||||
service/baz ClusterIP <none> <none> <none> <unknown>
|
||||
expected := `NAME AGE
|
||||
pod/foo <unknown>
|
||||
pod/bar <unknown>
|
||||
NAME AGE
|
||||
service/baz <unknown>
|
||||
`
|
||||
if e, a := expected, buf.String(); e != a {
|
||||
t.Errorf("expected\n%v\ngot\n%v", e, a)
|
||||
@ -448,8 +448,8 @@ func TestGetObjectsShowLabels(t *testing.T) {
|
||||
cmd.Flags().Set("show-labels", "true")
|
||||
cmd.Run(cmd, []string{"pods", "foo"})
|
||||
|
||||
expected := `NAME READY STATUS RESTARTS AGE LABELS
|
||||
foo 0/0 0 <unknown> <none>
|
||||
expected := `NAME AGE LABELS
|
||||
foo <unknown> <none>
|
||||
`
|
||||
if e, a := expected, buf.String(); e != a {
|
||||
t.Errorf("expected\n%v\ngot\n%v", e, a)
|
||||
@ -623,10 +623,10 @@ func TestGetSortedObjects(t *testing.T) {
|
||||
cmd.Flags().Set("sort-by", ".metadata.name")
|
||||
cmd.Run(cmd, []string{"pods"})
|
||||
|
||||
expected := `NAME READY STATUS RESTARTS AGE
|
||||
a 0/0 0 <unknown>
|
||||
b 0/0 0 <unknown>
|
||||
c 0/0 0 <unknown>
|
||||
expected := `NAME AGE
|
||||
a <unknown>
|
||||
b <unknown>
|
||||
c <unknown>
|
||||
`
|
||||
if e, a := expected, buf.String(); e != a {
|
||||
t.Errorf("expected\n%v\ngot\n%v", e, a)
|
||||
@ -892,8 +892,8 @@ func TestGetObjectsIdentifiedByFile(t *testing.T) {
|
||||
cmd.Flags().Set("filename", "../../../../test/e2e/testing-manifests/statefulset/cassandra/controller.yaml")
|
||||
cmd.Run(cmd, []string{})
|
||||
|
||||
expected := `NAME READY STATUS RESTARTS AGE
|
||||
foo 0/0 0 <unknown>
|
||||
expected := `NAME AGE
|
||||
foo <unknown>
|
||||
`
|
||||
if e, a := expected, buf.String(); e != a {
|
||||
t.Errorf("expected\n%v\ngot\n%v", e, a)
|
||||
@ -943,9 +943,9 @@ func TestGetListObjects(t *testing.T) {
|
||||
cmd.SetOutput(buf)
|
||||
cmd.Run(cmd, []string{"pods"})
|
||||
|
||||
expected := `NAME READY STATUS RESTARTS AGE
|
||||
foo 0/0 0 <unknown>
|
||||
bar 0/0 0 <unknown>
|
||||
expected := `NAME AGE
|
||||
foo <unknown>
|
||||
bar <unknown>
|
||||
`
|
||||
if e, a := expected, buf.String(); e != a {
|
||||
t.Errorf("expected\n%v\ngot\n%v", e, a)
|
||||
@ -995,10 +995,10 @@ func TestGetListComponentStatus(t *testing.T) {
|
||||
cmd.SetOutput(buf)
|
||||
cmd.Run(cmd, []string{"componentstatuses"})
|
||||
|
||||
expected := `NAME STATUS MESSAGE ERROR
|
||||
servergood Healthy ok
|
||||
serverbad Unhealthy bad status: 500
|
||||
serverunknown Unhealthy fizzbuzz error
|
||||
expected := `NAME AGE
|
||||
servergood <unknown>
|
||||
serverbad <unknown>
|
||||
serverunknown <unknown>
|
||||
`
|
||||
if e, a := expected, buf.String(); e != a {
|
||||
t.Errorf("expected\n%v\ngot\n%v", e, a)
|
||||
@ -1094,11 +1094,11 @@ func TestGetMultipleTypeObjects(t *testing.T) {
|
||||
cmd.SetOutput(buf)
|
||||
cmd.Run(cmd, []string{"pods,services"})
|
||||
|
||||
expected := `NAME READY STATUS RESTARTS AGE
|
||||
pod/foo 0/0 0 <unknown>
|
||||
pod/bar 0/0 0 <unknown>
|
||||
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
|
||||
service/baz ClusterIP <none> <none> <none> <unknown>
|
||||
expected := `NAME AGE
|
||||
pod/foo <unknown>
|
||||
pod/bar <unknown>
|
||||
NAME AGE
|
||||
service/baz <unknown>
|
||||
`
|
||||
if e, a := expected, buf.String(); e != a {
|
||||
t.Errorf("expected\n%v\ngot\n%v", e, a)
|
||||
@ -1276,11 +1276,11 @@ func TestGetMultipleTypeObjectsWithLabelSelector(t *testing.T) {
|
||||
cmd.Flags().Set("selector", "a=b")
|
||||
cmd.Run(cmd, []string{"pods,services"})
|
||||
|
||||
expected := `NAME READY STATUS RESTARTS AGE
|
||||
pod/foo 0/0 0 <unknown>
|
||||
pod/bar 0/0 0 <unknown>
|
||||
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
|
||||
service/baz ClusterIP <none> <none> <none> <unknown>
|
||||
expected := `NAME AGE
|
||||
pod/foo <unknown>
|
||||
pod/bar <unknown>
|
||||
NAME AGE
|
||||
service/baz <unknown>
|
||||
`
|
||||
if e, a := expected, buf.String(); e != a {
|
||||
t.Errorf("expected\n%v\ngot\n%v", e, a)
|
||||
@ -1362,11 +1362,11 @@ func TestGetMultipleTypeObjectsWithFieldSelector(t *testing.T) {
|
||||
cmd.Flags().Set("field-selector", "a=b")
|
||||
cmd.Run(cmd, []string{"pods,services"})
|
||||
|
||||
expected := `NAME READY STATUS RESTARTS AGE
|
||||
pod/foo 0/0 0 <unknown>
|
||||
pod/bar 0/0 0 <unknown>
|
||||
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
|
||||
service/baz ClusterIP <none> <none> <none> <unknown>
|
||||
expected := `NAME AGE
|
||||
pod/foo <unknown>
|
||||
pod/bar <unknown>
|
||||
NAME AGE
|
||||
service/baz <unknown>
|
||||
`
|
||||
if e, a := expected, buf.String(); e != a {
|
||||
t.Errorf("expected\n%v\ngot\n%v", e, a)
|
||||
@ -1449,10 +1449,10 @@ func TestGetMultipleTypeObjectsWithDirectReference(t *testing.T) {
|
||||
|
||||
cmd.Run(cmd, []string{"services/bar", "node/foo"})
|
||||
|
||||
expected := `NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
|
||||
service/baz ClusterIP <none> <none> <none> <unknown>
|
||||
NAME STATUS ROLES AGE VERSION
|
||||
node/foo Unknown <none> <unknown>
|
||||
expected := `NAME AGE
|
||||
service/baz <unknown>
|
||||
NAME AGE
|
||||
node/foo <unknown>
|
||||
`
|
||||
if e, a := expected, buf.String(); e != a {
|
||||
t.Errorf("expected\n%v\ngot\n%v", e, a)
|
||||
@ -1648,11 +1648,11 @@ func TestWatchLabelSelector(t *testing.T) {
|
||||
cmd.Flags().Set("selector", "a=b")
|
||||
cmd.Run(cmd, []string{"pods"})
|
||||
|
||||
expected := `NAME READY STATUS RESTARTS AGE
|
||||
bar 0/0 0 <unknown>
|
||||
foo 0/0 0 <unknown>
|
||||
foo 0/0 0 <unknown>
|
||||
foo 0/0 0 <unknown>
|
||||
expected := `NAME AGE
|
||||
bar <unknown>
|
||||
foo <unknown>
|
||||
foo <unknown>
|
||||
foo <unknown>
|
||||
`
|
||||
if e, a := expected, buf.String(); e != a {
|
||||
t.Errorf("expected\n%v\ngot\n%v", e, a)
|
||||
@ -1750,11 +1750,11 @@ func TestWatchFieldSelector(t *testing.T) {
|
||||
cmd.Flags().Set("field-selector", "a=b")
|
||||
cmd.Run(cmd, []string{"pods"})
|
||||
|
||||
expected := `NAME READY STATUS RESTARTS AGE
|
||||
bar 0/0 0 <unknown>
|
||||
foo 0/0 0 <unknown>
|
||||
foo 0/0 0 <unknown>
|
||||
foo 0/0 0 <unknown>
|
||||
expected := `NAME AGE
|
||||
bar <unknown>
|
||||
foo <unknown>
|
||||
foo <unknown>
|
||||
foo <unknown>
|
||||
`
|
||||
if e, a := expected, buf.String(); e != a {
|
||||
t.Errorf("expected\n%v\ngot\n%v", e, a)
|
||||
@ -1845,10 +1845,10 @@ func TestWatchResource(t *testing.T) {
|
||||
cmd.Flags().Set("watch", "true")
|
||||
cmd.Run(cmd, []string{"pods", "foo"})
|
||||
|
||||
expected := `NAME READY STATUS RESTARTS AGE
|
||||
foo 0/0 0 <unknown>
|
||||
foo 0/0 0 <unknown>
|
||||
foo 0/0 0 <unknown>
|
||||
expected := `NAME AGE
|
||||
foo <unknown>
|
||||
foo <unknown>
|
||||
foo <unknown>
|
||||
`
|
||||
if e, a := expected, buf.String(); e != a {
|
||||
t.Errorf("expected\n%v\ngot\n%v", e, a)
|
||||
@ -2039,10 +2039,10 @@ func TestWatchResourceIdentifiedByFile(t *testing.T) {
|
||||
cmd.Flags().Set("filename", "../../../../test/e2e/testing-manifests/statefulset/cassandra/controller.yaml")
|
||||
cmd.Run(cmd, []string{})
|
||||
|
||||
expected := `NAME READY STATUS RESTARTS AGE
|
||||
foo 0/0 0 <unknown>
|
||||
foo 0/0 0 <unknown>
|
||||
foo 0/0 0 <unknown>
|
||||
expected := `NAME AGE
|
||||
foo <unknown>
|
||||
foo <unknown>
|
||||
foo <unknown>
|
||||
`
|
||||
if e, a := expected, buf.String(); e != a {
|
||||
t.Errorf("expected\n%v\ngot\n%v", e, a)
|
||||
@ -2082,9 +2082,9 @@ func TestWatchOnlyResource(t *testing.T) {
|
||||
cmd.Flags().Set("watch-only", "true")
|
||||
cmd.Run(cmd, []string{"pods", "foo"})
|
||||
|
||||
expected := `NAME READY STATUS RESTARTS AGE
|
||||
foo 0/0 0 <unknown>
|
||||
foo 0/0 0 <unknown>
|
||||
expected := `NAME AGE
|
||||
foo <unknown>
|
||||
foo <unknown>
|
||||
`
|
||||
if e, a := expected, buf.String(); e != a {
|
||||
t.Errorf("expected\n%v\ngot\n%v", e, a)
|
||||
@ -2169,9 +2169,9 @@ func TestWatchOnlyList(t *testing.T) {
|
||||
cmd.Flags().Set("watch-only", "true")
|
||||
cmd.Run(cmd, []string{"pods"})
|
||||
|
||||
expected := `NAME READY STATUS RESTARTS AGE
|
||||
foo 0/0 0 <unknown>
|
||||
foo 0/0 0 <unknown>
|
||||
expected := `NAME AGE
|
||||
foo <unknown>
|
||||
foo <unknown>
|
||||
`
|
||||
if e, a := expected, buf.String(); e != a {
|
||||
t.Errorf("expected\n%v\ngot\n%v", e, a)
|
||||
|
@ -22,7 +22,6 @@ import (
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/kubernetes/pkg/printers"
|
||||
printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
|
||||
)
|
||||
|
||||
// HumanPrintFlags provides default flags necessary for printing.
|
||||
@ -96,7 +95,6 @@ func (f *HumanPrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrint
|
||||
ColumnLabels: columnLabels,
|
||||
ShowLabels: showLabels,
|
||||
})
|
||||
printersinternal.AddHandlers(p)
|
||||
|
||||
// TODO(juanvallejo): handle sorting here
|
||||
|
||||
|
@ -28,7 +28,6 @@ import (
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
)
|
||||
|
||||
func TestHumanReadablePrinterSupportsExpectedOptions(t *testing.T) {
|
||||
@ -48,19 +47,20 @@ func TestHumanReadablePrinterSupportsExpectedOptions(t *testing.T) {
|
||||
Object: runtime.RawExtension{
|
||||
Object: &corev1.Pod{
|
||||
TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Pod"},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", Labels: map[string]string{"l1": "value"}},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "foons", Labels: map[string]string{"l1": "value"}},
|
||||
},
|
||||
},
|
||||
Cells: []interface{}{"foo", "0/0", "", int64(0), "<unknown>", "<none>", "<none>", "<none>", "<none>"},
|
||||
}},
|
||||
}
|
||||
testPod := &api.Pod{
|
||||
testPod := &corev1.Pod{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: "v1",
|
||||
Kind: "Pod",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo",
|
||||
Name: "foo",
|
||||
Namespace: "foons",
|
||||
Labels: map[string]string{
|
||||
"l1": "value",
|
||||
},
|
||||
@ -89,7 +89,7 @@ func TestHumanReadablePrinterSupportsExpectedOptions(t *testing.T) {
|
||||
{
|
||||
name: "empty output format matches a humanreadable printer",
|
||||
testObject: testPod.DeepCopy(),
|
||||
expectedOutput: "NAME\\ +READY\\ +STATUS\\ +RESTARTS\\ +AGE\nfoo\\ +0/0\\ +0\\ +<unknown>\n",
|
||||
expectedOutput: "NAME\\ +AGE\nfoo\\ +<unknown>\n",
|
||||
},
|
||||
{
|
||||
name: "empty output format matches a humanreadable printer",
|
||||
@ -100,7 +100,7 @@ func TestHumanReadablePrinterSupportsExpectedOptions(t *testing.T) {
|
||||
name: "\"wide\" output format prints",
|
||||
testObject: testPod.DeepCopy(),
|
||||
outputFormat: "wide",
|
||||
expectedOutput: "NAME\\ +READY\\ +STATUS\\ +RESTARTS\\ +AGE\\ +IP\\ +NODE\\ +NOMINATED NODE\\ +READINESS GATES\nfoo\\ +0/0\\ +0\\ +<unknown>\\ +<none>\\ +<none>\\ +<none>\\ +<none>\n",
|
||||
expectedOutput: "NAME\\ +AGE\nfoo\\ +<unknown>\n",
|
||||
},
|
||||
{
|
||||
name: "\"wide\" output format prints",
|
||||
@ -112,7 +112,7 @@ func TestHumanReadablePrinterSupportsExpectedOptions(t *testing.T) {
|
||||
name: "no-headers prints output with no headers",
|
||||
testObject: testPod.DeepCopy(),
|
||||
noHeaders: true,
|
||||
expectedOutput: "foo\\ +0/0\\ +0\\ +<unknown>\n",
|
||||
expectedOutput: "foo\\ +<unknown>\n",
|
||||
},
|
||||
{
|
||||
name: "no-headers prints output with no headers",
|
||||
@ -125,7 +125,7 @@ func TestHumanReadablePrinterSupportsExpectedOptions(t *testing.T) {
|
||||
testObject: testPod.DeepCopy(),
|
||||
outputFormat: "wide",
|
||||
noHeaders: true,
|
||||
expectedOutput: "foo\\ +0/0\\ +0\\ +<unknown>\\ +<none>\\ +<none>\\ +<none>\\ +<none>\n",
|
||||
expectedOutput: "foo\\ +<unknown>\n",
|
||||
},
|
||||
{
|
||||
name: "no-headers and a \"wide\" output format prints output with no headers and additional columns",
|
||||
@ -138,7 +138,7 @@ func TestHumanReadablePrinterSupportsExpectedOptions(t *testing.T) {
|
||||
name: "show-kind displays the resource's kind, even when printing a single type of resource",
|
||||
testObject: testPod.DeepCopy(),
|
||||
showKind: true,
|
||||
expectedOutput: "NAME\\ +READY\\ +STATUS\\ +RESTARTS\\ +AGE\npod/foo\\ +0/0\\ +0\\ +<unknown>\n",
|
||||
expectedOutput: "NAME\\ +AGE\npod/foo\\ +<unknown>\n",
|
||||
},
|
||||
{
|
||||
name: "show-kind displays the resource's kind, even when printing a single type of resource",
|
||||
@ -150,7 +150,7 @@ func TestHumanReadablePrinterSupportsExpectedOptions(t *testing.T) {
|
||||
name: "label-columns prints specified label values in new column",
|
||||
testObject: testPod.DeepCopy(),
|
||||
columnLabels: []string{"l1"},
|
||||
expectedOutput: "NAME\\ +READY\\ +STATUS\\ +RESTARTS\\ +AGE\\ +L1\nfoo\\ +0/0\\ +0\\ +<unknown>\\ +value\n",
|
||||
expectedOutput: "NAME\\ +AGE\\ +L1\nfoo\\ +<unknown>\\ +value\n",
|
||||
},
|
||||
{
|
||||
name: "label-columns prints specified label values in new column",
|
||||
@ -162,13 +162,13 @@ func TestHumanReadablePrinterSupportsExpectedOptions(t *testing.T) {
|
||||
name: "withNamespace displays an additional NAMESPACE column",
|
||||
testObject: testPod.DeepCopy(),
|
||||
withNamespace: true,
|
||||
expectedOutput: "NAMESPACE\\ +NAME\\ +READY\\ +STATUS\\ +RESTARTS\\ +AGE\n\\ +foo\\ +0/0\\ +0\\ +<unknown>\n",
|
||||
expectedOutput: "NAMESPACE\\ +NAME\\ +AGE\nfoons\\ +foo\\ +<unknown>\n",
|
||||
},
|
||||
{
|
||||
name: "withNamespace displays an additional NAMESPACE column",
|
||||
testObject: testTable.DeepCopy(),
|
||||
withNamespace: true,
|
||||
expectedOutput: "NAMESPACE\\ +NAME\\ +READY\\ +STATUS\\ +RESTARTS\\ +AGE\n\\ +foo\\ +0/0\\ +0\\ +<unknown>\n",
|
||||
expectedOutput: "NAMESPACE\\ +NAME\\ +READY\\ +STATUS\\ +RESTARTS\\ +AGE\nfoons\\ +foo\\ +0/0\\ +0\\ +<unknown>\n",
|
||||
},
|
||||
{
|
||||
name: "no printer is matched on an invalid outputFormat",
|
||||
|
@ -44,7 +44,6 @@ source "${KUBE_ROOT}/test/cmd/get.sh"
|
||||
source "${KUBE_ROOT}/test/cmd/kubeadm.sh"
|
||||
source "${KUBE_ROOT}/test/cmd/kubeconfig.sh"
|
||||
source "${KUBE_ROOT}/test/cmd/node-management.sh"
|
||||
source "${KUBE_ROOT}/test/cmd/old-print.sh"
|
||||
source "${KUBE_ROOT}/test/cmd/plugins.sh"
|
||||
source "${KUBE_ROOT}/test/cmd/proxy.sh"
|
||||
source "${KUBE_ROOT}/test/cmd/rbac.sh"
|
||||
@ -503,7 +502,6 @@ runTests() {
|
||||
|
||||
if kube::test::if_supports_resource "${pods}" ; then
|
||||
record_command run_kubectl_get_tests
|
||||
record_command run_kubectl_old_print_tests
|
||||
fi
|
||||
|
||||
################
|
||||
|
@ -1,140 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Copyright 2018 The Kubernetes Authors.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
set -o errexit
|
||||
set -o nounset
|
||||
set -o pipefail
|
||||
|
||||
run_kubectl_old_print_tests() {
|
||||
set -o nounset
|
||||
set -o errexit
|
||||
|
||||
create_and_use_new_namespace
|
||||
kube::log::status "Testing kubectl get --server-print=false"
|
||||
### Test retrieval of all types in discovery
|
||||
# Pre-condition: no resources exist
|
||||
output_message=$(kubectl get pods --server-print=false 2>&1 "${kube_flags[@]:?}")
|
||||
# Post-condition: Expect text indicating no resources were found
|
||||
kube::test::if_has_string "${output_message}" 'No resources found.'
|
||||
|
||||
### Test retrieval of pods against server-side printing
|
||||
kubectl create -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml "${kube_flags[@]}"
|
||||
# Post-condition: valid-pod POD is created
|
||||
kube::test::get_object_assert pods "{{range.items}}{{${id_field:?}}}:{{end}}" 'valid-pod:'
|
||||
# Compare "old" output with experimental output and ensure both are the same
|
||||
# remove the last column, as it contains the object's AGE, which could cause a mismatch.
|
||||
expected_output=$(kubectl get pod "${kube_flags[@]}" | awk 'NF{NF--};1')
|
||||
actual_output=$(kubectl get pod --server-print=false "${kube_flags[@]}" | awk 'NF{NF--};1')
|
||||
kube::test::if_has_string "${actual_output}" "${expected_output}"
|
||||
|
||||
# Test printing objects with --use-openapi-print-columns
|
||||
actual_output=$(kubectl get namespaces --use-openapi-print-columns --v=7 "${kube_flags[@]}" 2>&1)
|
||||
# it should request full objects (not server-side printing)
|
||||
kube::test::if_has_not_string "${actual_output}" 'application/json;as=Table'
|
||||
kube::test::if_has_string "${actual_output}" 'application/json'
|
||||
|
||||
### Test retrieval of daemonsets against server-side printing
|
||||
kubectl apply -f hack/testdata/rollingupdate-daemonset.yaml "${kube_flags[@]}"
|
||||
# Post-condition: daemonset is created
|
||||
kube::test::get_object_assert ds "{{range.items}}{{$id_field}}:{{end}}" 'bind:'
|
||||
# Compare "old" output with experimental output and ensure both are the same
|
||||
# remove the last column, as it contains the object's AGE, which could cause a mismatch.
|
||||
expected_output=$(kubectl get ds "${kube_flags[@]}" | awk 'NF{NF--};1')
|
||||
actual_output=$(kubectl get ds --server-print=false "${kube_flags[@]}" | awk 'NF{NF--};1')
|
||||
kube::test::if_has_string "${actual_output}" "${expected_output}"
|
||||
|
||||
### Test retrieval of replicationcontrollers against server-side printing
|
||||
kubectl create -f hack/testdata/frontend-controller.yaml "${kube_flags[@]}"
|
||||
# Post-condition: frontend replication controller is created
|
||||
kube::test::get_object_assert rc "{{range.items}}{{$id_field}}:{{end}}" 'frontend:'
|
||||
# Compare "old" output with experimental output and ensure both are the same
|
||||
# remove the last column, as it contains the object's AGE, which could cause a mismatch.
|
||||
expected_output=$(kubectl get rc "${kube_flags[@]}" | awk 'NF{NF--};1')
|
||||
actual_output=$(kubectl get rc --server-print=false "${kube_flags[@]}" | awk 'NF{NF--};1')
|
||||
kube::test::if_has_string "${actual_output}" "${expected_output}"
|
||||
|
||||
### Test retrieval of replicasets against server-side printing
|
||||
kubectl create -f hack/testdata/frontend-replicaset.yaml "${kube_flags[@]}"
|
||||
# Post-condition: frontend replica set is created
|
||||
kube::test::get_object_assert rs "{{range.items}}{{$id_field}}:{{end}}" 'frontend:'
|
||||
# Compare "old" output with experimental output and ensure both are the same
|
||||
# remove the last column, as it contains the object's AGE, which could cause a mismatch.
|
||||
expected_output=$(kubectl get rs "${kube_flags[@]}" | awk 'NF{NF--};1')
|
||||
actual_output=$(kubectl get rs --server-print=false "${kube_flags[@]}" | awk 'NF{NF--};1')
|
||||
kube::test::if_has_string "${actual_output}" "${expected_output}"
|
||||
|
||||
### Test retrieval of jobs against server-side printing
|
||||
kubectl run pi --generator=job/v1 "--image=$IMAGE_PERL" --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(20)' "${kube_flags[@]}"
|
||||
# Post-Condition: assertion object exists
|
||||
kube::test::get_object_assert jobs "{{range.items}}{{$id_field}}:{{end}}" 'pi:'
|
||||
# Compare "old" output with experimental output and ensure both are the same
|
||||
# remove the last column, as it contains the object's AGE, which could cause a mismatch.
|
||||
expected_output=$(kubectl get jobs/pi "${kube_flags[@]}" | awk 'NF{NF--};1')
|
||||
actual_output=$(kubectl get jobs/pi --server-print=false "${kube_flags[@]}" | awk 'NF{NF--};1')
|
||||
kube::test::if_has_string "${actual_output}" "${expected_output}"
|
||||
|
||||
### Test retrieval of clusterroles against server-side printing
|
||||
kubectl create "${kube_flags[@]}" clusterrole sample-role --verb=* --resource=pods
|
||||
# Post-Condition: assertion object exists
|
||||
kube::test::get_object_assert clusterrole/sample-role "{{range.rules}}{{range.resources}}{{.}}:{{end}}{{end}}" 'pods:'
|
||||
# Compare "old" output with experimental output and ensure both are the same
|
||||
# remove the last column, as it contains the object's AGE, which could cause a mismatch.
|
||||
expected_output=$(kubectl get clusterroles/sample-role "${kube_flags[@]}" | awk 'NF{NF--};1')
|
||||
actual_output=$(kubectl get clusterroles/sample-role --server-print=false "${kube_flags[@]}" | awk 'NF{NF--};1')
|
||||
kube::test::if_has_string "${actual_output}" "${expected_output}"
|
||||
|
||||
### Test retrieval of crds against server-side printing
|
||||
kubectl "${kube_flags_with_token[@]:?}" create -f - << __EOF__
|
||||
{
|
||||
"kind": "CustomResourceDefinition",
|
||||
"apiVersion": "apiextensions.k8s.io/v1beta1",
|
||||
"metadata": {
|
||||
"name": "foos.company.com"
|
||||
},
|
||||
"spec": {
|
||||
"group": "company.com",
|
||||
"version": "v1",
|
||||
"scope": "Namespaced",
|
||||
"names": {
|
||||
"plural": "foos",
|
||||
"kind": "Foo"
|
||||
}
|
||||
}
|
||||
}
|
||||
__EOF__
|
||||
|
||||
# Post-Condition: assertion object exists
|
||||
kube::test::get_object_assert customresourcedefinitions "{{range.items}}{{if eq $id_field \\\"foos.company.com\\\"}}{{$id_field}}:{{end}}{{end}}" 'foos.company.com:'
|
||||
|
||||
# Test that we can list this new CustomResource
|
||||
kube::test::get_object_assert foos "{{range.items}}{{$id_field}}:{{end}}" ''
|
||||
# Compare "old" output with experimental output and ensure both are the same
|
||||
expected_output=$(kubectl get foos "${kube_flags[@]}" | awk 'NF{NF--};1')
|
||||
actual_output=$(kubectl get foos --server-print=false "${kube_flags[@]}" | awk 'NF{NF--};1')
|
||||
kube::test::if_has_string "${actual_output}" "${expected_output}"
|
||||
|
||||
# teardown
|
||||
kubectl delete customresourcedefinitions/foos.company.com "${kube_flags_with_token[@]}"
|
||||
kubectl delete clusterroles/sample-role "${kube_flags_with_token[@]}"
|
||||
kubectl delete jobs pi "${kube_flags[@]}"
|
||||
kubectl delete rs frontend "${kube_flags[@]}"
|
||||
kubectl delete rc frontend "${kube_flags[@]}"
|
||||
kubectl delete ds bind "${kube_flags[@]}"
|
||||
kubectl delete pod valid-pod "${kube_flags[@]}"
|
||||
|
||||
set +o nounset
|
||||
set +o errexit
|
||||
}
|
Loading…
Reference in New Issue
Block a user