Enumerate all versions when looking for an output conversion
Ensures that all objects can be printed, even if they don't match output-version (because they are only implemented in a newer API version).
This commit is contained in:
@@ -138,17 +138,24 @@ func RunGet(f *Factory, out io.Writer, cmd *cobra.Command, args []string) {
|
|||||||
|
|
||||||
// the outermost object will be converted to the output-version
|
// the outermost object will be converted to the output-version
|
||||||
version := util.OutputVersion(cmd, defaultVersion)
|
version := util.OutputVersion(cmd, defaultVersion)
|
||||||
if len(version) == 0 {
|
|
||||||
|
r := b.Flatten().Do()
|
||||||
|
obj, err := r.Object()
|
||||||
|
checkErr(err)
|
||||||
|
|
||||||
|
// try conversion to all the possible versions
|
||||||
|
// TODO: simplify by adding a ResourceBuilder mode
|
||||||
|
versions := []string{version, latest.Version}
|
||||||
|
infos, _ := r.Infos()
|
||||||
|
for _, info := range infos {
|
||||||
|
versions = append(versions, info.Mapping.APIVersion)
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: add a new ResourceBuilder mode for Object() that attempts to ensure the objects
|
// TODO: add a new ResourceBuilder mode for Object() that attempts to ensure the objects
|
||||||
// are in the appropriate version if one exists (and if not, use the best effort).
|
// are in the appropriate version if one exists (and if not, use the best effort).
|
||||||
// TODO: ensure api-version is set with the default preferred api version by the client
|
// TODO: ensure api-version is set with the default preferred api version by the client
|
||||||
// builder on initialization
|
// builder on initialization
|
||||||
version = latest.Version
|
printer := kubectl.NewVersionedPrinter(printer, api.Scheme, versions...)
|
||||||
}
|
|
||||||
printer = kubectl.NewVersionedPrinter(printer, api.Scheme, version)
|
|
||||||
|
|
||||||
obj, err := b.Flatten().Do().Object()
|
|
||||||
checkErr(err)
|
|
||||||
|
|
||||||
err = printer.PrintObj(obj, out)
|
err = printer.PrintObj(obj, out)
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
|
@@ -32,6 +32,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||||
"github.com/docker/docker/pkg/units"
|
"github.com/docker/docker/pkg/units"
|
||||||
"github.com/ghodss/yaml"
|
"github.com/ghodss/yaml"
|
||||||
@@ -98,11 +99,11 @@ func (fn ResourcePrinterFunc) PrintObj(obj runtime.Object, w io.Writer) error {
|
|||||||
type VersionedPrinter struct {
|
type VersionedPrinter struct {
|
||||||
printer ResourcePrinter
|
printer ResourcePrinter
|
||||||
convertor runtime.ObjectConvertor
|
convertor runtime.ObjectConvertor
|
||||||
version string
|
version []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewVersionedPrinter wraps a printer to convert objects to a known API version prior to printing.
|
// NewVersionedPrinter wraps a printer to convert objects to a known API version prior to printing.
|
||||||
func NewVersionedPrinter(printer ResourcePrinter, convertor runtime.ObjectConvertor, version string) ResourcePrinter {
|
func NewVersionedPrinter(printer ResourcePrinter, convertor runtime.ObjectConvertor, version ...string) ResourcePrinter {
|
||||||
return &VersionedPrinter{
|
return &VersionedPrinter{
|
||||||
printer: printer,
|
printer: printer,
|
||||||
convertor: convertor,
|
convertor: convertor,
|
||||||
@@ -115,12 +116,21 @@ func (p *VersionedPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
|
|||||||
if len(p.version) == 0 {
|
if len(p.version) == 0 {
|
||||||
return fmt.Errorf("no version specified, object cannot be converted")
|
return fmt.Errorf("no version specified, object cannot be converted")
|
||||||
}
|
}
|
||||||
converted, err := p.convertor.ConvertToVersion(obj, p.version)
|
for _, version := range p.version {
|
||||||
|
if len(version) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
converted, err := p.convertor.ConvertToVersion(obj, version)
|
||||||
|
if conversion.IsNotRegisteredError(err) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return p.printer.PrintObj(converted, w)
|
return p.printer.PrintObj(converted, w)
|
||||||
}
|
}
|
||||||
|
return fmt.Errorf("the object cannot be converted to any of the versions: %v", p.version)
|
||||||
|
}
|
||||||
|
|
||||||
// JSONPrinter is an implementation of ResourcePrinter which outputs an object as JSON.
|
// JSONPrinter is an implementation of ResourcePrinter which outputs an object as JSON.
|
||||||
type JSONPrinter struct {
|
type JSONPrinter struct {
|
||||||
|
Reference in New Issue
Block a user