2016-12-15 18:10:33 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// this file contains factories with no other dependencies
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2017-02-19 22:39:43 +00:00
|
|
|
"fmt"
|
2016-12-15 18:10:33 +00:00
|
|
|
"io"
|
2016-11-25 17:37:02 +00:00
|
|
|
"os"
|
2016-12-15 18:10:33 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
2017-02-19 22:39:43 +00:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2017-10-16 11:41:50 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
2016-11-25 17:37:02 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/plugins"
|
2016-12-15 18:10:33 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/resource"
|
2017-02-19 22:39:43 +00:00
|
|
|
"k8s.io/kubernetes/pkg/printers"
|
2016-12-15 18:10:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ring2Factory struct {
|
|
|
|
clientAccessFactory ClientAccessFactory
|
|
|
|
objectMappingFactory ObjectMappingFactory
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBuilderFactory(clientAccessFactory ClientAccessFactory, objectMappingFactory ObjectMappingFactory) BuilderFactory {
|
|
|
|
f := &ring2Factory{
|
|
|
|
clientAccessFactory: clientAccessFactory,
|
|
|
|
objectMappingFactory: objectMappingFactory,
|
|
|
|
}
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2017-05-16 21:52:51 +00:00
|
|
|
func (f *ring2Factory) PrinterForCommand(cmd *cobra.Command, isLocal bool, outputOpts *printers.OutputOptions, options printers.PrintOptions) (printers.ResourcePrinter, error) {
|
|
|
|
var mapper meta.RESTMapper
|
|
|
|
var typer runtime.ObjectTyper
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if isLocal {
|
2017-10-16 11:41:50 +00:00
|
|
|
mapper = legacyscheme.Registry.RESTMapper()
|
|
|
|
typer = legacyscheme.Scheme
|
2017-05-16 21:52:51 +00:00
|
|
|
} else {
|
|
|
|
mapper, typer, err = f.objectMappingFactory.UnstructuredObject()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-25 21:11:48 +00:00
|
|
|
}
|
2017-02-19 22:39:43 +00:00
|
|
|
// TODO: used by the custom column implementation and the name implementation, break this dependency
|
|
|
|
decoders := []runtime.Decoder{f.clientAccessFactory.Decoder(true), unstructured.UnstructuredJSONScheme}
|
2017-05-23 02:47:20 +00:00
|
|
|
encoder := f.clientAccessFactory.JSONEncoder()
|
2017-05-19 21:24:39 +00:00
|
|
|
return PrinterForCommand(cmd, outputOpts, mapper, typer, encoder, decoders, options)
|
2017-02-19 22:39:43 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 21:52:51 +00:00
|
|
|
func (f *ring2Factory) PrinterForMapping(cmd *cobra.Command, isLocal bool, outputOpts *printers.OutputOptions, mapping *meta.RESTMapping, withNamespace bool) (printers.ResourcePrinter, error) {
|
2017-05-23 02:47:20 +00:00
|
|
|
// Some callers do not have "label-columns" so we can't use the GetFlagStringSlice() helper
|
|
|
|
columnLabel, err := cmd.Flags().GetStringSlice("label-columns")
|
|
|
|
if err != nil {
|
|
|
|
columnLabel = []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
options := printers.PrintOptions{
|
|
|
|
NoHeaders: GetFlagBool(cmd, "no-headers"),
|
|
|
|
WithNamespace: withNamespace,
|
|
|
|
Wide: GetWideFlag(cmd),
|
|
|
|
ShowAll: GetFlagBool(cmd, "show-all"),
|
|
|
|
ShowLabels: GetFlagBool(cmd, "show-labels"),
|
|
|
|
AbsoluteTimestamps: isWatch(cmd),
|
|
|
|
ColumnLabels: columnLabel,
|
|
|
|
}
|
|
|
|
|
2017-05-16 21:52:51 +00:00
|
|
|
printer, err := f.PrinterForCommand(cmd, isLocal, outputOpts, options)
|
2017-02-19 22:39:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we output versioned data for generic printers
|
2017-05-23 02:47:20 +00:00
|
|
|
if printer.IsGeneric() {
|
2017-02-19 22:39:43 +00:00
|
|
|
if mapping == nil {
|
|
|
|
return nil, fmt.Errorf("no serialization format found")
|
|
|
|
}
|
|
|
|
version := mapping.GroupVersionKind.GroupVersion()
|
|
|
|
if version.Empty() {
|
|
|
|
return nil, fmt.Errorf("no serialization format found")
|
|
|
|
}
|
|
|
|
|
|
|
|
printer = printers.NewVersionedPrinter(printer, mapping.ObjectConvertor, version, mapping.GroupVersionKind.GroupVersion())
|
2017-05-23 02:47:20 +00:00
|
|
|
|
2017-02-19 22:39:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return printer, nil
|
|
|
|
}
|
|
|
|
|
2017-10-31 15:58:38 +00:00
|
|
|
func (f *ring2Factory) PrintSuccess(mapper meta.RESTMapper, shortOutput bool, out io.Writer, resource, name string, dryRun bool, operation string) {
|
|
|
|
resource, _ = mapper.ResourceSingularizer(resource)
|
|
|
|
dryRunMsg := ""
|
|
|
|
if dryRun {
|
|
|
|
dryRunMsg = " (dry run)"
|
|
|
|
}
|
|
|
|
if shortOutput {
|
|
|
|
// -o name: prints resource/name
|
|
|
|
if len(resource) > 0 {
|
|
|
|
fmt.Fprintf(out, "%s/%s\n", resource, name)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(out, "%s\n", name)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// understandable output by default
|
|
|
|
if len(resource) > 0 {
|
|
|
|
fmt.Fprintf(out, "%s \"%s\" %s%s\n", resource, name, operation, dryRunMsg)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(out, "\"%s\" %s%s\n", name, operation, dryRunMsg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-16 21:52:51 +00:00
|
|
|
func (f *ring2Factory) PrintObject(cmd *cobra.Command, isLocal bool, mapper meta.RESTMapper, obj runtime.Object, out io.Writer) error {
|
2017-01-26 01:48:18 +00:00
|
|
|
// try to get a typed object
|
|
|
|
_, typer := f.objectMappingFactory.Object()
|
|
|
|
gvks, _, err := typer.ObjectKinds(obj)
|
|
|
|
|
|
|
|
// fall back to an unstructured object if we get something unregistered
|
|
|
|
if runtime.IsNotRegisteredError(err) {
|
|
|
|
_, typer, unstructuredErr := f.objectMappingFactory.UnstructuredObject()
|
|
|
|
if unstructuredErr != nil {
|
|
|
|
// if we can't get an unstructured typer, return the original error
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
gvks, _, err = typer.ObjectKinds(obj)
|
|
|
|
}
|
|
|
|
|
2016-12-15 18:10:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-10 18:36:12 +00:00
|
|
|
// Prefer the existing external version if specified
|
|
|
|
var preferredVersion []string
|
|
|
|
if gvks[0].Version != "" && gvks[0].Version != runtime.APIVersionInternal {
|
|
|
|
preferredVersion = []string{gvks[0].Version}
|
|
|
|
}
|
2016-12-15 18:10:33 +00:00
|
|
|
|
2017-11-10 18:36:12 +00:00
|
|
|
mapping, err := mapper.RESTMapping(gvks[0].GroupKind(), preferredVersion...)
|
2016-12-15 18:10:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-16 21:52:51 +00:00
|
|
|
printer, err := f.PrinterForMapping(cmd, isLocal, nil, mapping, false)
|
2016-12-15 18:10:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return printer.PrintObj(obj, out)
|
|
|
|
}
|
|
|
|
|
2017-10-31 18:45:35 +00:00
|
|
|
func (f *ring2Factory) PrintResourceInfoForCommand(cmd *cobra.Command, info *resource.Info, out io.Writer) error {
|
|
|
|
printer, err := f.PrinterForCommand(cmd, false, nil, printers.PrintOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !printer.IsGeneric() {
|
|
|
|
printer, err = f.PrinterForMapping(cmd, false, nil, nil, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return printer.PrintObj(info.Object, out)
|
|
|
|
}
|
|
|
|
|
2017-11-08 01:42:03 +00:00
|
|
|
// NewBuilder returns a new resource builder for structured api objects.
|
2017-08-02 20:23:07 +00:00
|
|
|
func (f *ring2Factory) NewBuilder() *resource.Builder {
|
2017-05-16 21:52:51 +00:00
|
|
|
clientMapperFunc := resource.ClientMapperFunc(f.objectMappingFactory.ClientForMapping)
|
|
|
|
|
2016-12-15 18:10:33 +00:00
|
|
|
mapper, typer := f.objectMappingFactory.Object()
|
2017-03-24 12:02:10 +00:00
|
|
|
categoryExpander := f.objectMappingFactory.CategoryExpander()
|
2016-12-15 18:10:33 +00:00
|
|
|
|
2017-08-02 20:23:07 +00:00
|
|
|
return resource.NewBuilder(mapper, categoryExpander, typer, clientMapperFunc, f.clientAccessFactory.Decoder(true))
|
2016-12-15 18:10:33 +00:00
|
|
|
}
|
2016-11-25 17:37:02 +00:00
|
|
|
|
|
|
|
// PluginLoader loads plugins from a path set by the KUBECTL_PLUGINS_PATH env var.
|
|
|
|
// If this env var is not set, it defaults to
|
|
|
|
// "~/.kube/plugins", plus
|
|
|
|
// "./kubectl/plugins" directory under the "data dir" directory specified by the XDG
|
|
|
|
// system directory structure spec for the given platform.
|
|
|
|
func (f *ring2Factory) PluginLoader() plugins.PluginLoader {
|
|
|
|
if len(os.Getenv("KUBECTL_PLUGINS_PATH")) > 0 {
|
2017-08-31 06:36:17 +00:00
|
|
|
return plugins.KubectlPluginsPathPluginLoader()
|
2016-11-25 17:37:02 +00:00
|
|
|
}
|
|
|
|
return plugins.TolerantMultiPluginLoader{
|
2017-08-31 06:37:39 +00:00
|
|
|
plugins.XDGDataDirsPluginLoader(),
|
2016-11-25 17:37:02 +00:00
|
|
|
plugins.UserDirPluginLoader(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ring2Factory) PluginRunner() plugins.PluginRunner {
|
|
|
|
return &plugins.ExecPluginRunner{}
|
|
|
|
}
|