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 (
|
|
|
|
"fmt"
|
2014-11-14 19:56:41 +00:00
|
|
|
"io"
|
2015-02-26 18:50:12 +00:00
|
|
|
"reflect"
|
2014-11-14 19:56:41 +00:00
|
|
|
"sort"
|
2014-10-06 01:24:19 +00:00
|
|
|
"strings"
|
2014-12-16 22:20:51 +00:00
|
|
|
"time"
|
2014-10-06 01:24:19 +00:00
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2015-03-26 23:32:08 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
|
2014-10-06 01:24:19 +00:00
|
|
|
"github.com/golang/glog"
|
|
|
|
)
|
|
|
|
|
2014-10-27 19:56:34 +00:00
|
|
|
// Describer generates output for the named resource or an error
|
2015-02-26 18:50:12 +00:00
|
|
|
// if the output could not be generated. Implementors typically
|
|
|
|
// abstract the retrieval of the named object from a remote server.
|
2014-10-27 19:56:34 +00:00
|
|
|
type Describer interface {
|
|
|
|
Describe(namespace, name string) (output string, err error)
|
|
|
|
}
|
|
|
|
|
2015-02-26 18:50:12 +00:00
|
|
|
// ObjectDescriber is an interface for displaying arbitrary objects with extra
|
|
|
|
// information. Use when an object is in hand (on disk, or already retrieved).
|
|
|
|
// Implementors may ignore the additional information passed on extra, or use it
|
|
|
|
// by default. ObjectDescribers may return ErrNoDescriber if no suitable describer
|
|
|
|
// is found.
|
|
|
|
type ObjectDescriber interface {
|
|
|
|
DescribeObject(object interface{}, extra ...interface{}) (output string, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrNoDescriber is a structured error indicating the provided object or objects
|
|
|
|
// cannot be described.
|
|
|
|
type ErrNoDescriber struct {
|
|
|
|
Types []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error implements the error interface.
|
|
|
|
func (e ErrNoDescriber) Error() string {
|
|
|
|
return fmt.Sprintf("no describer has been defined for %v", e.Types)
|
|
|
|
}
|
|
|
|
|
2014-10-27 19:56:34 +00:00
|
|
|
// Describer returns the default describe functions for each of the standard
|
|
|
|
// Kubernetes types.
|
|
|
|
func DescriberFor(kind string, c *client.Client) (Describer, bool) {
|
|
|
|
switch kind {
|
|
|
|
case "Pod":
|
2014-11-14 01:42:50 +00:00
|
|
|
return &PodDescriber{c}, true
|
2014-10-27 19:56:34 +00:00
|
|
|
case "ReplicationController":
|
2014-11-14 01:42:50 +00:00
|
|
|
return &ReplicationControllerDescriber{c}, true
|
2014-10-27 19:56:34 +00:00
|
|
|
case "Service":
|
2014-11-14 01:42:50 +00:00
|
|
|
return &ServiceDescriber{c}, true
|
|
|
|
case "Minion", "Node":
|
2015-03-06 18:04:52 +00:00
|
|
|
return &NodeDescriber{c}, true
|
2015-01-22 21:52:40 +00:00
|
|
|
case "LimitRange":
|
|
|
|
return &LimitRangeDescriber{c}, true
|
2015-01-23 17:38:30 +00:00
|
|
|
case "ResourceQuota":
|
|
|
|
return &ResourceQuotaDescriber{c}, true
|
2014-10-27 19:56:34 +00:00
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2015-02-26 18:50:12 +00:00
|
|
|
// DefaultObjectDescriber can describe the default Kubernetes objects.
|
|
|
|
var DefaultObjectDescriber ObjectDescriber
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
d := &Describers{}
|
|
|
|
err := d.Add(
|
|
|
|
describeLimitRange,
|
|
|
|
describeQuota,
|
|
|
|
describePod,
|
|
|
|
describeService,
|
|
|
|
describeReplicationController,
|
|
|
|
describeNode,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("Cannot register describers: %v", err)
|
|
|
|
}
|
|
|
|
DefaultObjectDescriber = d
|
|
|
|
}
|
|
|
|
|
2015-01-22 21:52:40 +00:00
|
|
|
// LimitRangeDescriber generates information about a limit range
|
|
|
|
type LimitRangeDescriber struct {
|
|
|
|
client.Interface
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *LimitRangeDescriber) Describe(namespace, name string) (string, error) {
|
|
|
|
lr := d.LimitRanges(namespace)
|
|
|
|
|
|
|
|
limitRange, err := lr.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-02-26 18:50:12 +00:00
|
|
|
return describeLimitRange(limitRange)
|
|
|
|
}
|
2015-01-22 21:52:40 +00:00
|
|
|
|
2015-02-26 18:50:12 +00:00
|
|
|
func describeLimitRange(limitRange *api.LimitRange) (string, error) {
|
2015-01-22 21:52:40 +00:00
|
|
|
return tabbedString(func(out io.Writer) error {
|
|
|
|
fmt.Fprintf(out, "Name:\t%s\n", limitRange.Name)
|
2015-01-23 03:21:13 +00:00
|
|
|
fmt.Fprintf(out, "Type\tResource\tMin\tMax\n")
|
2015-01-22 21:52:40 +00:00
|
|
|
fmt.Fprintf(out, "----\t--------\t---\t---\n")
|
2015-01-23 04:17:04 +00:00
|
|
|
for i := range limitRange.Spec.Limits {
|
2015-01-22 21:52:40 +00:00
|
|
|
item := limitRange.Spec.Limits[i]
|
|
|
|
maxResources := item.Max
|
|
|
|
minResources := item.Min
|
|
|
|
|
|
|
|
set := map[api.ResourceName]bool{}
|
2015-01-23 04:17:04 +00:00
|
|
|
for k := range maxResources {
|
2015-01-22 21:52:40 +00:00
|
|
|
set[k] = true
|
|
|
|
}
|
2015-01-23 04:17:04 +00:00
|
|
|
for k := range minResources {
|
2015-01-22 21:52:40 +00:00
|
|
|
set[k] = true
|
|
|
|
}
|
|
|
|
|
2015-01-23 04:17:04 +00:00
|
|
|
for k := range set {
|
2015-01-22 21:52:40 +00:00
|
|
|
// if no value is set, we output -
|
|
|
|
maxValue := "-"
|
|
|
|
minValue := "-"
|
|
|
|
|
|
|
|
maxQuantity, maxQuantityFound := maxResources[k]
|
|
|
|
if maxQuantityFound {
|
|
|
|
maxValue = maxQuantity.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
minQuantity, minQuantityFound := minResources[k]
|
|
|
|
if minQuantityFound {
|
|
|
|
minValue = minQuantity.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := "%v\t%v\t%v\t%v\n"
|
2015-01-23 03:21:13 +00:00
|
|
|
fmt.Fprintf(out, msg, item.Type, k, minValue, maxValue)
|
2015-01-22 21:52:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-01-23 17:38:30 +00:00
|
|
|
// ResourceQuotaDescriber generates information about a resource quota
|
|
|
|
type ResourceQuotaDescriber struct {
|
|
|
|
client.Interface
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ResourceQuotaDescriber) Describe(namespace, name string) (string, error) {
|
|
|
|
rq := d.ResourceQuotas(namespace)
|
|
|
|
|
|
|
|
resourceQuota, err := rq.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-02-26 18:50:12 +00:00
|
|
|
return describeQuota(resourceQuota)
|
|
|
|
}
|
|
|
|
|
|
|
|
func describeQuota(resourceQuota *api.ResourceQuota) (string, error) {
|
2015-01-23 17:38:30 +00:00
|
|
|
return tabbedString(func(out io.Writer) error {
|
|
|
|
fmt.Fprintf(out, "Name:\t%s\n", resourceQuota.Name)
|
|
|
|
fmt.Fprintf(out, "Resource\tUsed\tHard\n")
|
|
|
|
fmt.Fprintf(out, "--------\t----\t----\n")
|
|
|
|
|
|
|
|
resources := []api.ResourceName{}
|
|
|
|
for resource := range resourceQuota.Status.Hard {
|
|
|
|
resources = append(resources, resource)
|
|
|
|
}
|
|
|
|
sort.Sort(SortableResourceNames(resources))
|
|
|
|
|
|
|
|
msg := "%v\t%v\t%v\n"
|
|
|
|
for i := range resources {
|
|
|
|
resource := resources[i]
|
|
|
|
hardQuantity := resourceQuota.Status.Hard[resource]
|
|
|
|
usedQuantity := resourceQuota.Status.Used[resource]
|
|
|
|
fmt.Fprintf(out, msg, resource, usedQuantity.String(), hardQuantity.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-10-27 19:56:34 +00:00
|
|
|
// PodDescriber generates information about a pod and the replication controllers that
|
|
|
|
// create it.
|
|
|
|
type PodDescriber struct {
|
2014-11-14 01:42:50 +00:00
|
|
|
client.Interface
|
2014-10-27 19:56:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *PodDescriber) Describe(namespace, name string) (string, error) {
|
2014-11-14 01:42:50 +00:00
|
|
|
rc := d.ReplicationControllers(namespace)
|
|
|
|
pc := d.Pods(namespace)
|
2014-10-06 01:24:19 +00:00
|
|
|
|
2014-10-27 19:56:34 +00:00
|
|
|
pod, err := pc.Get(name)
|
2014-10-06 01:24:19 +00:00
|
|
|
if err != nil {
|
2015-03-17 02:02:22 +00:00
|
|
|
eventsInterface := d.Events(namespace)
|
|
|
|
events, err2 := eventsInterface.List(
|
2014-11-14 19:56:41 +00:00
|
|
|
labels.Everything(),
|
2015-03-17 02:02:22 +00:00
|
|
|
eventsInterface.GetFieldSelector(&name, &namespace, nil, nil))
|
2014-11-14 19:56:41 +00:00
|
|
|
if err2 == nil && len(events.Items) > 0 {
|
|
|
|
return tabbedString(func(out io.Writer) error {
|
|
|
|
fmt.Fprintf(out, "Pod '%v': error '%v', but found events.\n", name, err)
|
|
|
|
describeEvents(events, out)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2014-10-06 01:24:19 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-11-24 23:17:28 +00:00
|
|
|
var events *api.EventList
|
|
|
|
if ref, err := api.GetReference(pod); err != nil {
|
|
|
|
glog.Errorf("Unable to construct reference to '%#v': %v", pod, err)
|
|
|
|
} else {
|
2015-03-16 12:20:03 +00:00
|
|
|
ref.Kind = ""
|
2014-11-24 23:17:28 +00:00
|
|
|
events, _ = d.Events(namespace).Search(ref)
|
|
|
|
}
|
2014-11-14 19:56:41 +00:00
|
|
|
|
2015-02-26 18:50:12 +00:00
|
|
|
rcs, err := getReplicationControllersForLabels(rc, labels.Set(pod.Labels))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return describePod(pod, rcs, events)
|
|
|
|
}
|
|
|
|
|
|
|
|
func describePod(pod *api.Pod, rcs []api.ReplicationController, events *api.EventList) (string, error) {
|
2014-11-14 19:56:41 +00:00
|
|
|
return tabbedString(func(out io.Writer) error {
|
2014-10-22 17:02:02 +00:00
|
|
|
fmt.Fprintf(out, "Name:\t%s\n", pod.Name)
|
2015-02-26 18:50:12 +00:00
|
|
|
fmt.Fprintf(out, "Image(s):\t%s\n", makeImageList(&pod.Spec))
|
2014-11-13 15:52:13 +00:00
|
|
|
fmt.Fprintf(out, "Host:\t%s\n", pod.Status.Host+"/"+pod.Status.HostIP)
|
2014-10-06 01:24:19 +00:00
|
|
|
fmt.Fprintf(out, "Labels:\t%s\n", formatLabels(pod.Labels))
|
2014-11-21 19:04:32 +00:00
|
|
|
fmt.Fprintf(out, "Status:\t%s\n", string(pod.Status.Phase))
|
2015-02-26 18:50:12 +00:00
|
|
|
fmt.Fprintf(out, "Replication Controllers:\t%s\n", printReplicationControllersByLabels(rcs))
|
2015-02-10 05:09:32 +00:00
|
|
|
if len(pod.Status.Conditions) > 0 {
|
2015-02-24 05:21:14 +00:00
|
|
|
fmt.Fprint(out, "Conditions:\n Type\tStatus\n")
|
2015-02-10 05:09:32 +00:00
|
|
|
for _, c := range pod.Status.Conditions {
|
|
|
|
fmt.Fprintf(out, " %v \t%v \n",
|
2015-02-24 05:21:14 +00:00
|
|
|
c.Type,
|
2015-02-10 05:09:32 +00:00
|
|
|
c.Status)
|
|
|
|
}
|
|
|
|
}
|
2014-11-14 19:56:41 +00:00
|
|
|
if events != nil {
|
|
|
|
describeEvents(events, out)
|
|
|
|
}
|
2014-10-06 01:24:19 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-10-27 19:56:34 +00:00
|
|
|
// ReplicationControllerDescriber generates information about a replication controller
|
|
|
|
// and the pods it has created.
|
|
|
|
type ReplicationControllerDescriber struct {
|
2014-11-14 01:42:50 +00:00
|
|
|
client.Interface
|
2014-10-27 19:56:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ReplicationControllerDescriber) Describe(namespace, name string) (string, error) {
|
2014-11-14 01:42:50 +00:00
|
|
|
rc := d.ReplicationControllers(namespace)
|
|
|
|
pc := d.Pods(namespace)
|
2014-10-06 01:24:19 +00:00
|
|
|
|
2014-10-27 19:56:34 +00:00
|
|
|
controller, err := rc.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-11-05 15:22:54 +00:00
|
|
|
running, waiting, succeeded, failed, err := getPodStatusForReplicationController(pc, controller)
|
2014-10-06 01:24:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-11-14 19:56:41 +00:00
|
|
|
events, _ := d.Events(namespace).Search(controller)
|
|
|
|
|
2015-02-26 18:50:12 +00:00
|
|
|
return describeReplicationController(controller, events, running, waiting, succeeded, failed)
|
|
|
|
}
|
|
|
|
|
|
|
|
func describeReplicationController(controller *api.ReplicationController, events *api.EventList, running, waiting, succeeded, failed int) (string, error) {
|
2014-11-14 19:56:41 +00:00
|
|
|
return tabbedString(func(out io.Writer) error {
|
2014-10-22 17:02:02 +00:00
|
|
|
fmt.Fprintf(out, "Name:\t%s\n", controller.Name)
|
2015-02-26 18:50:12 +00:00
|
|
|
if controller.Spec.Template != nil {
|
|
|
|
fmt.Fprintf(out, "Image(s):\t%s\n", makeImageList(&controller.Spec.Template.Spec))
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(out, "Image(s):\t%s\n", "<no template>")
|
|
|
|
}
|
2014-11-07 02:09:46 +00:00
|
|
|
fmt.Fprintf(out, "Selector:\t%s\n", formatLabels(controller.Spec.Selector))
|
2014-10-22 17:02:02 +00:00
|
|
|
fmt.Fprintf(out, "Labels:\t%s\n", formatLabels(controller.Labels))
|
2014-11-07 02:09:46 +00:00
|
|
|
fmt.Fprintf(out, "Replicas:\t%d current / %d desired\n", controller.Status.Replicas, controller.Spec.Replicas)
|
2014-11-05 15:22:54 +00:00
|
|
|
fmt.Fprintf(out, "Pods Status:\t%d Running / %d Waiting / %d Succeeded / %d Failed\n", running, waiting, succeeded, failed)
|
2014-11-14 19:56:41 +00:00
|
|
|
if events != nil {
|
|
|
|
describeEvents(events, out)
|
|
|
|
}
|
2014-10-06 01:24:19 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-10-27 19:56:34 +00:00
|
|
|
// ServiceDescriber generates information about a service.
|
|
|
|
type ServiceDescriber struct {
|
2014-11-14 01:42:50 +00:00
|
|
|
client.Interface
|
2014-10-27 19:56:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ServiceDescriber) Describe(namespace, name string) (string, error) {
|
2014-11-14 01:42:50 +00:00
|
|
|
c := d.Services(namespace)
|
2014-10-27 19:56:34 +00:00
|
|
|
|
|
|
|
service, err := c.Get(name)
|
2014-10-06 01:24:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-02-26 18:50:12 +00:00
|
|
|
endpoints, _ := d.Endpoints(namespace).Get(name)
|
2014-11-14 19:56:41 +00:00
|
|
|
events, _ := d.Events(namespace).Search(service)
|
|
|
|
|
2015-02-26 18:50:12 +00:00
|
|
|
return describeService(service, endpoints, events)
|
|
|
|
}
|
|
|
|
|
|
|
|
func describeService(service *api.Service, endpoints *api.Endpoints, events *api.EventList) (string, error) {
|
|
|
|
if endpoints == nil {
|
|
|
|
endpoints = &api.Endpoints{}
|
|
|
|
}
|
2014-11-14 19:56:41 +00:00
|
|
|
return tabbedString(func(out io.Writer) error {
|
2014-10-22 17:02:02 +00:00
|
|
|
fmt.Fprintf(out, "Name:\t%s\n", service.Name)
|
|
|
|
fmt.Fprintf(out, "Labels:\t%s\n", formatLabels(service.Labels))
|
2014-10-30 13:29:11 +00:00
|
|
|
fmt.Fprintf(out, "Selector:\t%s\n", formatLabels(service.Spec.Selector))
|
2015-02-21 23:13:28 +00:00
|
|
|
fmt.Fprintf(out, "IP:\t%s\n", service.Spec.PortalIP)
|
|
|
|
if len(service.Spec.PublicIPs) > 0 {
|
|
|
|
list := strings.Join(service.Spec.PublicIPs, ", ")
|
|
|
|
fmt.Fprintf(out, "Public IPs:\t%s\n", list)
|
|
|
|
}
|
2015-03-13 15:16:41 +00:00
|
|
|
for i := range service.Spec.Ports {
|
|
|
|
sp := &service.Spec.Ports[i]
|
|
|
|
|
|
|
|
name := sp.Name
|
|
|
|
if name == "" {
|
|
|
|
name = "<unnamed>"
|
|
|
|
}
|
2015-03-31 16:30:56 +00:00
|
|
|
fmt.Fprintf(out, "Port:\t%s\t%d/%s\n", name, sp.Port, sp.Protocol)
|
2015-03-13 15:16:41 +00:00
|
|
|
}
|
2015-03-20 21:24:43 +00:00
|
|
|
fmt.Fprintf(out, "Endpoints:\t%s\n", formatEndpoints(endpoints))
|
2015-02-21 23:13:28 +00:00
|
|
|
fmt.Fprintf(out, "Session Affinity:\t%s\n", service.Spec.SessionAffinity)
|
2014-11-14 19:56:41 +00:00
|
|
|
if events != nil {
|
|
|
|
describeEvents(events, out)
|
|
|
|
}
|
2014-10-06 01:24:19 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-03-06 18:04:52 +00:00
|
|
|
// NodeDescriber generates information about a node.
|
|
|
|
type NodeDescriber struct {
|
2014-11-14 01:42:50 +00:00
|
|
|
client.Interface
|
2014-10-27 19:56:34 +00:00
|
|
|
}
|
|
|
|
|
2015-03-06 18:04:52 +00:00
|
|
|
func (d *NodeDescriber) Describe(namespace, name string) (string, error) {
|
2014-12-08 05:56:43 +00:00
|
|
|
mc := d.Nodes()
|
2015-03-06 18:04:52 +00:00
|
|
|
node, err := mc.Get(name)
|
2014-10-06 01:24:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-02-21 17:07:09 +00:00
|
|
|
var pods []api.Pod
|
|
|
|
allPods, err := d.Pods(namespace).List(labels.Everything())
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
for _, pod := range allPods.Items {
|
|
|
|
if pod.Status.Host != name {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pods = append(pods, pod)
|
|
|
|
}
|
|
|
|
|
2015-03-26 23:32:08 +00:00
|
|
|
var events *api.EventList
|
|
|
|
if ref, err := api.GetReference(node); err != nil {
|
|
|
|
glog.Errorf("Unable to construct reference to '%#v': %v", node, err)
|
|
|
|
} else {
|
|
|
|
// TODO: We haven't decided the namespace for Node object yet.
|
|
|
|
ref.UID = types.UID(ref.Name)
|
|
|
|
events, _ = d.Events("").Search(ref)
|
|
|
|
}
|
2014-11-14 19:56:41 +00:00
|
|
|
|
2015-02-26 18:50:12 +00:00
|
|
|
return describeNode(node, pods, events)
|
|
|
|
}
|
|
|
|
|
|
|
|
func describeNode(node *api.Node, pods []api.Pod, events *api.EventList) (string, error) {
|
2014-11-14 19:56:41 +00:00
|
|
|
return tabbedString(func(out io.Writer) error {
|
2015-03-06 18:04:52 +00:00
|
|
|
fmt.Fprintf(out, "Name:\t%s\n", node.Name)
|
2015-03-23 16:14:18 +00:00
|
|
|
fmt.Fprintf(out, "Labels:\t%s\n", formatLabels(node.Labels))
|
|
|
|
fmt.Fprintf(out, "CreationTimestamp:\t%s\n", node.CreationTimestamp.Time.Format(time.RFC1123Z))
|
2015-03-06 18:04:52 +00:00
|
|
|
if len(node.Status.Conditions) > 0 {
|
2015-02-24 05:21:14 +00:00
|
|
|
fmt.Fprint(out, "Conditions:\n Type\tStatus\tLastProbeTime\tLastTransitionTime\tReason\tMessage\n")
|
2015-03-06 18:04:52 +00:00
|
|
|
for _, c := range node.Status.Conditions {
|
2015-02-10 05:09:32 +00:00
|
|
|
fmt.Fprintf(out, " %v \t%v \t%s \t%s \t%v \t%v\n",
|
2015-02-24 05:21:14 +00:00
|
|
|
c.Type,
|
2015-02-10 05:09:32 +00:00
|
|
|
c.Status,
|
|
|
|
c.LastProbeTime.Time.Format(time.RFC1123Z),
|
|
|
|
c.LastTransitionTime.Time.Format(time.RFC1123Z),
|
|
|
|
c.Reason,
|
|
|
|
c.Message)
|
|
|
|
}
|
|
|
|
}
|
2015-03-06 18:04:52 +00:00
|
|
|
var addresses []string
|
|
|
|
for _, address := range node.Status.Addresses {
|
|
|
|
addresses = append(addresses, address.Address)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(out, "Addresses:\t%s\n", strings.Join(addresses, ","))
|
2015-03-25 13:44:40 +00:00
|
|
|
if len(node.Status.Capacity) > 0 {
|
2015-03-02 19:51:30 +00:00
|
|
|
fmt.Fprintf(out, "Capacity:\n")
|
2015-03-25 13:44:40 +00:00
|
|
|
for resource, value := range node.Status.Capacity {
|
2015-03-02 19:51:30 +00:00
|
|
|
fmt.Fprintf(out, " %s:\t%s\n", resource, value.String())
|
|
|
|
}
|
|
|
|
}
|
2015-03-31 07:36:06 +00:00
|
|
|
|
|
|
|
fmt.Fprintf(out, "Version:\n")
|
|
|
|
fmt.Fprintf(out, " Kernel Version:\t%s\n", node.Status.NodeInfo.KernelVersion)
|
|
|
|
fmt.Fprintf(out, " OS Image:\t%s\n", node.Status.NodeInfo.OsImage)
|
|
|
|
fmt.Fprintf(out, " Container Runtime Version:\t%s\n", node.Status.NodeInfo.ContainerRuntimeVersion)
|
|
|
|
fmt.Fprintf(out, " Kubelet Version:\t%s\n", node.Status.NodeInfo.KubeletVersion)
|
|
|
|
fmt.Fprintf(out, " Kube-Proxy Version:\t%s\n", node.Status.NodeInfo.KubeProxyVersion)
|
|
|
|
|
2015-03-06 18:04:52 +00:00
|
|
|
if len(node.Spec.PodCIDR) > 0 {
|
|
|
|
fmt.Fprintf(out, "PodCIDR:\t%s\n", node.Spec.PodCIDR)
|
|
|
|
}
|
|
|
|
if len(node.Spec.ExternalID) > 0 {
|
|
|
|
fmt.Fprintf(out, "ExternalID:\t%s\n", node.Spec.ExternalID)
|
|
|
|
}
|
2015-02-21 17:07:09 +00:00
|
|
|
fmt.Fprintf(out, "Pods:\t(%d in total)\n", len(pods))
|
|
|
|
for _, pod := range pods {
|
|
|
|
fmt.Fprintf(out, " %s\n", pod.Name)
|
|
|
|
}
|
2014-11-14 19:56:41 +00:00
|
|
|
if events != nil {
|
|
|
|
describeEvents(events, out)
|
|
|
|
}
|
2014-10-06 01:24:19 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-11-14 19:56:41 +00:00
|
|
|
func describeEvents(el *api.EventList, w io.Writer) {
|
|
|
|
if len(el.Items) == 0 {
|
|
|
|
fmt.Fprint(w, "No events.")
|
|
|
|
return
|
|
|
|
}
|
2014-12-16 22:20:51 +00:00
|
|
|
sort.Sort(SortableEvents(el.Items))
|
2015-02-21 17:07:09 +00:00
|
|
|
fmt.Fprint(w, "Events:\n FirstSeen\tLastSeen\tCount\tFrom\tSubobjectPath\tReason\tMessage\n")
|
2014-11-14 19:56:41 +00:00
|
|
|
for _, e := range el.Items {
|
2015-02-21 17:07:09 +00:00
|
|
|
fmt.Fprintf(w, " %s\t%s\t%d\t%v\t%v\t%v\t%v\n",
|
2015-02-06 02:21:01 +00:00
|
|
|
e.FirstTimestamp.Time.Format(time.RFC1123Z),
|
2015-02-11 00:49:32 +00:00
|
|
|
e.LastTimestamp.Time.Format(time.RFC1123Z),
|
|
|
|
e.Count,
|
2014-12-16 22:20:51 +00:00
|
|
|
e.Source,
|
|
|
|
e.InvolvedObject.FieldPath,
|
|
|
|
e.Reason,
|
|
|
|
e.Message)
|
2014-11-14 19:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-06 01:24:19 +00:00
|
|
|
// Get all replication controllers whose selectors would match a given set of
|
|
|
|
// labels.
|
|
|
|
// TODO Move this to pkg/client and ideally implement it server-side (instead
|
|
|
|
// of getting all RC's and searching through them manually).
|
2015-02-26 18:50:12 +00:00
|
|
|
func getReplicationControllersForLabels(c client.ReplicationControllerInterface, labelsToMatch labels.Labels) ([]api.ReplicationController, error) {
|
2014-10-06 01:24:19 +00:00
|
|
|
// Get all replication controllers.
|
2014-10-21 21:14:35 +00:00
|
|
|
// TODO this needs a namespace scope as argument
|
2014-10-27 19:56:34 +00:00
|
|
|
rcs, err := c.List(labels.Everything())
|
2014-10-06 01:24:19 +00:00
|
|
|
if err != nil {
|
2015-02-26 18:50:12 +00:00
|
|
|
return nil, fmt.Errorf("error getting replication controllers: %v", err)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the ones that match labelsToMatch.
|
|
|
|
var matchingRCs []api.ReplicationController
|
2014-10-22 17:02:02 +00:00
|
|
|
for _, controller := range rcs.Items {
|
2014-11-07 02:09:46 +00:00
|
|
|
selector := labels.SelectorFromSet(controller.Spec.Selector)
|
2014-10-06 01:24:19 +00:00
|
|
|
if selector.Matches(labelsToMatch) {
|
2014-10-22 17:02:02 +00:00
|
|
|
matchingRCs = append(matchingRCs, controller)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-26 18:50:12 +00:00
|
|
|
return matchingRCs, nil
|
|
|
|
}
|
2014-10-06 01:24:19 +00:00
|
|
|
|
2015-02-26 18:50:12 +00:00
|
|
|
func printReplicationControllersByLabels(matchingRCs []api.ReplicationController) string {
|
2014-10-06 01:24:19 +00:00
|
|
|
// Format the matching RC's into strings.
|
|
|
|
var rcStrings []string
|
2014-10-22 17:02:02 +00:00
|
|
|
for _, controller := range matchingRCs {
|
2014-11-07 02:09:46 +00:00
|
|
|
rcStrings = append(rcStrings, fmt.Sprintf("%s (%d/%d replicas created)", controller.Name, controller.Status.Replicas, controller.Spec.Replicas))
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
list := strings.Join(rcStrings, ", ")
|
|
|
|
if list == "" {
|
|
|
|
return "<none>"
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2014-11-05 15:22:54 +00:00
|
|
|
func getPodStatusForReplicationController(c client.PodInterface, controller *api.ReplicationController) (running, waiting, succeeded, failed int, err error) {
|
2014-11-07 02:09:46 +00:00
|
|
|
rcPods, err := c.List(labels.SelectorFromSet(controller.Spec.Selector))
|
2014-10-06 01:24:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, pod := range rcPods.Items {
|
2014-11-21 19:04:32 +00:00
|
|
|
switch pod.Status.Phase {
|
2014-11-07 07:11:21 +00:00
|
|
|
case api.PodRunning:
|
2014-10-06 01:24:19 +00:00
|
|
|
running++
|
2014-11-07 07:11:21 +00:00
|
|
|
case api.PodPending:
|
2014-10-06 01:24:19 +00:00
|
|
|
waiting++
|
2014-11-07 07:11:21 +00:00
|
|
|
case api.PodSucceeded:
|
2014-11-05 15:22:54 +00:00
|
|
|
succeeded++
|
2014-11-07 07:11:21 +00:00
|
|
|
case api.PodFailed:
|
2014-11-05 15:22:54 +00:00
|
|
|
failed++
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2015-02-26 18:50:12 +00:00
|
|
|
|
|
|
|
// newErrNoDescriber creates a new ErrNoDescriber with the names of the provided types.
|
|
|
|
func newErrNoDescriber(types ...reflect.Type) error {
|
|
|
|
names := []string{}
|
|
|
|
for _, t := range types {
|
|
|
|
names = append(names, t.String())
|
|
|
|
}
|
|
|
|
return ErrNoDescriber{Types: names}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Describers implements ObjectDescriber against functions registered via Add. Those functions can
|
|
|
|
// be strongly typed. Types are exactly matched (no conversion or assignable checks).
|
|
|
|
type Describers struct {
|
|
|
|
searchFns map[reflect.Type][]typeFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
// DescribeObject implements ObjectDescriber and will attempt to print the provided object to a string,
|
|
|
|
// if at least one describer function has been registered with the exact types passed, or if any
|
|
|
|
// describer can print the exact object in its first argument (the remainder will be provided empty
|
|
|
|
// values). If no function registered with Add can satisfy the passed objects, an ErrNoDescriber will
|
|
|
|
// be returned
|
|
|
|
// TODO: reorder and partial match extra.
|
|
|
|
func (d *Describers) DescribeObject(exact interface{}, extra ...interface{}) (string, error) {
|
|
|
|
exactType := reflect.TypeOf(exact)
|
|
|
|
fns, ok := d.searchFns[exactType]
|
|
|
|
if !ok {
|
|
|
|
return "", newErrNoDescriber(exactType)
|
|
|
|
}
|
|
|
|
if len(extra) == 0 {
|
|
|
|
for _, typeFn := range fns {
|
|
|
|
if len(typeFn.Extra) == 0 {
|
|
|
|
return typeFn.Describe(exact, extra...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
typeFn := fns[0]
|
|
|
|
for _, t := range typeFn.Extra {
|
|
|
|
v := reflect.New(t).Elem()
|
|
|
|
extra = append(extra, v.Interface())
|
|
|
|
}
|
|
|
|
return fns[0].Describe(exact, extra...)
|
|
|
|
}
|
|
|
|
|
|
|
|
types := []reflect.Type{}
|
|
|
|
for _, obj := range extra {
|
|
|
|
types = append(types, reflect.TypeOf(obj))
|
|
|
|
}
|
|
|
|
for _, typeFn := range fns {
|
|
|
|
if typeFn.Matches(types) {
|
|
|
|
return typeFn.Describe(exact, extra...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", newErrNoDescriber(append([]reflect.Type{exactType}, types...)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds one or more describer functions to the Describer. The passed function must
|
|
|
|
// match the signature:
|
|
|
|
//
|
|
|
|
// func(...) (string, error)
|
|
|
|
//
|
|
|
|
// Any number of arguments may be provided.
|
|
|
|
func (d *Describers) Add(fns ...interface{}) error {
|
|
|
|
for _, fn := range fns {
|
|
|
|
fv := reflect.ValueOf(fn)
|
|
|
|
ft := fv.Type()
|
|
|
|
if ft.Kind() != reflect.Func {
|
|
|
|
return fmt.Errorf("expected func, got: %v", ft)
|
|
|
|
}
|
|
|
|
if ft.NumIn() == 0 {
|
|
|
|
return fmt.Errorf("expected at least one 'in' params, got: %v", ft)
|
|
|
|
}
|
|
|
|
if ft.NumOut() != 2 {
|
|
|
|
return fmt.Errorf("expected two 'out' params - (string, error), got: %v", ft)
|
|
|
|
}
|
|
|
|
types := []reflect.Type{}
|
|
|
|
for i := 0; i < ft.NumIn(); i++ {
|
|
|
|
types = append(types, ft.In(i))
|
|
|
|
}
|
|
|
|
if ft.Out(0) != reflect.TypeOf(string("")) {
|
|
|
|
return fmt.Errorf("expected string return, got: %v", ft)
|
|
|
|
}
|
|
|
|
var forErrorType error
|
|
|
|
// This convolution is necessary, otherwise TypeOf picks up on the fact
|
|
|
|
// that forErrorType is nil.
|
|
|
|
errorType := reflect.TypeOf(&forErrorType).Elem()
|
|
|
|
if ft.Out(1) != errorType {
|
|
|
|
return fmt.Errorf("expected error return, got: %v", ft)
|
|
|
|
}
|
|
|
|
|
|
|
|
exact := types[0]
|
|
|
|
extra := types[1:]
|
|
|
|
if d.searchFns == nil {
|
|
|
|
d.searchFns = make(map[reflect.Type][]typeFunc)
|
|
|
|
}
|
|
|
|
fns := d.searchFns[exact]
|
|
|
|
fn := typeFunc{Extra: extra, Fn: fv}
|
|
|
|
fns = append(fns, fn)
|
|
|
|
d.searchFns[exact] = fns
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// typeFunc holds information about a describer function and the types it accepts
|
|
|
|
type typeFunc struct {
|
|
|
|
Extra []reflect.Type
|
|
|
|
Fn reflect.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matches returns true when the passed types exactly match the Extra list.
|
|
|
|
// TODO: allow unordered types to be matched and reorderd.
|
|
|
|
func (fn typeFunc) Matches(types []reflect.Type) bool {
|
|
|
|
if len(fn.Extra) != len(types) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := range types {
|
|
|
|
if fn.Extra[i] != types[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Describe invokes the nested function with the exact number of arguments.
|
|
|
|
func (fn typeFunc) Describe(exact interface{}, extra ...interface{}) (string, error) {
|
|
|
|
values := []reflect.Value{reflect.ValueOf(exact)}
|
|
|
|
for _, obj := range extra {
|
|
|
|
values = append(values, reflect.ValueOf(obj))
|
|
|
|
}
|
|
|
|
out := fn.Fn.Call(values)
|
|
|
|
s := out[0].Interface().(string)
|
|
|
|
var err error
|
|
|
|
if !out[1].IsNil() {
|
|
|
|
err = out[1].Interface().(error)
|
|
|
|
}
|
|
|
|
return s, err
|
|
|
|
}
|