2014-10-06 01:24:19 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-10-06 01:24:19 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package kubectl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2015-09-03 04:06:48 +00:00
|
|
|
"os"
|
2014-10-06 01:24:19 +00:00
|
|
|
"reflect"
|
2014-12-16 22:20:51 +00:00
|
|
|
"sort"
|
2014-10-06 01:24:19 +00:00
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
"text/template"
|
2014-12-16 22:20:51 +00:00
|
|
|
"time"
|
2014-10-06 01:24:19 +00:00
|
|
|
|
2015-08-05 22:05:17 +00:00
|
|
|
"github.com/ghodss/yaml"
|
|
|
|
"github.com/golang/glog"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-08-18 07:35:35 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/meta"
|
2015-09-09 21:59:11 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2015-10-09 22:04:41 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
2015-08-16 09:33:35 +00:00
|
|
|
"k8s.io/kubernetes/pkg/labels"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
2015-12-21 05:34:11 +00:00
|
|
|
utilerrors "k8s.io/kubernetes/pkg/util/errors"
|
2015-08-04 08:14:31 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/jsonpath"
|
2015-09-09 17:45:01 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/sets"
|
2014-10-06 01:24:19 +00:00
|
|
|
)
|
|
|
|
|
2015-09-23 21:50:59 +00:00
|
|
|
const (
|
2015-10-14 01:48:06 +00:00
|
|
|
tabwriterMinWidth = 10
|
|
|
|
tabwriterWidth = 4
|
|
|
|
tabwriterPadding = 3
|
|
|
|
tabwriterPadChar = ' '
|
|
|
|
tabwriterFlags = 0
|
2015-09-23 21:50:59 +00:00
|
|
|
)
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
// GetPrinter takes a format type, an optional format argument. It will return true
|
|
|
|
// if the format is generic (untyped), otherwise it will return false. The printer
|
|
|
|
// is agnostic to schema versions, so you must send arguments to PrintObj in the
|
|
|
|
// version you wish them to be shown using a VersionedPrinter (typically when
|
|
|
|
// generic is true).
|
|
|
|
func GetPrinter(format, formatArgument string) (ResourcePrinter, bool, error) {
|
2014-10-06 01:24:19 +00:00
|
|
|
var printer ResourcePrinter
|
|
|
|
switch format {
|
|
|
|
case "json":
|
2014-12-31 00:42:55 +00:00
|
|
|
printer = &JSONPrinter{}
|
2014-10-06 01:24:19 +00:00
|
|
|
case "yaml":
|
2014-12-31 00:42:55 +00:00
|
|
|
printer = &YAMLPrinter{}
|
2015-08-18 07:35:35 +00:00
|
|
|
case "name":
|
2015-12-21 05:34:11 +00:00
|
|
|
printer = &NamePrinter{
|
|
|
|
Typer: runtime.ObjectTyperToTyper(api.Scheme),
|
2015-12-21 05:37:49 +00:00
|
|
|
Decoder: api.Codecs.UniversalDecoder(),
|
2015-12-21 05:34:11 +00:00
|
|
|
}
|
2015-08-21 13:09:41 +00:00
|
|
|
case "template", "go-template":
|
2014-11-18 18:04:10 +00:00
|
|
|
if len(formatArgument) == 0 {
|
2014-12-31 00:42:55 +00:00
|
|
|
return nil, false, fmt.Errorf("template format specified but no template given")
|
2014-10-30 02:32:25 +00:00
|
|
|
}
|
2014-11-07 22:41:59 +00:00
|
|
|
var err error
|
2014-12-31 00:42:55 +00:00
|
|
|
printer, err = NewTemplatePrinter([]byte(formatArgument))
|
2014-10-30 02:32:25 +00:00
|
|
|
if err != nil {
|
2014-12-31 00:42:55 +00:00
|
|
|
return nil, false, fmt.Errorf("error parsing template %s, %v\n", formatArgument, err)
|
2014-10-30 02:32:25 +00:00
|
|
|
}
|
2015-08-21 13:09:41 +00:00
|
|
|
case "templatefile", "go-template-file":
|
2014-11-18 18:04:10 +00:00
|
|
|
if len(formatArgument) == 0 {
|
2014-12-31 00:42:55 +00:00
|
|
|
return nil, false, fmt.Errorf("templatefile format specified but no template file given")
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
2014-11-18 18:04:10 +00:00
|
|
|
data, err := ioutil.ReadFile(formatArgument)
|
2014-10-06 01:24:19 +00:00
|
|
|
if err != nil {
|
2014-12-31 00:42:55 +00:00
|
|
|
return nil, false, fmt.Errorf("error reading template %s, %v\n", formatArgument, err)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
2014-12-31 00:42:55 +00:00
|
|
|
printer, err = NewTemplatePrinter(data)
|
2014-11-07 22:41:59 +00:00
|
|
|
if err != nil {
|
2014-12-31 00:42:55 +00:00
|
|
|
return nil, false, fmt.Errorf("error parsing template %s, %v\n", string(data), err)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
2015-08-04 08:14:31 +00:00
|
|
|
case "jsonpath":
|
|
|
|
if len(formatArgument) == 0 {
|
2015-08-21 13:09:41 +00:00
|
|
|
return nil, false, fmt.Errorf("jsonpath template format specified but no template given")
|
2015-08-04 08:14:31 +00:00
|
|
|
}
|
|
|
|
var err error
|
|
|
|
printer, err = NewJSONPathPrinter(formatArgument)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, fmt.Errorf("error parsing jsonpath %s, %v\n", formatArgument, err)
|
|
|
|
}
|
2015-08-21 13:09:41 +00:00
|
|
|
case "jsonpath-file":
|
|
|
|
if len(formatArgument) == 0 {
|
|
|
|
return nil, false, fmt.Errorf("jsonpath file format specified but no template file file given")
|
|
|
|
}
|
|
|
|
data, err := ioutil.ReadFile(formatArgument)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, fmt.Errorf("error reading template %s, %v\n", formatArgument, err)
|
|
|
|
}
|
|
|
|
printer, err = NewJSONPathPrinter(string(data))
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, fmt.Errorf("error parsing template %s, %v\n", string(data), err)
|
|
|
|
}
|
2015-09-03 04:06:48 +00:00
|
|
|
case "custom-columns":
|
|
|
|
var err error
|
2016-01-27 19:14:27 +00:00
|
|
|
if printer, err = NewCustomColumnsPrinterFromSpec(formatArgument, api.Codecs.UniversalDecoder()); err != nil {
|
2015-09-03 04:06:48 +00:00
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
case "custom-columns-file":
|
|
|
|
file, err := os.Open(formatArgument)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, fmt.Errorf("error reading template %s, %v\n", formatArgument, err)
|
|
|
|
}
|
2016-01-27 19:14:27 +00:00
|
|
|
if printer, err = NewCustomColumnsPrinterFromTemplate(file, api.Codecs.UniversalDecoder()); err != nil {
|
2015-09-03 04:06:48 +00:00
|
|
|
return nil, false, err
|
2015-08-21 13:09:41 +00:00
|
|
|
}
|
2015-06-29 18:36:06 +00:00
|
|
|
case "wide":
|
|
|
|
fallthrough
|
2014-11-01 22:44:03 +00:00
|
|
|
case "":
|
2014-12-31 00:42:55 +00:00
|
|
|
return nil, false, nil
|
2014-11-01 22:44:03 +00:00
|
|
|
default:
|
2014-12-31 00:42:55 +00:00
|
|
|
return nil, false, fmt.Errorf("output format %q not recognized", format)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
2014-12-31 00:42:55 +00:00
|
|
|
return printer, true, nil
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 18:04:10 +00:00
|
|
|
// ResourcePrinter is an interface that knows how to print runtime objects.
|
2014-10-06 01:24:19 +00:00
|
|
|
type ResourcePrinter interface {
|
2014-12-31 00:42:55 +00:00
|
|
|
// Print receives a runtime object, formats it and prints it to a writer.
|
2014-10-06 01:24:19 +00:00
|
|
|
PrintObj(runtime.Object, io.Writer) error
|
2015-08-31 19:04:12 +00:00
|
|
|
HandledResources() []string
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
// ResourcePrinterFunc is a function that can print objects
|
|
|
|
type ResourcePrinterFunc func(runtime.Object, io.Writer) error
|
|
|
|
|
|
|
|
// PrintObj implements ResourcePrinter
|
|
|
|
func (fn ResourcePrinterFunc) PrintObj(obj runtime.Object, w io.Writer) error {
|
|
|
|
return fn(obj, w)
|
|
|
|
}
|
|
|
|
|
2015-08-31 19:04:12 +00:00
|
|
|
// TODO: implement HandledResources()
|
|
|
|
func (fn ResourcePrinterFunc) HandledResources() []string {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
// VersionedPrinter takes runtime objects and ensures they are converted to a given API version
|
|
|
|
// prior to being passed to a nested printer.
|
|
|
|
type VersionedPrinter struct {
|
|
|
|
printer ResourcePrinter
|
2014-11-18 18:04:10 +00:00
|
|
|
convertor runtime.ObjectConvertor
|
2015-12-01 16:52:11 +00:00
|
|
|
versions []unversioned.GroupVersion
|
2014-11-12 00:15:22 +00:00
|
|
|
}
|
2014-10-06 01:24:19 +00:00
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
// NewVersionedPrinter wraps a printer to convert objects to a known API version prior to printing.
|
2015-12-01 16:52:11 +00:00
|
|
|
func NewVersionedPrinter(printer ResourcePrinter, convertor runtime.ObjectConvertor, versions ...unversioned.GroupVersion) ResourcePrinter {
|
2014-12-31 00:42:55 +00:00
|
|
|
return &VersionedPrinter{
|
|
|
|
printer: printer,
|
|
|
|
convertor: convertor,
|
2015-12-01 16:52:11 +00:00
|
|
|
versions: versions,
|
2014-12-31 00:42:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrintObj implements ResourcePrinter
|
|
|
|
func (p *VersionedPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
|
2015-12-01 16:52:11 +00:00
|
|
|
if len(p.versions) == 0 {
|
2015-01-13 23:51:33 +00:00
|
|
|
return fmt.Errorf("no version specified, object cannot be converted")
|
|
|
|
}
|
2015-12-01 16:52:11 +00:00
|
|
|
for _, version := range p.versions {
|
|
|
|
if version.IsEmpty() {
|
2015-03-04 03:18:15 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-12-01 16:52:11 +00:00
|
|
|
converted, err := p.convertor.ConvertToVersion(obj, version.String())
|
2016-01-25 23:47:26 +00:00
|
|
|
if runtime.IsNotRegisteredError(err) {
|
2015-03-04 03:18:15 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.printer.PrintObj(converted, w)
|
2014-11-12 00:15:22 +00:00
|
|
|
}
|
2015-12-01 16:52:11 +00:00
|
|
|
return fmt.Errorf("the object cannot be converted to any of the versions: %v", p.versions)
|
2014-12-31 00:42:55 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 19:04:12 +00:00
|
|
|
// TODO: implement HandledResources()
|
|
|
|
func (p *VersionedPrinter) HandledResources() []string {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
2015-08-18 07:35:35 +00:00
|
|
|
// NamePrinter is an implementation of ResourcePrinter which outputs "resource/name" pair of an object.
|
|
|
|
type NamePrinter struct {
|
2015-12-21 05:34:11 +00:00
|
|
|
Decoder runtime.Decoder
|
|
|
|
Typer runtime.Typer
|
2015-08-18 07:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrintObj is an implementation of ResourcePrinter.PrintObj which decodes the object
|
|
|
|
// and print "resource/name" pair. If the object is a List, print all items in it.
|
|
|
|
func (p *NamePrinter) PrintObj(obj runtime.Object, w io.Writer) error {
|
2015-12-21 05:34:11 +00:00
|
|
|
gvk, _, _ := p.Typer.ObjectKind(obj)
|
2015-12-07 13:12:24 +00:00
|
|
|
|
2015-12-21 05:34:11 +00:00
|
|
|
if meta.IsListType(obj) {
|
|
|
|
items, err := meta.ExtractList(obj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if errs := runtime.DecodeList(items, p.Decoder, runtime.UnstructuredJSONScheme); len(errs) > 0 {
|
|
|
|
return utilerrors.NewAggregate(errs)
|
|
|
|
}
|
|
|
|
for _, obj := range items {
|
|
|
|
if err := p.PrintObj(obj, w); err != nil {
|
|
|
|
return err
|
2015-08-18 07:35:35 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-21 05:34:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: this is wrong, runtime.Unknown and runtime.Unstructured are not handled properly here.
|
|
|
|
|
|
|
|
name := "<unknown>"
|
|
|
|
if acc, err := meta.Accessor(obj); err == nil {
|
|
|
|
if n := acc.GetName(); len(n) > 0 {
|
|
|
|
name = n
|
2015-08-18 07:35:35 +00:00
|
|
|
}
|
2015-12-21 05:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if gvk != nil {
|
|
|
|
// TODO: this is wrong, it assumes that meta knows about all Kinds - should take a RESTMapper
|
|
|
|
_, resource := meta.KindToResource(*gvk, false)
|
2015-08-18 07:35:35 +00:00
|
|
|
|
2015-12-07 13:12:24 +00:00
|
|
|
fmt.Fprintf(w, "%s/%s\n", resource.Resource, name)
|
2015-12-21 05:34:11 +00:00
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "<unknown>/%s\n", name)
|
2015-08-18 07:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-31 19:04:12 +00:00
|
|
|
// TODO: implement HandledResources()
|
|
|
|
func (p *NamePrinter) HandledResources() []string {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
// JSONPrinter is an implementation of ResourcePrinter which outputs an object as JSON.
|
|
|
|
type JSONPrinter struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrintObj is an implementation of ResourcePrinter.PrintObj which simply writes the object to the Writer.
|
|
|
|
func (p *JSONPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
|
|
|
|
data, err := json.Marshal(obj)
|
2014-10-06 01:24:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-11 23:29:01 +00:00
|
|
|
dst := bytes.Buffer{}
|
|
|
|
err = json.Indent(&dst, data, "", " ")
|
|
|
|
dst.WriteByte('\n')
|
|
|
|
_, err = w.Write(dst.Bytes())
|
2014-10-06 01:24:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-31 19:04:12 +00:00
|
|
|
// TODO: implement HandledResources()
|
|
|
|
func (p *JSONPrinter) HandledResources() []string {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
2014-11-18 18:04:10 +00:00
|
|
|
// YAMLPrinter is an implementation of ResourcePrinter which outputs an object as YAML.
|
|
|
|
// The input object is assumed to be in the internal version of an API and is converted
|
|
|
|
// to the given version first.
|
2014-11-12 00:15:22 +00:00
|
|
|
type YAMLPrinter struct {
|
2014-11-18 18:04:10 +00:00
|
|
|
version string
|
|
|
|
convertor runtime.ObjectConvertor
|
2014-11-12 00:15:22 +00:00
|
|
|
}
|
2014-10-06 01:24:19 +00:00
|
|
|
|
|
|
|
// PrintObj prints the data as YAML.
|
2014-11-18 18:04:10 +00:00
|
|
|
func (p *YAMLPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
|
2014-12-31 00:42:55 +00:00
|
|
|
output, err := yaml.Marshal(obj)
|
2014-10-06 01:24:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = fmt.Fprint(w, string(output))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-31 19:04:12 +00:00
|
|
|
// TODO: implement HandledResources()
|
|
|
|
func (p *YAMLPrinter) HandledResources() []string {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
2014-10-06 01:24:19 +00:00
|
|
|
type handlerEntry struct {
|
|
|
|
columns []string
|
|
|
|
printFunc reflect.Value
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
type PrintOptions struct {
|
|
|
|
NoHeaders bool
|
|
|
|
WithNamespace bool
|
|
|
|
Wide bool
|
|
|
|
ShowAll bool
|
2016-02-04 07:08:44 +00:00
|
|
|
ShowLabels bool
|
2016-01-20 15:39:15 +00:00
|
|
|
AbsoluteTimestamps bool
|
|
|
|
ColumnLabels []string
|
2015-11-10 11:34:28 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 01:31:13 +00:00
|
|
|
// HumanReadablePrinter is an implementation of ResourcePrinter which attempts to provide
|
|
|
|
// more elegant output. It is not threadsafe, but you may call PrintObj repeatedly; headers
|
|
|
|
// will only be printed if the object type changes. This makes it useful for printing items
|
2015-08-08 21:29:57 +00:00
|
|
|
// received from watches.
|
2014-10-06 01:24:19 +00:00
|
|
|
type HumanReadablePrinter struct {
|
2015-11-10 11:34:28 +00:00
|
|
|
handlerMap map[reflect.Type]*handlerEntry
|
2016-01-20 15:39:15 +00:00
|
|
|
options PrintOptions
|
2015-11-10 11:34:28 +00:00
|
|
|
lastType reflect.Type
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewHumanReadablePrinter creates a HumanReadablePrinter.
|
2016-02-04 07:08:44 +00:00
|
|
|
func NewHumanReadablePrinter(noHeaders, withNamespace bool, wide bool, showAll bool, showLabels bool, absoluteTimestamps bool, columnLabels []string) *HumanReadablePrinter {
|
2014-10-16 01:54:46 +00:00
|
|
|
printer := &HumanReadablePrinter{
|
2015-11-10 11:34:28 +00:00
|
|
|
handlerMap: make(map[reflect.Type]*handlerEntry),
|
2016-01-20 15:39:15 +00:00
|
|
|
options: PrintOptions{
|
|
|
|
NoHeaders: noHeaders,
|
|
|
|
WithNamespace: withNamespace,
|
|
|
|
Wide: wide,
|
|
|
|
ShowAll: showAll,
|
2016-02-04 07:08:44 +00:00
|
|
|
ShowLabels: showLabels,
|
2016-01-20 15:39:15 +00:00
|
|
|
AbsoluteTimestamps: absoluteTimestamps,
|
|
|
|
ColumnLabels: columnLabels,
|
2015-11-10 11:34:28 +00:00
|
|
|
},
|
2014-10-16 01:54:46 +00:00
|
|
|
}
|
2014-10-06 01:24:19 +00:00
|
|
|
printer.addDefaultHandlers()
|
|
|
|
return printer
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler adds a print handler with a given set of columns to HumanReadablePrinter instance.
|
2015-06-29 18:36:06 +00:00
|
|
|
// See validatePrintHandlerFunc for required method signature.
|
2014-10-06 01:24:19 +00:00
|
|
|
func (h *HumanReadablePrinter) Handler(columns []string, printFunc interface{}) error {
|
|
|
|
printFuncValue := reflect.ValueOf(printFunc)
|
|
|
|
if err := h.validatePrintHandlerFunc(printFuncValue); err != nil {
|
|
|
|
glog.Errorf("Unable to add print handler: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
objType := printFuncValue.Type().In(0)
|
|
|
|
h.handlerMap[objType] = &handlerEntry{
|
|
|
|
columns: columns,
|
|
|
|
printFunc: printFuncValue,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-29 18:36:06 +00:00
|
|
|
// validatePrintHandlerFunc validates print handler signature.
|
|
|
|
// printFunc is the function that will be called to print an object.
|
|
|
|
// It must be of the following type:
|
2015-11-10 11:34:28 +00:00
|
|
|
// func printFunc(object ObjectType, w io.Writer, options PrintOptions) error
|
2015-06-29 18:36:06 +00:00
|
|
|
// where ObjectType is the type of the object that will be printed.
|
2014-10-06 01:24:19 +00:00
|
|
|
func (h *HumanReadablePrinter) validatePrintHandlerFunc(printFunc reflect.Value) error {
|
|
|
|
if printFunc.Kind() != reflect.Func {
|
2015-06-16 16:30:11 +00:00
|
|
|
return fmt.Errorf("invalid print handler. %#v is not a function", printFunc)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
funcType := printFunc.Type()
|
2015-11-10 11:34:28 +00:00
|
|
|
if funcType.NumIn() != 3 || funcType.NumOut() != 1 {
|
2014-11-07 06:56:39 +00:00
|
|
|
return fmt.Errorf("invalid print handler." +
|
2015-11-10 11:34:28 +00:00
|
|
|
"Must accept 3 parameters and return 1 value.")
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
if funcType.In(1) != reflect.TypeOf((*io.Writer)(nil)).Elem() ||
|
2016-01-20 15:39:15 +00:00
|
|
|
funcType.In(2) != reflect.TypeOf((*PrintOptions)(nil)).Elem() ||
|
2014-10-06 01:24:19 +00:00
|
|
|
funcType.Out(0) != reflect.TypeOf((*error)(nil)).Elem() {
|
2014-11-07 06:56:39 +00:00
|
|
|
return fmt.Errorf("invalid print handler. The expected signature is: "+
|
2016-01-20 15:39:15 +00:00
|
|
|
"func handler(obj %v, w io.Writer, options PrintOptions) error", funcType.In(0))
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-17 17:01:57 +00:00
|
|
|
func (h *HumanReadablePrinter) HandledResources() []string {
|
|
|
|
keys := make([]string, 0)
|
|
|
|
|
|
|
|
for k := range h.handlerMap {
|
|
|
|
// k.String looks like "*api.PodList" and we want just "pod"
|
|
|
|
api := strings.Split(k.String(), ".")
|
|
|
|
resource := api[len(api)-1]
|
|
|
|
if strings.HasSuffix(resource, "List") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
resource = strings.ToLower(resource)
|
|
|
|
keys = append(keys, resource)
|
|
|
|
}
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
2015-07-07 19:20:45 +00:00
|
|
|
// NOTE: When adding a new resource type here, please update the list
|
|
|
|
// pkg/kubectl/cmd/get.go to reflect the new resource type.
|
2015-07-01 14:05:08 +00:00
|
|
|
var podColumns = []string{"NAME", "READY", "STATUS", "RESTARTS", "AGE"}
|
2015-03-04 00:54:17 +00:00
|
|
|
var podTemplateColumns = []string{"TEMPLATE", "CONTAINER(S)", "IMAGE(S)", "PODLABELS"}
|
2015-06-18 06:32:03 +00:00
|
|
|
var replicationControllerColumns = []string{"CONTROLLER", "CONTAINER(S)", "IMAGE(S)", "SELECTOR", "REPLICAS", "AGE"}
|
2015-08-21 14:23:12 +00:00
|
|
|
var jobColumns = []string{"JOB", "CONTAINER(S)", "IMAGE(S)", "SELECTOR", "SUCCESSFUL"}
|
2016-01-22 18:40:37 +00:00
|
|
|
var serviceColumns = []string{"NAME", "CLUSTER-IP", "EXTERNAL-IP", "PORT(S)", "SELECTOR", "AGE"}
|
2015-09-23 21:50:59 +00:00
|
|
|
var ingressColumns = []string{"NAME", "RULE", "BACKEND", "ADDRESS"}
|
2015-06-18 06:32:03 +00:00
|
|
|
var endpointColumns = []string{"NAME", "ENDPOINTS", "AGE"}
|
2016-02-04 07:08:44 +00:00
|
|
|
var nodeColumns = []string{"NAME", "STATUS", "AGE"}
|
2015-09-12 16:46:10 +00:00
|
|
|
var daemonSetColumns = []string{"NAME", "CONTAINER(S)", "IMAGE(S)", "SELECTOR", "NODE-SELECTOR"}
|
2015-11-13 22:30:01 +00:00
|
|
|
var eventColumns = []string{"FIRSTSEEN", "LASTSEEN", "COUNT", "NAME", "KIND", "SUBOBJECT", "TYPE", "REASON", "SOURCE", "MESSAGE"}
|
2015-06-18 06:32:03 +00:00
|
|
|
var limitRangeColumns = []string{"NAME", "AGE"}
|
|
|
|
var resourceQuotaColumns = []string{"NAME", "AGE"}
|
2016-02-04 07:08:44 +00:00
|
|
|
var namespaceColumns = []string{"NAME", "STATUS", "AGE"}
|
2015-06-18 06:32:03 +00:00
|
|
|
var secretColumns = []string{"NAME", "TYPE", "DATA", "AGE"}
|
|
|
|
var serviceAccountColumns = []string{"NAME", "SECRETS", "AGE"}
|
2016-02-04 07:08:44 +00:00
|
|
|
var persistentVolumeColumns = []string{"NAME", "CAPACITY", "ACCESSMODES", "STATUS", "CLAIM", "REASON", "AGE"}
|
|
|
|
var persistentVolumeClaimColumns = []string{"NAME", "STATUS", "VOLUME", "CAPACITY", "ACCESSMODES", "AGE"}
|
2015-04-15 19:23:02 +00:00
|
|
|
var componentStatusColumns = []string{"NAME", "STATUS", "MESSAGE", "ERROR"}
|
2015-08-15 05:10:15 +00:00
|
|
|
var thirdPartyResourceColumns = []string{"NAME", "DESCRIPTION", "VERSION(S)"}
|
2015-09-02 10:20:11 +00:00
|
|
|
var horizontalPodAutoscalerColumns = []string{"NAME", "REFERENCE", "TARGET", "CURRENT", "MINPODS", "MAXPODS", "AGE"}
|
2015-07-01 20:56:31 +00:00
|
|
|
var withNamespacePrefixColumns = []string{"NAMESPACE"} // TODO(erictune): print cluster name too.
|
2016-01-22 18:40:37 +00:00
|
|
|
var deploymentColumns = []string{"NAME", "DESIRED", "CURRENT", "UP-TO-DATE", "AVAILABLE", "AGE"}
|
2015-12-15 15:09:42 +00:00
|
|
|
var configMapColumns = []string{"NAME", "DATA", "AGE"}
|
2015-12-14 13:31:23 +00:00
|
|
|
var podSecurityPolicyColumns = []string{"NAME", "PRIV", "CAPS", "VOLUMEPLUGINS", "SELINUX", "RUNASUSER"}
|
2014-10-06 01:24:19 +00:00
|
|
|
|
|
|
|
// addDefaultHandlers adds print handlers for default Kubernetes types.
|
|
|
|
func (h *HumanReadablePrinter) addDefaultHandlers() {
|
|
|
|
h.Handler(podColumns, printPod)
|
|
|
|
h.Handler(podColumns, printPodList)
|
2015-03-04 00:54:17 +00:00
|
|
|
h.Handler(podTemplateColumns, printPodTemplate)
|
|
|
|
h.Handler(podTemplateColumns, printPodTemplateList)
|
2014-10-06 01:24:19 +00:00
|
|
|
h.Handler(replicationControllerColumns, printReplicationController)
|
|
|
|
h.Handler(replicationControllerColumns, printReplicationControllerList)
|
2015-09-12 16:46:10 +00:00
|
|
|
h.Handler(daemonSetColumns, printDaemonSet)
|
|
|
|
h.Handler(daemonSetColumns, printDaemonSetList)
|
2015-08-21 14:23:12 +00:00
|
|
|
h.Handler(jobColumns, printJob)
|
|
|
|
h.Handler(jobColumns, printJobList)
|
2014-10-06 01:24:19 +00:00
|
|
|
h.Handler(serviceColumns, printService)
|
|
|
|
h.Handler(serviceColumns, printServiceList)
|
2015-09-23 21:50:59 +00:00
|
|
|
h.Handler(ingressColumns, printIngress)
|
|
|
|
h.Handler(ingressColumns, printIngressList)
|
2015-02-05 23:45:53 +00:00
|
|
|
h.Handler(endpointColumns, printEndpoints)
|
2015-02-24 02:20:10 +00:00
|
|
|
h.Handler(endpointColumns, printEndpointsList)
|
2015-02-21 17:07:09 +00:00
|
|
|
h.Handler(nodeColumns, printNode)
|
|
|
|
h.Handler(nodeColumns, printNodeList)
|
2014-11-04 02:02:27 +00:00
|
|
|
h.Handler(eventColumns, printEvent)
|
|
|
|
h.Handler(eventColumns, printEventList)
|
2015-01-22 21:52:40 +00:00
|
|
|
h.Handler(limitRangeColumns, printLimitRange)
|
|
|
|
h.Handler(limitRangeColumns, printLimitRangeList)
|
2015-01-23 17:38:30 +00:00
|
|
|
h.Handler(resourceQuotaColumns, printResourceQuota)
|
|
|
|
h.Handler(resourceQuotaColumns, printResourceQuotaList)
|
2015-01-19 21:50:00 +00:00
|
|
|
h.Handler(namespaceColumns, printNamespace)
|
|
|
|
h.Handler(namespaceColumns, printNamespaceList)
|
2015-02-18 01:24:50 +00:00
|
|
|
h.Handler(secretColumns, printSecret)
|
|
|
|
h.Handler(secretColumns, printSecretList)
|
2015-04-27 22:53:28 +00:00
|
|
|
h.Handler(serviceAccountColumns, printServiceAccount)
|
|
|
|
h.Handler(serviceAccountColumns, printServiceAccountList)
|
2015-03-26 19:50:36 +00:00
|
|
|
h.Handler(persistentVolumeClaimColumns, printPersistentVolumeClaim)
|
|
|
|
h.Handler(persistentVolumeClaimColumns, printPersistentVolumeClaimList)
|
|
|
|
h.Handler(persistentVolumeColumns, printPersistentVolume)
|
|
|
|
h.Handler(persistentVolumeColumns, printPersistentVolumeList)
|
2015-04-15 19:23:02 +00:00
|
|
|
h.Handler(componentStatusColumns, printComponentStatus)
|
|
|
|
h.Handler(componentStatusColumns, printComponentStatusList)
|
2015-08-15 05:10:15 +00:00
|
|
|
h.Handler(thirdPartyResourceColumns, printThirdPartyResource)
|
|
|
|
h.Handler(thirdPartyResourceColumns, printThirdPartyResourceList)
|
2015-08-31 21:43:24 +00:00
|
|
|
h.Handler(deploymentColumns, printDeployment)
|
|
|
|
h.Handler(deploymentColumns, printDeploymentList)
|
2015-09-02 10:20:11 +00:00
|
|
|
h.Handler(horizontalPodAutoscalerColumns, printHorizontalPodAutoscaler)
|
|
|
|
h.Handler(horizontalPodAutoscalerColumns, printHorizontalPodAutoscalerList)
|
2015-12-15 15:09:42 +00:00
|
|
|
h.Handler(configMapColumns, printConfigMap)
|
|
|
|
h.Handler(configMapColumns, printConfigMapList)
|
2015-12-14 13:31:23 +00:00
|
|
|
h.Handler(podSecurityPolicyColumns, printPodSecurityPolicy)
|
|
|
|
h.Handler(podSecurityPolicyColumns, printPodSecurityPolicyList)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HumanReadablePrinter) unknown(data []byte, w io.Writer) error {
|
|
|
|
_, err := fmt.Fprintf(w, "Unknown object: %s", string(data))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HumanReadablePrinter) printHeader(columnNames []string, w io.Writer) error {
|
|
|
|
if _, err := fmt.Fprintf(w, "%s\n", strings.Join(columnNames, "\t")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-28 05:17:51 +00:00
|
|
|
// Pass ports=nil for all ports.
|
2015-09-09 17:45:01 +00:00
|
|
|
func formatEndpoints(endpoints *api.Endpoints, ports sets.String) string {
|
2015-03-20 21:24:43 +00:00
|
|
|
if len(endpoints.Subsets) == 0 {
|
2015-02-21 23:13:28 +00:00
|
|
|
return "<none>"
|
2015-02-05 23:45:53 +00:00
|
|
|
}
|
2015-02-19 03:54:15 +00:00
|
|
|
list := []string{}
|
2015-03-20 21:24:43 +00:00
|
|
|
max := 3
|
|
|
|
more := false
|
2015-05-25 09:44:56 +00:00
|
|
|
count := 0
|
2015-03-20 21:24:43 +00:00
|
|
|
for i := range endpoints.Subsets {
|
|
|
|
ss := &endpoints.Subsets[i]
|
|
|
|
for i := range ss.Ports {
|
|
|
|
port := &ss.Ports[i]
|
2015-04-28 05:17:51 +00:00
|
|
|
if ports == nil || ports.Has(port.Name) {
|
2015-03-20 21:24:43 +00:00
|
|
|
for i := range ss.Addresses {
|
|
|
|
if len(list) == max {
|
|
|
|
more = true
|
|
|
|
}
|
|
|
|
addr := &ss.Addresses[i]
|
2015-05-25 09:44:56 +00:00
|
|
|
if !more {
|
|
|
|
list = append(list, fmt.Sprintf("%s:%d", addr.IP, port.Port))
|
|
|
|
}
|
|
|
|
count++
|
2015-03-20 21:24:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret := strings.Join(list, ",")
|
|
|
|
if more {
|
2015-05-25 09:44:56 +00:00
|
|
|
return fmt.Sprintf("%s + %d more...", ret, count-max)
|
2015-02-23 21:53:21 +00:00
|
|
|
}
|
2015-03-20 21:24:43 +00:00
|
|
|
return ret
|
2015-02-05 23:45:53 +00:00
|
|
|
}
|
|
|
|
|
2015-05-30 01:42:44 +00:00
|
|
|
func shortHumanDuration(d time.Duration) string {
|
2015-08-14 09:10:22 +00:00
|
|
|
// Allow deviation no more than 2 seconds(excluded) to tolerate machine time
|
|
|
|
// inconsistence, it can be considered as almost now.
|
|
|
|
if seconds := int(d.Seconds()); seconds < -1 {
|
|
|
|
return fmt.Sprintf("<invalid>")
|
|
|
|
} else if seconds < 0 {
|
|
|
|
return fmt.Sprintf("0s")
|
|
|
|
} else if seconds < 60 {
|
2015-05-30 01:42:44 +00:00
|
|
|
return fmt.Sprintf("%ds", seconds)
|
|
|
|
} else if minutes := int(d.Minutes()); minutes < 60 {
|
|
|
|
return fmt.Sprintf("%dm", minutes)
|
|
|
|
} else if hours := int(d.Hours()); hours < 24 {
|
|
|
|
return fmt.Sprintf("%dh", hours)
|
|
|
|
} else if hours < 24*364 {
|
|
|
|
return fmt.Sprintf("%dd", hours/24)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%dy", int(d.Hours()/24/365))
|
|
|
|
}
|
|
|
|
|
2015-04-20 23:38:21 +00:00
|
|
|
// translateTimestamp returns the elapsed time since timestamp in
|
|
|
|
// human-readable approximation.
|
2015-09-17 22:21:55 +00:00
|
|
|
func translateTimestamp(timestamp unversioned.Time) string {
|
2015-08-13 08:23:10 +00:00
|
|
|
if timestamp.IsZero() {
|
|
|
|
return "<unknown>"
|
|
|
|
}
|
2015-05-30 01:42:44 +00:00
|
|
|
return shortHumanDuration(time.Now().Sub(timestamp.Time))
|
2015-04-20 23:38:21 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printPod(pod *api.Pod, w io.Writer, options PrintOptions) error {
|
2015-12-03 19:32:22 +00:00
|
|
|
return printPodBase(pod, w, options)
|
2015-09-06 11:39:58 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printPodBase(pod *api.Pod, w io.Writer, options PrintOptions) error {
|
2015-05-30 01:42:44 +00:00
|
|
|
name := pod.Name
|
2015-07-01 20:56:31 +00:00
|
|
|
namespace := pod.Namespace
|
2015-05-20 19:51:35 +00:00
|
|
|
|
2015-05-30 01:42:44 +00:00
|
|
|
restarts := 0
|
|
|
|
totalContainers := len(pod.Spec.Containers)
|
|
|
|
readyContainers := 0
|
2015-06-09 15:58:16 +00:00
|
|
|
|
2015-05-30 01:42:44 +00:00
|
|
|
reason := string(pod.Status.Phase)
|
2015-07-31 23:42:34 +00:00
|
|
|
// if not printing all pods, skip terminated pods (default)
|
2016-01-20 15:39:15 +00:00
|
|
|
if !options.ShowAll && (reason == string(api.PodSucceeded) || reason == string(api.PodFailed)) {
|
2015-07-31 23:42:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-06-09 15:58:16 +00:00
|
|
|
if pod.Status.Reason != "" {
|
|
|
|
reason = pod.Status.Reason
|
|
|
|
}
|
2015-05-30 01:42:44 +00:00
|
|
|
|
|
|
|
for i := len(pod.Status.ContainerStatuses) - 1; i >= 0; i-- {
|
|
|
|
container := pod.Status.ContainerStatuses[i]
|
|
|
|
|
|
|
|
restarts += container.RestartCount
|
|
|
|
if container.State.Waiting != nil && container.State.Waiting.Reason != "" {
|
|
|
|
reason = container.State.Waiting.Reason
|
|
|
|
} else if container.State.Terminated != nil && container.State.Terminated.Reason != "" {
|
|
|
|
reason = container.State.Terminated.Reason
|
|
|
|
} else if container.State.Terminated != nil && container.State.Terminated.Reason == "" {
|
|
|
|
if container.State.Terminated.Signal != 0 {
|
|
|
|
reason = fmt.Sprintf("Signal:%d", container.State.Terminated.Signal)
|
|
|
|
} else {
|
|
|
|
reason = fmt.Sprintf("ExitCode:%d", container.State.Terminated.ExitCode)
|
2015-04-20 23:38:21 +00:00
|
|
|
}
|
2015-05-30 01:42:44 +00:00
|
|
|
} else if container.Ready && container.State.Running != nil {
|
|
|
|
readyContainers++
|
2015-04-20 23:38:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-20 01:52:34 +00:00
|
|
|
if pod.DeletionTimestamp != nil {
|
|
|
|
reason = "Terminating"
|
|
|
|
}
|
2015-04-20 23:38:21 +00:00
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-07-01 20:56:31 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-06-16 16:30:11 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t%d/%d\t%s\t%d\t%s",
|
2015-05-30 01:42:44 +00:00
|
|
|
name,
|
|
|
|
readyContainers,
|
|
|
|
totalContainers,
|
|
|
|
reason,
|
|
|
|
restarts,
|
2015-06-16 16:30:11 +00:00
|
|
|
translateTimestamp(pod.CreationTimestamp),
|
|
|
|
); err != nil {
|
2015-05-30 01:42:44 +00:00
|
|
|
return err
|
2014-11-15 00:20:43 +00:00
|
|
|
}
|
2015-06-29 18:36:06 +00:00
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.Wide {
|
2015-06-29 18:36:06 +00:00
|
|
|
nodeName := pod.Spec.NodeName
|
|
|
|
if _, err := fmt.Fprintf(w, "\t%s",
|
|
|
|
nodeName,
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabels(pod.Labels, options.ColumnLabels)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, pod.Labels)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printPodList(podList *api.PodList, w io.Writer, options PrintOptions) error {
|
2014-10-06 01:24:19 +00:00
|
|
|
for _, pod := range podList.Items {
|
2015-12-03 19:32:22 +00:00
|
|
|
if err := printPodBase(&pod, w, options); err != nil {
|
2014-10-06 01:24:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printPodTemplate(pod *api.PodTemplate, w io.Writer, options PrintOptions) error {
|
2015-07-01 20:56:31 +00:00
|
|
|
name := pod.Name
|
|
|
|
namespace := pod.Namespace
|
2015-05-20 19:51:35 +00:00
|
|
|
|
2015-03-04 15:46:27 +00:00
|
|
|
containers := pod.Template.Spec.Containers
|
2015-03-04 00:54:17 +00:00
|
|
|
var firstContainer api.Container
|
|
|
|
if len(containers) > 0 {
|
|
|
|
firstContainer, containers = containers[0], containers[1:]
|
|
|
|
}
|
2015-06-16 16:30:11 +00:00
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-07-01 20:56:31 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-06-16 16:30:11 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s",
|
2015-05-20 19:51:35 +00:00
|
|
|
name,
|
2015-03-04 00:54:17 +00:00
|
|
|
firstContainer.Name,
|
|
|
|
firstContainer.Image,
|
2015-08-16 09:33:35 +00:00
|
|
|
labels.FormatLabels(pod.Template.Labels),
|
2015-06-16 16:30:11 +00:00
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-20 15:39:15 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabels(pod.Labels, options.ColumnLabels)); err != nil {
|
2015-03-04 00:54:17 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, pod.Labels)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-16 16:30:11 +00:00
|
|
|
|
2015-03-04 00:54:17 +00:00
|
|
|
// Lay out all the other containers on separate lines.
|
2015-07-07 18:35:24 +00:00
|
|
|
extraLinePrefix := "\t"
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-07-07 18:35:24 +00:00
|
|
|
extraLinePrefix = "\t\t"
|
|
|
|
}
|
2015-03-04 00:54:17 +00:00
|
|
|
for _, container := range containers {
|
2015-07-07 18:35:24 +00:00
|
|
|
_, err := fmt.Fprintf(w, "%s%s\t%s\t%s", extraLinePrefix, container.Name, container.Image, "")
|
2015-03-04 00:54:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-20 15:39:15 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabelTabs(options.ColumnLabels)); err != nil {
|
2015-07-07 18:35:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-03-04 00:54:17 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printPodTemplateList(podList *api.PodTemplateList, w io.Writer, options PrintOptions) error {
|
2015-03-04 00:54:17 +00:00
|
|
|
for _, pod := range podList.Items {
|
2015-11-10 11:34:28 +00:00
|
|
|
if err := printPodTemplate(&pod, w, options); err != nil {
|
2015-03-04 00:54:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printReplicationController(controller *api.ReplicationController, w io.Writer, options PrintOptions) error {
|
2015-07-01 20:56:31 +00:00
|
|
|
name := controller.Name
|
|
|
|
namespace := controller.Namespace
|
2014-12-19 02:15:47 +00:00
|
|
|
containers := controller.Spec.Template.Spec.Containers
|
|
|
|
var firstContainer api.Container
|
|
|
|
if len(containers) > 0 {
|
|
|
|
firstContainer, containers = containers[0], containers[1:]
|
|
|
|
}
|
2015-06-16 16:30:11 +00:00
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-07-01 20:56:31 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-06-18 06:32:03 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d\t%s",
|
2015-05-20 19:51:35 +00:00
|
|
|
name,
|
2014-12-19 02:15:47 +00:00
|
|
|
firstContainer.Name,
|
|
|
|
firstContainer.Image,
|
2015-08-16 09:33:35 +00:00
|
|
|
labels.FormatLabels(controller.Spec.Selector),
|
2015-06-16 16:30:11 +00:00
|
|
|
controller.Spec.Replicas,
|
2015-06-18 06:32:03 +00:00
|
|
|
translateTimestamp(controller.CreationTimestamp),
|
2015-06-16 16:30:11 +00:00
|
|
|
); err != nil {
|
2014-12-19 02:15:47 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-01-20 15:39:15 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabels(controller.Labels, options.ColumnLabels)); err != nil {
|
2015-06-16 16:30:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, controller.Labels)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-16 16:30:11 +00:00
|
|
|
|
2014-12-19 02:15:47 +00:00
|
|
|
// Lay out all the other containers on separate lines.
|
2015-07-07 18:35:24 +00:00
|
|
|
extraLinePrefix := "\t"
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-07-07 18:35:24 +00:00
|
|
|
extraLinePrefix = "\t\t"
|
|
|
|
}
|
2014-12-19 02:15:47 +00:00
|
|
|
for _, container := range containers {
|
2015-07-07 18:35:24 +00:00
|
|
|
_, err := fmt.Fprintf(w, "%s%s\t%s\t%s\t%s", extraLinePrefix, container.Name, container.Image, "", "")
|
2014-12-19 02:15:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-20 15:39:15 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabelTabs(options.ColumnLabels)); err != nil {
|
2015-07-07 18:35:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-12-19 02:15:47 +00:00
|
|
|
}
|
|
|
|
return nil
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printReplicationControllerList(list *api.ReplicationControllerList, w io.Writer, options PrintOptions) error {
|
2014-10-22 17:02:02 +00:00
|
|
|
for _, controller := range list.Items {
|
2015-11-10 11:34:28 +00:00
|
|
|
if err := printReplicationController(&controller, w, options); err != nil {
|
2014-10-06 01:24:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printJob(job *extensions.Job, w io.Writer, options PrintOptions) error {
|
2015-09-26 04:45:18 +00:00
|
|
|
name := job.Name
|
|
|
|
namespace := job.Namespace
|
2015-08-21 14:23:12 +00:00
|
|
|
containers := job.Spec.Template.Spec.Containers
|
|
|
|
var firstContainer api.Container
|
|
|
|
if len(containers) > 0 {
|
2015-09-26 04:45:18 +00:00
|
|
|
firstContainer, containers = containers[0], containers[1:]
|
|
|
|
}
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-09-26 04:45:18 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-21 14:23:12 +00:00
|
|
|
}
|
2015-10-14 18:04:33 +00:00
|
|
|
|
2016-02-02 05:34:42 +00:00
|
|
|
selector, _ := unversioned.LabelSelectorAsSelector(job.Spec.Selector)
|
2015-10-27 00:23:56 +00:00
|
|
|
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d",
|
2015-09-26 04:45:18 +00:00
|
|
|
name,
|
2015-08-21 14:23:12 +00:00
|
|
|
firstContainer.Name,
|
|
|
|
firstContainer.Image,
|
2015-10-14 18:04:33 +00:00
|
|
|
selector.String(),
|
2015-10-08 17:33:39 +00:00
|
|
|
job.Status.Succeeded)
|
2015-08-21 14:23:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-20 15:39:15 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabels(job.Labels, options.ColumnLabels)); err != nil {
|
2015-09-26 04:45:18 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, job.Labels)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-26 04:45:18 +00:00
|
|
|
|
|
|
|
// Lay out all the other containers on separate lines.
|
|
|
|
extraLinePrefix := "\t"
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-09-26 04:45:18 +00:00
|
|
|
extraLinePrefix = "\t\t"
|
|
|
|
}
|
|
|
|
for _, container := range containers {
|
|
|
|
_, err := fmt.Fprintf(w, "%s%s\t%s\t%s\t%s", extraLinePrefix, container.Name, container.Image, "", "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-20 15:39:15 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabelTabs(options.ColumnLabels)); err != nil {
|
2015-09-26 04:45:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-08-21 14:23:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printJobList(list *extensions.JobList, w io.Writer, options PrintOptions) error {
|
2015-08-21 14:23:12 +00:00
|
|
|
for _, job := range list.Items {
|
2015-11-10 11:34:28 +00:00
|
|
|
if err := printJob(&job, w, options); err != nil {
|
2015-08-21 14:23:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 21:50:59 +00:00
|
|
|
// loadBalancerStatusStringer behaves just like a string interface and converts the given status to a string.
|
|
|
|
func loadBalancerStatusStringer(s api.LoadBalancerStatus) string {
|
|
|
|
ingress := s.Ingress
|
|
|
|
result := []string{}
|
|
|
|
for i := range ingress {
|
|
|
|
if ingress[i].IP != "" {
|
|
|
|
result = append(result, ingress[i].IP)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(result, ",")
|
|
|
|
}
|
|
|
|
|
2015-08-08 04:08:43 +00:00
|
|
|
func getServiceExternalIP(svc *api.Service) string {
|
|
|
|
switch svc.Spec.Type {
|
|
|
|
case api.ServiceTypeClusterIP:
|
2015-08-12 00:18:21 +00:00
|
|
|
if len(svc.Spec.ExternalIPs) > 0 {
|
|
|
|
return strings.Join(svc.Spec.ExternalIPs, ",")
|
|
|
|
}
|
2015-08-08 04:08:43 +00:00
|
|
|
return "<none>"
|
|
|
|
case api.ServiceTypeNodePort:
|
2015-08-12 00:18:21 +00:00
|
|
|
if len(svc.Spec.ExternalIPs) > 0 {
|
|
|
|
return strings.Join(svc.Spec.ExternalIPs, ",")
|
|
|
|
}
|
2015-08-08 04:08:43 +00:00
|
|
|
return "nodes"
|
|
|
|
case api.ServiceTypeLoadBalancer:
|
2015-09-23 21:50:59 +00:00
|
|
|
lbIps := loadBalancerStatusStringer(svc.Status.LoadBalancer)
|
2015-08-12 00:18:21 +00:00
|
|
|
if len(svc.Spec.ExternalIPs) > 0 {
|
2015-09-23 21:50:59 +00:00
|
|
|
result := append(strings.Split(lbIps, ","), svc.Spec.ExternalIPs...)
|
|
|
|
return strings.Join(result, ",")
|
2015-08-12 00:18:21 +00:00
|
|
|
}
|
2015-09-23 21:50:59 +00:00
|
|
|
return lbIps
|
2015-08-08 04:08:43 +00:00
|
|
|
}
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
func makePortString(ports []api.ServicePort) string {
|
|
|
|
pieces := make([]string, len(ports))
|
|
|
|
for ix := range ports {
|
|
|
|
port := &ports[ix]
|
|
|
|
pieces[ix] = fmt.Sprintf("%d/%s", port.Port, port.Protocol)
|
|
|
|
}
|
|
|
|
return strings.Join(pieces, ",")
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printService(svc *api.Service, w io.Writer, options PrintOptions) error {
|
2015-07-01 20:56:31 +00:00
|
|
|
name := svc.Name
|
|
|
|
namespace := svc.Namespace
|
2015-05-20 19:51:35 +00:00
|
|
|
|
2015-08-08 04:08:43 +00:00
|
|
|
internalIP := svc.Spec.ClusterIP
|
|
|
|
externalIP := getServiceExternalIP(svc)
|
2015-05-20 19:51:35 +00:00
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-07-01 20:56:31 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-08-08 04:08:43 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s",
|
2015-06-18 06:32:03 +00:00
|
|
|
name,
|
2015-08-08 04:08:43 +00:00
|
|
|
internalIP,
|
|
|
|
externalIP,
|
|
|
|
makePortString(svc.Spec.Ports),
|
2015-08-16 09:33:35 +00:00
|
|
|
labels.FormatLabels(svc.Spec.Selector),
|
2015-06-18 06:32:03 +00:00
|
|
|
translateTimestamp(svc.CreationTimestamp),
|
|
|
|
); err != nil {
|
2015-03-13 15:16:41 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-01-20 15:39:15 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabels(svc.Labels, options.ColumnLabels)); err != nil {
|
2015-06-16 16:30:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-02-04 07:08:44 +00:00
|
|
|
_, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, svc.Labels))
|
|
|
|
return err
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printServiceList(list *api.ServiceList, w io.Writer, options PrintOptions) error {
|
2014-10-06 01:24:19 +00:00
|
|
|
for _, svc := range list.Items {
|
2015-11-10 11:34:28 +00:00
|
|
|
if err := printService(&svc, w, options); err != nil {
|
2014-10-06 01:24:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 21:50:59 +00:00
|
|
|
// backendStringer behaves just like a string interface and converts the given backend to a string.
|
2015-10-09 22:49:10 +00:00
|
|
|
func backendStringer(backend *extensions.IngressBackend) string {
|
2015-09-23 21:50:59 +00:00
|
|
|
if backend == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%v:%v", backend.ServiceName, backend.ServicePort.String())
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printIngress(ingress *extensions.Ingress, w io.Writer, options PrintOptions) error {
|
2015-09-23 21:50:59 +00:00
|
|
|
name := ingress.Name
|
|
|
|
namespace := ingress.Namespace
|
|
|
|
|
|
|
|
hostRules := ingress.Spec.Rules
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-09-23 21:50:59 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t%v\t%v\t%v",
|
2015-09-23 21:50:59 +00:00
|
|
|
name,
|
|
|
|
"-",
|
|
|
|
backendStringer(ingress.Spec.Backend),
|
|
|
|
loadBalancerStatusStringer(ingress.Status.LoadBalancer)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, ingress.Labels)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-23 21:50:59 +00:00
|
|
|
// Lay out all the rules on separate lines.
|
|
|
|
extraLinePrefix := ""
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-09-23 21:50:59 +00:00
|
|
|
extraLinePrefix = "\t"
|
|
|
|
}
|
|
|
|
for _, rules := range hostRules {
|
|
|
|
if rules.HTTP == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprintf(w, "%s\t%v\t", extraLinePrefix, rules.Host)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-20 15:39:15 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabelTabs(options.ColumnLabels)); err != nil {
|
2015-09-23 21:50:59 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, rule := range rules.HTTP.Paths {
|
|
|
|
_, err := fmt.Fprintf(w, "%s\t%v\t%v", extraLinePrefix, rule.Path, backendStringer(&rule.Backend))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-20 15:39:15 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabelTabs(options.ColumnLabels)); err != nil {
|
2015-09-23 21:50:59 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printIngressList(ingressList *extensions.IngressList, w io.Writer, options PrintOptions) error {
|
2015-09-23 21:50:59 +00:00
|
|
|
for _, ingress := range ingressList.Items {
|
2015-11-10 11:34:28 +00:00
|
|
|
if err := printIngress(&ingress, w, options); err != nil {
|
2015-09-23 21:50:59 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printDaemonSet(ds *extensions.DaemonSet, w io.Writer, options PrintOptions) error {
|
2015-09-12 16:46:10 +00:00
|
|
|
name := ds.Name
|
|
|
|
namespace := ds.Namespace
|
2015-08-27 17:18:16 +00:00
|
|
|
|
2015-09-12 16:46:10 +00:00
|
|
|
containers := ds.Spec.Template.Spec.Containers
|
2015-08-27 17:18:16 +00:00
|
|
|
var firstContainer api.Container
|
|
|
|
if len(containers) > 0 {
|
|
|
|
firstContainer, containers = containers[0], containers[1:]
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-08-27 17:18:16 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-02-02 05:34:42 +00:00
|
|
|
selector, err := unversioned.LabelSelectorAsSelector(ds.Spec.Selector)
|
2015-10-26 06:11:09 +00:00
|
|
|
if err != nil {
|
2015-12-01 09:24:21 +00:00
|
|
|
// this shouldn't happen if LabelSelector passed validation
|
2015-10-26 06:11:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-09-12 16:46:10 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s",
|
2015-08-27 17:18:16 +00:00
|
|
|
name,
|
|
|
|
firstContainer.Name,
|
|
|
|
firstContainer.Image,
|
2015-10-26 06:11:09 +00:00
|
|
|
selector,
|
2015-09-12 16:46:10 +00:00
|
|
|
labels.FormatLabels(ds.Spec.Template.Spec.NodeSelector),
|
2015-08-27 17:18:16 +00:00
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-20 15:39:15 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabels(ds.Labels, options.ColumnLabels)); err != nil {
|
2015-08-27 17:18:16 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, ds.Labels)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-27 17:18:16 +00:00
|
|
|
|
|
|
|
// Lay out all the other containers on separate lines.
|
|
|
|
extraLinePrefix := "\t"
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-08-27 17:18:16 +00:00
|
|
|
extraLinePrefix = "\t\t"
|
|
|
|
}
|
|
|
|
for _, container := range containers {
|
|
|
|
_, err := fmt.Fprintf(w, "%s%s\t%s\t%s\t%s", extraLinePrefix, container.Name, container.Image, "", "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-20 15:39:15 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabelTabs(options.ColumnLabels)); err != nil {
|
2015-08-27 17:18:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printDaemonSetList(list *extensions.DaemonSetList, w io.Writer, options PrintOptions) error {
|
2015-09-12 16:46:10 +00:00
|
|
|
for _, ds := range list.Items {
|
2015-11-10 11:34:28 +00:00
|
|
|
if err := printDaemonSet(&ds, w, options); err != nil {
|
2015-08-27 17:18:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printEndpoints(endpoints *api.Endpoints, w io.Writer, options PrintOptions) error {
|
2015-07-01 20:56:31 +00:00
|
|
|
name := endpoints.Name
|
|
|
|
namespace := endpoints.Namespace
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-07-01 20:56:31 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-20 19:51:35 +00:00
|
|
|
}
|
2015-06-18 06:32:03 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t%s\t%s", name, formatEndpoints(endpoints, nil), translateTimestamp(endpoints.CreationTimestamp)); err != nil {
|
2015-06-16 16:30:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabels(endpoints.Labels, options.ColumnLabels)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, endpoints.Labels))
|
2015-02-05 23:45:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printEndpointsList(list *api.EndpointsList, w io.Writer, options PrintOptions) error {
|
2015-02-24 02:20:10 +00:00
|
|
|
for _, item := range list.Items {
|
2015-11-10 11:34:28 +00:00
|
|
|
if err := printEndpoints(&item, w, options); err != nil {
|
2015-02-24 02:20:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printNamespace(item *api.Namespace, w io.Writer, options PrintOptions) error {
|
|
|
|
if options.WithNamespace {
|
2015-07-01 20:41:54 +00:00
|
|
|
return fmt.Errorf("namespace is not namespaced")
|
|
|
|
}
|
2015-06-18 06:32:03 +00:00
|
|
|
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t%s\t%s", item.Name, item.Status.Phase, translateTimestamp(item.CreationTimestamp)); err != nil {
|
2015-06-16 16:30:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabels(item.Labels, options.ColumnLabels)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, item.Labels))
|
2015-01-19 21:50:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printNamespaceList(list *api.NamespaceList, w io.Writer, options PrintOptions) error {
|
2015-01-19 21:50:00 +00:00
|
|
|
for _, item := range list.Items {
|
2015-11-10 11:34:28 +00:00
|
|
|
if err := printNamespace(&item, w, options); err != nil {
|
2015-01-19 21:50:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printSecret(item *api.Secret, w io.Writer, options PrintOptions) error {
|
2015-07-01 20:56:31 +00:00
|
|
|
name := item.Name
|
|
|
|
namespace := item.Namespace
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-07-01 20:56:31 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-20 19:51:35 +00:00
|
|
|
}
|
2015-06-18 06:32:03 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t%s\t%v\t%s", name, item.Type, len(item.Data), translateTimestamp(item.CreationTimestamp)); err != nil {
|
2015-06-16 16:30:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabels(item.Labels, options.ColumnLabels)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, item.Labels))
|
2015-02-18 01:24:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printSecretList(list *api.SecretList, w io.Writer, options PrintOptions) error {
|
2015-02-18 01:24:50 +00:00
|
|
|
for _, item := range list.Items {
|
2015-11-10 11:34:28 +00:00
|
|
|
if err := printSecret(&item, w, options); err != nil {
|
2015-02-18 01:24:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printServiceAccount(item *api.ServiceAccount, w io.Writer, options PrintOptions) error {
|
2015-07-01 20:56:31 +00:00
|
|
|
name := item.Name
|
|
|
|
namespace := item.Namespace
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-07-01 20:56:31 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-20 19:51:35 +00:00
|
|
|
}
|
2015-06-18 06:32:03 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t%d\t%s", name, len(item.Secrets), translateTimestamp(item.CreationTimestamp)); err != nil {
|
2015-06-16 16:30:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabels(item.Labels, options.ColumnLabels)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, item.Labels))
|
2015-04-27 22:53:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printServiceAccountList(list *api.ServiceAccountList, w io.Writer, options PrintOptions) error {
|
2015-04-27 22:53:28 +00:00
|
|
|
for _, item := range list.Items {
|
2015-11-10 11:34:28 +00:00
|
|
|
if err := printServiceAccount(&item, w, options); err != nil {
|
2015-04-27 22:53:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printNode(node *api.Node, w io.Writer, options PrintOptions) error {
|
|
|
|
if options.WithNamespace {
|
2015-07-01 20:41:54 +00:00
|
|
|
return fmt.Errorf("node is not namespaced")
|
|
|
|
}
|
2015-02-24 05:21:14 +00:00
|
|
|
conditionMap := make(map[api.NodeConditionType]*api.NodeCondition)
|
2015-04-03 13:43:51 +00:00
|
|
|
NodeAllConditions := []api.NodeConditionType{api.NodeReady}
|
2015-02-21 17:07:09 +00:00
|
|
|
for i := range node.Status.Conditions {
|
|
|
|
cond := node.Status.Conditions[i]
|
2015-02-24 05:21:14 +00:00
|
|
|
conditionMap[cond.Type] = &cond
|
2015-01-15 01:11:34 +00:00
|
|
|
}
|
|
|
|
var status []string
|
|
|
|
for _, validCondition := range NodeAllConditions {
|
|
|
|
if condition, ok := conditionMap[validCondition]; ok {
|
2015-03-23 18:33:55 +00:00
|
|
|
if condition.Status == api.ConditionTrue {
|
2015-02-24 05:21:14 +00:00
|
|
|
status = append(status, string(condition.Type))
|
2015-01-15 01:11:34 +00:00
|
|
|
} else {
|
2015-02-24 05:21:14 +00:00
|
|
|
status = append(status, "Not"+string(condition.Type))
|
2015-01-15 01:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(status) == 0 {
|
|
|
|
status = append(status, "Unknown")
|
|
|
|
}
|
2015-04-16 01:53:03 +00:00
|
|
|
if node.Spec.Unschedulable {
|
|
|
|
status = append(status, "SchedulingDisabled")
|
|
|
|
}
|
2015-06-16 16:30:11 +00:00
|
|
|
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t%s\t%s", node.Name, strings.Join(status, ","), translateTimestamp(node.CreationTimestamp)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Display caller specify column labels first.
|
|
|
|
if _, err := fmt.Fprint(w, appendLabels(node.Labels, options.ColumnLabels)); err != nil {
|
2015-06-16 16:30:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-02-04 07:08:44 +00:00
|
|
|
_, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, node.Labels))
|
2014-10-06 01:24:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printNodeList(list *api.NodeList, w io.Writer, options PrintOptions) error {
|
2015-02-21 17:07:09 +00:00
|
|
|
for _, node := range list.Items {
|
2015-11-10 11:34:28 +00:00
|
|
|
if err := printNode(&node, w, options); err != nil {
|
2014-10-06 01:24:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printPersistentVolume(pv *api.PersistentVolume, w io.Writer, options PrintOptions) error {
|
|
|
|
if options.WithNamespace {
|
2015-07-01 20:56:31 +00:00
|
|
|
return fmt.Errorf("persistentVolume is not namespaced")
|
2015-05-20 19:51:35 +00:00
|
|
|
}
|
2015-07-01 20:56:31 +00:00
|
|
|
name := pv.Name
|
2015-05-20 19:51:35 +00:00
|
|
|
|
2015-03-26 19:50:36 +00:00
|
|
|
claimRefUID := ""
|
|
|
|
if pv.Spec.ClaimRef != nil {
|
2015-05-13 00:44:29 +00:00
|
|
|
claimRefUID += pv.Spec.ClaimRef.Namespace
|
|
|
|
claimRefUID += "/"
|
2015-03-26 19:50:36 +00:00
|
|
|
claimRefUID += pv.Spec.ClaimRef.Name
|
|
|
|
}
|
|
|
|
|
2015-07-13 19:10:04 +00:00
|
|
|
modesStr := api.GetAccessModesAsString(pv.Spec.AccessModes)
|
2015-03-26 19:50:36 +00:00
|
|
|
|
|
|
|
aQty := pv.Spec.Capacity[api.ResourceStorage]
|
2015-08-11 03:55:15 +00:00
|
|
|
aSize := aQty.String()
|
2015-03-26 19:50:36 +00:00
|
|
|
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s",
|
2015-06-18 06:32:03 +00:00
|
|
|
name,
|
|
|
|
aSize, modesStr,
|
|
|
|
pv.Status.Phase,
|
|
|
|
claimRefUID,
|
|
|
|
pv.Status.Reason,
|
|
|
|
translateTimestamp(pv.CreationTimestamp),
|
|
|
|
); err != nil {
|
2015-06-16 16:30:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabels(pv.Labels, options.ColumnLabels)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, pv.Labels))
|
2015-03-26 19:50:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printPersistentVolumeList(list *api.PersistentVolumeList, w io.Writer, options PrintOptions) error {
|
2015-03-26 19:50:36 +00:00
|
|
|
for _, pv := range list.Items {
|
2015-11-10 11:34:28 +00:00
|
|
|
if err := printPersistentVolume(&pv, w, options); err != nil {
|
2015-03-26 19:50:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printPersistentVolumeClaim(pvc *api.PersistentVolumeClaim, w io.Writer, options PrintOptions) error {
|
2015-07-01 20:56:31 +00:00
|
|
|
name := pvc.Name
|
|
|
|
namespace := pvc.Namespace
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-07-01 20:56:31 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-20 19:51:35 +00:00
|
|
|
}
|
|
|
|
|
2015-08-11 03:55:15 +00:00
|
|
|
phase := pvc.Status.Phase
|
|
|
|
storage := pvc.Spec.Resources.Requests[api.ResourceStorage]
|
|
|
|
capacity := ""
|
|
|
|
accessModes := ""
|
|
|
|
if pvc.Spec.VolumeName != "" {
|
2015-07-13 19:10:04 +00:00
|
|
|
accessModes = api.GetAccessModesAsString(pvc.Status.AccessModes)
|
2015-08-11 03:55:15 +00:00
|
|
|
storage = pvc.Status.Capacity[api.ResourceStorage]
|
|
|
|
capacity = storage.String()
|
2015-07-01 20:56:31 +00:00
|
|
|
}
|
2015-06-18 06:32:03 +00:00
|
|
|
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s", name, phase, pvc.Spec.VolumeName, capacity, accessModes, translateTimestamp(pvc.CreationTimestamp)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := fmt.Fprint(w, appendLabels(pvc.Labels, options.ColumnLabels)); err != nil {
|
2015-06-16 16:30:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-02-04 07:08:44 +00:00
|
|
|
_, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, pvc.Labels))
|
2015-03-26 19:50:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printPersistentVolumeClaimList(list *api.PersistentVolumeClaimList, w io.Writer, options PrintOptions) error {
|
2015-03-26 19:50:36 +00:00
|
|
|
for _, psd := range list.Items {
|
2015-11-10 11:34:28 +00:00
|
|
|
if err := printPersistentVolumeClaim(&psd, w, options); err != nil {
|
2015-03-26 19:50:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printEvent(event *api.Event, w io.Writer, options PrintOptions) error {
|
2015-07-01 20:56:31 +00:00
|
|
|
namespace := event.Namespace
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-07-01 20:56:31 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-01 20:41:54 +00:00
|
|
|
}
|
2015-11-10 11:34:28 +00:00
|
|
|
|
|
|
|
// While watching event, we should print absolute time.
|
|
|
|
var FirstTimestamp, LastTimestamp string
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.AbsoluteTimestamps {
|
2015-11-10 11:34:28 +00:00
|
|
|
FirstTimestamp = event.FirstTimestamp.String()
|
|
|
|
LastTimestamp = event.LastTimestamp.String()
|
|
|
|
} else {
|
|
|
|
FirstTimestamp = translateTimestamp(event.FirstTimestamp)
|
|
|
|
LastTimestamp = translateTimestamp(event.LastTimestamp)
|
|
|
|
}
|
|
|
|
|
2015-06-16 16:30:11 +00:00
|
|
|
if _, err := fmt.Fprintf(
|
2015-11-13 22:30:01 +00:00
|
|
|
w, "%s\t%s\t%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s",
|
2015-11-10 11:34:28 +00:00
|
|
|
FirstTimestamp,
|
|
|
|
LastTimestamp,
|
2015-02-11 00:49:32 +00:00
|
|
|
event.Count,
|
2014-11-04 02:02:27 +00:00
|
|
|
event.InvolvedObject.Name,
|
|
|
|
event.InvolvedObject.Kind,
|
2014-12-30 01:10:38 +00:00
|
|
|
event.InvolvedObject.FieldPath,
|
2015-11-13 22:30:01 +00:00
|
|
|
event.Type,
|
2014-11-04 02:02:27 +00:00
|
|
|
event.Reason,
|
2015-01-05 19:03:51 +00:00
|
|
|
event.Source,
|
2014-11-04 02:02:27 +00:00
|
|
|
event.Message,
|
2015-06-16 16:30:11 +00:00
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabels(event.Labels, options.ColumnLabels)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, event.Labels))
|
2014-11-04 02:02:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-16 22:20:51 +00:00
|
|
|
// Sorts and prints the EventList in a human-friendly format.
|
2016-01-20 15:39:15 +00:00
|
|
|
func printEventList(list *api.EventList, w io.Writer, options PrintOptions) error {
|
2014-12-16 22:20:51 +00:00
|
|
|
sort.Sort(SortableEvents(list.Items))
|
2014-11-04 02:02:27 +00:00
|
|
|
for i := range list.Items {
|
2015-11-10 11:34:28 +00:00
|
|
|
if err := printEvent(&list.Items[i], w, options); err != nil {
|
2014-11-04 02:02:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printLimitRange(limitRange *api.LimitRange, w io.Writer, options PrintOptions) error {
|
2015-07-01 20:56:31 +00:00
|
|
|
name := limitRange.Name
|
|
|
|
namespace := limitRange.Namespace
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-07-01 20:56:31 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-20 19:51:35 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 06:32:03 +00:00
|
|
|
if _, err := fmt.Fprintf(
|
|
|
|
w, "%s\t%s",
|
|
|
|
name,
|
|
|
|
translateTimestamp(limitRange.CreationTimestamp),
|
|
|
|
); err != nil {
|
2015-06-16 16:30:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabels(limitRange.Labels, options.ColumnLabels)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, limitRange.Labels))
|
2015-01-22 21:52:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prints the LimitRangeList in a human-friendly format.
|
2016-01-20 15:39:15 +00:00
|
|
|
func printLimitRangeList(list *api.LimitRangeList, w io.Writer, options PrintOptions) error {
|
2015-01-22 21:52:40 +00:00
|
|
|
for i := range list.Items {
|
2015-11-10 11:34:28 +00:00
|
|
|
if err := printLimitRange(&list.Items[i], w, options); err != nil {
|
2015-01-22 21:52:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printResourceQuota(resourceQuota *api.ResourceQuota, w io.Writer, options PrintOptions) error {
|
2015-07-01 20:56:31 +00:00
|
|
|
name := resourceQuota.Name
|
|
|
|
namespace := resourceQuota.Namespace
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-07-01 20:56:31 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-20 19:51:35 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 06:32:03 +00:00
|
|
|
if _, err := fmt.Fprintf(
|
|
|
|
w, "%s\t%s",
|
|
|
|
name,
|
|
|
|
translateTimestamp(resourceQuota.CreationTimestamp),
|
|
|
|
); err != nil {
|
2015-06-16 16:30:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabels(resourceQuota.Labels, options.ColumnLabels)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, resourceQuota.Labels))
|
2015-01-23 17:38:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prints the ResourceQuotaList in a human-friendly format.
|
2016-01-20 15:39:15 +00:00
|
|
|
func printResourceQuotaList(list *api.ResourceQuotaList, w io.Writer, options PrintOptions) error {
|
2015-01-23 17:38:30 +00:00
|
|
|
for i := range list.Items {
|
2015-11-10 11:34:28 +00:00
|
|
|
if err := printResourceQuota(&list.Items[i], w, options); err != nil {
|
2015-01-23 17:38:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printComponentStatus(item *api.ComponentStatus, w io.Writer, options PrintOptions) error {
|
|
|
|
if options.WithNamespace {
|
2015-07-01 20:41:54 +00:00
|
|
|
return fmt.Errorf("componentStatus is not namespaced")
|
|
|
|
}
|
2015-04-15 19:23:02 +00:00
|
|
|
status := "Unknown"
|
|
|
|
message := ""
|
|
|
|
error := ""
|
|
|
|
for _, condition := range item.Conditions {
|
|
|
|
if condition.Type == api.ComponentHealthy {
|
|
|
|
if condition.Status == api.ConditionTrue {
|
|
|
|
status = "Healthy"
|
|
|
|
} else {
|
|
|
|
status = "Unhealthy"
|
|
|
|
}
|
|
|
|
message = condition.Message
|
|
|
|
error = condition.Error
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-06-16 16:30:11 +00:00
|
|
|
|
|
|
|
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s", item.Name, status, message, error); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabels(item.Labels, options.ColumnLabels)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, item.Labels))
|
2015-04-15 19:23:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printComponentStatusList(list *api.ComponentStatusList, w io.Writer, options PrintOptions) error {
|
2015-04-15 19:23:02 +00:00
|
|
|
for _, item := range list.Items {
|
2015-11-10 11:34:28 +00:00
|
|
|
if err := printComponentStatus(&item, w, options); err != nil {
|
2015-04-15 19:23:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printThirdPartyResource(rsrc *extensions.ThirdPartyResource, w io.Writer, options PrintOptions) error {
|
2015-08-15 05:10:15 +00:00
|
|
|
versions := make([]string, len(rsrc.Versions))
|
|
|
|
for ix := range rsrc.Versions {
|
|
|
|
version := &rsrc.Versions[ix]
|
2015-12-07 22:17:55 +00:00
|
|
|
versions[ix] = fmt.Sprintf("%s/%s", version.APIGroup, version.Name)
|
2015-08-15 05:10:15 +00:00
|
|
|
}
|
|
|
|
versionsString := strings.Join(versions, ",")
|
2015-12-07 22:17:55 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\n", rsrc.Name, rsrc.Description, versionsString); err != nil {
|
2015-08-15 05:10:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printThirdPartyResourceList(list *extensions.ThirdPartyResourceList, w io.Writer, options PrintOptions) error {
|
2015-08-15 05:10:15 +00:00
|
|
|
for _, item := range list.Items {
|
2015-11-10 11:34:28 +00:00
|
|
|
if err := printThirdPartyResource(&item, w, options); err != nil {
|
2015-08-15 05:10:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printDeployment(deployment *extensions.Deployment, w io.Writer, options PrintOptions) error {
|
|
|
|
if options.WithNamespace {
|
2015-08-31 21:43:24 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t", deployment.Namespace); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 18:40:37 +00:00
|
|
|
desiredReplicas := deployment.Spec.Replicas
|
|
|
|
currentReplicas := deployment.Status.Replicas
|
|
|
|
updatedReplicas := deployment.Status.UpdatedReplicas
|
|
|
|
availableReplicas := deployment.Status.AvailableReplicas
|
2015-08-31 21:43:24 +00:00
|
|
|
age := translateTimestamp(deployment.CreationTimestamp)
|
2016-01-22 18:40:37 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t%d\t%d\t%d\t%d\t%s", deployment.Name, desiredReplicas, currentReplicas, updatedReplicas, availableReplicas, age); err != nil {
|
2015-08-31 21:43:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabels(deployment.Labels, options.ColumnLabels)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, deployment.Labels))
|
2015-08-31 21:43:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printDeploymentList(list *extensions.DeploymentList, w io.Writer, options PrintOptions) error {
|
2015-08-31 21:43:24 +00:00
|
|
|
for _, item := range list.Items {
|
2015-11-10 11:34:28 +00:00
|
|
|
if err := printDeployment(&item, w, options); err != nil {
|
2015-08-31 21:43:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printHorizontalPodAutoscaler(hpa *extensions.HorizontalPodAutoscaler, w io.Writer, options PrintOptions) error {
|
2015-09-02 10:20:11 +00:00
|
|
|
namespace := hpa.Namespace
|
|
|
|
name := hpa.Name
|
2015-11-02 14:26:48 +00:00
|
|
|
reference := fmt.Sprintf("%s/%s/%s",
|
2015-09-02 10:20:11 +00:00
|
|
|
hpa.Spec.ScaleRef.Kind,
|
|
|
|
hpa.Spec.ScaleRef.Name,
|
|
|
|
hpa.Spec.ScaleRef.Subresource)
|
2015-10-21 00:30:56 +00:00
|
|
|
target := "<unset>"
|
2015-10-13 15:24:23 +00:00
|
|
|
if hpa.Spec.CPUUtilization != nil {
|
|
|
|
target = fmt.Sprintf("%d%%", hpa.Spec.CPUUtilization.TargetPercentage)
|
|
|
|
}
|
2015-09-02 10:20:11 +00:00
|
|
|
current := "<waiting>"
|
2015-10-13 15:24:23 +00:00
|
|
|
if hpa.Status.CurrentCPUUtilizationPercentage != nil {
|
|
|
|
current = fmt.Sprintf("%d%%", *hpa.Status.CurrentCPUUtilizationPercentage)
|
2015-09-02 10:20:11 +00:00
|
|
|
}
|
2015-10-21 00:30:56 +00:00
|
|
|
minPods := "<unset>"
|
|
|
|
if hpa.Spec.MinReplicas != nil {
|
|
|
|
minPods = fmt.Sprintf("%d", *hpa.Spec.MinReplicas)
|
|
|
|
}
|
2015-09-17 12:08:39 +00:00
|
|
|
maxPods := hpa.Spec.MaxReplicas
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-09-02 10:20:11 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-21 00:30:56 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%d\t%s",
|
2015-09-02 10:20:11 +00:00
|
|
|
name,
|
|
|
|
reference,
|
|
|
|
target,
|
|
|
|
current,
|
2015-10-21 00:30:56 +00:00
|
|
|
minPods,
|
2015-09-02 10:20:11 +00:00
|
|
|
maxPods,
|
|
|
|
translateTimestamp(hpa.CreationTimestamp),
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabels(hpa.Labels, options.ColumnLabels)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, hpa.Labels))
|
2015-09-02 10:20:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printHorizontalPodAutoscalerList(list *extensions.HorizontalPodAutoscalerList, w io.Writer, options PrintOptions) error {
|
2015-09-02 10:20:11 +00:00
|
|
|
for i := range list.Items {
|
2015-11-10 11:34:28 +00:00
|
|
|
if err := printHorizontalPodAutoscaler(&list.Items[i], w, options); err != nil {
|
2015-09-02 10:20:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printConfigMap(configMap *api.ConfigMap, w io.Writer, options PrintOptions) error {
|
2015-12-15 15:09:42 +00:00
|
|
|
name := configMap.Name
|
|
|
|
namespace := configMap.Namespace
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
if options.WithNamespace {
|
2015-12-15 15:09:42 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err := fmt.Fprintf(w, "%s\t%v\t%s", name, len(configMap.Data), translateTimestamp(configMap.CreationTimestamp)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-04 07:08:44 +00:00
|
|
|
if _, err := fmt.Fprint(w, appendLabels(configMap.Labels, options.ColumnLabels)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, configMap.Labels))
|
2015-12-15 15:09:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:39:15 +00:00
|
|
|
func printConfigMapList(list *api.ConfigMapList, w io.Writer, options PrintOptions) error {
|
2015-12-15 15:09:42 +00:00
|
|
|
for i := range list.Items {
|
|
|
|
if err := printConfigMap(&list.Items[i], w, options); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-14 13:31:23 +00:00
|
|
|
func printPodSecurityPolicy(item *extensions.PodSecurityPolicy, w io.Writer, options PrintOptions) error {
|
|
|
|
_, err := fmt.Fprintf(w, "%s\t%t\t%v\t%t\t%s\t%s\n", item.Name, item.Spec.Privileged,
|
|
|
|
item.Spec.Capabilities, item.Spec.Volumes, item.Spec.SELinuxContext.Type,
|
|
|
|
item.Spec.RunAsUser.Type)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func printPodSecurityPolicyList(list *extensions.PodSecurityPolicyList, w io.Writer, options PrintOptions) error {
|
|
|
|
for _, item := range list.Items {
|
|
|
|
if err := printPodSecurityPolicy(&item, w, options); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-16 16:30:11 +00:00
|
|
|
func appendLabels(itemLabels map[string]string, columnLabels []string) string {
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
|
|
|
|
for _, cl := range columnLabels {
|
|
|
|
buffer.WriteString(fmt.Sprint("\t"))
|
|
|
|
if il, ok := itemLabels[cl]; ok {
|
|
|
|
buffer.WriteString(fmt.Sprint(il))
|
|
|
|
} else {
|
2015-08-08 04:08:43 +00:00
|
|
|
buffer.WriteString("<none>")
|
2015-06-16 16:30:11 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-04 07:08:44 +00:00
|
|
|
|
|
|
|
return buffer.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append all labels to a single column. We need this even when show-labels flag* is
|
|
|
|
// false, since this adds newline delimiter to the end of each row.
|
|
|
|
func appendAllLabels(showLabels bool, itemLabels map[string]string) string {
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
|
|
|
|
if showLabels {
|
|
|
|
buffer.WriteString(fmt.Sprint("\t"))
|
|
|
|
buffer.WriteString(labels.FormatLabels(itemLabels))
|
|
|
|
}
|
2015-06-16 16:30:11 +00:00
|
|
|
buffer.WriteString("\n")
|
|
|
|
|
|
|
|
return buffer.String()
|
|
|
|
}
|
|
|
|
|
2015-07-07 18:35:24 +00:00
|
|
|
// Append a set of tabs for each label column. We need this in the case where
|
|
|
|
// we have extra lines so that the tabwriter will still line things up.
|
|
|
|
func appendLabelTabs(columnLabels []string) string {
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
|
2015-07-09 21:53:57 +00:00
|
|
|
for i := range columnLabels {
|
|
|
|
// NB: This odd dance is to make the loop both compatible with go 1.3 and
|
|
|
|
// pass `gofmt -s`
|
|
|
|
_ = i
|
2015-07-07 18:35:24 +00:00
|
|
|
buffer.WriteString("\t")
|
|
|
|
}
|
|
|
|
buffer.WriteString("\n")
|
|
|
|
|
|
|
|
return buffer.String()
|
|
|
|
}
|
|
|
|
|
2015-06-16 16:30:11 +00:00
|
|
|
func formatLabelHeaders(columnLabels []string) []string {
|
|
|
|
formHead := make([]string, len(columnLabels))
|
|
|
|
for i, l := range columnLabels {
|
|
|
|
p := strings.Split(l, "/")
|
|
|
|
formHead[i] = strings.ToUpper((p[len(p)-1]))
|
|
|
|
}
|
|
|
|
return formHead
|
|
|
|
}
|
|
|
|
|
2015-06-29 18:36:06 +00:00
|
|
|
// headers for -o wide
|
|
|
|
func formatWideHeaders(wide bool, t reflect.Type) []string {
|
|
|
|
if wide {
|
|
|
|
if t.String() == "*api.Pod" || t.String() == "*api.PodList" {
|
|
|
|
return []string{"NODE"}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-04 07:08:44 +00:00
|
|
|
// headers for --show-labels=true
|
|
|
|
func formatShowLabelsHeader(showLabels bool, t reflect.Type) []string {
|
|
|
|
if showLabels {
|
|
|
|
if t.String() != "*api.ThirdPartyResource" && t.String() != "*api.ThirdPartyResourceList" {
|
|
|
|
return []string{"LABELS"}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-14 01:48:06 +00:00
|
|
|
// GetNewTabWriter returns a tabwriter that translates tabbed columns in input into properly aligned text.
|
|
|
|
func GetNewTabWriter(output io.Writer) *tabwriter.Writer {
|
|
|
|
return tabwriter.NewWriter(output, tabwriterMinWidth, tabwriterWidth, tabwriterPadding, tabwriterPadChar, tabwriterFlags)
|
|
|
|
}
|
|
|
|
|
2014-10-06 01:24:19 +00:00
|
|
|
// PrintObj prints the obj in a human-friendly format according to the type of the obj.
|
|
|
|
func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) error {
|
2015-10-14 01:48:06 +00:00
|
|
|
// if output is a tabwriter (when it's called by kubectl get), we use it; create a new tabwriter otherwise
|
2015-10-13 18:36:22 +00:00
|
|
|
w, found := output.(*tabwriter.Writer)
|
|
|
|
if !found {
|
2015-10-14 01:48:06 +00:00
|
|
|
w = GetNewTabWriter(output)
|
2015-10-13 18:36:22 +00:00
|
|
|
defer w.Flush()
|
|
|
|
}
|
2014-11-12 01:31:13 +00:00
|
|
|
t := reflect.TypeOf(obj)
|
|
|
|
if handler := h.handlerMap[t]; handler != nil {
|
2016-01-20 15:39:15 +00:00
|
|
|
if !h.options.NoHeaders && t != h.lastType {
|
|
|
|
headers := append(handler.columns, formatWideHeaders(h.options.Wide, t)...)
|
|
|
|
headers = append(headers, formatLabelHeaders(h.options.ColumnLabels)...)
|
2016-02-04 07:08:44 +00:00
|
|
|
// LABELS is always the last column.
|
|
|
|
headers = append(headers, formatShowLabelsHeader(h.options.ShowLabels, t)...)
|
2016-01-20 15:39:15 +00:00
|
|
|
if h.options.WithNamespace {
|
2015-07-01 20:56:31 +00:00
|
|
|
headers = append(withNamespacePrefixColumns, headers...)
|
|
|
|
}
|
2015-06-16 16:30:11 +00:00
|
|
|
h.printHeader(headers, w)
|
2014-11-12 01:31:13 +00:00
|
|
|
h.lastType = t
|
2014-10-16 01:54:46 +00:00
|
|
|
}
|
2015-11-10 11:34:28 +00:00
|
|
|
args := []reflect.Value{reflect.ValueOf(obj), reflect.ValueOf(w), reflect.ValueOf(h.options)}
|
2014-10-06 01:24:19 +00:00
|
|
|
resultValue := handler.printFunc.Call(args)[0]
|
|
|
|
if resultValue.IsNil() {
|
|
|
|
return nil
|
|
|
|
}
|
2014-12-16 22:20:51 +00:00
|
|
|
return resultValue.Interface().(error)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
2014-12-16 22:20:51 +00:00
|
|
|
return fmt.Errorf("error: unknown type %#v", obj)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TemplatePrinter is an implementation of ResourcePrinter which formats data with a Go Template.
|
|
|
|
type TemplatePrinter struct {
|
2014-11-20 22:09:59 +00:00
|
|
|
rawTemplate string
|
|
|
|
template *template.Template
|
2014-11-07 22:41:59 +00:00
|
|
|
}
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
func NewTemplatePrinter(tmpl []byte) (*TemplatePrinter, error) {
|
2014-12-23 22:05:26 +00:00
|
|
|
t, err := template.New("output").
|
|
|
|
Funcs(template.FuncMap{"exists": exists}).
|
|
|
|
Parse(string(tmpl))
|
2014-11-07 22:41:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-11-20 22:09:59 +00:00
|
|
|
return &TemplatePrinter{
|
|
|
|
rawTemplate: string(tmpl),
|
|
|
|
template: t,
|
|
|
|
}, nil
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrintObj formats the obj with the Go Template.
|
2014-11-18 18:04:10 +00:00
|
|
|
func (p *TemplatePrinter) PrintObj(obj runtime.Object, w io.Writer) error {
|
2014-12-31 00:42:55 +00:00
|
|
|
data, err := json.Marshal(obj)
|
2014-11-07 22:41:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-18 18:04:10 +00:00
|
|
|
out := map[string]interface{}{}
|
|
|
|
if err := json.Unmarshal(data, &out); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-23 22:05:26 +00:00
|
|
|
if err = p.safeExecute(w, out); err != nil {
|
2014-12-23 19:21:38 +00:00
|
|
|
// It is way easier to debug this stuff when it shows up in
|
|
|
|
// stdout instead of just stdin. So in addition to returning
|
|
|
|
// a nice error, also print useful stuff with the writer.
|
2015-11-03 23:09:00 +00:00
|
|
|
fmt.Fprintf(w, "Error executing template: %v. Printing more information for debugging the template:\n", err)
|
|
|
|
fmt.Fprintf(w, "\ttemplate was:\n\t\t%v\n", p.rawTemplate)
|
|
|
|
fmt.Fprintf(w, "\traw data was:\n\t\t%v\n", string(data))
|
|
|
|
fmt.Fprintf(w, "\tobject given to template engine was:\n\t\t%+v\n\n", out)
|
|
|
|
return fmt.Errorf("error executing template %q: %v", p.rawTemplate, err)
|
2014-11-20 22:09:59 +00:00
|
|
|
}
|
|
|
|
return nil
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 19:04:12 +00:00
|
|
|
// TODO: implement HandledResources()
|
|
|
|
func (p *TemplatePrinter) HandledResources() []string {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
2014-12-23 22:05:26 +00:00
|
|
|
// safeExecute tries to execute the template, but catches panics and returns an error
|
|
|
|
// should the template engine panic.
|
|
|
|
func (p *TemplatePrinter) safeExecute(w io.Writer, obj interface{}) error {
|
|
|
|
var panicErr error
|
|
|
|
// Sorry for the double anonymous function. There's probably a clever way
|
|
|
|
// to do this that has the defer'd func setting the value to be returned, but
|
|
|
|
// that would be even less obvious.
|
|
|
|
retErr := func() error {
|
|
|
|
defer func() {
|
|
|
|
if x := recover(); x != nil {
|
|
|
|
panicErr = fmt.Errorf("caught panic: %+v", x)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return p.template.Execute(w, obj)
|
|
|
|
}()
|
|
|
|
if panicErr != nil {
|
|
|
|
return panicErr
|
|
|
|
}
|
|
|
|
return retErr
|
|
|
|
}
|
|
|
|
|
2014-11-14 19:56:41 +00:00
|
|
|
func tabbedString(f func(io.Writer) error) (string, error) {
|
2014-10-06 01:24:19 +00:00
|
|
|
out := new(tabwriter.Writer)
|
2014-11-20 16:50:50 +00:00
|
|
|
buf := &bytes.Buffer{}
|
2014-10-06 01:24:19 +00:00
|
|
|
out.Init(buf, 0, 8, 1, '\t', 0)
|
|
|
|
|
|
|
|
err := f(out)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
out.Flush()
|
|
|
|
str := string(buf.String())
|
|
|
|
return str, nil
|
|
|
|
}
|
2014-12-23 22:05:26 +00:00
|
|
|
|
|
|
|
// exists returns true if it would be possible to call the index function
|
|
|
|
// with these arguments.
|
|
|
|
//
|
|
|
|
// TODO: how to document this for users?
|
|
|
|
//
|
|
|
|
// index returns the result of indexing its first argument by the following
|
|
|
|
// arguments. Thus "index x 1 2 3" is, in Go syntax, x[1][2][3]. Each
|
|
|
|
// indexed item must be a map, slice, or array.
|
|
|
|
func exists(item interface{}, indices ...interface{}) bool {
|
|
|
|
v := reflect.ValueOf(item)
|
|
|
|
for _, i := range indices {
|
|
|
|
index := reflect.ValueOf(i)
|
|
|
|
var isNil bool
|
|
|
|
if v, isNil = indirect(v); isNil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice, reflect.String:
|
|
|
|
var x int64
|
|
|
|
switch index.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
x = index.Int()
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
|
|
|
x = int64(index.Uint())
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if x < 0 || x >= int64(v.Len()) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
v = v.Index(int(x))
|
|
|
|
case reflect.Map:
|
|
|
|
if !index.IsValid() {
|
|
|
|
index = reflect.Zero(v.Type().Key())
|
|
|
|
}
|
|
|
|
if !index.Type().AssignableTo(v.Type().Key()) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if x := v.MapIndex(index); x.IsValid() {
|
|
|
|
v = x
|
|
|
|
} else {
|
|
|
|
v = reflect.Zero(v.Type().Elem())
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, isNil := indirect(v); isNil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// stolen from text/template
|
|
|
|
// indirect returns the item at the end of indirection, and a bool to indicate if it's nil.
|
|
|
|
// We indirect through pointers and empty interfaces (only) because
|
|
|
|
// non-empty interfaces have methods we might need.
|
|
|
|
func indirect(v reflect.Value) (rv reflect.Value, isNil bool) {
|
|
|
|
for ; v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface; v = v.Elem() {
|
|
|
|
if v.IsNil() {
|
|
|
|
return v, true
|
|
|
|
}
|
|
|
|
if v.Kind() == reflect.Interface && v.NumMethod() > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v, false
|
|
|
|
}
|
2015-08-04 08:14:31 +00:00
|
|
|
|
|
|
|
// JSONPathPrinter is an implementation of ResourcePrinter which formats data with jsonpath expression.
|
|
|
|
type JSONPathPrinter struct {
|
|
|
|
rawTemplate string
|
|
|
|
*jsonpath.JSONPath
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewJSONPathPrinter(tmpl string) (*JSONPathPrinter, error) {
|
|
|
|
j := jsonpath.New("out")
|
|
|
|
if err := j.Parse(tmpl); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &JSONPathPrinter{tmpl, j}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrintObj formats the obj with the JSONPath Template.
|
|
|
|
func (j *JSONPathPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
|
2015-12-21 05:34:11 +00:00
|
|
|
var queryObj interface{} = obj
|
|
|
|
if meta.IsListType(obj) {
|
2015-08-13 21:11:23 +00:00
|
|
|
data, err := json.Marshal(obj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
queryObj = map[string]interface{}{}
|
|
|
|
if err := json.Unmarshal(data, &queryObj); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-04 08:14:31 +00:00
|
|
|
}
|
2015-08-13 21:11:23 +00:00
|
|
|
|
|
|
|
if err := j.JSONPath.Execute(w, queryObj); err != nil {
|
2015-11-03 23:09:00 +00:00
|
|
|
fmt.Fprintf(w, "Error executing template: %v. Printing more information for debugging the template:\n", err)
|
|
|
|
fmt.Fprintf(w, "\ttemplate was:\n\t\t%v\n", j.rawTemplate)
|
|
|
|
fmt.Fprintf(w, "\tobject given to jsonpath engine was:\n\t\t%#v\n\n", queryObj)
|
|
|
|
return fmt.Errorf("error executing jsonpath %q: %v\n", j.rawTemplate, err)
|
2015-08-04 08:14:31 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-08-31 19:04:12 +00:00
|
|
|
|
|
|
|
// TODO: implement HandledResources()
|
|
|
|
func (p *JSONPathPrinter) HandledResources() []string {
|
|
|
|
return []string{}
|
|
|
|
}
|