Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove internal object printing from kubectl #79530

Merged
merged 2 commits into from
Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion build/visible_to/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ package_group(
name = "pkg_kubectl_testing_CONSUMERS",
packages = [
"//pkg/kubectl",
"//pkg/printers/internalversion",
],
)

Expand Down
1 change: 0 additions & 1 deletion pkg/kubectl/.import-restrictions
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 1 addition & 4 deletions pkg/kubectl/cmd/get/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
42 changes: 4 additions & 38 deletions pkg/kubectl/cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
liggitt marked this conversation as resolved.
Show resolved Hide resolved
"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"
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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()
Expand All @@ -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
Expand Down