forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_printer.go
212 lines (190 loc) · 6.17 KB
/
resource_printer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package cloudcfg
import (
"encoding/json"
"fmt"
"io"
"strings"
"text/tabwriter"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"gopkg.in/v1/yaml"
)
// ResourcePrinter is an interface that knows how to print API resources
type ResourcePrinter interface {
// Print receives an arbitrary JSON body, formats it and prints it to a writer
Print(string, io.Writer) error
}
// Identity printer simply copies the body out to the output stream
type IdentityPrinter struct{}
func (i *IdentityPrinter) Print(data string, w io.Writer) error {
_, err := fmt.Fprint(w, data)
return err
}
// YAMLPrinter parses JSON, and re-formats as YAML
type YAMLPrinter struct{}
func (y *YAMLPrinter) Print(data string, w io.Writer) error {
var obj interface{}
if err := json.Unmarshal([]byte(data), &obj); err != nil {
return err
}
output, err := yaml.Marshal(obj)
if err != nil {
return err
}
_, err = fmt.Fprint(w, string(output))
return err
}
// HumanReadablePrinter attempts to provide more elegant output
type HumanReadablePrinter struct{}
var podColumns = []string{"Name", "Image(s)", "Host", "Labels"}
var replicationControllerColumns = []string{"Name", "Image(s)", "Label Query", "Replicas"}
var serviceColumns = []string{"Name", "Label Query", "Port"}
func (h *HumanReadablePrinter) unknown(data string, w io.Writer) error {
_, err := fmt.Fprintf(w, "Unknown object: %s", 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
}
var lines []string
for _, _ = range columnNames {
lines = append(lines, "----------")
}
_, err := fmt.Fprintf(w, "%s\n", strings.Join(lines, "\t"))
return err
}
func (h *HumanReadablePrinter) makeImageList(manifest api.ContainerManifest) string {
var images []string
for _, container := range manifest.Containers {
images = append(images, container.Image)
}
return strings.Join(images, ",")
}
func (h *HumanReadablePrinter) makeLabelsList(labels map[string]string) string {
var vals []string
for key, value := range labels {
vals = append(vals, fmt.Sprintf("%s=%s", key, value))
}
return strings.Join(vals, ",")
}
func (h *HumanReadablePrinter) printPod(pod api.Pod, w io.Writer) error {
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\n",
pod.ID, h.makeImageList(pod.DesiredState.Manifest), pod.CurrentState.Host, h.makeLabelsList(pod.Labels))
return err
}
func (h *HumanReadablePrinter) printPodList(podList api.PodList, w io.Writer) error {
for _, pod := range podList.Items {
if err := h.printPod(pod, w); err != nil {
return err
}
}
return nil
}
func (h *HumanReadablePrinter) printReplicationController(ctrl api.ReplicationController, w io.Writer) error {
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%d\n",
ctrl.ID, h.makeImageList(ctrl.DesiredState.PodTemplate.DesiredState.Manifest), h.makeLabelsList(ctrl.DesiredState.ReplicasInSet), ctrl.DesiredState.Replicas)
return err
}
func (h *HumanReadablePrinter) printReplicationControllerList(list api.ReplicationControllerList, w io.Writer) error {
for _, ctrl := range list.Items {
if err := h.printReplicationController(ctrl, w); err != nil {
return err
}
}
return nil
}
func (h *HumanReadablePrinter) printService(svc api.Service, w io.Writer) error {
_, err := fmt.Fprintf(w, "%s\t%s\t%d\n", svc.ID, h.makeLabelsList(svc.Labels), svc.Port)
return err
}
func (h *HumanReadablePrinter) printServiceList(list api.ServiceList, w io.Writer) error {
for _, svc := range list.Items {
if err := h.printService(svc, w); err != nil {
return err
}
}
return nil
}
// TODO replace this with something that returns a concrete printer object, rather than
// having the secondary switch below.
func (h *HumanReadablePrinter) extractObject(data, kind string) (interface{}, error) {
// TODO: I think this can be replaced with some reflection and a map[string]type
switch kind {
case "cluster#pod":
var obj api.Pod
if err := json.Unmarshal([]byte(data), &obj); err != nil {
return nil, err
}
return obj, nil
case "cluster#podList":
var list api.PodList
if err := json.Unmarshal([]byte(data), &list); err != nil {
return nil, err
}
return list, nil
case "cluster#replicationController":
var ctrl api.ReplicationController
if err := json.Unmarshal([]byte(data), &ctrl); err != nil {
return nil, err
}
return ctrl, nil
case "cluster#replicationControllerList":
var list api.ReplicationControllerList
if err := json.Unmarshal([]byte(data), &list); err != nil {
return nil, err
}
return list, nil
case "cluster#service":
var ctrl api.Service
if err := json.Unmarshal([]byte(data), &ctrl); err != nil {
return nil, err
}
return ctrl, nil
case "cluster#serviceList":
var list api.ServiceList
if err := json.Unmarshal([]byte(data), &list); err != nil {
return nil, err
}
return list, nil
default:
return nil, fmt.Errorf("Unknown kind: %s", kind)
}
}
func (h *HumanReadablePrinter) Print(data string, output io.Writer) error {
w := tabwriter.NewWriter(output, 20, 5, 3, ' ', 0)
defer w.Flush()
var obj interface{}
if err := json.Unmarshal([]byte(data), &obj); err != nil {
return err
}
if _, contains := obj.(map[string]interface{})["kind"]; !contains {
return fmt.Errorf("Unexpected object with no 'kind' field: %s", data)
}
kind := (obj.(map[string]interface{})["kind"]).(string)
obj, err := h.extractObject(data, kind)
if err != nil {
return err
}
switch obj.(type) {
case api.Pod:
h.printHeader(podColumns, w)
return h.printPod(obj.(api.Pod), w)
case api.PodList:
h.printHeader(podColumns, w)
return h.printPodList(obj.(api.PodList), w)
case api.ReplicationController:
h.printHeader(replicationControllerColumns, w)
return h.printReplicationController(obj.(api.ReplicationController), w)
case api.ReplicationControllerList:
h.printHeader(replicationControllerColumns, w)
return h.printReplicationControllerList(obj.(api.ReplicationControllerList), w)
case api.Service:
h.printHeader(serviceColumns, w)
return h.printService(obj.(api.Service), w)
case api.ServiceList:
h.printHeader(serviceColumns, w)
return h.printServiceList(obj.(api.ServiceList), w)
default:
return h.unknown(data, w)
}
}