2014-10-06 01:24:19 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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"
|
|
|
|
"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
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2015-03-04 03:18:15 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
|
2014-10-06 01:24:19 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2015-03-26 19:50:36 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/volume"
|
2015-02-27 00:32:14 +00:00
|
|
|
"github.com/docker/docker/pkg/units"
|
2014-12-01 04:12:37 +00:00
|
|
|
"github.com/ghodss/yaml"
|
2014-10-06 01:24:19 +00:00
|
|
|
"github.com/golang/glog"
|
|
|
|
)
|
|
|
|
|
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{}
|
2014-10-06 01:24:19 +00:00
|
|
|
case "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
|
|
|
}
|
|
|
|
case "templatefile":
|
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
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-03-04 03:18:15 +00:00
|
|
|
version []string
|
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-03-04 03:18:15 +00:00
|
|
|
func NewVersionedPrinter(printer ResourcePrinter, convertor runtime.ObjectConvertor, version ...string) ResourcePrinter {
|
2014-12-31 00:42:55 +00:00
|
|
|
return &VersionedPrinter{
|
|
|
|
printer: printer,
|
|
|
|
convertor: convertor,
|
|
|
|
version: version,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrintObj implements ResourcePrinter
|
|
|
|
func (p *VersionedPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
|
2015-01-13 23:51:33 +00:00
|
|
|
if len(p.version) == 0 {
|
|
|
|
return fmt.Errorf("no version specified, object cannot be converted")
|
|
|
|
}
|
2015-03-04 03:18:15 +00:00
|
|
|
for _, version := range p.version {
|
|
|
|
if len(version) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
converted, err := p.convertor.ConvertToVersion(obj, version)
|
|
|
|
if conversion.IsNotRegisteredError(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.printer.PrintObj(converted, w)
|
2014-11-12 00:15:22 +00:00
|
|
|
}
|
2015-03-04 03:18:15 +00:00
|
|
|
return fmt.Errorf("the object cannot be converted to any of the versions: %v", p.version)
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type handlerEntry struct {
|
|
|
|
columns []string
|
|
|
|
printFunc reflect.Value
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
// recieved from watches.
|
2014-10-06 01:24:19 +00:00
|
|
|
type HumanReadablePrinter struct {
|
|
|
|
handlerMap map[reflect.Type]*handlerEntry
|
2014-10-16 01:54:46 +00:00
|
|
|
noHeaders bool
|
2014-11-12 01:31:13 +00:00
|
|
|
lastType reflect.Type
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewHumanReadablePrinter creates a HumanReadablePrinter.
|
2014-10-16 01:54:46 +00:00
|
|
|
func NewHumanReadablePrinter(noHeaders bool) *HumanReadablePrinter {
|
|
|
|
printer := &HumanReadablePrinter{
|
|
|
|
handlerMap: make(map[reflect.Type]*handlerEntry),
|
|
|
|
noHeaders: noHeaders,
|
|
|
|
}
|
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.
|
|
|
|
// printFunc is the function that will be called to print an object.
|
|
|
|
// It must be of the following type:
|
|
|
|
// func printFunc(object ObjectType, w io.Writer) error
|
|
|
|
// where ObjectType is the type of the object that will be printed.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HumanReadablePrinter) validatePrintHandlerFunc(printFunc reflect.Value) error {
|
|
|
|
if printFunc.Kind() != reflect.Func {
|
2014-11-07 06:56:39 +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()
|
|
|
|
if funcType.NumIn() != 2 || funcType.NumOut() != 1 {
|
2014-11-07 06:56:39 +00:00
|
|
|
return fmt.Errorf("invalid print handler." +
|
2014-10-06 01:24:19 +00:00
|
|
|
"Must accept 2 parameters and return 1 value.")
|
|
|
|
}
|
|
|
|
if funcType.In(1) != reflect.TypeOf((*io.Writer)(nil)).Elem() ||
|
|
|
|
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: "+
|
2014-10-06 01:24:19 +00:00
|
|
|
"func handler(obj %v, w io.Writer) error", funcType.In(0))
|
|
|
|
}
|
|
|
|
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-02-27 00:32:14 +00:00
|
|
|
var podColumns = []string{"POD", "IP", "CONTAINER(S)", "IMAGE(S)", "HOST", "LABELS", "STATUS", "CREATED"}
|
2014-12-19 02:15:47 +00:00
|
|
|
var replicationControllerColumns = []string{"CONTROLLER", "CONTAINER(S)", "IMAGE(S)", "SELECTOR", "REPLICAS"}
|
2015-03-13 15:16:41 +00:00
|
|
|
var serviceColumns = []string{"NAME", "LABELS", "SELECTOR", "IP", "PORT(S)"}
|
2015-02-05 23:45:53 +00:00
|
|
|
var endpointColumns = []string{"NAME", "ENDPOINTS"}
|
2015-02-21 17:07:09 +00:00
|
|
|
var nodeColumns = []string{"NAME", "LABELS", "STATUS"}
|
2014-10-06 01:24:19 +00:00
|
|
|
var statusColumns = []string{"STATUS"}
|
2015-02-11 00:49:32 +00:00
|
|
|
var eventColumns = []string{"FIRSTSEEN", "LASTSEEN", "COUNT", "NAME", "KIND", "SUBOBJECT", "REASON", "SOURCE", "MESSAGE"}
|
2015-01-22 21:52:40 +00:00
|
|
|
var limitRangeColumns = []string{"NAME"}
|
2015-01-23 17:38:30 +00:00
|
|
|
var resourceQuotaColumns = []string{"NAME"}
|
2015-03-10 15:21:09 +00:00
|
|
|
var namespaceColumns = []string{"NAME", "LABELS", "STATUS"}
|
2015-02-18 01:24:50 +00:00
|
|
|
var secretColumns = []string{"NAME", "DATA"}
|
2015-03-26 19:50:36 +00:00
|
|
|
var persistentVolumeColumns = []string{"NAME", "LABELS", "CAPACITY", "ACCESSMODES", "STATUS", "CLAIM"}
|
|
|
|
var persistentVolumeClaimColumns = []string{"NAME", "LABELS", "STATUS", "VOLUME"}
|
2015-04-15 19:23:02 +00:00
|
|
|
var componentStatusColumns = []string{"NAME", "STATUS", "MESSAGE", "ERROR"}
|
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)
|
|
|
|
h.Handler(replicationControllerColumns, printReplicationController)
|
|
|
|
h.Handler(replicationControllerColumns, printReplicationControllerList)
|
|
|
|
h.Handler(serviceColumns, printService)
|
|
|
|
h.Handler(serviceColumns, printServiceList)
|
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-10-06 01:24:19 +00:00
|
|
|
h.Handler(statusColumns, printStatus)
|
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-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)
|
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-03-20 21:24:43 +00:00
|
|
|
func formatEndpoints(endpoints *api.Endpoints) string {
|
|
|
|
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
|
|
|
|
Loop:
|
|
|
|
for i := range endpoints.Subsets {
|
|
|
|
ss := &endpoints.Subsets[i]
|
|
|
|
for i := range ss.Ports {
|
|
|
|
port := &ss.Ports[i]
|
|
|
|
if port.Name == "" { // TODO: add multi-port support.
|
|
|
|
for i := range ss.Addresses {
|
|
|
|
if len(list) == max {
|
|
|
|
more = true
|
|
|
|
break Loop
|
|
|
|
}
|
|
|
|
addr := &ss.Addresses[i]
|
|
|
|
list = append(list, fmt.Sprintf("%s:%d", addr.IP, port.Port))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret := strings.Join(list, ",")
|
|
|
|
if more {
|
|
|
|
ret += "..."
|
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
|
|
|
}
|
|
|
|
|
2014-10-06 01:24:19 +00:00
|
|
|
func podHostString(host, ip string) string {
|
|
|
|
if host == "" && ip == "" {
|
|
|
|
return "<unassigned>"
|
|
|
|
}
|
|
|
|
return host + "/" + ip
|
|
|
|
}
|
|
|
|
|
|
|
|
func printPod(pod *api.Pod, w io.Writer) error {
|
2014-11-07 02:09:46 +00:00
|
|
|
// TODO: remove me when pods are converted
|
|
|
|
spec := &api.PodSpec{}
|
2014-11-13 15:52:13 +00:00
|
|
|
if err := api.Scheme.Convert(&pod.Spec, spec); err != nil {
|
2014-11-07 02:09:46 +00:00
|
|
|
glog.Errorf("Unable to convert pod manifest: %v", err)
|
|
|
|
}
|
2014-12-19 02:15:47 +00:00
|
|
|
containers := spec.Containers
|
|
|
|
var firstContainer api.Container
|
|
|
|
if len(containers) > 0 {
|
|
|
|
firstContainer, containers = containers[0], containers[1:]
|
2014-11-15 00:20:43 +00:00
|
|
|
}
|
2015-02-27 00:32:14 +00:00
|
|
|
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
|
2014-12-19 02:15:47 +00:00
|
|
|
pod.Name,
|
2015-01-09 22:18:10 +00:00
|
|
|
pod.Status.PodIP,
|
2014-12-19 02:15:47 +00:00
|
|
|
firstContainer.Name,
|
|
|
|
firstContainer.Image,
|
2015-04-02 12:52:03 +00:00
|
|
|
podHostString(pod.Spec.Host, pod.Status.HostIP),
|
2014-12-19 02:15:47 +00:00
|
|
|
formatLabels(pod.Labels),
|
2015-02-27 00:32:14 +00:00
|
|
|
pod.Status.Phase,
|
|
|
|
units.HumanDuration(time.Now().Sub(pod.CreationTimestamp.Time)))
|
2014-11-15 00:20:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-19 02:15:47 +00:00
|
|
|
// Lay out all the other containers on separate lines.
|
|
|
|
for _, container := range containers {
|
2015-02-27 00:32:14 +00:00
|
|
|
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "", "", container.Name, container.Image, "", "", "", "")
|
2014-11-15 00:20:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func printPodList(podList *api.PodList, w io.Writer) error {
|
|
|
|
for _, pod := range podList.Items {
|
|
|
|
if err := printPod(&pod, w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-22 17:02:02 +00:00
|
|
|
func printReplicationController(controller *api.ReplicationController, w io.Writer) error {
|
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:]
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d\n",
|
|
|
|
controller.Name,
|
|
|
|
firstContainer.Name,
|
|
|
|
firstContainer.Image,
|
|
|
|
formatLabels(controller.Spec.Selector),
|
|
|
|
controller.Spec.Replicas)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Lay out all the other containers on separate lines.
|
|
|
|
for _, container := range containers {
|
|
|
|
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", "", container.Name, container.Image, "", "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func printReplicationControllerList(list *api.ReplicationControllerList, w io.Writer) error {
|
2014-10-22 17:02:02 +00:00
|
|
|
for _, controller := range list.Items {
|
|
|
|
if err := printReplicationController(&controller, w); err != nil {
|
2014-10-06 01:24:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func printService(svc *api.Service, w io.Writer) error {
|
2015-04-01 21:46:33 +00:00
|
|
|
ips := []string{svc.Spec.PortalIP}
|
|
|
|
for _, publicIP := range svc.Spec.PublicIPs {
|
|
|
|
ips = append(ips, publicIP)
|
|
|
|
}
|
2015-03-13 15:16:41 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d/%s\n", svc.Name, formatLabels(svc.Labels),
|
2015-04-01 21:46:33 +00:00
|
|
|
formatLabels(svc.Spec.Selector), ips[0], svc.Spec.Ports[0].Port, svc.Spec.Ports[0].Protocol); err != nil {
|
2015-03-13 15:16:41 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-04-01 21:46:33 +00:00
|
|
|
|
|
|
|
count := len(svc.Spec.Ports)
|
|
|
|
if len(ips) > count {
|
|
|
|
count = len(ips)
|
|
|
|
}
|
|
|
|
for i := 1; i < count; i++ {
|
|
|
|
ip := ""
|
|
|
|
if len(ips) > i {
|
|
|
|
ip = ips[i]
|
|
|
|
}
|
|
|
|
port := ""
|
|
|
|
if len(svc.Spec.Ports) > i {
|
|
|
|
port = fmt.Sprintf("%d/%s", svc.Spec.Ports[i].Port, svc.Spec.Ports[i].Protocol)
|
|
|
|
}
|
2015-03-13 15:16:41 +00:00
|
|
|
// Lay out additional ports.
|
2015-04-01 21:46:33 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", "", "", "", ip, port); err != nil {
|
2015-03-13 15:16:41 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func printServiceList(list *api.ServiceList, w io.Writer) error {
|
|
|
|
for _, svc := range list.Items {
|
|
|
|
if err := printService(&svc, w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-20 21:24:43 +00:00
|
|
|
func printEndpoints(endpoints *api.Endpoints, w io.Writer) error {
|
|
|
|
_, err := fmt.Fprintf(w, "%s\t%s\n", endpoints.Name, formatEndpoints(endpoints))
|
2015-02-05 23:45:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-24 02:20:10 +00:00
|
|
|
func printEndpointsList(list *api.EndpointsList, w io.Writer) error {
|
|
|
|
for _, item := range list.Items {
|
|
|
|
if err := printEndpoints(&item, w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-19 21:50:00 +00:00
|
|
|
func printNamespace(item *api.Namespace, w io.Writer) error {
|
2015-03-10 15:21:09 +00:00
|
|
|
_, err := fmt.Fprintf(w, "%s\t%s\t%s\n", item.Name, formatLabels(item.Labels), item.Status.Phase)
|
2015-01-19 21:50:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func printNamespaceList(list *api.NamespaceList, w io.Writer) error {
|
|
|
|
for _, item := range list.Items {
|
|
|
|
if err := printNamespace(&item, w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-18 01:24:50 +00:00
|
|
|
func printSecret(item *api.Secret, w io.Writer) error {
|
|
|
|
_, err := fmt.Fprintf(w, "%s\t%v\n", item.Name, len(item.Data))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func printSecretList(list *api.SecretList, w io.Writer) error {
|
|
|
|
for _, item := range list.Items {
|
|
|
|
if err := printSecret(&item, w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-21 17:07:09 +00:00
|
|
|
func printNode(node *api.Node, w io.Writer) error {
|
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")
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprintf(w, "%s\t%s\t%s\n", node.Name, formatLabels(node.Labels), strings.Join(status, ","))
|
2014-10-06 01:24:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-21 17:07:09 +00:00
|
|
|
func printNodeList(list *api.NodeList, w io.Writer) error {
|
|
|
|
for _, node := range list.Items {
|
|
|
|
if err := printNode(&node, w); err != nil {
|
2014-10-06 01:24:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-26 19:50:36 +00:00
|
|
|
func printPersistentVolume(pv *api.PersistentVolume, w io.Writer) error {
|
|
|
|
claimRefUID := ""
|
|
|
|
if pv.Spec.ClaimRef != nil {
|
|
|
|
claimRefUID += pv.Spec.ClaimRef.Name
|
|
|
|
claimRefUID += " / "
|
|
|
|
claimRefUID += string(pv.Spec.ClaimRef.UID)
|
|
|
|
}
|
|
|
|
|
|
|
|
modesStr := volume.GetAccessModesAsString(pv.Spec.AccessModes)
|
|
|
|
|
|
|
|
aQty := pv.Spec.Capacity[api.ResourceStorage]
|
|
|
|
aSize := aQty.Value()
|
|
|
|
|
|
|
|
_, err := fmt.Fprintf(w, "%s\t%s\t%d\t%s\t%s\t%s\n", pv.Name, pv.Labels, aSize, modesStr, pv.Status.Phase, claimRefUID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func printPersistentVolumeList(list *api.PersistentVolumeList, w io.Writer) error {
|
|
|
|
for _, pv := range list.Items {
|
|
|
|
if err := printPersistentVolume(&pv, w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func printPersistentVolumeClaim(pvc *api.PersistentVolumeClaim, w io.Writer) error {
|
|
|
|
volumeRefUID := ""
|
|
|
|
if pvc.Status.VolumeRef != nil {
|
|
|
|
volumeRefUID = string(pvc.Status.VolumeRef.UID)
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", pvc.Name, pvc.Labels, pvc.Status.Phase, volumeRefUID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func printPersistentVolumeClaimList(list *api.PersistentVolumeClaimList, w io.Writer) error {
|
|
|
|
for _, psd := range list.Items {
|
|
|
|
if err := printPersistentVolumeClaim(&psd, w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-06 01:24:19 +00:00
|
|
|
func printStatus(status *api.Status, w io.Writer) error {
|
|
|
|
_, err := fmt.Fprintf(w, "%v\n", status.Status)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-04 02:02:27 +00:00
|
|
|
func printEvent(event *api.Event, w io.Writer) error {
|
|
|
|
_, err := fmt.Fprintf(
|
2015-02-11 00:49:32 +00:00
|
|
|
w, "%s\t%s\t%d\t%s\t%s\t%s\t%s\t%s\t%s\n",
|
2015-02-06 02:21:01 +00:00
|
|
|
event.FirstTimestamp.Time.Format(time.RFC1123Z),
|
2015-02-11 00:49:32 +00:00
|
|
|
event.LastTimestamp.Time.Format(time.RFC1123Z),
|
|
|
|
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,
|
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,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-16 22:20:51 +00:00
|
|
|
// Sorts and prints the EventList in a human-friendly format.
|
2014-11-04 02:02:27 +00:00
|
|
|
func printEventList(list *api.EventList, w io.Writer) 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 {
|
|
|
|
if err := printEvent(&list.Items[i], w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-22 21:52:40 +00:00
|
|
|
func printLimitRange(limitRange *api.LimitRange, w io.Writer) error {
|
|
|
|
_, err := fmt.Fprintf(
|
|
|
|
w, "%s\n",
|
|
|
|
limitRange.Name,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prints the LimitRangeList in a human-friendly format.
|
|
|
|
func printLimitRangeList(list *api.LimitRangeList, w io.Writer) error {
|
|
|
|
for i := range list.Items {
|
|
|
|
if err := printLimitRange(&list.Items[i], w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-23 17:38:30 +00:00
|
|
|
func printResourceQuota(resourceQuota *api.ResourceQuota, w io.Writer) error {
|
|
|
|
_, err := fmt.Fprintf(
|
|
|
|
w, "%s\n",
|
|
|
|
resourceQuota.Name,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prints the ResourceQuotaList in a human-friendly format.
|
|
|
|
func printResourceQuotaList(list *api.ResourceQuotaList, w io.Writer) error {
|
|
|
|
for i := range list.Items {
|
|
|
|
if err := printResourceQuota(&list.Items[i], w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-15 19:23:02 +00:00
|
|
|
func printComponentStatus(item *api.ComponentStatus, w io.Writer) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", item.Name, status, message, error)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func printComponentStatusList(list *api.ComponentStatusList, w io.Writer) error {
|
|
|
|
for _, item := range list.Items {
|
|
|
|
if err := printComponentStatus(&item, w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-03-27 23:50:29 +00:00
|
|
|
w := tabwriter.NewWriter(output, 10, 4, 3, ' ', 0)
|
2014-10-06 01:24:19 +00:00
|
|
|
defer w.Flush()
|
2014-11-12 01:31:13 +00:00
|
|
|
t := reflect.TypeOf(obj)
|
|
|
|
if handler := h.handlerMap[t]; handler != nil {
|
|
|
|
if !h.noHeaders && t != h.lastType {
|
2014-10-16 01:54:46 +00:00
|
|
|
h.printHeader(handler.columns, w)
|
2014-11-12 01:31:13 +00:00
|
|
|
h.lastType = t
|
2014-10-16 01:54:46 +00:00
|
|
|
}
|
2014-10-06 01:24:19 +00:00
|
|
|
args := []reflect.Value{reflect.ValueOf(obj), reflect.ValueOf(w)}
|
|
|
|
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.
|
|
|
|
fmt.Fprintf(w, "Error executing template: %v\n", err)
|
|
|
|
fmt.Fprintf(w, "template was:\n\t%v\n", p.rawTemplate)
|
|
|
|
fmt.Fprintf(w, "raw data was:\n\t%v\n", string(data))
|
|
|
|
fmt.Fprintf(w, "object given to template engine was:\n\t%+v\n", out)
|
|
|
|
return fmt.Errorf("error executing template '%v': '%v'\n----data----\n%+v\n", p.rawTemplate, err, out)
|
2014-11-20 22:09:59 +00:00
|
|
|
}
|
|
|
|
return nil
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|